Skip to main content

cobalt_generator 1.7.0 copy "cobalt_generator: ^1.7.0" to clipboard
cobalt_generator: ^1.7.0 copied to clipboard

Code generator for Cobalt dependency injection. Annotate classes with @cobaltInject and build_runner writes the container, failing the build when a dependency is missing.

1.7.0 #

  • The generated names lose the $: CobaltRoot is the class, startCobalt, cobaltBootstrapSteps, cobaltRootScopeName and NoteEditorArgs the functions, constant and records. The bootstrap list is cobaltBootstrapSteps because cobaltBootstrap is the annotation.
  • The $ names are still written, as @Deprecated aliases, and go in 2.0. Code that uses them gets a deprecation hint; with --fatal-infos that fails the analysis, so rename: $CobaltRootScope to CobaltRoot, $startCobalt to startCobalt, $cobaltBootstrap to cobaltBootstrapSteps, $cobaltRootScopeName to cobaltRootScopeName, $XArgs to XArgs.
  • A decorator of a class only child scopes register is applied in each of those scopes and may inject what that scope sees. Before, the build refused it.

1.6.0 #

  • Each call-site record also gets a name without the $: typedef NoteEditorArgs = $NoteEditorArgs;. Left out when two classes would share the name or a class of the package already has it. The $ name stays until 2.0.
  • The generated root overrides hasBootstrap when the package has bootstrap steps, which needs cobalt 1.6.0.

1.5.0 #

  • Child scopes. Each @CobaltChildScope marker gets a builder, final class SessionScope implements CobaltScopeBuilder, that registers the marker's values and every class with scope: Session. The completeness check runs per scope along its parent chain: a registration may inject what its scope, the scopes above it and the root register, and the root injecting what only a child scope registers fails the build naming that scope. Decorators and hooks stay in the root.
  • $CobaltRootScope, and with it CobaltRoot, extends CobaltRootBuilder: it carries the name from @CobaltScopeRoot and, when the package has bootstrap steps, a bootstrap() that returns new ones. CobaltAppScope.builder(root: const CobaltRoot()) starts under that name. The root takes an environment whenever the bootstrap list depends on one.
  • The root promises each type in provides with scope.promise, so a promise the wrapping builder does not keep is reported at startup.
  • The generated code needs cobalt 1.5.0, which cobalt_analyzer 1.5.0 requires.

1.4.0 #

  • A constructor parameter with a default value is left out of the call when nothing in the graph registers its type, so the default applies. One that is positional and comes before an injected parameter is still a build error, which now says to make it named.
  • @CobaltInject(exposeAs: Store, instantiations: [...]) registers each instantiation under its own Store<Note>, Store<User>.
  • cobalt.g.dart also declares CobaltRoot, the root scope under a name without a dollar. Requires cobalt_analyzer 1.4.0.

1.3.0 #

  • @injected fields work on a generic class with @CobaltInject(instantiations: [...]). The class mixes in one _$Cache<T> carrying its type parameters, bounds included; each instantiation resolves the field under its own type arguments, and a dependency missing for one instantiation fails the build for that one alone. Requires cobalt_analyzer 1.3.0.

1.2.0 #

  • Generic classes: @CobaltInject(instantiations: [Cache<Note>, Cache<Tag>]) registers each listed instantiation with its own factory, named after its type arguments (_CacheOfNoteFactory; Pair<String, int> becomes _PairOfStringAndIntFactory), and each constructor resolves its own Store<Note> or Store<Tag>. A raw type, a duplicate entry, exposeAs or an @injected field next to instantiations is a build error.
  • A type argument keeps its ? in a registration's key: Cache<Note?> and Cache<Note> are two registrations, as they are two types at runtime. Before, the ? inside type arguments was dropped.
  • Requires cobalt_analyzer and cobalt_annotations 1.2.0.
  • The README opens with how to install it and a Quick start, and the pubspec description says what the generator does.

1.1.1 #

  • No code changes in this package. Republished in lockstep with 1.1.1: a shorter README with a Quick start, examples/hello as the smallest app, docs/OVERVIEW.md for everything the README no longer holds, and docs/TROUBLESHOOTING.md, one entry per error.

1.1.0 #

  • No code changes in this package. Republished in lockstep with 1.1.0, which adds Korean to the inspector and to the documentation.

1.0.0 #

  • The API is stable: from here on, only a major release breaks it — see Compatibility in the README. No code changes in this package since 0.9.0; coming from an older 0.x, MIGRATION lists what to change.

0.9.0 #

  • $startCobalt(initTimeout:), passed through to CobaltApplication.start.
  • Every generated factory implements CobaltDescribedFactory, naming the class it builds, so describeGraph shows it — ApiClient — lazySingleton, as LiveApiClient. The generated file changes accordingly.
  • A hook class is final class … extends CobaltHook<T> now that CobaltHook is a base class.

0.8.0 #

  • Emits scope.hookAll<T>(...) for every @cobaltHookAll class, first in the root scope — ahead of every registration, so an eager one passes through it — by order, then class name, and guarded by @CobaltEnvironment like any registration.
  • CobaltGenerationError is final.

0.7.0 #

  • No code changes in this package. Republished in lockstep with 0.7.0, which makes CobaltResolver a base class (breaking), adds getters to CobaltRegistrationKind and lets the inspector open on a chosen tab — see cobalt's changelog.

0.6.0 #

  • No code changes in this package. Republished in lockstep with 0.6.0, which adds build times to observers and the inspector, a graph snapshot in cobalt_test and a lint rule — see cobalt's changelog.

0.5.0 #

  • Async transients: a transient @CobaltInit class, or a transient module member returning a Future, becomes a CobaltAsyncFactory registered with registerAsyncFactory, awaiting lazy dependencies through getAsync. A synchronous or eager dependent, an @injected field of its type, a decorator taking it and a dependsOn naming it are build errors.
  • @CobaltDecorates(allNames: true) is emitted as scope.decorateAll. Decorators are emitted by type and then by order, and a decorator of every name counts for each key of its type in the order check, graph edges and cycles, and derived dependsOn; any registration of the type — or a provides: of it — satisfies its target.
  • @injected fields on a decorator class: the property injection builder writes its _$ClassName mixin, and the generated decorator builds it as Class(inner, ...)..onInject(resolver).

0.4.0 #

  • Async parameterized factories: an @CobaltInit class taking an @CobaltParam becomes a CobaltAsyncParamFactory that constructs, awaits init() and awaits any lazy dependency through getAsync, registered with registerAsyncParamFactory. A dependsOn naming one is a build error, and it takes no derived dependsOn of its own.

0.3.0 #

  • Decorators. Each @CobaltDecorates class becomes a const CobaltDecorator and a scope.decorate<Target>(...) after the registrations in build(), guarded by its environments, with the annotated class as its debugLabel.
  • The build refuses a target nothing registers in some environment of the decorator, a decorator dependency nothing registers, two decorators of one registration without distinct orders, a decorator taking a lazy async registration, and a cycle through a decorator.
  • An eager async class that resolves a decorated registration while phase 1 runs — directly or through synchronous registrations — waits in its dependsOn for the async dependencies of those decorators. The wait sits on the consumer, not the target, so an override of the target keeps it.

0.2.1 #

  • Accepts analyzer up to 14.x (>=10.0.1 <15.0.0, was <13.0.0) and dart_style up to 3.1.x (>=3.1.7 <3.2.0, was <3.1.9). Flutter 3.38.9 stays the floor.
  • The container is formatted at language version 3.10 rather than at DartFormatter.latestLanguageVersion, which moves with each dart_style release and gates its style changes. Every analyzer row emits identical bytes, and a future formatter release cannot change the committed output. Generated code does not change with this release.

0.2.0 #

  • Lazy async registrations are emitted with registerLazyAsyncSingleton, and their factories await resolver.getAsync<T>() for every dependency that is lazy too — isRegistered first for a nullable one.
  • Build errors for what would fail on the device instead: a synchronous or eager async class injecting a lazy one, an @injected field of a lazy type on any class, and dependsOn naming a lazy registration. A lazy module member is not given a derived dependsOn.
  • An eager singleton is emitted as registerEagerSingleton, so the scope builds it and an override can skip it; before, the instance existed before the registration was made.
  • $startCobalt takes overrides: and hands them to CobaltApplication.start.

0.1.2 #

  • No code changes in this package. Republished in lockstep with a packaging fix in cobalt 0.1.2 (a stray file removed from its archive) — see its changelog. A fix in one package still ships as a patch for all fifteen, because publishing a subset is what lets the set drift.

0.1.1 #

  • No code changes in this package. Republished in lockstep with the fix in cobalt_lint 0.1.1 — see its changelog. Lockstep is the whole versioning policy: a fix in one package still ships as a patch for all fifteen, because publishing a subset is what lets the set drift.

0.1.0 #

  • Requires Dart ^3.10.0 — Flutter 3.38 — instead of ^3.13.0, and moves analyzer to >=10.0.1 <13.0.0 with dart_style to >=3.1.6 <3.1.9. The two formatters that range admits emit identical bytes for generated code, so no generated file changed; the floor job regenerates and diffs to keep that true.

  • A nullable @CobaltParam keeps its ? in the generated record. It used to be dropped, narrowing an argument the constructor was willing to take as null.

  • A module member may take named parameters. They were refused because the emitter passed everything positionally; it no longer does. An optional parameter is still refused, for the reason it always was.

  • A class with @CobaltParam parameters is generated as a parameterized factory, with a named record type emitted beside the container. Marked parameters are not dependencies: they are skipped by the completeness check and are no edge in the ordering.

  • Constructors with named parameters are called the way they were declared. Every argument used to go in positionally, producing a file that did not compile.

  • The _$ClassName injection mixin is written for every class the container registers, @CobaltInit included. Such a class used to be registered and left with its @injected fields unassigned.

  • @CobaltInit(dependsOn:) naming a registration that is not itself @CobaltInit fails the build, listing every such edge at once.

  • Two libraries in one package declaring classes of the same name no longer emit two factories of the same name. Every claimant of a contested base name gets a suffix from its own library; uncontested names are untouched, so the fix renames nothing in an existing file.

  • Initial release.

  • Three builders: cobalt_property_injection (per-file mixins via SharedPartBuilder), cobalt_scan (per-library IR to .cobalt.json, cached) and cobalt_container (aggregates every IR into lib/cobalt.g.dart). All are auto_apply: dependents — declaring the dev dependency is the setup.

  • Emits named const factory classes, never closures, and orders registrations by a compile-time topological sort in which property-injected fields count as dependency edges.

  • Import aliases come from a hash of the URL, so adding one import does not renumber the rest and produce a whole-file diff.

  • Rejects a graph with a dependency nothing registers, listing every gap in one message and checking each environment separately. Registrations made by hand are declared through @CobaltScopeRoot(provides: [...]).

  • Rejects at build time what would otherwise be silent: a dependency cycle, two registrations of one key whose environments overlap, and @CobaltInject on a class with type parameters.

  • Generic types work as dependencies and as exposeAs targets: Repository<User> and Repository<Order> are separate registrations.

  • Emits factories for @CobaltModule members, calling the member on a const module instance. Async ordering between module members is derived from the graph rather than declared.

  • A nullable dependency is optional: it is emitted as getOrNull and skipped by the completeness check, while still ordering registration when the type is present. @CobaltInit(dependsOn:) is never optional, and a module member may not return a nullable type.

0
likes
160
points
1.59k
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Code generator for Cobalt dependency injection. Annotate classes with @cobaltInject and build_runner writes the container, failing the build when a dependency is missing.

Repository (GitHub)
View/report issues
Contributing

Topics

#dependency-injection #codegen #build-runner #di

License

MIT (license)

Dependencies

analyzer, build, cobalt_analyzer, cobalt_annotations, code_builder, collection, dart_style, glob, path, pub_semver, source_gen

More

Packages that depend on cobalt_generator