runHooks method

Future<void> runHooks(
  1. List<HookConfig> hooks,
  2. HookContext context
)

Runs hooks that apply to context.generatorType.

In --dry-run mode, logs the command that would be executed without actually running it.

Implementation

Future<void> runHooks(List<HookConfig> hooks, HookContext context) async {
  for (final hook in hooks) {
    if (!hook.appliesTo(context.generatorType)) continue;

    final label = hook.description != null
        ? '${hook.description} (${hook.run})'
        : hook.run;

    if (context.dryRun) {
      _log.warn('DRY RUN: would run hook "$label"');
      continue;
    }

    _log.info('Running hook: $label');

    final env = Map<String, String>.from(Platform.environment)
      ..['REKEENS_GENERATOR_TYPE'] = context.generatorType
      ..['REKEENS_FEATURE_NAME'] = context.featureName;
    if (context.entityName != null) {
      env['REKEENS_ENTITY_NAME'] = context.entityName!;
    }

    final result = await Process.run(
      _shellExecutable(),
      [..._shellFlags(), hook.run],
      workingDirectory: _workingDirectory,
      environment: env,
    );

    if (result.exitCode != 0) {
      final stderr = result.stderr.toString().trim();
      final stdout = result.stdout.toString().trim();
      _log.err('Hook failed (exit ${result.exitCode}): $label');
      if (stdout.isNotEmpty) _log.info(stdout);
      if (stderr.isNotEmpty) _log.err(stderr);
      throw Exception(
        'Hook "$label" failed with exit code ${result.exitCode}.',
      );
    }

    final stdout = result.stdout.toString().trim();
    if (stdout.isNotEmpty) _log.info(stdout);
  }
}