Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build_runner builder #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<dart|flutter> pub global run build_runner <build|watch>`



## Features and bugs

Please file feature requests and bugs at the [issue tracker](https://github.com/BreX900/index_generator/issues).
Expand Down
15 changes: 14 additions & 1 deletion build.yaml
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,4 +18,15 @@ targets:
field_rename: snake
generic_argument_factories: false
ignore_unannotated: false
include_if_null: true
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
required_inputs: ['.gen.dart']
9 changes: 7 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
path:

dev_dependencies:
build_runner: ^2.3.3
index_generator:
path: ../
1 change: 1 addition & 0 deletions lib/index_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
86 changes: 86 additions & 0 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
@@ -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<IndexGenerator> generators;
final PackageSettings package;

static AssetId _createFileOutput(BuildStep buildStep, String filePath) {
return AssetId(
buildStep.inputId.package,
filePath,
);
}

@override
Map<String, List<String>> get buildExtensions {
return {
r'$package$': generators.map((g) => g.indexFile.path).toList(),
};
}

@override
Future<void> 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<T>(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!);
}
}
}
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down