From d56b1270f4b042af1cf609676ff2540e69d2e126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Beffa?= Date: Sat, 2 Mar 2024 22:30:25 +0100 Subject: [PATCH 1/2] Add build_runner builder --- README.md | 14 +++++++ build.yaml | 14 ++++++- example/pubspec.yaml | 9 ++++- lib/index_generator.dart | 1 + lib/src/builder.dart | 86 ++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 8 ++-- 6 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 lib/src/builder.dart diff --git a/README.md b/README.md index 6266628..7940fc3 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,20 @@ index_generator: -h, --help ``` +## Build runner + +`index_generator` can be run via [build_runner](https://pub.dev/packages/build_runner). To do so, add the following dependancies: + +```yaml +dev_dependencies: + build_runner: ^2.3.3 + index_generator: ^3.5.0 +``` + +To generate use ` pub global run build_runner ` + + + ## Features and bugs Please file feature requests and bugs at the [issue tracker](https://github.com/BreX900/index_generator/issues). diff --git a/build.yaml b/build.yaml index b55e41b..6744ece 100644 --- a/build.yaml +++ b/build.yaml @@ -1,7 +1,9 @@ targets: $default: + auto_apply_builders: false builders: json_serializable: + enabled: false options: # Options configure how source code is generated for every # `@JsonSerializable`-annotated class in the package. @@ -16,4 +18,14 @@ targets: field_rename: snake generic_argument_factories: false ignore_unannotated: false - include_if_null: true \ No newline at end of file + include_if_null: true + index_generator: + enabled: true + +builders: + index_generator: + import: "package:index_generator/src/builder.dart" + builder_factories: ["indexGeneratorBuilder"] + build_extensions: { '$package$': ['.dart'] } + auto_apply: root_package + build_to: source \ No newline at end of file diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 63b8870..1b1f8d0 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -2,8 +2,13 @@ name: example publish_to: 'none' environment: - sdk: '>=2.10.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: args: - path: \ No newline at end of file + path: + +dev_dependencies: + build_runner: ^2.3.3 + index_generator: + path: ../ \ No newline at end of file diff --git a/lib/index_generator.dart b/lib/index_generator.dart index 2f8c74b..55174a5 100644 --- a/lib/index_generator.dart +++ b/lib/index_generator.dart @@ -2,6 +2,7 @@ /// with all the export needed for your library. library index_generator; +export 'src/builder.dart'; export 'src/converters.dart'; export 'src/dart_code/dart_export.dart'; export 'src/index_generator.dart'; diff --git a/lib/src/builder.dart b/lib/src/builder.dart new file mode 100644 index 0000000..3531f05 --- /dev/null +++ b/lib/src/builder.dart @@ -0,0 +1,86 @@ +// ignore_for_file: depend_on_referenced_packages +import 'dart:io'; + +import 'package:build/build.dart'; +import 'package:glob/glob.dart'; +import 'package:path/path.dart' as p; +import 'package:index_generator/src/settings/library_settings.dart'; +import 'package:index_generator/src/settings/package_settings.dart'; +import 'package:index_generator/src/index_generator.dart'; +import 'package:yaml/yaml.dart'; +import 'package:checked_yaml/checked_yaml.dart'; +import 'package:dart_style/dart_style.dart'; + +Builder indexGeneratorBuilder(BuilderOptions options) { + final toolBox = ToolBoxSync(name: 'index_generator'); + final project = ToolBoxSync.loadYaml(toolBox.projectYaml, ProjectSettings.fromJson); + final settingsFile = toolBox.findYaml(null); + final settings = ToolBoxSync.loadYaml(settingsFile, PackageSettings.fromYaml); + return IndexGeneratorBuilder(project, settings); +} + +class IndexGeneratorBuilder implements Builder { + IndexGeneratorBuilder(ProjectSettings project, this.package) { + generators = package.libraries.map((library) { + return IndexGenerator.from(project, package, library); + }).toList(); + } + late final List generators; + final PackageSettings package; + + static AssetId _createFileOutput(BuildStep buildStep, String filePath) { + return AssetId( + buildStep.inputId.package, + filePath, + ); + } + + @override + Map> get buildExtensions { + return { + r'$package$': generators.map((g) => g.indexFile.path).toList(), + }; + } + + @override + Future build(BuildStep buildStep) async { + final formatter = DartFormatter( + lineEnding: package.lineBreak, + pageWidth: package.pageWidth, + ); + for (var generator in generators) { + final content = formatter.format(generator.generate().join(package.lineBreak)); + final output = _createFileOutput(buildStep, generator.indexFile.path); + buildStep.writeAsString(output, content); + } + } +} + +class ToolBoxSync { + ToolBoxSync({required String name}) : packageYaml = File('$name.yaml'); + final File projectYaml = File('pubspec.yaml'); + final File packageYaml; + + File findYaml(String? path) { + if (path != null) return File(path); + + if (packageYaml.existsSync()) return packageYaml; + + return projectYaml; + } + + static T loadYaml(File file, T Function(Map map) from) { + if (!file.existsSync()) { + throw Exception('Not find ${file.path} file in this project'); + } + try { + return checkedYamlDecode( + file.readAsStringSync(), + (map) => from(map!), + sourceUrl: Uri.file(file.path), + ); + } on ParsedYamlException catch (error) { + throw Exception(error.formattedMessage!); + } + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 1407b49..ab10946 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: index_generator description: Automatically generate index / barrel / library files with all the export needed for your library. -version: 3.4.1 +version: 3.5.0 homepage: https://github.com/BreX900/index_generator topics: - library @@ -24,20 +24,20 @@ dependencies: yaml: ^3.1.1 checked_yaml: ^2.0.1 mek_data_class: ^1.0.1 - json_annotation: ^4.8.0 + json_annotation: ^4.8.1 glob: ^2.1.0 pubspec: ^2.2.0 dart_style: ^2.2.3 dev_dependencies: - lints: ^2.0.0 + lints: ^3.0.0 test: ^1.21.6 build_runner: ^2.3.3 mek_data_class_generator: ^1.1.1 - json_serializable: ^6.6.1 + json_serializable: ^6.7.1 executables: index_generator: From 95bafbcefc9261316548d303dbb6bbd0467e6a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Beffa?= Date: Sat, 2 Mar 2024 23:44:22 +0100 Subject: [PATCH 2/2] Add gen files --- build.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.yaml b/build.yaml index 6744ece..d0fcd62 100644 --- a/build.yaml +++ b/build.yaml @@ -28,4 +28,5 @@ builders: builder_factories: ["indexGeneratorBuilder"] build_extensions: { '$package$': ['.dart'] } auto_apply: root_package - build_to: source \ No newline at end of file + build_to: source + required_inputs: ['.gen.dart'] \ No newline at end of file