putInOven method

Future<ExitCode> putInOven(
  1. Set<Brick> bricks
)

cooks the bricks

If isWatch is true, the bricks will be cooked on every change to the BrickOvenYaml.file or Brick.configPath and the Brick.sourcePath directory/files.

Implementation

Future<ExitCode> putInOven(Set<Brick> bricks) async {
  logger.preheat();

  for (final brick in bricks) {
    if (isWatch) {
      brick.watcher
        ..addEvent(
          logger.fileChanged,
          runBefore: true,
        )
        ..addEvent((_) => logger.preheat(), runBefore: true)
        ..addEvent((_) => logger.dingDing(), runAfter: true)
        ..addEvent((_) => logger.watching(), runAfter: true)
        ..addEvent((_) => logger.keyStrokes(), runAfter: true);
    }

    try {
      brick.cook();
    } on ConfigException catch (e, stacktrace) {
      logger
        ..warn(e.message)
        ..err('Could not cook brick: ${brick.name}');

      if (isVerbose) {
        logger.info(stacktrace.toString());
      }

      continue;
    } catch (e, stacktrace) {
      logger
        ..warn('Unknown error: $e')
        ..err('Could not cook brick: ${brick.name}');

      if (isVerbose) {
        logger.info(stacktrace.toString());
      }
      continue;
    }
  }

  logger.dingDing();

  if (!isWatch) {
    return ExitCode.success;
  }

  logger.watching();

  keyListener.listenToKeystrokes();

  for (final brick in bricks) {
    if (brick.configPath == null) {
      continue;
    }

    unawaited(
      watchForConfigChanges(
        brick.configPath!,
        onChange: () async {
          logger.configChanged();

          await cancelConfigWatchers(shouldQuit: false);
          await brick.watcher.stop();
        },
      ),
    );
  }

  final shouldQuit = await watchForConfigChanges(
    BrickOvenYaml.file,
    onChange: () async {
      logger.configChanged();

      for (final brick in bricks) {
        await brick.watcher.stop();
      }
    },
  );

  if (shouldQuit) {
    return ExitCode.success;
  }

  return ExitCode.tempFail;
}