super_annotations 0.1.1 copy "super_annotations: ^0.1.1" to clipboard
super_annotations: ^0.1.1 copied to clipboard

outdated

Write your code generation functions naturally alongside your normal code. Define and use custom annotations in the same file or project.

example/lib/main.dart

import 'package:super_annotations/super_annotations.dart';

/// First part of the file:
/// Define classes as you normally would,
/// and annotate them with a custom annotation

@MyAnnotation()
class MyClass {
  final String data;

  MyClass(this.data);
}

/// Second part of the file:
/// Define your custom annotations and what they should do
/// This will be executed during the build phase

/// Choose a name and extend [ClassAnnotation]
class MyAnnotation extends ClassAnnotation {
  /// You need a const constructor to be usable as an annotation
  const MyAnnotation();

  /// You have to implement the [apply] method, which will be
  /// executed during the build phase
  /// @param clazz: A formal description of the annotated class, e.g. its name and fields
  /// @param library: The library that will be generated as output of the build phase
  @override
  void apply(Class clazz, LibraryBuilder library) {
    library.body.add(Code('// - ${clazz.name}'));
  }
}

/// Optional: Define a custom function that will be executed
/// at the beginning of the build phase, before the annotations
/// Alternatively, you can also use @CodeGen.runAfter()
/// @param library: The library that will be generated as output of the build phase
@CodeGen.runBefore()
void writeOutput(LibraryBuilder library) {
  /// Modify the contents of the library
  /// Here: Add a title comment
  library.body.add(Code('// Classes annotated with @MyAnnotation\n'));
}

void main() {}

/// After running the build step, a new file `example.g.dart` is created with
/// the following contents:
///   // Classes annotated with @MyAnnotation
///   // - MyClass
37
likes
0
pub points
58%
popularity

Publisher

verified publisherschultek.de

Write your code generation functions naturally alongside your normal code. Define and use custom annotations in the same file or project.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, code_builder, dart_style, path, source_gen

More

Packages that depend on super_annotations