executable.dart 2.69 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6 7 8
import 'dart:async';
import 'dart:io';

import 'package:args/command_runner.dart';
9
import 'package:logging/logging.dart';
10
import 'package:stack_trace/stack_trace.dart';
11

Hixie's avatar
Hixie committed
12
import 'src/commands/analyze.dart';
13 14
import 'src/commands/build.dart';
import 'src/commands/cache.dart';
Devon Carew's avatar
Devon Carew committed
15
import 'src/commands/daemon.dart';
16
import 'src/commands/flutter_command_runner.dart';
17 18 19 20 21 22 23 24
import 'src/commands/init.dart';
import 'src/commands/install.dart';
import 'src/commands/list.dart';
import 'src/commands/listen.dart';
import 'src/commands/logs.dart';
import 'src/commands/run_mojo.dart';
import 'src/commands/start.dart';
import 'src/commands/stop.dart';
25
import 'src/commands/test.dart';
26
import 'src/commands/trace.dart';
27
import 'src/commands/upgrade.dart';
28
import 'src/process.dart';
29 30 31 32

/// Main entry point for commands.
///
/// This function is intended to be used from the [flutter] command line tool.
33
Future main(List<String> args) async {
34
  // This level can be adjusted by users through the `--verbose` option.
Devon Carew's avatar
Devon Carew committed
35
  Logger.root.level = Level.WARNING;
36
  Logger.root.onRecord.listen((LogRecord record) {
37
    String level = record.level.name.toLowerCase();
38
    if (record.level >= Level.WARNING) {
39
      stderr.writeln('$level: ${record.message}');
40
    } else {
41
      print('$level: ${record.message}');
42
    }
43
    if (record.error != null)
44
      stderr.writeln(record.error);
45
    if (record.stackTrace != null)
46
      stderr.writeln(record.stackTrace);
47 48
  });

49
  FlutterCommandRunner runner = new FlutterCommandRunner()
Hixie's avatar
Hixie committed
50
    ..addCommand(new AnalyzeCommand())
51 52
    ..addCommand(new BuildCommand())
    ..addCommand(new CacheCommand())
Devon Carew's avatar
Devon Carew committed
53
    ..addCommand(new DaemonCommand())
54 55 56 57 58 59 60 61
    ..addCommand(new InitCommand())
    ..addCommand(new InstallCommand())
    ..addCommand(new ListCommand())
    ..addCommand(new ListenCommand())
    ..addCommand(new LogsCommand())
    ..addCommand(new RunMojoCommand())
    ..addCommand(new StartCommand())
    ..addCommand(new StopCommand())
62
    ..addCommand(new TestCommand())
63 64
    ..addCommand(new TraceCommand())
    ..addCommand(new UpgradeCommand());
65

66
  return Chain.capture(() async {
67 68 69
    dynamic result = await runner.run(args);
    if (result is int)
      exit(result);
70 71 72 73 74 75
  }, onError: (error, Chain chain) {
    if (error is UsageException) {
      stderr.writeln(error);
      // Argument error exit code.
      exit(64);
    } else if (error is ProcessExit) {
76
      // We've caught an exit code.
77 78 79 80 81
      exit(error.exitCode);
    } else {
      stderr.writeln(error);
      Logger.root.log(Level.SEVERE, '\nException:', null, chain.terse.toTrace());
      exit(1);
82
    }
83
  });
84
}