install method

Future<ModuleInstallationResult> install({
  1. required InitConfig config,
  2. required TemplateDefinition template,
  3. required String moduleName,
  4. PubspecService? pubspecService,
  5. bool syncDependencies = true,
  6. bool postGenerate = true,
})

Installs a module using the specified configuration and template.

The config defines the project configuration, template describes the module to generate, and moduleName identifies the module being installed.

An optional pubspecService can be provided to manage dependency changes. When syncDependencies is true, required dependencies are synchronized. When postGenerate is true, post-generation tasks are executed.

Returns a ModuleInstallationResult describing the installation outcome.

Implementation

Future<ModuleInstallationResult> install({
  required InitConfig config,
  required TemplateDefinition template,
  required String moduleName,
  PubspecService? pubspecService,
  bool syncDependencies = true,
  bool postGenerate = true,
}) async {
  final module = await const ModuleService().load(
    template: template.name,
    module: moduleName,
  );

  final options = await const ModuleOptionResolver().resolve(
    module,
  );

  final context = ModuleContext(
    config: config,
    template: template,
    module: module,
    options: options,
  );

  final generated = await const ModuleGenerator().generate(
    context,
  );

  if (!generated) {
    return ModuleInstallationResult(
      moduleName: moduleName,
      installed: false,
      requiresBuildRunner: module.requiresBuildRunner,
    );
  }

  final pubspec = pubspecService ?? PubspecService();

  if (context.enabledPackages.isNotEmpty) {
    await pubspec.ensureDependencies(
      context.enabledPackages,
    );
  }

  if (context.enabledDevPackages.isNotEmpty) {
    await pubspec.ensureDevDependencies(
      context.enabledDevPackages,
    );
  }

  if (syncDependencies) {
    await pubspec.save();
  }

  await const ModuleIntegrationService().integrate(
    context,
  );

  if (postGenerate) {
    await FlutterService(config).postGenerate(
      buildRunner: module.requiresBuildRunner,
    );
  }

  return ModuleInstallationResult(
    moduleName: moduleName,
    installed: true,
    requiresBuildRunner: module.requiresBuildRunner,
  );
}