Unverified Commit 82a4ba40 authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tool] Send analytics command before the command runs (#36490)

parent d1190b63
...@@ -32,11 +32,12 @@ class LogsCommand extends FlutterCommand { ...@@ -32,11 +32,12 @@ class LogsCommand extends FlutterCommand {
Device device; Device device;
@override @override
Future<FlutterCommandResult> verifyThenRunCommand() async { Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
device = await findTargetDevice(); device = await findTargetDevice();
if (device == null) if (device == null) {
throwToolExit(null); throwToolExit(null);
return super.verifyThenRunCommand(); }
return super.verifyThenRunCommand(commandPath);
} }
@override @override
......
...@@ -64,15 +64,18 @@ class ScreenshotCommand extends FlutterCommand { ...@@ -64,15 +64,18 @@ class ScreenshotCommand extends FlutterCommand {
Device device; Device device;
@override @override
Future<FlutterCommandResult> verifyThenRunCommand() async { Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
device = await findTargetDevice(); device = await findTargetDevice();
if (device == null) if (device == null) {
throwToolExit('Must have a connected device'); throwToolExit('Must have a connected device');
if (argResults[_kType] == _kDeviceType && !device.supportsScreenshot) }
if (argResults[_kType] == _kDeviceType && !device.supportsScreenshot) {
throwToolExit('Screenshot not supported for ${device.name}.'); throwToolExit('Screenshot not supported for ${device.name}.');
if (argResults[_kType] != _kDeviceType && argResults[_kObservatoryUri] == null) }
if (argResults[_kType] != _kDeviceType && argResults[_kObservatoryUri] == null) {
throwToolExit('Observatory URI must be specified for screenshot type ${argResults[_kType]}'); throwToolExit('Observatory URI must be specified for screenshot type ${argResults[_kType]}');
return super.verifyThenRunCommand(); }
return super.verifyThenRunCommand(commandPath);
} }
@override @override
......
...@@ -379,9 +379,10 @@ abstract class FlutterCommand extends Command<void> { ...@@ -379,9 +379,10 @@ abstract class FlutterCommand extends Command<void> {
body: () async { body: () async {
if (flutterUsage.isFirstRun) if (flutterUsage.isFirstRun)
flutterUsage.printWelcome(); flutterUsage.printWelcome();
final String commandPath = await usagePath;
FlutterCommandResult commandResult; FlutterCommandResult commandResult;
try { try {
commandResult = await verifyThenRunCommand(); commandResult = await verifyThenRunCommand(commandPath);
} on ToolExit { } on ToolExit {
commandResult = const FlutterCommandResult(ExitStatus.fail); commandResult = const FlutterCommandResult(ExitStatus.fail);
rethrow; rethrow;
...@@ -389,8 +390,7 @@ abstract class FlutterCommand extends Command<void> { ...@@ -389,8 +390,7 @@ abstract class FlutterCommand extends Command<void> {
final DateTime endTime = systemClock.now(); final DateTime endTime = systemClock.now();
printTrace(userMessages.flutterElapsedTime(name, getElapsedAsMilliseconds(endTime.difference(startTime)))); printTrace(userMessages.flutterElapsedTime(name, getElapsedAsMilliseconds(endTime.difference(startTime))));
printTrace('"flutter $name" took ${getElapsedAsMilliseconds(endTime.difference(startTime))}.'); printTrace('"flutter $name" took ${getElapsedAsMilliseconds(endTime.difference(startTime))}.');
_sendPostUsage(commandPath, commandResult, startTime, endTime);
await _sendUsage(commandResult, startTime, endTime);
} }
}, },
); );
...@@ -400,33 +400,28 @@ abstract class FlutterCommand extends Command<void> { ...@@ -400,33 +400,28 @@ abstract class FlutterCommand extends Command<void> {
/// ///
/// For example, the command path (e.g. `build/apk`) and the result, /// For example, the command path (e.g. `build/apk`) and the result,
/// as well as the time spent running it. /// as well as the time spent running it.
Future<void> _sendUsage(FlutterCommandResult commandResult, DateTime startTime, DateTime endTime) async { void _sendPostUsage(String commandPath, FlutterCommandResult commandResult,
final String commandPath = await usagePath; DateTime startTime, DateTime endTime) {
if (commandPath == null) { if (commandPath == null) {
return; return;
} }
// Send screen. // Send command result.
final Map<String, String> currentUsageValues = await usageValues; String result = 'unspecified';
final Map<String, String> additionalUsageValues = <String, String>{
...?currentUsageValues,
};
if (commandResult != null) { if (commandResult != null) {
switch (commandResult.exitStatus) { switch (commandResult.exitStatus) {
case ExitStatus.success: case ExitStatus.success:
additionalUsageValues[kCommandResult] = 'success'; result = 'success';
break; break;
case ExitStatus.warning: case ExitStatus.warning:
additionalUsageValues[kCommandResult] = 'warning'; result = 'warning';
break; break;
case ExitStatus.fail: case ExitStatus.fail:
additionalUsageValues[kCommandResult] = 'fail'; result = 'fail';
break; break;
} }
} }
additionalUsageValues[kCommandHasTerminal] = io.stdout.hasTerminal ? 'true' : 'false'; flutterUsage.sendEvent(commandPath, result);
flutterUsage.sendCommand(commandPath, parameters: additionalUsageValues);
// Send timing. // Send timing.
final List<String> labels = <String>[ final List<String> labels = <String>[
...@@ -459,7 +454,7 @@ abstract class FlutterCommand extends Command<void> { ...@@ -459,7 +454,7 @@ abstract class FlutterCommand extends Command<void> {
/// then call this method to execute the command /// then call this method to execute the command
/// rather than calling [runCommand] directly. /// rather than calling [runCommand] directly.
@mustCallSuper @mustCallSuper
Future<FlutterCommandResult> verifyThenRunCommand() async { Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
await validateCommand(); await validateCommand();
// Populate the cache. We call this before pub get below so that the sky_engine // Populate the cache. We call this before pub get below so that the sky_engine
...@@ -476,6 +471,15 @@ abstract class FlutterCommand extends Command<void> { ...@@ -476,6 +471,15 @@ abstract class FlutterCommand extends Command<void> {
setupApplicationPackages(); setupApplicationPackages();
if (commandPath != null) {
final Map<String, String> additionalUsageValues = <String,String>{
...?await usageValues,
};
additionalUsageValues[kCommandHasTerminal] =
io.stdout.hasTerminal ? 'true' : 'false';
flutterUsage.sendCommand(commandPath, parameters: additionalUsageValues);
}
return await runCommand(); return await runCommand();
} }
......
...@@ -60,11 +60,8 @@ void main() { ...@@ -60,11 +60,8 @@ void main() {
); );
await flutterCommand.run(); await flutterCommand.run();
expect( verify(usage.sendCommand(captureAny, parameters: captureAnyNamed('parameters')));
verify(usage.sendCommand(captureAny, verify(usage.sendEvent(captureAny, 'success'));
parameters: captureAnyNamed('parameters'))).captured[1]['cd26'],
equals('success'),
);
}, },
overrides: <Type, Generator>{ overrides: <Type, Generator>{
SystemClock: () => clock, SystemClock: () => clock,
...@@ -82,11 +79,8 @@ void main() { ...@@ -82,11 +79,8 @@ void main() {
); );
await flutterCommand.run(); await flutterCommand.run();
expect( verify(usage.sendCommand(captureAny, parameters: captureAnyNamed('parameters')));
verify(usage.sendCommand(captureAny, verify(usage.sendEvent(captureAny, 'warning'));
parameters: captureAnyNamed('parameters'))).captured[1]['cd26'],
equals('warning'),
);
}, },
overrides: <Type, Generator>{ overrides: <Type, Generator>{
SystemClock: () => clock, SystemClock: () => clock,
...@@ -106,11 +100,8 @@ void main() { ...@@ -106,11 +100,8 @@ void main() {
try { try {
await flutterCommand.run(); await flutterCommand.run();
} on ToolExit { } on ToolExit {
expect( verify(usage.sendCommand(captureAny, parameters: captureAnyNamed('parameters')));
verify(usage.sendCommand(captureAny, verify(usage.sendEvent(captureAny, 'fail'));
parameters: captureAnyNamed('parameters'))).captured[1]['cd26'],
equals('fail'),
);
} }
}, },
overrides: <Type, Generator>{ overrides: <Type, Generator>{
...@@ -133,11 +124,8 @@ void main() { ...@@ -133,11 +124,8 @@ void main() {
await flutterCommand.run(); await flutterCommand.run();
fail('Mock should make this fail'); fail('Mock should make this fail');
} on ToolExit { } on ToolExit {
expect( verify(usage.sendCommand(captureAny, parameters: captureAnyNamed('parameters')));
verify(usage.sendCommand(captureAny, verify(usage.sendEvent(captureAny, 'fail'));
parameters: captureAnyNamed('parameters'))).captured[1]['cd26'],
equals('fail'),
);
} }
}, },
overrides: <Type, Generator>{ overrides: <Type, Generator>{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment