2015-10-10 06:41:40 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-10-13 20:36:39 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:args/command_runner.dart';
|
2017-02-23 21:37:26 +00:00
|
|
|
import 'package:intl/intl_standalone.dart' as intl;
|
2017-03-04 00:54:47 +00:00
|
|
|
import 'package:meta/meta.dart';
|
2017-01-23 17:46:38 +00:00
|
|
|
import 'package:process/process.dart';
|
2015-10-10 06:41:40 +00:00
|
|
|
|
2017-02-23 21:37:26 +00:00
|
|
|
import 'src/artifacts.dart';
|
2016-10-06 21:49:14 +00:00
|
|
|
import 'src/base/common.dart';
|
2016-11-29 19:22:48 +00:00
|
|
|
import 'src/base/config.dart';
|
2016-02-07 03:19:50 +00:00
|
|
|
import 'src/base/context.dart';
|
2017-01-07 00:51:44 +00:00
|
|
|
import 'src/base/file_system.dart';
|
2017-01-09 16:37:00 +00:00
|
|
|
import 'src/base/io.dart';
|
2016-02-07 03:19:50 +00:00
|
|
|
import 'src/base/logger.dart';
|
2017-01-26 00:06:41 +00:00
|
|
|
import 'src/base/platform.dart';
|
2015-11-29 05:07:16 +00:00
|
|
|
import 'src/base/process.dart';
|
2016-03-25 23:04:22 +00:00
|
|
|
import 'src/base/utils.dart';
|
2016-11-29 19:22:48 +00:00
|
|
|
import 'src/cache.dart';
|
2015-11-11 18:29:05 +00:00
|
|
|
import 'src/commands/analyze.dart';
|
2015-10-10 06:41:40 +00:00
|
|
|
import 'src/commands/build.dart';
|
2016-08-25 19:16:21 +00:00
|
|
|
import 'src/commands/channel.dart';
|
2016-04-26 23:25:11 +00:00
|
|
|
import 'src/commands/config.dart';
|
2016-01-27 22:37:29 +00:00
|
|
|
import 'src/commands/create.dart';
|
2015-11-02 21:05:05 +00:00
|
|
|
import 'src/commands/daemon.dart';
|
2016-02-14 07:50:20 +00:00
|
|
|
import 'src/commands/devices.dart';
|
2016-02-17 02:23:43 +00:00
|
|
|
import 'src/commands/doctor.dart';
|
2016-02-20 00:31:34 +00:00
|
|
|
import 'src/commands/drive.dart';
|
2016-08-31 13:40:17 +00:00
|
|
|
import 'src/commands/format.dart';
|
2017-03-14 18:12:19 +00:00
|
|
|
import 'src/commands/fuchsia_reload.dart';
|
2015-10-10 06:41:40 +00:00
|
|
|
import 'src/commands/install.dart';
|
|
|
|
import 'src/commands/logs.dart';
|
2016-08-26 02:37:48 +00:00
|
|
|
import 'src/commands/packages.dart';
|
2016-04-08 15:51:44 +00:00
|
|
|
import 'src/commands/precache.dart';
|
2016-02-14 07:50:20 +00:00
|
|
|
import 'src/commands/run.dart';
|
2016-03-25 23:04:22 +00:00
|
|
|
import 'src/commands/screenshot.dart';
|
2015-10-10 06:41:40 +00:00
|
|
|
import 'src/commands/stop.dart';
|
2015-11-05 07:43:15 +00:00
|
|
|
import 'src/commands/test.dart';
|
2015-10-10 06:41:40 +00:00
|
|
|
import 'src/commands/trace.dart';
|
2016-02-25 23:29:58 +00:00
|
|
|
import 'src/commands/update_packages.dart';
|
2015-11-12 18:13:40 +00:00
|
|
|
import 'src/commands/upgrade.dart';
|
2017-03-04 00:54:47 +00:00
|
|
|
import 'src/crash_reporting.dart';
|
2016-11-10 16:31:11 +00:00
|
|
|
import 'src/devfs.dart';
|
2016-02-07 03:19:50 +00:00
|
|
|
import 'src/device.dart';
|
2016-02-17 02:23:43 +00:00
|
|
|
import 'src/doctor.dart';
|
2016-03-25 18:49:07 +00:00
|
|
|
import 'src/globals.dart';
|
2016-11-29 19:22:48 +00:00
|
|
|
import 'src/ios/simulators.dart';
|
2017-01-28 22:26:49 +00:00
|
|
|
import 'src/run_hot.dart';
|
2017-03-04 00:54:47 +00:00
|
|
|
import 'src/runner/flutter_command.dart';
|
2015-11-29 05:07:16 +00:00
|
|
|
import 'src/runner/flutter_command_runner.dart';
|
2016-11-29 19:22:48 +00:00
|
|
|
import 'src/usage.dart';
|
2017-05-03 23:12:08 +00:00
|
|
|
import 'src/version.dart';
|
2016-11-29 19:22:48 +00:00
|
|
|
|
2015-10-10 06:41:40 +00:00
|
|
|
/// Main entry point for commands.
|
|
|
|
///
|
2016-05-19 17:21:38 +00:00
|
|
|
/// This function is intended to be used from the `flutter` command line tool.
|
2016-03-10 02:05:05 +00:00
|
|
|
Future<Null> main(List<String> args) async {
|
2017-03-04 01:50:46 +00:00
|
|
|
final bool verbose = args.contains('-v') || args.contains('--verbose');
|
|
|
|
final bool help = args.contains('-h') || args.contains('--help') ||
|
2016-09-22 09:58:45 +00:00
|
|
|
(args.isNotEmpty && args.first == 'help') || (args.length == 1 && verbose);
|
2017-03-04 01:50:46 +00:00
|
|
|
final bool verboseHelp = help && verbose;
|
2016-01-29 22:32:12 +00:00
|
|
|
|
2017-03-04 00:54:47 +00:00
|
|
|
await run(args, <FlutterCommand>[
|
|
|
|
new AnalyzeCommand(verboseHelp: verboseHelp),
|
|
|
|
new BuildCommand(verboseHelp: verboseHelp),
|
|
|
|
new ChannelCommand(),
|
|
|
|
new ConfigCommand(),
|
|
|
|
new CreateCommand(),
|
|
|
|
new DaemonCommand(hidden: !verboseHelp),
|
|
|
|
new DevicesCommand(),
|
|
|
|
new DoctorCommand(),
|
|
|
|
new DriveCommand(),
|
|
|
|
new FormatCommand(),
|
2017-03-14 18:12:19 +00:00
|
|
|
new FuchsiaReloadCommand(),
|
2017-03-04 00:54:47 +00:00
|
|
|
new InstallCommand(),
|
|
|
|
new LogsCommand(),
|
|
|
|
new PackagesCommand(),
|
|
|
|
new PrecacheCommand(),
|
|
|
|
new RunCommand(verboseHelp: verboseHelp),
|
|
|
|
new ScreenshotCommand(),
|
|
|
|
new StopCommand(),
|
|
|
|
new TestCommand(),
|
|
|
|
new TraceCommand(),
|
|
|
|
new UpdatePackagesCommand(hidden: !verboseHelp),
|
|
|
|
new UpgradeCommand(),
|
|
|
|
], verbose: verbose, verboseHelp: verboseHelp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> run(List<String> args, List<FlutterCommand> subCommands, {
|
|
|
|
bool verbose: false,
|
|
|
|
bool verboseHelp: false,
|
|
|
|
bool reportCrashes,
|
|
|
|
String flutterVersion,
|
|
|
|
}) async {
|
|
|
|
reportCrashes ??= !isRunningOnBot;
|
|
|
|
|
2016-03-14 16:41:00 +00:00
|
|
|
if (verboseHelp) {
|
|
|
|
// Remove the verbose option; for help, users don't need to see verbose logs.
|
|
|
|
args = new List<String>.from(args);
|
|
|
|
args.removeWhere((String option) => option == '-v' || option == '--verbose');
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:50:46 +00:00
|
|
|
final FlutterCommandRunner runner = new FlutterCommandRunner(verboseHelp: verboseHelp);
|
2017-03-04 00:54:47 +00:00
|
|
|
subCommands.forEach(runner.addCommand);
|
2015-10-13 20:36:39 +00:00
|
|
|
|
2016-11-29 15:54:20 +00:00
|
|
|
// Construct a context.
|
2017-03-04 01:50:46 +00:00
|
|
|
final AppContext _executableContext = new AppContext();
|
2016-11-29 15:54:20 +00:00
|
|
|
|
|
|
|
// Make the context current.
|
2017-03-04 00:54:47 +00:00
|
|
|
return await _executableContext.runInZone(() async {
|
2016-11-29 15:54:20 +00:00
|
|
|
// Initialize the context with some defaults.
|
2016-11-30 18:06:48 +00:00
|
|
|
// NOTE: Similar lists also exist in `bin/fuchsia_builder.dart` and
|
|
|
|
// `test/src/context.dart`. If you update this list of defaults, look
|
|
|
|
// in those locations as well to see if you need a similar update there.
|
|
|
|
|
2016-11-30 16:42:42 +00:00
|
|
|
// Seed these context entries first since others depend on them
|
2017-02-20 22:07:16 +00:00
|
|
|
context.putIfAbsent(Platform, () => const LocalPlatform());
|
|
|
|
context.putIfAbsent(FileSystem, () => const LocalFileSystem());
|
|
|
|
context.putIfAbsent(ProcessManager, () => const LocalProcessManager());
|
2017-02-15 21:20:17 +00:00
|
|
|
context.putIfAbsent(Logger, () => platform.isWindows ? new WindowsStdoutLogger() : new StdoutLogger());
|
2017-02-20 10:02:50 +00:00
|
|
|
context.putIfAbsent(Config, () => new Config());
|
2016-11-30 16:42:42 +00:00
|
|
|
|
|
|
|
// Order-independent context entries
|
2016-11-29 19:22:48 +00:00
|
|
|
context.putIfAbsent(DeviceManager, () => new DeviceManager());
|
|
|
|
context.putIfAbsent(DevFSConfig, () => new DevFSConfig());
|
|
|
|
context.putIfAbsent(Doctor, () => new Doctor());
|
|
|
|
context.putIfAbsent(HotRunnerConfig, () => new HotRunnerConfig());
|
|
|
|
context.putIfAbsent(Cache, () => new Cache());
|
2017-02-17 07:10:11 +00:00
|
|
|
context.putIfAbsent(Artifacts, () => new CachedArtifacts());
|
2016-11-29 19:22:48 +00:00
|
|
|
context.putIfAbsent(IOSSimulatorUtils, () => new IOSSimulatorUtils());
|
|
|
|
context.putIfAbsent(SimControl, () => new SimControl());
|
2016-02-07 03:19:50 +00:00
|
|
|
|
2017-02-10 17:36:04 +00:00
|
|
|
// Initialize the system locale.
|
|
|
|
await intl.findSystemLocale();
|
|
|
|
|
2017-03-15 22:39:01 +00:00
|
|
|
try {
|
2016-11-29 15:54:20 +00:00
|
|
|
await runner.run(args);
|
2017-02-27 23:38:47 +00:00
|
|
|
await _exit(0);
|
2017-03-15 22:39:01 +00:00
|
|
|
} catch (error, stackTrace) {
|
2017-03-07 18:13:40 +00:00
|
|
|
String getVersion() => flutterVersion ?? FlutterVersion.getVersionString();
|
2017-03-15 22:39:01 +00:00
|
|
|
return await _handleToolError(error, stackTrace, verbose, args, reportCrashes, getVersion);
|
|
|
|
}
|
|
|
|
return 0;
|
2017-03-04 00:54:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:51:46 +00:00
|
|
|
/// Writes the [string] to one of the standard output streams.
|
|
|
|
@visibleForTesting
|
|
|
|
typedef void WriteCallback([String string]);
|
|
|
|
|
|
|
|
/// Writes a line to STDERR.
|
|
|
|
///
|
|
|
|
/// Overwrite this in tests to avoid spurious test output.
|
|
|
|
@visibleForTesting
|
|
|
|
WriteCallback writelnStderr = stderr.writeln;
|
|
|
|
|
2017-03-04 00:54:47 +00:00
|
|
|
Future<int> _handleToolError(
|
|
|
|
dynamic error,
|
2017-03-15 22:39:01 +00:00
|
|
|
StackTrace stackTrace,
|
2017-03-04 00:54:47 +00:00
|
|
|
bool verbose,
|
|
|
|
List<String> args,
|
|
|
|
bool reportCrashes,
|
2017-03-07 18:13:40 +00:00
|
|
|
String getFlutterVersion(),
|
2017-03-04 00:54:47 +00:00
|
|
|
) async {
|
|
|
|
if (error is UsageException) {
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr(error.message);
|
|
|
|
writelnStderr();
|
|
|
|
writelnStderr(
|
2017-03-04 00:54:47 +00:00
|
|
|
"Run 'flutter -h' (or 'flutter <command> -h') for available "
|
|
|
|
"flutter commands and options."
|
|
|
|
);
|
|
|
|
// Argument error exit code.
|
|
|
|
return _exit(64);
|
|
|
|
} else if (error is ToolExit) {
|
|
|
|
if (error.message != null)
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr(error.message);
|
2017-03-04 00:54:47 +00:00
|
|
|
if (verbose) {
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr();
|
2017-03-15 22:39:01 +00:00
|
|
|
writelnStderr(stackTrace.toString());
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr();
|
2017-03-04 00:54:47 +00:00
|
|
|
}
|
|
|
|
return _exit(error.exitCode ?? 1);
|
|
|
|
} else if (error is ProcessExit) {
|
|
|
|
// We've caught an exit code.
|
2017-03-06 21:23:03 +00:00
|
|
|
if (error.immediate) {
|
|
|
|
exit(error.exitCode);
|
|
|
|
return error.exitCode;
|
|
|
|
} else {
|
|
|
|
return _exit(error.exitCode);
|
|
|
|
}
|
2017-03-04 00:54:47 +00:00
|
|
|
} else {
|
|
|
|
// We've crashed; emit a log report.
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr();
|
2017-03-04 00:54:47 +00:00
|
|
|
|
|
|
|
if (!reportCrashes) {
|
|
|
|
// Print the stack trace on the bots - don't write a crash report.
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr('$error');
|
2017-03-15 22:39:01 +00:00
|
|
|
writelnStderr(stackTrace.toString());
|
2017-03-04 00:54:47 +00:00
|
|
|
return _exit(1);
|
|
|
|
} else {
|
2017-03-15 22:39:01 +00:00
|
|
|
flutterUsage.sendException(error, stackTrace);
|
2017-03-09 20:58:31 +00:00
|
|
|
|
2017-03-04 00:54:47 +00:00
|
|
|
if (error is String)
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr('Oops; flutter has exited unexpectedly: "$error".');
|
2017-03-04 00:54:47 +00:00
|
|
|
else
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr('Oops; flutter has exited unexpectedly.');
|
2017-03-04 00:54:47 +00:00
|
|
|
|
|
|
|
await CrashReportSender.instance.sendReport(
|
|
|
|
error: error,
|
2017-03-15 22:39:01 +00:00
|
|
|
stackTrace: stackTrace,
|
2017-03-09 20:58:31 +00:00
|
|
|
getFlutterVersion: getFlutterVersion,
|
2017-03-04 00:54:47 +00:00
|
|
|
);
|
|
|
|
try {
|
2017-03-15 22:39:01 +00:00
|
|
|
final File file = await _createLocalCrashReport(args, error, stackTrace);
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr(
|
2017-03-04 00:54:47 +00:00
|
|
|
'Crash report written to ${file.path};\n'
|
|
|
|
'please let us know at https://github.com/flutter/flutter/issues.',
|
2016-11-29 15:54:20 +00:00
|
|
|
);
|
2017-03-04 00:54:47 +00:00
|
|
|
return _exit(1);
|
|
|
|
} catch (error) {
|
2017-03-10 00:51:46 +00:00
|
|
|
writelnStderr(
|
2017-03-04 00:54:47 +00:00
|
|
|
'Unable to generate crash report due to secondary error: $error\n'
|
|
|
|
'please let us know at https://github.com/flutter/flutter/issues.',
|
|
|
|
);
|
2017-03-06 21:23:03 +00:00
|
|
|
// Any exception throw here (including one thrown by `_exit()`) will
|
|
|
|
// get caught by our zone's `onError` handler. In order to avoid an
|
|
|
|
// infinite error loop, we throw an error that is recognized above
|
|
|
|
// and will trigger an immediate exit.
|
|
|
|
throw new ProcessExit(1, immediate: true);
|
2016-04-01 15:33:22 +00:00
|
|
|
}
|
2017-03-04 00:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-10 06:41:40 +00:00
|
|
|
}
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2017-03-04 00:54:47 +00:00
|
|
|
/// File system used by the crash reporting logic.
|
|
|
|
///
|
|
|
|
/// We do not want to use the file system stored in the context because it may
|
|
|
|
/// be recording. Additionally, in the case of a crash we do not trust the
|
|
|
|
/// integrity of the [AppContext].
|
|
|
|
@visibleForTesting
|
2017-03-08 22:58:14 +00:00
|
|
|
FileSystem crashFileSystem = const LocalFileSystem();
|
2017-03-04 00:54:47 +00:00
|
|
|
|
|
|
|
/// Saves the crash report to a local file.
|
2017-03-15 22:39:01 +00:00
|
|
|
Future<File> _createLocalCrashReport(List<String> args, dynamic error, StackTrace stackTrace) async {
|
2017-03-04 00:54:47 +00:00
|
|
|
File crashFile = getUniqueFile(crashFileSystem.currentDirectory, 'flutter', 'log');
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2017-03-04 01:50:46 +00:00
|
|
|
final StringBuffer buffer = new StringBuffer();
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-05-20 21:08:46 +00:00
|
|
|
buffer.writeln('Flutter crash report; please file at https://github.com/flutter/flutter/issues.\n');
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-05-20 21:08:46 +00:00
|
|
|
buffer.writeln('## command\n');
|
|
|
|
buffer.writeln('flutter ${args.join(' ')}\n');
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-05-20 21:08:46 +00:00
|
|
|
buffer.writeln('## exception\n');
|
2017-03-13 21:50:30 +00:00
|
|
|
buffer.writeln('${error.runtimeType}: $error\n');
|
2017-03-15 22:39:01 +00:00
|
|
|
buffer.writeln('```\n$stackTrace```\n');
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-05-20 21:08:46 +00:00
|
|
|
buffer.writeln('## flutter doctor\n');
|
2016-09-01 21:55:16 +00:00
|
|
|
buffer.writeln('```\n${await _doctorText()}```');
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-09-28 19:44:18 +00:00
|
|
|
try {
|
2017-02-23 18:05:00 +00:00
|
|
|
await crashFile.writeAsString(buffer.toString());
|
2016-09-28 19:44:18 +00:00
|
|
|
} on FileSystemException catch (_) {
|
|
|
|
// Fallback to the system temporary directory.
|
2017-03-04 00:54:47 +00:00
|
|
|
crashFile = getUniqueFile(crashFileSystem.systemTempDirectory, 'flutter', 'log');
|
2016-09-28 19:44:18 +00:00
|
|
|
try {
|
2017-02-23 18:05:00 +00:00
|
|
|
await crashFile.writeAsString(buffer.toString());
|
2016-09-28 19:44:18 +00:00
|
|
|
} on FileSystemException catch (e) {
|
|
|
|
printError('Could not write crash report to disk: $e');
|
|
|
|
printError(buffer.toString());
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 18:49:07 +00:00
|
|
|
|
|
|
|
return crashFile;
|
|
|
|
}
|
|
|
|
|
2016-09-01 21:55:16 +00:00
|
|
|
Future<String> _doctorText() async {
|
2016-03-25 18:49:07 +00:00
|
|
|
try {
|
2017-03-04 01:50:46 +00:00
|
|
|
final BufferLogger logger = new BufferLogger();
|
|
|
|
final AppContext appContext = new AppContext();
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-11-29 19:22:48 +00:00
|
|
|
appContext.setVariable(Logger, logger);
|
2016-03-25 18:49:07 +00:00
|
|
|
|
2016-09-01 21:55:16 +00:00
|
|
|
await appContext.runInZone(() => doctor.diagnose());
|
2016-03-25 18:49:07 +00:00
|
|
|
|
|
|
|
return logger.statusText;
|
2016-03-28 23:20:43 +00:00
|
|
|
} catch (error, trace) {
|
2016-04-06 22:11:54 +00:00
|
|
|
return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
|
2016-03-25 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 23:25:11 +00:00
|
|
|
|
2017-03-04 00:54:47 +00:00
|
|
|
Future<int> _exit(int code) async {
|
2016-05-03 22:17:30 +00:00
|
|
|
if (flutterUsage.isFirstRun)
|
2017-04-02 00:47:09 +00:00
|
|
|
flutterUsage.printWelcome();
|
2016-05-03 22:17:30 +00:00
|
|
|
|
2016-04-26 23:25:11 +00:00
|
|
|
// Send any last analytics calls that are in progress without overly delaying
|
|
|
|
// the tool's exit (we wait a maximum of 250ms).
|
|
|
|
if (flutterUsage.enabled) {
|
2017-03-04 01:50:46 +00:00
|
|
|
final Stopwatch stopwatch = new Stopwatch()..start();
|
2016-04-26 23:25:11 +00:00
|
|
|
await flutterUsage.ensureAnalyticsSent();
|
|
|
|
printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:17:50 +00:00
|
|
|
// Run shutdown hooks before flushing logs
|
|
|
|
await runShutdownHooks();
|
|
|
|
|
2017-03-04 01:50:46 +00:00
|
|
|
final Completer<Null> completer = new Completer<Null>();
|
2017-02-27 23:38:47 +00:00
|
|
|
|
2016-04-26 23:25:11 +00:00
|
|
|
// Give the task / timer queue one cycle through before we hard exit.
|
2016-05-20 21:08:46 +00:00
|
|
|
Timer.run(() {
|
2017-03-06 21:23:03 +00:00
|
|
|
try {
|
|
|
|
printTrace('exiting with code $code');
|
|
|
|
exit(code);
|
|
|
|
completer.complete();
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
completer.completeError(error, stackTrace);
|
|
|
|
}
|
2016-04-26 23:25:11 +00:00
|
|
|
});
|
2017-02-27 23:38:47 +00:00
|
|
|
|
|
|
|
await completer.future;
|
2017-03-04 00:54:47 +00:00
|
|
|
return code;
|
2016-04-26 23:25:11 +00:00
|
|
|
}
|