source_gen 0.1.0 copy "source_gen: ^0.1.0" to clipboard
source_gen: ^0.1.0 copied to clipboard

outdatedDart 1 only

A source generation tool for Dart

Pub Build Status Coverage Status Stories in Ready

Easily generate boiler-plate source code for your Dart libraries.

source_gen provides:

  • A tool for generating code that is part of your Dart project.
  • A framework for creating and using multiple code generators in a single project.
  • A convention for human and tool generated Dart code to coexist with clean seperation.

Example #

Given a library person.dart:

library source_gen.example.person;

import 'package:source_gen/json_serial/json_annotation.dart';

part 'person.g.dart';

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
  String firstName, middleName, lastName;
  DateTime dob;

  Person();

  factory Person.fromJson(json) => _$PersonFromJson(json);
}

source_gen creates the corresponding part person.g.dart:

part of source_gen.example.person;

Person _$PersonFromJson(Map<String, Object> json) => new Person()
  ..firstName = json['firstName']
  ..middleName = json['middleName']
  ..lastName = json['lastName']
  ..dob = json['dob'];

abstract class _$PersonSerializerMixin {
  String get firstName;
  String get middleName;
  String get lastName;
  DateTime get dob;
  Map<String, Object> toJson() => {
    'firstName': firstName,
    'middleName': middleName,
    'lastName': lastName,
    'dob': dob
  };
}

See the example code in the source_gen GitHub repo.

Creating a generator #

Extend the Generator class to plug into source_gen.

Running generators #

Create a script that invokes the generate method.

Alternatively, you can create a build.dart file which calls the build method.

You can invoke this script directly. It also understands the Dart Editor build system so code is updated as your modify files.

See build.dart in the repository for an example.

source_gen vs Dart Transformers #

Dart Transformers are often used create and modify code and assets as part of a Dart project.

Transformers allow modification of existing code and encapsulates changes by having developers use pub commands – run, serve, and build.

source_gen provides a different model. Code is generated and updated as part of a project. It is designed to create part files that agument developer maintained Dart libraries.

Generated code MAY be checked in as part of our project source, although the decision may vary depending on project needs.

Generated code SHOULD be included when publishing a project as a pub package. The fact that source_gen is used in a package is an implementation detail.

148
likes
0
pub points
97%
popularity

Publisher

verified publishertools.dart.dev

A source generation tool for Dart

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, args, cli_util, dart_style, path

More

Packages that depend on source_gen