run.dart 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:args/args.dart';
10
import 'package:path/path.dart' as path;
11 12 13 14 15

import 'package:flutter_devicelab/framework/manifest.dart';
import 'package:flutter_devicelab/framework/runner.dart';
import 'package:flutter_devicelab/framework/utils.dart';

16 17
List<String> _taskNames = <String>[];

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/// Runs tasks.
///
/// The tasks are chosen depending on the command-line options
/// (see [_argParser]).
Future<Null> main(List<String> rawArgs) async {
  ArgResults args;
  try {
    args = _argParser.parse(rawArgs);
  } on FormatException catch(error) {
    stderr.writeln('${error.message}\n');
    stderr.writeln('Usage:\n');
    stderr.writeln(_argParser.usage);
    exitCode = 1;
    return null;
  }

34 35
  if (!args.wasParsed('task')) {
    if (args.wasParsed('stage')) {
36 37
      final String stageName = args['stage'];
      final List<ManifestTask> tasks = loadTaskManifest().tasks;
38 39 40 41 42
      for (ManifestTask task in tasks) {
        if (task.stage == stageName)
          _taskNames.add(task.name);
      }
    } else if (args.wasParsed('all')) {
43
      final List<ManifestTask> tasks = loadTaskManifest().tasks;
44 45 46
      for (ManifestTask task in tasks) {
        _taskNames.add(task.name);
      }
47 48 49
    }
  }

50
  if (_taskNames.isEmpty) {
51 52 53 54 55
    stderr.writeln('Failed to find tasks to run based on supplied options.');
    exitCode = 1;
    return null;
  }

56
  final bool silent = args['silent'];
57

58
  for (String taskName in _taskNames) {
59
    section('Running task "$taskName"');
60
    final Map<String, dynamic> result = await runTask(taskName, silent: silent);
61 62 63 64 65

    if (!result['success'])
      exitCode = 1;

    print('Task result:');
66
    print(const JsonEncoder.withIndent('  ').convert(result));
67 68 69 70 71 72 73 74 75 76 77
    section('Finished task "$taskName"');
  }
}

/// Command-line options for the `run.dart` command.
final ArgParser _argParser = new ArgParser()
  ..addOption(
    'task',
    abbr: 't',
    allowMultiple: true,
    splitCommas: true,
78 79 80 81 82 83 84
    help: 'Either:\n'
        ' - the name of a task defined in manifest.yaml. Example: complex_layout__start_up.\n'
        ' - the path to a Dart file corresponding to a task, which resides in bin/tasks. Example: bin/tasks/complex_layout__start_up.dart.\n'
        '\n'
        'This option may be repeated to specify multiple tasks.',
    callback: (List<String> value) {
      for (String nameOrPath in value) {
85 86
        final List<String> fragments = path.split(nameOrPath);
        final bool isDartFile = fragments.last.endsWith('.dart');
87 88 89 90 91 92 93 94 95 96 97 98

        if (fragments.length == 1 && !isDartFile) {
          // Not a path
          _taskNames.add(nameOrPath);
        } else if (!isDartFile || fragments.length != 3 || !_listsEqual(<String>['bin', 'tasks'], fragments.take(2).toList())) {
          // Unsupported executable location
          throw new FormatException('Invalid value for option -t (--task): $nameOrPath');
        } else {
          _taskNames.add(path.withoutExtension(fragments.last));
        }
      }
    },
99 100 101 102 103 104 105
  )
  ..addOption(
    'stage',
    abbr: 's',
    help: 'Name of the stage. Runs all tasks for that stage. '
        'The tasks and their stages are read from manifest.yaml.',
  )
106
  ..addFlag(
107 108 109 110 111 112 113 114 115 116 117
    'all',
    abbr: 'a',
    help: 'Runs all tasks defined in manifest.yaml.',
  )
  ..addOption(
    'test',
    hide: true,
    allowMultiple: true,
    splitCommas: true,
    callback: (List<String> value) {
      if (value.isNotEmpty) {
118
        throw const FormatException(
119 120 121 122
          'Invalid option --test. Did you mean --task (-t)?',
        );
      }
    },
123 124 125 126 127
  )
  ..addFlag(
    'silent',
    negatable: true,
    defaultsTo: false,
128
  );
129 130

bool _listsEqual(List<dynamic> a, List<dynamic> b) {
131 132
  if (a.length != b.length)
    return false;
133 134 135 136 137 138 139 140

  for (int i = 0; i < a.length; i++) {
    if (a[i] != b[i])
      return false;
  }

  return true;
}