DartleDart constructor

DartleDart([
  1. DartConfig config = const DartConfig()
])

Implementation

DartleDart([this.config = const DartConfig()])
    : rootDir = config.rootDir ?? '.',
      _enableBuildRunner = config.buildRunnerRunCondition != null {
  final productionDartFiles =
      config.sources ?? dirs(['lib', 'bin'], fileExtensions: const {'dart'});
  final testDartFiles = config.testSources ??
      dirs(['test', 'example'], fileExtensions: const {'dart'});
  final buildDartFiles = config.buildSources ??
      (dir('dartle-src', fileExtensions: const {'dart'}) +
          file('dartle.dart'));
  final allDartFiles = productionDartFiles + testDartFiles + buildDartFiles;

  formatCode = Task(_formatCode,
      name: 'format',
      dependsOn: {if (_enableBuildRunner) 'runBuildRunner'},
      description: 'Formats all Dart source code.',
      runCondition: RunOnChanges(inputs: allDartFiles));

  runBuildRunner = Task(_runBuildRunner,
      name: 'runBuildRunner',
      description: 'Runs the Dart build_runner tool.',
      dependsOn: {'runPubGet'},
      runCondition: config.buildRunnerRunCondition ?? const AlwaysRun());

  analyzeCode = Task(_analyzeCode,
      name: 'analyzeCode',
      dependsOn: {
        'runPubGet',
        if (config.formatCode) 'format',
        if (_enableBuildRunner) 'runBuildRunner',
      },
      description: 'Analyzes Dart source code',
      runCondition: RunOnChanges(inputs: allDartFiles));

  compileExe = Task(_compileExe,
      name: 'compileExe',
      description: 'Compiles Dart executables declared in pubspec. '
          'Argument may specify the name(s) of the executable(s) to compile.',
      dependsOn: {'analyzeCode'},
      argsValidator: const AcceptAnyArgs(),
      runCondition: RunOnChanges(
          inputs: productionDartFiles,
          outputs: dir(join(rootDir, 'build', 'bin'))));

  runPubGet = Task(utils.runPubGet,
      name: 'runPubGet',
      description: 'Runs "pub get" in order to update dependencies.',
      runCondition: OrCondition([
        RunAtMostEvery(config.runPubGetAtMostEvery),
        RunOnChanges(
            inputs: files([
          join(rootDir, 'pubspec.yaml'),
          join(rootDir, 'pubspec.lock')
        ]))
      ]));

  test = Task(_test,
      name: 'test',
      description: 'Runs Dart tests.',
      dependsOn: {if (config.runAnalyzer) 'analyzeCode'},
      argsValidator: const TestTaskArgsValidator(),
      runCondition: RunOnChanges(inputs: testDartFiles));

  final buildTasks = {
    if (config.formatCode) formatCode,
    if (config.runAnalyzer) analyzeCode,
    if (config.compileExe) compileExe,
    if (_enableBuildRunner) runBuildRunner,
    if (config.runTests) test,
    runPubGet,
  };

  clean = createCleanTask(
      tasks: buildTasks,
      name: 'clean',
      description: 'Deletes the outputs of all other tasks in this build.');

  build = Task((_) => null, // no action, just grouping other tasks
      name: 'build',
      description: 'Runs all enabled tasks.');

  build.dependsOn(buildTasks
      // exclude tasks already added into buildTasks but that
      // should not run by default
      .where((t) => t != compileExe)
      .map((t) => t.name)
      .toSet());

  buildTasks.add(clean);
  buildTasks.add(build);

  tasks = Set.unmodifiable(buildTasks);
}