Skip to content

Put custom builders in their own file, enable build_test #154

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

Merged
merged 5 commits into from
May 12, 2018
Merged
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
3 changes: 2 additions & 1 deletion json_serializable/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import 'package:build/build.dart';

import 'src/json_part_builder.dart';

/// Supports `package:build_runner` creation and configuration of `build_cli`.
/// Supports `package:build_runner` creation and configuration of
/// `json_serializable`.
///
/// Not meant to be invoked by hand-authored code.
Builder jsonSerializable(BuilderOptions options) {
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.5.2
version: 0.5.3-dev
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand Down
105 changes: 11 additions & 94 deletions json_serializable/tool/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@
// 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 'dart:io' show exitCode;

import 'package:build/build.dart';
import 'package:build_config/build_config.dart';
import 'package:build_runner/build_runner.dart';
import 'package:build_test/builder.dart';
import 'package:json_serializable/src/json_part_builder.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';

import 'builder.dart';

final List<BuilderApplication> builders = [
applyToRoot(
new LibraryBuilder(new _NonNullableGenerator(),
generatedExtension: '.non_nullable.dart', header: _copyrightHeader),
applyToRoot(nonNull(),
generateFor: const InputSet(include: const [
'test/test_files/kitchen_sink.dart',
'test/test_files/json_test_example.dart'
])),
applyToRoot(
new LibraryBuilder(new _WrappedGenerator(),
generatedExtension: '.wrapped.dart', header: _copyrightHeader),
applyToRoot(wrapped(),
generateFor: const InputSet(include: const [
'test/test_files/kitchen_sink.dart',
'test/test_files/kitchen_sink.non_nullable.dart',
'test/test_files/json_test_example.dart',
'test/test_files/json_test_example.non_nullable.dart',
])),
applyToRoot(jsonPartBuilder(header: _copyrightHeader),
applyToRoot(jsonPartBuilder(header: copyrightHeader),
generateFor: const InputSet(
include: const [
'example/example.dart',
Expand All @@ -41,96 +36,18 @@ final List<BuilderApplication> builders = [
'test/test_files/kitchen_sink.non_nullable.dart'
],
)),
applyToRoot(jsonPartBuilder(useWrappers: true, header: _copyrightHeader),
applyToRoot(jsonPartBuilder(useWrappers: true, header: copyrightHeader),
generateFor: const InputSet(
include: const [
'test/test_files/kitchen_sink*wrapped.dart',
'test/test_files/json_test_example*wrapped.dart',
],
))
)),
applyToRoot(testBootstrapBuilder(null),
generateFor: const InputSet(include: const ['test/**']),
hideOutput: true),
];

final _copyrightContent =
'''// Copyright (c) 2017, the Dart project authors. 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.
''';

final _copyrightHeader = '$_copyrightContent\n$defaultFileHeader';

class _NonNullableGenerator extends Generator {
@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
final path = buildStep.inputId.path;
final baseName = p.basenameWithoutExtension(path);

final content = await buildStep.readAsString(buildStep.inputId);
var replacements = <_Replacement>[
new _Replacement(_copyrightContent, ''),
new _Replacement(
"part '$baseName.g.dart",
"part '$baseName.non_nullable.g.dart",
),
new _Replacement(
'@JsonSerializable()', '@JsonSerializable(nullable: false)'),
];

if (baseName == 'kitchen_sink') {
replacements.addAll([
new _Replacement('List<T> _defaultList<T>() => null;',
'List<T> _defaultList<T>() => <T>[];'),
new _Replacement(
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
new _Replacement('DateTime dateTime;',
'DateTime dateTime = new DateTime(1981, 6, 5);')
]);
}

return _Replacement.generate(content, replacements);
}
}

class _WrappedGenerator extends Generator {
@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
final path = buildStep.inputId.path;
final baseName = p.basenameWithoutExtension(path);

final content = await buildStep.readAsString(buildStep.inputId);
var replacements = <_Replacement>[
new _Replacement(_copyrightContent, ''),
new _Replacement(
"part '$baseName.g.dart",
"part '$baseName.wrapped.g.dart",
),
];

return _Replacement.generate(content, replacements);
}
}

class _Replacement {
final Pattern existing;
final String replacement;

_Replacement(this.existing, this.replacement);

static String generate(
String inputContent, Iterable<_Replacement> replacements) {
var outputContent = inputContent;

for (var r in replacements) {
if (!outputContent.contains(r.existing)) {
throw new StateError(
'Input string did not contain `${r.existing}` as expected.');
}
outputContent = outputContent.replaceAll(r.existing, r.replacement);
}

return outputContent;
}
}

main(List<String> args) async {
exitCode = await run(args, builders);
}
96 changes: 96 additions & 0 deletions json_serializable/tool/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) 2018, the Dart project authors. 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:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';

final _copyrightContent =
'''// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018?

// 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.
''';

final copyrightHeader = '$_copyrightContent\n$defaultFileHeader';

Builder nonNull([_]) => new LibraryBuilder(new _NonNullableGenerator(),
generatedExtension: '.non_nullable.dart', header: copyrightHeader);

Builder wrapped([_]) => new LibraryBuilder(new _WrappedGenerator(),
generatedExtension: '.wrapped.dart', header: copyrightHeader);

class _NonNullableGenerator extends Generator {
@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
final path = buildStep.inputId.path;
final baseName = p.basenameWithoutExtension(path);

final content = await buildStep.readAsString(buildStep.inputId);
var replacements = [
new _Replacement(_copyrightContent, ''),
new _Replacement(
"part '$baseName.g.dart",
"part '$baseName.non_nullable.g.dart",
),
new _Replacement(
'@JsonSerializable()', '@JsonSerializable(nullable: false)'),
];

if (baseName == 'kitchen_sink') {
replacements.addAll([
new _Replacement('List<T> _defaultList<T>() => null;',
'List<T> _defaultList<T>() => <T>[];'),
new _Replacement(
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
new _Replacement('DateTime dateTime;',
'DateTime dateTime = new DateTime(1981, 6, 5);')
]);
}

return _Replacement.generate(content, replacements);
}
}

class _WrappedGenerator extends Generator {
@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
final path = buildStep.inputId.path;
final baseName = p.basenameWithoutExtension(path);

final content = await buildStep.readAsString(buildStep.inputId);
var replacements = [
new _Replacement(_copyrightContent, ''),
new _Replacement(
"part '$baseName.g.dart",
"part '$baseName.wrapped.g.dart",
),
];

return _Replacement.generate(content, replacements);
}
}

class _Replacement {
final Pattern existing;
final String replacement;

_Replacement(this.existing, this.replacement);

static String generate(
String inputContent, Iterable<_Replacement> replacements) {
var outputContent = inputContent;

for (var r in replacements) {
if (!outputContent.contains(r.existing)) {
throw new StateError(
'Input string did not contain `${r.existing}` as expected.');
}
outputContent = outputContent.replaceAll(r.existing, r.replacement);
}

return outputContent;
}
}