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

Custom field name and part file generation #9

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.0

- Users can now customize the version constant name and the generated
version file could be a part of an existing package library.

## 2.0.0

- The builder now runs when `build_version` is a dependency. `build.yaml`
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,47 @@ Include the version of your package in our source code.
// Generated code. Do not modify.
const packageVersion = '1.2.3';
```

3. To customize the name of the version constant, a `build.yaml` option can be used.

```yaml
targets:
$default:
builders:
build_version:
options:
field_name: 'myVersion' # defaults to 'packageVersion'
```

4. It is also possible to generate the version string as a part of an existing library
in your package. In such a case, the default version builder needs to be disabled and
the version part builder should be used.

```yaml
targets:
$default:
builders:
build_version:
enabled: false
build_version|build_version_part:
enabled: true
generate_for: ['lib/src/my_lib.dart']
options:
field_name: 'myLibraryVersion' # defaults to 'packageVersion'
```

Assuming that `lib/src/my_lib.dart` contains `part 'my_lib.version.g.dart';`,
`lib/src/my_lib.version.g.dart` will be generated with content:

```dart
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'my_lib.dart';

// **************************************************************************
// _VersionPartGenerator
// **************************************************************************

// Generated code. Do not modify.
const packageVersion = '1.2.3';
```
5 changes: 5 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ builders:
build_extensions: {"$lib$": ["version.dart"]}
build_to: source
auto_apply: dependents
build_version_part:
import: "package:build_version/builder.dart"
builder_factories: ["buildVersionPart"]
build_extensions: {"$lib$": [".version.g.dart"]}
build_to: source
54 changes: 39 additions & 15 deletions lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,58 @@
library builder;

import 'dart:async';

import 'package:build/build.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:source_gen/source_gen.dart';

Builder buildVersion([BuilderOptions options]) => _VersionBuilder();
Builder buildVersion([BuilderOptions options]) => _VersionBuilder(options);

class _VersionBuilder implements Builder {
@override
Future build(BuildStep buildStep) async {
final assetId = AssetId(buildStep.inputId.package, 'pubspec.yaml');
Builder buildVersionPart([BuilderOptions options]) => PartBuilder(
[_VersionPartGenerator(_fieldName(options))], '.version.g.dart');

final content = await buildStep.readAsString(assetId);
String _fieldName(BuilderOptions options) =>
options != null && options.config.containsKey('field_name')
? options.config['field_name'] as String
: 'packageVersion';

final pubspec = Pubspec.parse(content, sourceUrl: assetId.uri);
class _VersionBuilder implements Builder {
_VersionBuilder([BuilderOptions options]) : fieldName = _fieldName(options);

if (pubspec.version == null) {
throw StateError('pubspec.yaml does not have a version defined.');
}
final String fieldName;

@override
Future build(BuildStep buildStep) async {
await buildStep.writeAsString(
AssetId(buildStep.inputId.package, 'lib/src/version.dart'), '''
// Generated code. Do not modify.
const packageVersion = '${pubspec.version}';
''');
AssetId(buildStep.inputId.package, 'lib/src/version.dart'),
await _versionSource(buildStep, fieldName));
}

@override
final buildExtensions = const {
r'$lib$': ['src/version.dart']
};
}

class _VersionPartGenerator extends Generator {
_VersionPartGenerator(this.fieldName);

final String fieldName;

@override
Future<String> generate(LibraryReader library, BuildStep buildStep) async =>
_versionSource(buildStep, fieldName);
}

Future<String> _versionSource(BuildStep buildStep, String fieldName) async {
final assetId = AssetId(buildStep.inputId.package, 'pubspec.yaml');
final content = await buildStep.readAsString(assetId);
final pubspec = Pubspec.parse(content, sourceUrl: assetId.uri);

if (pubspec.version == null) {
throw StateError('pubspec.yaml does not have a version defined.');
}
return '''
// Generated code. Do not modify.
const $fieldName = '${pubspec.version}';
''';
}
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: build_version
description: A builder for extracting a package version into code.
version: 2.0.0
version: 2.1.0
homepage: https://github.com/kevmoo/build_version
author: Kevin Moore <[email protected]>

Expand All @@ -12,6 +12,7 @@ dependencies:
# Not imported in code, but used to constrain `build.yaml` requirements
build_config: ^0.3.0
pubspec_parse: ^0.1.2+2
source_gen: ^0.9.4

dev_dependencies:
build_runner: ^1.0.0
Expand Down
12 changes: 12 additions & 0 deletions test/build_version_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:build_version/builder.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
Expand Down Expand Up @@ -33,6 +34,17 @@ void main() {
'pkg|lib/src/version.dart': r'''
// Generated code. Do not modify.
const packageVersion = '1.0.0';
'''
});
});
test('valid input with custom field', () async {
await testBuilder(
buildVersion(const BuilderOptions({'field_name': 'myVersion'})),
_createPackageStub({'name': 'pkg', 'version': '1.0.0'}),
outputs: {
'pkg|lib/src/version.dart': r'''
// Generated code. Do not modify.
const myVersion = '1.0.0';
'''
});
});
Expand Down