Skip to content

Commit

Permalink
Example: add GeneratorForAnnotation example
Browse files Browse the repository at this point in the history
More progress towards #322
  • Loading branch information
kevmoo committed Dec 22, 2018
1 parent 829bef0 commit 4d2662d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ builders:
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]

property_multiply:
import: "package:source_gen_example/builder.dart"
builder_factories: ["multiplyBuilder"]
build_extensions: {".dart": ["multiply.g.part"]}
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
9 changes: 9 additions & 0 deletions example/lib/annotations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.

class Multiplier {
final num value;

const Multiplier(this.value);
}
4 changes: 4 additions & 0 deletions example/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import 'src/member_count_library_generator.dart';
import 'src/multiplier_generator.dart';
import 'src/property_product_generator.dart';
import 'src/property_sum_generator.dart';

Expand All @@ -28,3 +29,6 @@ Builder productBuilder(BuilderOptions options) =>

Builder sumBuilder(BuilderOptions options) =>
SharedPartBuilder([PropertySumGenerator()], 'sum');

Builder multiplyBuilder(BuilderOptions options) =>
SharedPartBuilder([MultiplierGenerator()], 'multiply');
19 changes: 19 additions & 0 deletions example/lib/src/multiplier_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import '../annotations.dart';

class MultiplierGenerator extends GeneratorForAnnotation<Multiplier> {
@override
String generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
final numValue = annotation.read('value').literalValue as num;

return 'num ${element.name}Multiplied() => ${element.name} * $numValue;';
}
}
6 changes: 6 additions & 0 deletions example_usage/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ targets:
# Only run this builder on the specified input.
generate_for:
- lib/library_source.dart
source_gen_example|property_sum:
generate_for:
- lib/*.dart
source_gen_example|property_product:
generate_for:
- lib/*.dart
4 changes: 4 additions & 0 deletions example_usage/lib/library_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import 'dart:math';

import 'package:source_gen_example/annotations.dart';

part 'library_source.g.dart';

@Multiplier(2)
final answer = 42;

final tau = pi * 2;
6 changes: 6 additions & 0 deletions example_usage/lib/library_source.g.dart

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

0 comments on commit 4d2662d

Please sign in to comment.