flutter/packages/flutter_tools/lib/executable.dart

158 lines
5 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 'dart:io';
import 'package:args/command_runner.dart';
2016-03-25 18:49:07 +00:00
import 'package:path/path.dart' as path;
import 'package:stack_trace/stack_trace.dart';
import 'src/base/context.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
2016-03-25 23:04:22 +00:00
import 'src/base/utils.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';
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/install.dart';
import 'src/commands/listen.dart';
import 'src/commands/logs.dart';
import 'src/commands/refresh.dart';
2016-02-14 07:50:20 +00:00
import 'src/commands/run.dart';
import 'src/commands/run_mojo.dart';
2016-03-25 23:04:22 +00:00
import 'src/commands/screenshot.dart';
import 'src/commands/skia.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/device.dart';
import 'src/doctor.dart';
2016-03-25 18:49:07 +00:00
import 'src/globals.dart';
import 'src/runner/flutter_command_runner.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 {
2016-01-29 22:32:12 +00:00
bool help = args.contains('-h') || args.contains('--help');
bool verbose = args.contains('-v') || args.contains('--verbose');
2016-02-14 07:50:20 +00:00
bool verboseHelp = help && verbose;
2016-01-29 22:32:12 +00:00
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');
}
2016-02-14 07:50:20 +00:00
FlutterCommandRunner runner = new FlutterCommandRunner(verboseHelp: verboseHelp)
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
..addCommand(new AnalyzeCommand())
..addCommand(new BuildCommand())
2016-01-27 22:37:29 +00:00
..addCommand(new CreateCommand())
2016-03-15 16:27:58 +00:00
..addCommand(new DaemonCommand(hidden: !verboseHelp))
2016-02-14 07:50:20 +00:00
..addCommand(new DevicesCommand())
..addCommand(new DoctorCommand())
..addCommand(new DriveCommand())
..addCommand(new InstallCommand())
..addCommand(new ListenCommand())
..addCommand(new LogsCommand())
..addCommand(new RefreshCommand())
2016-02-14 07:50:20 +00:00
..addCommand(new RunCommand())
2016-03-15 16:27:58 +00:00
..addCommand(new RunMojoCommand(hidden: !verboseHelp))
2016-03-25 23:04:22 +00:00
..addCommand(new ScreenshotCommand())
..addCommand(new SkiaCommand())
..addCommand(new StopCommand())
2015-11-05 07:43:15 +00:00
..addCommand(new TestCommand())
..addCommand(new TraceCommand())
2016-03-15 16:27:58 +00:00
..addCommand(new UpdatePackagesCommand(hidden: !verboseHelp))
..addCommand(new UpgradeCommand());
return Chain.capture(() async {
// Initialize globals.
context[Logger] = new StdoutLogger();
context[DeviceManager] = new DeviceManager();
Doctor.initGlobal();
2015-10-17 18:50:23 +00:00
dynamic result = await runner.run(args);
2015-10-17 18:50:23 +00:00
if (result is int)
exit(result);
}, onError: (dynamic error, Chain chain) {
if (error is UsageException) {
2016-03-14 16:41:00 +00:00
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.
exit(64);
} else if (error is ProcessExit) {
// We've caught an exit code.
exit(error.exitCode);
} else {
2016-03-25 18:49:07 +00:00
// We've crashed; emit a log report.
stderr.writeln();
if (error is String)
stderr.writeln('Oops; flutter has exited unexpectedly: "$error".');
else
stderr.writeln('Oops; flutter has exited unexpectedly.');
2016-04-01 15:33:22 +00:00
if (Platform.environment.containsKey('FLUTTER_DEV')) {
// If we're working in the tools themselves, just print the stack trace.
stderr.writeln(chain.terse.toString());
} else {
File file = _createCrashReport(args, error, chain);
stderr.writeln(
'Crash report written to ${path.relative(file.path)}; '
'please let us know at https://github.com/flutter/flutter/issues.');
2016-04-01 15:33:22 +00:00
}
exit(1);
}
});
}
2016-03-25 18:49:07 +00:00
File _createCrashReport(List<String> args, dynamic error, Chain chain) {
2016-03-25 23:04:22 +00:00
File crashFile = getUniqueFile(Directory.current, 'flutter', 'log');
2016-03-25 18:49:07 +00:00
StringBuffer buf = new StringBuffer();
buf.writeln('Flutter crash report; please file at https://github.com/flutter/flutter/issues.\n');
buf.writeln('## command\n');
buf.writeln('flutter ${args.join(' ')}\n');
buf.writeln('## exception\n');
buf.writeln('$error\n');
buf.writeln('```\n${chain.terse}```\n');
2016-03-25 18:49:07 +00:00
buf.writeln('## flutter doctor\n');
buf.writeln('```\n${_doctorText()}```');
2016-03-25 18:49:07 +00:00
crashFile.writeAsStringSync(buf.toString());
return crashFile;
}
String _doctorText() {
try {
BufferLogger logger = new BufferLogger();
AppContext appContext = new AppContext();
appContext[Logger] = logger;
appContext.runInZone(() => doctor.diagnose());
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
}
}