flutter/packages/flutter_tools/lib/executable.dart

323 lines
11 KiB
Dart
Raw Normal View History

// 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.
import 'dart:async';
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:intl/intl_standalone.dart' as intl;
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'package:stack_trace/stack_trace.dart';
import 'src/artifacts.dart';
import 'src/base/common.dart';
2016-11-29 19:22:48 +00:00
import 'src/base/config.dart';
import 'src/base/context.dart';
import 'src/base/file_system.dart';
import 'src/base/io.dart';
import 'src/base/logger.dart';
2016-11-29 19:22:48 +00:00
import 'src/base/os.dart';
import 'src/base/platform.dart';
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';
flutter analyze command Other changes in this patch: - Make the 'flutter' tool say "Updating flutter tool..." when it calls pub get, to avoid confusion about what the pub get output is about. - Make the bash flutter tool call pub get when the revision has changed. (This was already happening on Windows.) - Fix a raft of bugs found by the analyzer. - Fix some style nits in various bits of code that happened to be near things the analyzer noticed. - Remove the logic in "flutter test" that would run "pub get", since upon further reflexion it was determined it didn't work anyway. We'll probably have to add better diagnostics here and say to run the updater script. - Remove the native velocity tracker script, since it was testing code that has since been removed. Notes on ignored warnings: - We ignore warnings in any packages that are not in the Flutter repo or in the author's current directory. - We ignore various irrelevant Strong Mode warnings. We still enable strong mode because even though it's not really relevant to our needs, it does (more or less accidentally) catch a few things that are helpful to us. - We allow CONSTANTS_LIKE_THIS, since we get some of those from other platforms that we are copying for sanity and consistency. - We allow one-member abstract classes since we have a number of them where it's perfectly reasonable. - We unfortunately still ignore warnings in mojom.dart autogenerated files. We should really fix those but that's a separate patch. - We verify the actual source file when we see the 'Name non-constant identifiers using lowerCamelCase.' lint, to allow one-letter variables that use capital letters (e.g. for physics expressions) and to allow multiple-underscore variable names. - We ignore all errors on lines that contain the following magic incantation and a "#" character: // analyzer doesn't like constructor tear-offs - For all remaining errors, if the line contains a comment of the form // analyzer says "..." ...then we ignore any errors that have that "..." string in them.
2015-11-11 18:29:05 +00:00
import 'src/commands/analyze.dart';
import 'src/commands/build.dart';
import 'src/commands/channel.dart';
import 'src/commands/config.dart';
2016-01-27 22:37:29 +00:00
import 'src/commands/create.dart';
import 'src/commands/daemon.dart';
2016-02-14 07:50:20 +00:00
import 'src/commands/devices.dart';
import 'src/commands/doctor.dart';
import 'src/commands/drive.dart';
import 'src/commands/format.dart';
import 'src/commands/install.dart';
import 'src/commands/logs.dart';
import 'src/commands/packages.dart';
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';
import 'src/commands/stop.dart';
2015-11-05 07:43:15 +00:00
import 'src/commands/test.dart';
import 'src/commands/trace.dart';
import 'src/commands/update_packages.dart';
import 'src/commands/upgrade.dart';
import 'src/crash_reporting.dart';
import 'src/devfs.dart';
import 'src/device.dart';
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/mac.dart';
import 'src/ios/simulators.dart';
import 'src/run_hot.dart';
import 'src/runner/flutter_command.dart';
import 'src/runner/flutter_command_runner.dart';
2016-11-29 19:22:48 +00:00
import 'src/usage.dart';
/// Main entry point for commands.
///
/// This function is intended to be used from the `flutter` command line tool.
Future<Null> main(List<String> args) async {
final bool verbose = args.contains('-v') || args.contains('--verbose');
final bool help = args.contains('-h') || args.contains('--help') ||
(args.isNotEmpty && args.first == 'help') || (args.length == 1 && verbose);
final bool verboseHelp = help && verbose;
2016-01-29 22:32:12 +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(),
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');
}
final FlutterCommandRunner runner = new FlutterCommandRunner(verboseHelp: verboseHelp);
subCommands.forEach(runner.addCommand);
// Construct a context.
final AppContext _executableContext = new AppContext();
// Make the context current.
return await _executableContext.runInZone(() async {
// 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.
// 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());
context.putIfAbsent(Logger, () => platform.isWindows ? new WindowsStdoutLogger() : new StdoutLogger());
context.putIfAbsent(Config, () => new Config());
// 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());
context.putIfAbsent(Artifacts, () => new CachedArtifacts());
2016-11-29 19:22:48 +00:00
context.putIfAbsent(OperatingSystemUtils, () => new OperatingSystemUtils());
context.putIfAbsent(Xcode, () => new Xcode());
2016-11-29 19:22:48 +00:00
context.putIfAbsent(IOSSimulatorUtils, () => new IOSSimulatorUtils());
context.putIfAbsent(SimControl, () => new SimControl());
context.putIfAbsent(Usage, () => new Usage());
2017-02-10 17:36:04 +00:00
// Initialize the system locale.
await intl.findSystemLocale();
final Completer<int> runCompleter = new Completer<int>();
Chain.capture<Future<Null>>(() async {
await runner.run(args);
await _exit(0);
runCompleter.complete(0);
}, onError: (dynamic error, Chain chain) {
String getVersion() => flutterVersion ?? FlutterVersion.getVersionString();
_handleToolError(error, chain, verbose, args, reportCrashes, getVersion)
.then(runCompleter.complete, onError: runCompleter.completeError);
});
return runCompleter.future;
});
}
Future<int> _handleToolError(
dynamic error,
Chain chain,
bool verbose,
List<String> args,
bool reportCrashes,
String getFlutterVersion(),
) async {
if (error is UsageException) {
stderr.writeln(error.message);
stderr.writeln();
stderr.writeln(
"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)
stderr.writeln(error.message);
if (verbose) {
stderr.writeln();
stderr.writeln(chain.terse.toString());
stderr.writeln();
}
return _exit(error.exitCode ?? 1);
} else if (error is ProcessExit) {
// We've caught an exit code.
if (error.immediate) {
exit(error.exitCode);
return error.exitCode;
} else {
return _exit(error.exitCode);
}
} else {
// We've crashed; emit a log report.
stderr.writeln();
flutterUsage.sendException(error, chain);
if (!reportCrashes) {
// Print the stack trace on the bots - don't write a crash report.
stderr.writeln('$error');
stderr.writeln(chain.terse.toString());
return _exit(1);
} else {
if (error is String)
stderr.writeln('Oops; flutter has exited unexpectedly: "$error".');
else
stderr.writeln('Oops; flutter has exited unexpectedly.');
await CrashReportSender.instance.sendReport(
error: error,
stackTrace: chain,
flutterVersion: getFlutterVersion(),
);
try {
final File file = await _createLocalCrashReport(args, error, chain);
stderr.writeln(
'Crash report written to ${file.path};\n'
'please let us know at https://github.com/flutter/flutter/issues.',
);
return _exit(1);
} catch (error) {
stderr.writeln(
'Unable to generate crash report due to secondary error: $error\n'
'please let us know at https://github.com/flutter/flutter/issues.',
);
// 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
}
}
}
}
2016-03-25 18:49:07 +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
FileSystem crashFileSystem = new LocalFileSystem();
/// Saves the crash report to a local file.
Future<File> _createLocalCrashReport(List<String> args, dynamic error, Chain chain) async {
File crashFile = getUniqueFile(crashFileSystem.currentDirectory, 'flutter', 'log');
2016-03-25 18:49:07 +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');
buffer.writeln('$error\n');
buffer.writeln('```\n${chain.terse}```\n');
2016-03-25 18:49:07 +00:00
2016-05-20 21:08:46 +00:00
buffer.writeln('## flutter doctor\n');
buffer.writeln('```\n${await _doctorText()}```');
2016-03-25 18:49:07 +00:00
try {
await crashFile.writeAsString(buffer.toString());
} on FileSystemException catch (_) {
// Fallback to the system temporary directory.
crashFile = getUniqueFile(crashFileSystem.systemTempDirectory, 'flutter', 'log');
try {
await crashFile.writeAsString(buffer.toString());
} 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;
}
Future<String> _doctorText() async {
2016-03-25 18:49:07 +00:00
try {
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
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) {
return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
2016-03-25 18:49:07 +00:00
}
}
Future<int> _exit(int code) async {
if (flutterUsage.isFirstRun)
flutterUsage.printUsage();
// 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) {
final Stopwatch stopwatch = new Stopwatch()..start();
await flutterUsage.ensureAnalyticsSent();
printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
}
// Run shutdown hooks before flushing logs
await runShutdownHooks();
final Completer<Null> completer = new Completer<Null>();
// Give the task / timer queue one cycle through before we hard exit.
2016-05-20 21:08:46 +00:00
Timer.run(() {
try {
printTrace('exiting with code $code');
exit(code);
completer.complete();
} catch (error, stackTrace) {
completer.completeError(error, stackTrace);
}
});
await completer.future;
return code;
}