DartleDart constructor

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

Implementation

DartleDart([this.config = const DartConfig()])
    : rootDir = config.rootDir ?? '.',
      _enableBuildRunner = config.buildRunnerRunCondition != null {
  final allDartFiles = dir(rootDir,
      exclusions: const {'build'}, fileExtensions: const {'dart'});
  final productionDartFiles =
      dirs(['lib', 'bin'], fileExtensions: const {'dart'});

  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('$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(['$rootDir/pubspec.yaml', '$rootDir/pubspec.lock']))
      ]));

  test = Task(_test,
      name: 'test',
      description: 'Runs Dart tests.',
      dependsOn: {if (config.runAnalyzer) 'analyzeCode'},
      argsValidator: const TestTaskArgsValidator(),
      runCondition: RunOnChanges(
          inputs: dirs(['lib', 'bin', 'test', 'example']
              .map((e) => join(rootDir, e)))));

  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);
}