Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.5.8

* Add `formatOutput` optional parameter to the `GeneratorBuilder` constructor.
This is a lamda of the form `String formatOutput(String originalCode)` which
allows you do do custom formatting.

## 0.5.7

* Support for package:analyzer 0.30.0
Expand Down
13 changes: 10 additions & 3 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import 'generated_output.dart';
import 'generator.dart';
import 'utils.dart';

typedef String OutputFormatter(String generatedCode);

class GeneratorBuilder extends Builder {
final OutputFormatter formatOutput;
final List<Generator> generators;
final String generatedExtension;
final bool isStandalone;

GeneratorBuilder(this.generators,
{this.generatedExtension: '.g.dart', this.isStandalone: false}) {
{OutputFormatter formatOutput,
this.generatedExtension: '.g.dart',
this.isStandalone: false})
: formatOutput = formatOutput ?? _formatter.format {
// TODO: validate that generatedExtension starts with a `.'
// not null, empty, etc
if (this.isStandalone && this.generators.length > 1) {
Expand Down Expand Up @@ -72,9 +78,8 @@ class GeneratorBuilder extends Builder {

var genPartContent = contentBuffer.toString();

var formatter = new DartFormatter();
try {
genPartContent = formatter.format(genPartContent);
genPartContent = formatOutput(genPartContent);
} catch (e, stack) {
log.severe(
'Error formatting generated source code for ${library.identifier}'
Expand Down Expand Up @@ -124,6 +129,8 @@ Stream<GeneratedOutput> _processUnitMember(
}
}

final _formatter = new DartFormatter();

const _topHeader = '''// GENERATED CODE - DO NOT MODIFY BY HAND

''';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 0.5.7
version: 0.5.8
author: Dart Team <[email protected]>
description: Automatic sourcecode generation for Dart
homepage: https://github.com/dart-lang/source_gen
Expand Down
36 changes: 36 additions & 0 deletions test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';

import 'src/comment_generator.dart';
import 'src/unformatted_code_generator.dart';

void main() {
test('Simple Generator test', _simpleTest);
Expand Down Expand Up @@ -93,6 +94,41 @@ void main() {
'$pkgName|lib/test_lib.g.dart': _testGenPartContentError,
});
});

test('defaults to formatting generated code with the DartFormatter',
() async {
await testBuilder(new GeneratorBuilder([new UnformattedCodeGenerator()]),
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
generateFor: new Set.from(['$pkgName|lib/a.dart']),
outputs: {
'$pkgName|lib/a.g.dart':
contains(UnformattedCodeGenerator.formattedCode),
});
});

test('can skip formatting with a trivial lambda', () async {
await testBuilder(
new GeneratorBuilder([new UnformattedCodeGenerator()],
formatOutput: (s) => s),
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
generateFor: new Set.from(['$pkgName|lib/a.dart']),
outputs: {
'$pkgName|lib/a.g.dart':
contains(UnformattedCodeGenerator.unformattedCode),
});
});

test('can pass a custom formatter with formatOutput', () async {
var customOutput = 'final String hello = "hello";';
await testBuilder(
new GeneratorBuilder([new UnformattedCodeGenerator()],
formatOutput: (_) => customOutput),
{'$pkgName|lib/a.dart': 'library a; part "a.part.dart";'},
generateFor: new Set.from(['$pkgName|lib/a.dart']),
outputs: {
'$pkgName|lib/a.g.dart': contains(customOutput),
});
});
}

Future _simpleTest() => _generateTest(
Expand Down
26 changes: 26 additions & 0 deletions test/src/unformatted_code_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

import 'dart:async';

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

/// Generates a single-line of unformatted code.
class UnformattedCodeGenerator extends Generator {
const UnformattedCodeGenerator();

@override
Future<String> generate(Element element, _) async {
return unformattedCode;
}

static const formattedCode = '''
void hello() => print('hello');
''';

static const unformattedCode = '''
void hello ()=> print('hello');
''';
}