prepareExecutables method

Iterable<({String executablePath, Future<ProcessResult> process})> prepareExecutables(
  1. List<DefinedHook> definedHooks, {
  2. required Directory hooksDartToolDir,
  3. required Directory executablesDir,
})

Implementation

Iterable<
    ({
      String executablePath,
      Future<ProcessResult> process,
    })> prepareExecutables(
  List<DefinedHook> definedHooks, {
  required Directory hooksDartToolDir,
  required Directory executablesDir,
}) sync* {
  if (hooksDartToolDir.existsSync()) {
    hooksDartToolDir.deleteSync(recursive: true);
  }
  executablesDir.createSync(recursive: true);

  logger.info(yellow.wrap('Preparing hooks'));

  for (final hook in definedHooks.where((e) => e.isDart)) {
    final relativePath =
        fs.path.relative(hook.path, from: hooksDartToolDir.path);

    final content = '''
import 'package:hooksman/hooksman.dart';

import '$relativePath' as hook;

void main() {
executeHook('${hook.name}', hook.main());
}''';

    final file = fs
        .file(fs.path.join(hooksDartToolDir.path, p.basename(hook.fileName)))
      ..createSync(recursive: true)
      ..writeAsStringSync(content);

    logger.info(darkGray.wrap('  - ${hook.name}'));

    final outFile = executablesDir.childFile(hook.name);

    final process = compiler.compile(
      file: file.path,
      outFile: outFile.path,
    );

    yield (executablePath: outFile.path, process: process);
  }

  for (final hook in definedHooks.where((e) => e.isShell)) {
    logger.info(darkGray.wrap('  - ${hook.name}'));

    final outFile = executablesDir.childFile(hook.name);

    final process = compiler.prepareShellExecutable(
      file: hook.path,
      outFile: outFile.path,
    );

    yield (executablePath: outFile.path, process: process);
  }

  logger.write('\n');
}