runDartlex function

Future<void> runDartlex(
  1. List<String> args,
  2. Options options, {
  3. bool doNotExit = false,
})

Run the binary version of 'dartle.dart', (re)compiling it if necessary.

This method will normally not return as Dartle will exit with the appropriate code. To avoid that, set doNotExit to true.

Implementation

Future<void> runDartlex(List<String> args, Options options,
    {bool doNotExit = false}) async {
  await checkProjectInit(doNotExit, options.runPubGet);

  final compileTask = await _createDartCompileTask();
  final recompileCondition = compileTask.runCondition as RunOnChanges;
  final compileDartlexInvocation = TaskInvocation(compileTask);

  if (await recompileCondition.shouldRun(compileDartlexInvocation)) {
    logger.info('Detected changes in dartle.dart or pubspec, '
        'compiling Dartle executable.');

    // as the build sources have changed, we must invalidate the cache
    await recompileCondition.cache.clean();
    recompileCondition.cache.init();

    final stopWatch = Stopwatch()..start();
    final success = await _runTask(compileDartlexInvocation);
    stopWatch.stop();
    if (success) {
      logger.info('Re-compiled dartle.dart in ${elapsedTime(stopWatch)}');
    } else {
      if (doNotExit) {
        throw Exception('Error running task ${compileTask.name}');
      } else {
        exit(2);
      }
    }
  }

  logger.fine('Running cached dartlex...');
  final proc = await runDartExe(_cachedDartlex, args: args);
  final stdoutFuture = stdout.addStream(proc.stdout);
  final stderrFuture = stderr.addStream(proc.stderr);

  final exitCode = await proc.exitCode;

  await stdoutFuture;
  await stderrFuture;

  if (doNotExit) {
    if (exitCode != 0) {
      throw Exception('dartle process exited with code $exitCode');
    }
  } else {
    exit(exitCode);
  }
}