Unverified Commit 96942bc7 authored by Mehmet Fidanboylu's avatar Mehmet Fidanboylu Committed by GitHub

Get rid of static version methods which ignore the AppContext (#12971)

* Get rid of static version methods which ignore the AppContext

* Review comments

* Review Comments

* Make branch behave the same as original implementation

* Fix tests
parent 870da175
...@@ -89,7 +89,7 @@ Future<int> run( ...@@ -89,7 +89,7 @@ Future<int> run(
await runner.run(args); await runner.run(args);
await _exit(0); await _exit(0);
} catch (error, stackTrace) { } catch (error, stackTrace) {
String getVersion() => flutterVersion ?? FlutterVersion.getVersionString(); String getVersion() => flutterVersion ?? FlutterVersion.instance.getVersionString();
return await _handleToolError(error, stackTrace, verbose, args, reportCrashes, getVersion); return await _handleToolError(error, stackTrace, verbose, args, reportCrashes, getVersion);
} }
return 0; return 0;
......
...@@ -21,13 +21,14 @@ Usage get flutterUsage => Usage.instance; ...@@ -21,13 +21,14 @@ Usage get flutterUsage => Usage.instance;
class Usage { class Usage {
/// Create a new Usage instance; [versionOverride] is used for testing. /// Create a new Usage instance; [versionOverride] is used for testing.
Usage({ String settingsName: 'flutter', String versionOverride }) { Usage({ String settingsName: 'flutter', String versionOverride }) {
final String version = versionOverride ?? FlutterVersion.getVersionString(whitelistBranchName: true); final FlutterVersion flutterVersion = FlutterVersion.instance;
final String version = versionOverride ?? flutterVersion.getVersionString(whitelistBranchName: true);
_analytics = new AnalyticsIO(_kFlutterUA, settingsName, version); _analytics = new AnalyticsIO(_kFlutterUA, settingsName, version);
// Report a more detailed OS version string than package:usage does by default. // Report a more detailed OS version string than package:usage does by default.
_analytics.setSessionValue('cd1', os.name); _analytics.setSessionValue('cd1', os.name);
// Send the branch name as the "channel". // Send the branch name as the "channel".
_analytics.setSessionValue('cd2', FlutterVersion.getBranchName(whitelistBranchName: true)); _analytics.setSessionValue('cd2', flutterVersion.getBranchName(whitelistBranchName: true));
// Record the host as the application installer ID - the context that flutter_tools is running in. // Record the host as the application installer ID - the context that flutter_tools is running in.
if (platform.environment.containsKey('FLUTTER_HOST')) { if (platform.environment.containsKey('FLUTTER_HOST')) {
_analytics.setSessionValue('aiid', platform.environment['FLUTTER_HOST']); _analytics.setSessionValue('aiid', platform.environment['FLUTTER_HOST']);
......
...@@ -28,6 +28,8 @@ class FlutterVersion { ...@@ -28,6 +28,8 @@ class FlutterVersion {
@visibleForTesting @visibleForTesting
FlutterVersion(this._clock) { FlutterVersion(this._clock) {
_channel = _runGit('git rev-parse --abbrev-ref --symbolic @{u}'); _channel = _runGit('git rev-parse --abbrev-ref --symbolic @{u}');
final String branch = _runGit('git rev-parse --abbrev-ref HEAD');
_branch = branch == 'HEAD' ? _channel : branch;
final int slash = _channel.indexOf('/'); final int slash = _channel.indexOf('/');
if (slash != -1) { if (slash != -1) {
...@@ -51,6 +53,9 @@ class FlutterVersion { ...@@ -51,6 +53,9 @@ class FlutterVersion {
/// `master`, `alpha`, `hackathon`, ... /// `master`, `alpha`, `hackathon`, ...
String get channel => _channel; String get channel => _channel;
/// The name of the local branch
String _branch;
String _frameworkRevision; String _frameworkRevision;
String get frameworkRevision => _frameworkRevision; String get frameworkRevision => _frameworkRevision;
String get frameworkRevisionShort => _shortGitRevision(frameworkRevision); String get frameworkRevisionShort => _shortGitRevision(frameworkRevision);
...@@ -145,30 +150,21 @@ class FlutterVersion { ...@@ -145,30 +150,21 @@ class FlutterVersion {
static FlutterVersion get instance => context.putIfAbsent(FlutterVersion, () => new FlutterVersion(const Clock())); static FlutterVersion get instance => context.putIfAbsent(FlutterVersion, () => new FlutterVersion(const Clock()));
/// Return a short string for the version (`alpha/a76bc8e22b`). /// Return a short string for the version (`alpha/a76bc8e22b`).
static String getVersionString({ bool whitelistBranchName: false }) { String getVersionString({bool whitelistBranchName: false}) {
String commit = _shortGitRevision(_runSync(<String>['git', 'rev-parse', 'HEAD'])); return '${getBranchName(whitelistBranchName: whitelistBranchName)}/$frameworkRevisionShort';
commit = commit.isEmpty ? 'unknown' : commit;
final String branch = getBranchName(whitelistBranchName: whitelistBranchName);
return '$branch/$commit';
} }
/// Return the branch name. /// Return the branch name.
/// ///
/// If whitelistBranchName is true and the branch is unknown, /// If [whitelistBranchName] is true and the branch is unknown,
/// the branch name will be returned as 'dev'. /// the branch name will be returned as 'dev'.
static String getBranchName({ bool whitelistBranchName: false }) { String getBranchName({ bool whitelistBranchName: false }) {
String branch = _runSync(<String>['git', 'rev-parse', '--abbrev-ref', 'HEAD']); if (whitelistBranchName || _branch.isEmpty) {
branch = branch == 'HEAD' ? 'master' : branch;
if (whitelistBranchName || branch.isEmpty) {
// Only return the branch names we know about; arbitrary branch names might contain PII. // Only return the branch names we know about; arbitrary branch names might contain PII.
if (!kKnownBranchNames.contains(branch)) if (!kKnownBranchNames.contains(_branch))
branch = 'dev'; return 'dev';
} }
return _branch;
return branch;
} }
/// The amount of time we wait before pinging the server to check for the /// The amount of time we wait before pinging the server to check for the
......
...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/commands/config.dart'; ...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/commands/config.dart';
import 'package:flutter_tools/src/commands/doctor.dart'; import 'package:flutter_tools/src/commands/doctor.dart';
import 'package:flutter_tools/src/doctor.dart'; import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/usage.dart'; import 'package:flutter_tools/src/usage.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
import 'package:quiver/time.dart'; import 'package:quiver/time.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
...@@ -53,6 +54,7 @@ void main() { ...@@ -53,6 +54,7 @@ void main() {
await runner.run(<String>['doctor']); await runner.run(<String>['doctor']);
expect(count, 0); expect(count, 0);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => new FlutterVersion(const Clock()),
Usage: () => new Usage(), Usage: () => new Usage(),
}); });
...@@ -71,6 +73,7 @@ void main() { ...@@ -71,6 +73,7 @@ void main() {
await runner.run(<String>['config']); await runner.run(<String>['config']);
expect(count, 0); expect(count, 0);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FlutterVersion: () => new FlutterVersion(const Clock()),
Usage: () => new Usage(), Usage: () => new Usage(),
}); });
}); });
...@@ -101,7 +104,7 @@ void main() { ...@@ -101,7 +104,7 @@ void main() {
verify(mockClock.now()).called(2); verify(mockClock.now()).called(2);
expect( expect(
verify(mockUsage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured, verify(mockUsage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured,
<dynamic>['flutter', 'doctor', const Duration(milliseconds: 1000), 'success'] <dynamic>['flutter', 'doctor', const Duration(milliseconds: 1000), 'success']
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
...@@ -120,7 +123,7 @@ void main() { ...@@ -120,7 +123,7 @@ void main() {
verify(mockClock.now()).called(2); verify(mockClock.now()).called(2);
expect( expect(
verify(mockUsage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured, verify(mockUsage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured,
<dynamic>['flutter', 'doctor', const Duration(milliseconds: 1000), 'warning'] <dynamic>['flutter', 'doctor', const Duration(milliseconds: 1000), 'warning']
); );
}, 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