executable.dart 8.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'package:meta/meta.dart';

9
import 'runner.dart' as runner;
10
import 'src/artifacts.dart';
11
import 'src/base/context.dart';
12
import 'src/base/io.dart';
13
import 'src/base/logger.dart';
14
import 'src/base/platform.dart';
15
import 'src/base/template.dart';
16
import 'src/base/terminal.dart';
17 18
import 'src/base/user_messages.dart';
import 'src/cache.dart';
Hixie's avatar
Hixie committed
19
import 'src/commands/analyze.dart';
20
import 'src/commands/assemble.dart';
21
import 'src/commands/attach.dart';
22
import 'src/commands/build.dart';
23
import 'src/commands/channel.dart';
24
import 'src/commands/clean.dart';
25
import 'src/commands/config.dart';
26
import 'src/commands/create.dart';
27
import 'src/commands/custom_devices.dart';
Devon Carew's avatar
Devon Carew committed
28
import 'src/commands/daemon.dart';
29
import 'src/commands/debug_adapter.dart';
30
import 'src/commands/devices.dart';
31
import 'src/commands/doctor.dart';
32
import 'src/commands/downgrade.dart';
yjbanov's avatar
yjbanov committed
33
import 'src/commands/drive.dart';
34
import 'src/commands/emulators.dart';
35
import 'src/commands/format.dart';
36
import 'src/commands/generate.dart';
37
import 'src/commands/generate_localizations.dart';
38
import 'src/commands/ide_config.dart';
39 40
import 'src/commands/install.dart';
import 'src/commands/logs.dart';
41
import 'src/commands/make_host_app_editable.dart';
42
import 'src/commands/packages.dart';
43
import 'src/commands/precache.dart';
44
import 'src/commands/run.dart';
Devon Carew's avatar
Devon Carew committed
45
import 'src/commands/screenshot.dart';
46
import 'src/commands/shell_completion.dart';
47
import 'src/commands/symbolize.dart';
48
import 'src/commands/test.dart';
49
import 'src/commands/update_packages.dart';
50
import 'src/commands/upgrade.dart';
51
import 'src/devtools_launcher.dart';
52
import 'src/features.dart';
53
import 'src/globals.dart' as globals;
54 55 56
// Files in `isolated` are intentionally excluded from google3 tooling.
import 'src/isolated/mustache_template.dart';
import 'src/isolated/resident_web_runner.dart';
57
import 'src/pre_run_validator.dart';
58
import 'src/resident_runner.dart';
59
import 'src/runner/flutter_command.dart';
60
import 'src/web/web_runner.dart';
61

62 63
/// Main entry point for commands.
///
64
/// This function is intended to be used from the `flutter` command line tool.
65
Future<void> main(List<String> args) async {
66 67
  final bool veryVerbose = args.contains('-vv');
  final bool verbose = args.contains('-v') || args.contains('--verbose') || veryVerbose;
68
  final bool prefixedErrors = args.contains('--prefixed-errors');
69 70 71 72 73
  // Support the -? Powershell help idiom.
  final int powershellHelpIndex = args.indexOf('-?');
  if (powershellHelpIndex != -1) {
    args[powershellHelpIndex] = '-h';
  }
74 75 76

  final bool doctor = (args.isNotEmpty && args.first == 'doctor') ||
      (args.length == 2 && verbose && args.last == 'doctor');
77
  final bool help = args.contains('-h') || args.contains('--help') ||
78
      (args.isNotEmpty && args.first == 'help') || (args.length == 1 && verbose);
79
  final bool muteCommandLogging = (help || doctor) && !veryVerbose;
80
  final bool verboseHelp = help && verbose;
81
  final bool daemon = args.contains('daemon');
82 83
  final bool runMachine = (args.contains('--machine') && args.contains('run')) ||
                          (args.contains('--machine') && args.contains('attach'));
Devon Carew's avatar
Devon Carew committed
84

85 86 87 88 89
  // Cache.flutterRoot must be set early because other features use it (e.g.
  // enginePath's initializer uses it). This can only work with the real
  // instances of the platform or filesystem, so just use those.
  Cache.flutterRoot = Cache.defaultFlutterRoot(
    platform: const LocalPlatform(),
90
    fileSystem: globals.localFileSystem,
91 92 93
    userMessages: UserMessages(),
  );

94 95 96
  await runner.run(
    args,
    () => generateCommands(
97
      verboseHelp: verboseHelp,
98
      verbose: verbose,
99
    ),
100 101 102 103 104 105 106 107 108 109 110 111 112
    verbose: verbose,
    muteCommandLogging: muteCommandLogging,
    verboseHelp: verboseHelp,
    overrides: <Type, Generator>{
      // The web runner is not supported in google3 because it depends
      // on dwds.
      WebRunnerFactory: () => DwdsWebRunnerFactory(),
      // The mustache dependency is different in google3
      TemplateRenderer: () => const MustacheTemplateRenderer(),
      // The devtools launcher is not supported in google3 because it depends on
      // devtools source code.
      DevtoolsLauncher: () => DevtoolsServerLauncher(
        processManager: globals.processManager,
113
        dartExecutable: globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
114
        logger: globals.logger,
115
        botDetector: globals.botDetector,
116 117
      ),
      Logger: () {
118 119 120 121 122 123 124
        final LoggerFactory loggerFactory = LoggerFactory(
          outputPreferences: globals.outputPreferences,
          terminal: globals.terminal,
          stdio: globals.stdio,
        );
        return loggerFactory.createLogger(
          daemon: daemon,
125
          machine: runMachine,
126 127 128 129
          verbose: verbose && !muteCommandLogging,
          prefixedErrors: prefixedErrors,
          windows: globals.platform.isWindows,
        );
130
      },
131
      PreRunValidator: () => PreRunValidator(fileSystem: globals.fs),
132 133
    },
  );
134
}
135

136 137 138 139 140 141 142 143 144 145 146 147 148
List<FlutterCommand> generateCommands({
  @required bool verboseHelp,
  @required bool verbose,
}) => <FlutterCommand>[
  AnalyzeCommand(
    verboseHelp: verboseHelp,
    fileSystem: globals.fs,
    platform: globals.platform,
    processManager: globals.processManager,
    logger: globals.logger,
    terminal: globals.terminal,
    artifacts: globals.artifacts,
  ),
149
  AssembleCommand(verboseHelp: verboseHelp, buildSystem: globals.buildSystem),
150 151 152 153 154
  AttachCommand(verboseHelp: verboseHelp),
  BuildCommand(verboseHelp: verboseHelp),
  ChannelCommand(verboseHelp: verboseHelp),
  CleanCommand(verbose: verbose),
  ConfigCommand(verboseHelp: verboseHelp),
155 156 157 158 159 160 161 162 163 164
  CustomDevicesCommand(
    customDevicesConfig: globals.customDevicesConfig,
    operatingSystemUtils: globals.os,
    terminal: globals.terminal,
    platform: globals.platform,
    featureFlags: featureFlags,
    processManager: globals.processManager,
    fileSystem: globals.fs,
    logger: globals.logger
  ),
165 166
  CreateCommand(verboseHelp: verboseHelp),
  DaemonCommand(hidden: !verboseHelp),
167
  DebugAdapterCommand(verboseHelp: verboseHelp),
168 169 170 171 172 173
  DevicesCommand(verboseHelp: verboseHelp),
  DoctorCommand(verbose: verbose),
  DowngradeCommand(verboseHelp: verboseHelp),
  DriveCommand(verboseHelp: verboseHelp,
    fileSystem: globals.fs,
    logger: globals.logger,
174
    platform: globals.platform,
175 176
  ),
  EmulatorsCommand(),
177
  FormatCommand(verboseHelp: verboseHelp),
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  GenerateCommand(),
  GenerateLocalizationsCommand(
    fileSystem: globals.fs,
    logger: globals.logger,
  ),
  InstallCommand(),
  LogsCommand(),
  MakeHostAppEditableCommand(),
  PackagesCommand(),
  PrecacheCommand(
    verboseHelp: verboseHelp,
    cache: globals.cache,
    logger: globals.logger,
    platform: globals.platform,
    featureFlags: featureFlags,
  ),
  RunCommand(verboseHelp: verboseHelp),
  ScreenshotCommand(),
  ShellCompletionCommand(),
  TestCommand(verboseHelp: verboseHelp),
  UpgradeCommand(verboseHelp: verboseHelp),
  SymbolizeCommand(
    stdio: globals.stdio,
    fileSystem: globals.fs,
  ),
  // Development-only commands. These are always hidden,
  IdeConfigCommand(),
  UpdatePackagesCommand(),
];

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/// An abstraction for instantiation of the correct logger type.
///
/// Our logger class hierarchy and runtime requirements are overly complicated.
class LoggerFactory {
  LoggerFactory({
    @required Terminal terminal,
    @required Stdio stdio,
    @required OutputPreferences outputPreferences,
    StopwatchFactory stopwatchFactory = const StopwatchFactory(),
  }) : _terminal = terminal,
       _stdio = stdio,
       _stopwatchFactory = stopwatchFactory,
       _outputPreferences = outputPreferences;

  final Terminal _terminal;
  final Stdio _stdio;
  final StopwatchFactory _stopwatchFactory;
  final OutputPreferences _outputPreferences;

  /// Create the appropriate logger for the current platform and configuration.
  Logger createLogger({
    @required bool verbose,
230
    @required bool prefixedErrors,
231
    @required bool machine,
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    @required bool daemon,
    @required bool windows,
  }) {
    Logger logger;
    if (windows) {
      logger = WindowsStdoutLogger(
        terminal: _terminal,
        stdio: _stdio,
        outputPreferences: _outputPreferences,
        stopwatchFactory: _stopwatchFactory,
      );
    } else {
      logger = StdoutLogger(
        terminal: _terminal,
        stdio: _stdio,
        outputPreferences: _outputPreferences,
248
        stopwatchFactory: _stopwatchFactory
249 250 251 252 253
      );
    }
    if (verbose) {
      logger = VerboseLogger(logger, stopwatchFactory: _stopwatchFactory);
    }
254 255 256
    if (prefixedErrors) {
      logger = PrefixedErrorLogger(logger);
    }
257 258 259
    if (daemon) {
      return NotifyingLogger(verbose: verbose, parent: logger);
    }
260
    if (machine) {
261 262 263 264 265
      return AppRunLogger(parent: logger);
    }
    return logger;
  }
}