Skip to content

Commit

Permalink
Merge pull request #27 from davidmorgan/update-source-gen
Browse files Browse the repository at this point in the history
Upgrade to source_gen 0.5.0.
  • Loading branch information
davidmorgan committed May 25, 2016
2 parents 1583889 + 46f1f89 commit 144573d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.0

- Upgrade to source_gen 0.5.0.
- Breaking change; see example for required changes to build.dart.

## 0.0.6

- Move null checks to "build" method for compatibility with Strong Mode
Expand Down
2 changes: 1 addition & 1 deletion built_value/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: built_value
version: 0.0.6
version: 0.1.0
description: >
Value types with builders. This library is the runtime dependency.
authors:
Expand Down
19 changes: 12 additions & 7 deletions built_value_generator/lib/built_value_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
library built_value_generator;

import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:built_collection/built_collection.dart';
import 'package:quiver/iterables.dart' show concat;

import 'package:analyzer/src/generated/element.dart';
import 'package:source_gen/source_gen.dart';

/// Generator for Built Values.
///
/// See <https://github.com/google/built_value.dart/tree/master/example>
class BuiltValueGenerator extends Generator {
Future<String> generate(Element element) async {
@override
Future<String> generate(Element element, BuildStep buildStep) async {
if (element is! ClassElement) {
return null;
}
Expand Down Expand Up @@ -70,14 +72,17 @@ class BuiltValueGenerator extends Generator {
result.add('Make class abstract');
}

final expectedConstructor = '$name._();';
final expectedConstructor = '$name._()';
final constructors = classElement.constructors
.where((constructor) => !constructor.isFactory);
if (constructors.length != 1 ||
constructors.single.isSynthetic ||
constructors.single.computeNode().toSource() != expectedConstructor) {
result
.add('Make class have exactly one constructor: $expectedConstructor');
!(constructors.single
.computeNode()
.toSource()
.startsWith(expectedConstructor))) {
result.add(
'Make class have exactly one constructor: $expectedConstructor;');
}

final expectedFactory =
Expand Down
8 changes: 5 additions & 3 deletions built_value_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: built_value_generator
version: 0.0.6
version: 0.1.0
description: >
Value types with builders. This library is the dev dependency.
authors:
Expand All @@ -11,10 +11,12 @@ environment:

dependencies:
analyzer: '>=0.27.1 <0.28.0'
build: '^0.3.0'
built_collection: '^1.0.0'
built_value: '^0.0.6'
source_gen: '>=0.4.3 <0.5.0'
built_value: '^0.1.0'
source_gen: '>=0.5.0 <0.6.0'
quiver: '>=0.21.0 <0.22.0'

dev_dependencies:
build_test: '^0.1.0'
test: any
35 changes: 18 additions & 17 deletions built_value_generator/test/built_value_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -225,24 +226,24 @@ abstract class ValueBuilder extends Builder<Value, ValueBuilder> {

// Test setup.

Future<String> generate(String source) async {
final tempDir =
Directory.systemTemp.createTempSync('built_value_generator.dart.');
final packageDir = new Directory(tempDir.path + '/packages')..createSync();
final builtValueDir = new Directory(packageDir.path + '/built_value')
..createSync();
final builtValueFile = new File(builtValueDir.path + '/built_value.dart')
..createSync();
builtValueFile.writeAsStringSync(builtValueSource);
final String pkgName = 'pkg';
final PackageGraph packageGraph =
new PackageGraph.fromRoot(new PackageNode(pkgName, null, null, null));

final PhaseGroup phaseGroup = new PhaseGroup.singleAction(
new GeneratorBuilder([new BuiltValueGenerator()]),
new InputSet(pkgName, const ['lib/*.dart']));

final libDir = new Directory(tempDir.path + '/lib')..createSync();
final sourceFile = new File(libDir.path + '/value.dart');
sourceFile.writeAsStringSync(source);
Future<String> generate(String source) async {
final srcs = <String, String>{
'built_value|lib/built_value.dart': builtValueSource,
'$pkgName|lib/value.dart': source,
};

await build([], [new BuiltValueGenerator()],
projectPath: tempDir.path, librarySearchPaths: <String>['lib']);
final outputFile = new File(libDir.path + '/value.g.dart');
return outputFile.existsSync() ? outputFile.readAsStringSync() : '';
final writer = new InMemoryAssetWriter();
await testPhases(phaseGroup, srcs,
packageGraph: packageGraph, writer: writer);
return writer.assets[new AssetId(pkgName, 'lib/value.g.dart')]?.value;
}

const String builtValueSource = r'''
Expand Down
1 change: 0 additions & 1 deletion example/lib/compound_value.g.dart

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

1 change: 0 additions & 1 deletion example/lib/value.g.dart

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

6 changes: 3 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: example
version: 0.0.6
version: 0.1.0
description: >
Just an example, not for publishing.
authors:
Expand All @@ -11,8 +11,8 @@ environment:

dependencies:
built_collection: '^1.0.0'
built_value: '^0.0.6'
built_value: '^0.1.0'

dev_dependencies:
built_value_generator: '^0.0.6'
built_value_generator: '^0.1.0'
test: any
21 changes: 21 additions & 0 deletions example/tool/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import 'dart:async';

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

/// Example of how to use source_gen with [BuiltValueGenerator].
///
/// Import the generators you want and pass them to [build] as shown,
/// specifying which files in which packages you want to run against.
Future main(List<String> args) async {
await build(
new PhaseGroup.singleAction(
new GeneratorBuilder([new BuiltValueGenerator()]),
new InputSet('example', const ['lib/*.dart'])),
deleteFilesByDefault: true);
}
13 changes: 0 additions & 13 deletions example/tools/build.dart

This file was deleted.

0 comments on commit 144573d

Please sign in to comment.