PackageInfo class

Provides package identification for connecting macros to the Macro server.

This class specifies which package(s) the macro should analyze, enabling the macro to establish a connection with the server and access the analysis context.

Development vs CI/CD Environments

In Development (IDE/Editor): The analyzer plugin automatically provides package information to the macro server, so you typically only need to specify the package name:

final packageInfo = PackageInfo('my_app');

In CI/CD or Without Plugin: When the analyzer plugin isn't running (e.g., in CI pipelines, automated tests, or standalone builds), you must provide the absolute path to the package root so the macro server can initialize its analysis context:

final packageInfo = PackageInfo.path('/workspace/my_app');

Package Identification Methods

You can identify packages in two ways:

  1. By name - Works when the analyzer plugin is active (development)
  2. By path - Required when the plugin isn't available (CI/CD, standalone)

Usage Examples

Single Package:

// Development: by package name (plugin provides context)
final packageInfo = PackageInfo('my_app');

// CI/CD: by absolute path (no plugin available)
final packageInfo = PackageInfo.path('/workspace/my_app');

// For test directory specifically
final packageInfo = PackageInfo.path('/workspace/my_app/test');

Multiple Packages (Mono-repos):

final packageInfo = PackageInfo.mixed([
  'my_app',                           // By name (dev)
  '/workspace/shared_models',         // By path (CI)
  '/workspace/core_lib',              // By path (CI)
]);

Auto-Rebuild & Regeneration

Providing the correct package path is crucial for auto-rebuild functionality. When enabled, the macro server monitors the specified package(s) and automatically regenerates code based on your macro.json configuration.

Auto-Rebuild Behavior:

  • When you call runMacro() in your main.dart and have auto_rebuild_on_connect: true in macro.json, any macro within the specified package/directory will automatically rebuild upon connection.
  • If always_rebuild_on_connect: true, macros will rebuild on every client connection, including when you restart your Flutter app multiple times.

Example for CI with auto-rebuild:

final packageInfo = PackageInfo.path(
  Platform.environment['CI_PROJECT_DIR'] ?? '/workspace/my_app'
);

Important Notes

  • Package names must exactly match the name in pubspec.yaml
  • Paths must be absolute and point to a directory containing pubspec.yaml (or a valid subdirectory like test/)
  • In CI/CD environments, always use absolute paths since the plugin isn't available
  • All specified packages must be valid and accessible for initialization to succeed
  • If you need code regeneration in CI, ensure you run the macro server before executing your code or tests:
dart pub global activate macro_kit
macro  # Start the server before runMacro()

Constructors

PackageInfo(String name)
Creates a PackageInfo for a single package by name.
factory
PackageInfo.mixed(Iterable<String> nameOrAbsolutePath)
Creates a PackageInfo for multiple packages using names and/or paths.
factory
PackageInfo.path(String absolutePath)
Creates a PackageInfo for a single package by absolute path.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
values List<String>
The list of package names that can connect to the MacroPlugin server.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
parsedPackageWithId() List<({String id, String name})>
Return a list of tuple for each package with extracted id
toJson() Map<String, dynamic>
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromJson(Map<String, dynamic> json) PackageInfo