p-hatan/ mirror

pub repository

mirror.p-hatan.ir

code_builder

1.0.1

A fluent API for generating Dart code

Download archive

Readme

code_builder

pub package Build Status Coverage Status

code_builder is a fluent Dart API for generating valid Dart source code.

Code generation was traditionally done through a series of package-specific string concatenations which usually results in messy and sometimes invalid Dart code that is not easily readable and is very difficult to refactor.

code_builder uses the analyzer package to create real Dart language ASTs, which, while not guaranteed to be correct, always follows the analyzer's own understood format.

Contributing

If a feature is missing (the Dart language is always evolving) or you'd like an easier or better way to do something, consider opening a pull request. You can always file an issue, but generally speaking feature requests will be on a best-effort basis.

Usage

Code builder has a narrow and user-friendly API.

For example creating a class with a method:

var base = reference('Organism');
var clazz = new ClassBuilder('Animal', asExtends: base);
clazz.addMethod(
  new MethodBuilder.returnVoid(
    'eat',
    returns: reference('print').call([literal('Yum')]),
  ),
);

Outputs:

class Animal extends Organism {
  void eat() => print('Yum!');
}

Have a complicated set of dependencies for your generated code? code_builder supports automatic scoping of your ASTs to automatically use prefixes to avoid symbol conflicts:

var lib = new LibraryBuilder.scope()
  ..addMembers([
    new MethodBuilder(
      'doThing',
      returnType: reference('Thing', 'package:thing/thing.dart'),
    ),
    new MethodBuilder(
      'doOtherThing',
      returnType: reference('Thing', 'package:thing/alternative.dart'),
    ),
  ]);

Outputs:

import 'package:thing/thing.dart' as _i1;
import 'package:thing/alternative.dart' as _i2;

_i1.Thing doThing() {}
_i2.Thing doOtherThing() {}
Changelog

1.0.1

  • Support the latest version of package:dart_style.

1.0.0

First full release. At this point all changes until 2.0.0 will be backwards compatible (new features) or bug fixes that are not breaking. This doesn't mean that the entire Dart language is buildable with our API, though.

Contributions are welcome.

  • Exposed uri in ImportBuilder, ExportBuilder, and Part[Of]Builder.

1.0.0-beta+7

  • Added ExpressionBuilder#ternary.

1.0.0-beta+6

  • Added TypeDefBuilder.
  • Added FunctionParameterBuilder.
  • Added asAbstract to various MethodBuilder constructors.

1.0.0-beta+5

  • Re-published the package without merge conflicts.

1.0.0-beta+4

  • Renamed PartBuilder to PartOfBuilder.
  • Added a new class, PartBuilder, to represent part '...dart' directives.
  • Added the HasAnnotations interface to all library/part/directive builders.
  • Added asFactory and asConst to ConstructorBuilder.
  • Added ConstructorBuilder.redirectTo for a redirecting factory constructor.
  • Added a name getter to ReferenceBuilder.
  • Supplying an empty constructor name ('') is equivalent to null (default).
  • Automatically encodes string literals with multiple lines as '''.
  • Added asThrow to ExpressionBuilder.
  • Fixed a bug that prevented FieldBuilder from being used at the top-level.

1.0.0-beta+3

  • Added support for genericTypes parameter for ExpressionBuilder#invoke:
expect(
  explicitThis.invoke('doThing', [literal(true)], genericTypes: [
    lib$core.bool,
  ]),
  equalsSource(r'''
    this.doThing<bool>(true)
  '''),
);
  • Added a castAs method to ExpressionBuilder:
expect(
  literal(1.0).castAs(lib$core.num),
  equalsSource(r'''
    1.0 as num
  '''),
);

BREAKING CHANGES

  • Removed namedNewInstance and namedConstInstance, replaced with constructor: :
expect(
  reference('Foo').newInstance([], constructor: 'other'),
  equalsSource(r'''
    new Foo.other()
  '''),
);
  • Renamed named parameter to namedArguments:
expect(
  reference('doThing').call(
    [literal(true)],
    namedArguments: {
      'otherFlag': literal(false),
    },
  ),
  equalsSource(r'''
    doThing(true, otherFlag: false)
  '''),
);

1.0.0-beta+2

BREAKING CHANGES

Avoid creating symbols that can collide with the Dart language:

  • MethodModifier.async -> MethodModifier.asAsync
  • MethodModifier.asyncStar -> MethodModifier.asAsyncStar
  • MethodModifier.syncStar -> MethodModifier.asSyncStar

1.0.0-beta+1

  • Add support for switch statements
  • Add support for a raw expression and statement
    • new ExpressionBuilder.raw(...)
    • new StatemnetBuilder.raw(...)

This should help cover any cases not covered with builders today.

  • Allow referring to a ClassBuilder and TypeBuilder as an expression
  • Add support for accessing the index [] operator on an expression

BREAKING CHANGES

  • Changed ExpressionBuilder.asAssign to always take an ExpressionBuilder as target and removed the value property. Most changes are pretty simple, and involve just using reference(...). For example:
literal(true).asAssign(reference('flag'))

... emits flag = true.

1.0.0-beta

  • Add support for async, sync, sync* functions
  • Add support for expression asAwait, asYield, asYieldStar
  • Add toExportBuilder and toImportBuilder to types and references
  • Fix an import scoping bug in return statements and named constructor invocations.
  • Added constructor initializer support
  • Add while and do {} while loop support
  • Add for and for-in support
  • Added a name getter for ParameterBuilder

1.0.0-alpha+7

  • Make use of new analyzer API in preparation for analyzer version 0.30.

1.0.0-alpha+6

  • MethodBuilder.closure emits properly as a top-level function

1.0.0-alpha+5

  • MethodBuilder with no statements will create an empty block instead of a semicolon.
// main() {}
method('main')
  • Fix lambdas and closures to not include a trailing semicolon when used as an expression.
 // () => false
 new MethodBuilder.closure(returns: literal(false));

1.0.0-alpha+4

  • Add support for latest pkg/analyzer.

1.0.0-alpha+3

  • BREAKING CHANGE: Added generics support to TypeBuilder:

importFrom becomes a named, not positional argument, and the named argument genericTypes is added (Iterable<TypeBuilder>).

// List<String>
new TypeBuilder('List', genericTypes: [reference('String')])
  • Added generic support to ReferenceBuilder:
// List<String>
reference('List').toTyped([reference('String')])
  • Fixed a bug where ReferenceBuilder.buildAst was not implemented
  • Added and and or methods to ExpressionBuilder:
// true || false
literal(true).or(literal(false));

// true && false
literal(true).and(literal(false));
  • Added support for creating closures - MethodBuilder.closure:
// () => true
new MethodBuilder.closure(
  returns: literal(true),
  returnType: lib$core.bool,
)

1.0.0-alpha+2

  • Added returnVoid to well, return;
  • Added support for top-level field assignments:
new LibraryBuilder()..addMember(literal(false).asConst('foo'))
  • Added support for specifying a target when using asAssign:
// Outputs bank.bar = goldBar
reference('goldBar').asAssign('bar', target: reference('bank'))
  • Added support for the cascade operator:
// Outputs foo..doThis()..doThat()
reference('foo').cascade((c) => <ExpressionBuilder> [
  c.invoke('doThis', []),
  c.invoke('doThat', []),
]);
  • Added support for accessing a property
// foo.bar
reference('foo').property('bar');

1.0.0-alpha+1

  • Slight updates to confusing documentation.
  • Added support for null-aware assignments.
  • Added show and hide support to ImportBuilder
  • Added deferred support to ImportBuilder
  • Added ExportBuilder
  • Added list and map literals that support generic types

1.0.0-alpha

  • Large refactor that makes the library more feature complete.

0.1.1

  • Add concept of Scope and change toAst to support it

Now your entire AST tree can be scoped and import directives automatically added to a LibraryBuilder for you if you use LibraryBuilder.scope.

0.1.0

  • Initial version

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add code_builder
pubspec.yaml
name: code_builder
version: 1.0.1
description: A fluent API for generating Dart code
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/code_builder

environment:
  sdk: '>=1.22.0 <2.0.0'

dependencies:
  analyzer: '>=0.29.1 <0.30.0'
  dart_style: '>=0.2.10 <2.0.0'
  func: ^0.1.0
  matcher: ^0.12.0+2
  meta: ^1.0.2

dev_dependencies:
  test: ^0.12.0

Version history

VersionPublishedArchiveSHA-256
0.1.22016-09-19T16:57:46.247ZDownload9cd403e7176d3d178985364e81ea0f1af40d1acbc1e24a883a8e470089bf3225
0.1.32016-09-23T21:47:16.036ZDownload1d4e453be10156673fca78390ae3b1a6d16ed6eae37f1eecaccd9781dca1bd73
1.0.0-alpha2016-11-15T01:01:03.962ZDownload268ae27827c2318f838f92e5021d645a60eb0202bb83956f311fb9e9a6aabbe7
1.0.0-alpha+22016-11-16T20:23:55.457ZDownload0c8924ab86b4311c259b31b9384da7f2f2540b879be1cd53c693c1b11525b499
1.0.0-alpha+42016-11-21T00:05:44.078ZDownloadb8043f240784b7f08af5f6133e65fad4110f2f2ecf4a19201543c72184dfc7e0
1.0.0-alpha+52016-11-22T01:20:42.818ZDownload196f178cca029bdd2fa414c2bace87b1d9419434dd18deeeee3371f23ef2ecc8
1.0.0-alpha+62016-11-22T06:42:22.866ZDownloadb7f305804e95406b5354ed044906135a8ceedaafa7124b8c15d17a761dcf8f76
1.0.0-alpha+72016-11-29T22:49:21.605ZDownloadc9ee27df27fd0566ad5e5c04a5bfe5777dec906145416057d63c60fe3d1bb53e
1.0.0-beta2017-01-05T01:24:27.556ZDownload89303bf07eff14500374898babb61d9b7bacbf11888bfe34f445ce0d48561884
1.0.0-beta+12017-01-12T01:55:11.500ZDownload2ff7ff41473686f00be4987a4e701ec4cff610376296b3f2ecb37887a768dcbe
1.0.0-beta+32017-01-31T21:41:30.371ZDownloadae98b857a2b9af06bb95165ca76ee86ce4472341501a8ea689c6d4a402ed6808
1.0.0-beta+42017-03-01T04:01:37.233ZDownload3d9ab8e6cf03b8087905f2fcaf4e0e67366932e7801e2efcaead36d6267ae4c7
1.0.0-beta+52017-03-02T18:38:54.862ZDownload555ad921ac351ca4860020fc0ef5fc648100a3d722d65c70511c113ceb17fce5
1.0.0-beta+62017-03-16T03:51:27.818ZDownload858e0a5af9b1a099f866527d17b824de1b004e6bcc64524018f31ea1a08f7027
1.0.0-beta+72017-03-29T20:30:00.855802ZDownloadeda8f5bac58aafb7755bae33c1364c644751673802571481237e91d5b098e567
1.0.02017-03-31T00:41:29.698443ZDownloadfca402614b32a5775c165450ba77b53514771043190a0f052861a1e96b3fd50b
1.0.12017-04-05T02:53:26.209277ZDownload92429d94e12e45eca3ae9be35cbe7514d4356166fd5995c27a7713da69500f8b
1.0.22017-05-02T22:17:00.009084ZDownload6944c551a66b9f8a17a47e9b858fb401c4f002ef0ec760b921f765df7b12908a
1.0.32017-05-12T20:09:25.614898ZDownloadcff7c9953f9ca0896a6bd177ac23446024fadc65d6308182c7776c758eb70b1d
1.0.42017-06-16T15:48:34.323104ZDownload2479a44e047e992c6c74e0cad90e0ccd8bf43a360976820941ffd68810d54c3e
2.0.0-alpha2017-07-14T19:49:02.037122ZDownload09dc46aa901c700895ce1d50ab9485a7603e6dbfd3794da290bc925271f6e2b0
2.0.0-alpha+12017-07-14T21:23:20.302711ZDownload4e7e3655bdb6906b09413156986df78869970ef62d69b512fbff8e820b7a8857
2.0.0-alpha+22017-10-13T21:09:51.756086ZDownload69beb1e90d1a65d8145d39e4da27edb03681ea48065d1da10cd502adc74f357d
2.0.0-alpha+32017-10-16T17:50:44.667036ZDownload7e52f318901ae4f635ce2a6f04a601872405e6c2da2cc610d53bcf0d407e6f17
2.0.0-beta2017-10-24T17:38:40.925426ZDownload9bd3380ee57826daf0f0bf19916a4e944b4e40ab683780fe1afcdd9a8c38d5bf
2.0.02017-10-28T00:30:37.995010ZDownloadb144561b1118661122a462be72aec5f4f28b1e749cc40753d656c0e8065b8f14
2.1.0-dev2017-11-04T18:50:46.177705ZDownload2261237758604aad58e128dee211fdc60e68a772ee50fdda46a4a144d996901b
2.1.02017-11-04T19:00:12.159407ZDownloade9204b7b926caaa0960a9e80ac87a77164e745f8259d5c2f98f1d5bbd4e21e9d
2.2.02017-11-22T05:04:25.986601ZDownload2d77d79e2071ed1e8cdd6c78e9d30a1542ea36f5d377e653ad53f5741661e019
2.3.02017-11-27T04:00:06.080179ZDownload4ebfad42c99d4b674a371517bafb55a660a104a01dbbf16bfe94e2a72cd705ef
2.4.02017-12-11T02:32:13.665974ZDownload4f9a7207f438682599ed2d580a6c273a5102a30e0a4c4ee311103fed5e187d7b
3.0.0-alpha2017-12-22T20:21:47.613324ZDownloade7c727532fd0fd21b8103f5496abc34ff55ee40bd100869c887860b6259ef383
3.0.02018-01-09T22:06:43.966436ZDownload2efd14a2c05850aef186ee334660ab264bd1526cbb6cffb0964ec53c911ddf99
3.0.12018-01-30T03:51:30.697731ZDownloade3dc0196ddfb0059bef1e86287a0ed8b609738fb331fdfbdcd8461c491b49189
3.0.22018-02-10T01:15:04.068303ZDownloadfe56fce2bc71db3b8816c30bbef376e8edffc070984ba127f3b065d6f695422a
3.0.32018-02-10T18:56:26.277859ZDownloadcf31a3a5026c94d2230871341af0347b901ec6aa45c59b4e5430b8763ef5208a
3.1.02018-06-01T19:02:47.270893ZDownloadc2f44d74e6725c76d33dc6170eb23d21257613bbc0309568e53729d31188debf
3.1.12018-06-26T21:01:05.669551ZDownload1c8d55ebfa5597edbfd502f8bc92074c5619ca184c7e7e94228c18294937b194
3.1.22018-07-19T20:53:15.601046ZDownload64231d2dc1f2e9750071e487cea82ece2db3d251e2752834033f79effc84832f
3.1.32018-08-30T21:51:29.967770ZDownloadd5b26724036ef2963ad17a763e841276e75d92739cb2a4d677f24b9c79b852cb
3.2.02019-01-31T04:44:59.155758ZDownload6bde4e9614390381a78aa238e89d652b66bb1cbc0f6b3b0cf073c57f6ea4b1b7
3.2.12019-11-14T20:43:16.867324ZDownloadab5554329658ef482ca2c9a2813b10b90cd1bf8f2b9ad242d16ae7b6dfcaaf0c
3.3.02020-05-21T16:16:30.635656ZDownload0807294e83dcf467c441f6f308aad66ea09708d74c6fa625ceba25e9a85001a1
3.4.02020-07-08T19:47:40.192212ZDownload2c93d78a14af7dc767e590341b82be48be85119734a4a536dd6b49e6d94f0f0e
3.4.12020-08-18T16:11:34.885582ZDownload4844ffd63e8f3c1bb691369c2f9601885f6af00bb2a2631435bfc297e57db011
3.5.02020-10-09T21:31:50.079510ZDownload15385e1d0ba3c4a39993abdecc13917802d2e3d301ef9c5abed004703c05652b
3.6.02021-01-13T19:06:02.376795ZDownload5a10b34a0f4fd65d8858a3b1a5274038c4efd69318f242eb346c5cea8d44fa21
3.7.02021-03-10T20:14:40.892394ZDownload48ae73ba8dbf1923de91e0fd65736148f63f6c6fb0925bdda05c1df0298c55e1
4.0.02021-04-19T20:59:22.240928ZDownload98013335a650bf674a6f80d4ff99a1571eabc7f813df05ff253d9fa6daad064b
4.1.02021-07-20T20:01:36.331912ZDownloadbdb1ab29be158c4784d7f9b7b693745a0719c5899e31c01112782bb1cb871e80
4.2.02022-07-27T15:23:40.647887ZDownload43743b95913fd28b95184eb1bed7e4bd85b802b8fad0a52522702dbeda4ee3d5
4.3.02022-09-14T16:54:00.088958ZDownload02ce3596b459c666530f045ad6f96209474e8fee6e4855940a3cee65fb872ec5
4.4.02022-12-20T20:47:08.297918ZDownload0d43dd1288fd145de1ecc9a3948ad4a6d5a82f0a14c4fdd0892260787d975cbe
4.5.02023-06-01T04:06:21.641930ZDownload4ad01d6e56db961d29661561effde45e519939fdaeb46c351275b182eac70189
4.6.02023-08-30T00:13:33.006412ZDownload315a598c7fbe77f22de1c9da7cfd6fd21816312f16ffa124453b4fc679e540f1
4.7.02023-09-25T11:51:32.145164ZDownload1be9be30396d7e4c0db42c35ea6ccd7cc6a1e19916b5dc64d6ac216b5544d677
4.8.02023-11-20T20:50:39.959525ZDownloadb2151ce26a06171005b379ecff6e08d34c470180ffe16b8e14b6d52be292b55f
4.9.02023-12-15T18:45:17.725677ZDownloadfeee43a5c05e7b3199bb375a86430b8ada1b04104f2923d0e03cc01ca87b6d84
4.10.02024-01-08T18:07:42.286076ZDownloadf692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37
4.10.12024-10-29T15:12:38.031826ZDownload0ec10bf4a89e4c613960bf1e8b42c64127021740fb21640c29c909826a5eea3e
4.11.02025-09-10T17:40:30.797285ZDownload11654819532ba94c34de52ff5feb52bd81cba1de00ef2ed622fd50295f9d4243
4.11.12026-01-05T09:58:56.855476ZDownload6a6cab2ba4680d6423f34a9b972a4c9a94ebe1b62ecec4e1a1f2cba91fd1319d
4.12.02026-08-28T20:42:19.300883ZDownloadaa5932e94c6c39c2f9ec4e5e06dfdd11a9430a61f6c41b6ba75b28ce0c481baf
4.13.02026-10-13T23:21:33.033075ZDownload811709c1badd02fc054039a13f66e0db1961865f9a314175f68c3eaeebec786f

Dependencies

analyzer "\u003e=0.29.1 \u003c0.30.0"

dart_style "\u003e=0.2.10 \u003c2.0.0"

func "^0.1.0"

matcher "^0.12.0+2"

meta "^1.0.2"