diff --git a/packages/flutter_tools/lib/src/commands/channel.dart b/packages/flutter_tools/lib/src/commands/channel.dart index 2a271a4060..bedbedfd40 100644 --- a/packages/flutter_tools/lib/src/commands/channel.dart +++ b/packages/flutter_tools/lib/src/commands/channel.dart @@ -72,7 +72,7 @@ class ChannelCommand extends FlutterCommand { throwToolExit('List channels failed: $result$details', exitCode: result); } - final List officialChannels = FlutterVersion.officialChannels.toList(); + final List officialChannels = kOfficialChannels.toList(); final List availableChannels = List.filled(officialChannels.length, false); for (final String line in rawOutput) { @@ -116,10 +116,10 @@ class ChannelCommand extends FlutterCommand { Future _switchChannel(String branchName) async { globals.printStatus("Switching to flutter channel '$branchName'..."); - if (FlutterVersion.obsoleteBranches.containsKey(branchName)) { - final String alternative = FlutterVersion.obsoleteBranches[branchName]; + if (kObsoleteBranches.containsKey(branchName)) { + final String alternative = kObsoleteBranches[branchName]; globals.printStatus("This channel is obsolete. Consider switching to the '$alternative' channel instead."); - } else if (!FlutterVersion.officialChannels.contains(branchName)) { + } else if (!kOfficialChannels.contains(branchName)) { globals.printStatus('This is not an official channel. For a list of available channels, try "flutter channel".'); } await _checkout(branchName); @@ -129,8 +129,8 @@ class ChannelCommand extends FlutterCommand { static Future upgradeChannel() async { final String channel = globals.flutterVersion.channel; - if (FlutterVersion.obsoleteBranches.containsKey(channel)) { - final String alternative = FlutterVersion.obsoleteBranches[channel]; + if (kObsoleteBranches.containsKey(channel)) { + final String alternative = kObsoleteBranches[channel]; globals.printStatus("Transitioning from '$channel' to '$alternative'..."); return _checkout(alternative); } diff --git a/packages/flutter_tools/lib/src/commands/upgrade.dart b/packages/flutter_tools/lib/src/commands/upgrade.dart index fb36b2a3dc..39177ebfec 100644 --- a/packages/flutter_tools/lib/src/commands/upgrade.dart +++ b/packages/flutter_tools/lib/src/commands/upgrade.dart @@ -107,7 +107,7 @@ class UpgradeCommandRunner { if (!force && gitTagVersion == const GitTagVersion.unknown()) { // If the commit is a recognized branch and not master, // explain that we are avoiding potential damage. - if (flutterVersion.channel != 'master' && FlutterVersion.officialChannels.contains(flutterVersion.channel)) { + if (flutterVersion.channel != 'master' && kOfficialChannels.contains(flutterVersion.channel)) { throwToolExit( 'Unknown flutter tag. Abandoning upgrade to avoid destroying local ' 'changes. It is recommended to use git directly if not working on ' diff --git a/packages/flutter_tools/lib/src/version.dart b/packages/flutter_tools/lib/src/version.dart index 9fe312a22c..dcfd8d60e9 100644 --- a/packages/flutter_tools/lib/src/version.dart +++ b/packages/flutter_tools/lib/src/version.dart @@ -13,6 +13,20 @@ import 'cache.dart'; import 'convert.dart'; import 'globals.dart' as globals; +/// The flutter GitHub repository. +String get _flutterGit => globals.platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git'; + +/// This maps old branch names to the names of branches that replaced them. +/// +/// For example, in early 2018 we changed from having an "alpha" branch to +/// having a "dev" branch, so anyone using "alpha" now gets transitioned to +/// "dev". +const Map kObsoleteBranches = { + 'alpha': 'dev', + 'hackathon': 'dev', + 'codelab': 'dev', +}; + /// The names of each channel/branch in order of increasing stability. enum Channel { master, @@ -21,23 +35,28 @@ enum Channel { stable, } -/// The flutter GitHub repository. -String get _flutterGit => globals.platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git'; +// Beware: Keep order in accordance with stability +const Set kOfficialChannels = { + 'master', + 'dev', + 'beta', + 'stable', +}; /// Retrieve a human-readable name for a given [channel]. /// -/// Requires [FlutterVersion.officialChannels] to be correctly ordered. +/// Requires [kOfficialChannels] to be correctly ordered. String getNameForChannel(Channel channel) { - return FlutterVersion.officialChannels.elementAt(channel.index); + return kOfficialChannels.elementAt(channel.index); } /// Retrieve the [Channel] representation for a string [name]. /// /// Returns `null` if [name] is not in the list of official channels, according -/// to [FlutterVersion.officialChannels]. +/// to [kOfficialChannels]. Channel getChannelForName(String name) { - if (FlutterVersion.officialChannels.contains(name)) { - return Channel.values[FlutterVersion.officialChannels.toList().indexOf(name)]; + if (kOfficialChannels.contains(name)) { + return Channel.values[kOfficialChannels.toList().indexOf(name)]; } return null; } @@ -84,25 +103,6 @@ class FlutterVersion { return !['dev', 'beta', 'stable'].contains(branchName); } - // Beware: Keep order in accordance with stability - static const Set officialChannels = { - 'master', - 'dev', - 'beta', - 'stable', - }; - - /// This maps old branch names to the names of branches that replaced them. - /// - /// For example, in early 2018 we changed from having an "alpha" branch to - /// having a "dev" branch, so anyone using "alpha" now gets transitioned to - /// "dev". - static Map obsoleteBranches = { - 'alpha': 'dev', - 'hackathon': 'dev', - 'codelab': 'dev', - }; - String _channel; /// The channel is the upstream branch. /// `master`, `dev`, `beta`, `stable`; or old ones, like `alpha`, `hackathon`, ... @@ -298,8 +298,8 @@ class FlutterVersion { }(); if (redactUnknownBranches || _branch.isEmpty) { // Only return the branch names we know about; arbitrary branch names might contain PII. - if (!officialChannels.contains(_branch) && - !obsoleteBranches.containsKey(_branch)) { + if (!kOfficialChannels.contains(_branch) && + !kObsoleteBranches.containsKey(_branch)) { return '[user-branch]'; } } @@ -388,7 +388,7 @@ class FlutterVersion { /// writes shared cache files. Future checkFlutterVersionFreshness() async { // Don't perform update checks if we're not on an official channel. - if (!officialChannels.contains(channel)) { + if (!kOfficialChannels.contains(channel)) { return; } diff --git a/packages/flutter_tools/test/general.shard/channel_test.dart b/packages/flutter_tools/test/general.shard/channel_test.dart index 7fc6172406..93851e710f 100644 --- a/packages/flutter_tools/test/general.shard/channel_test.dart +++ b/packages/flutter_tools/test/general.shard/channel_test.dart @@ -86,7 +86,7 @@ void main() { final Iterable rows = testLogger.statusText .split('\n') .map((String line) => line.substring(2)); // remove '* ' or ' ' from output - expect(rows, containsAllInOrder(FlutterVersion.officialChannels)); + expect(rows, containsAllInOrder(kOfficialChannels)); // clear buffer for next process testLogger.clear(); @@ -107,7 +107,7 @@ void main() { final Iterable rows2 = testLogger.statusText .split('\n') .map((String line) => line.substring(2)); // remove '* ' or ' ' from output - expect(rows2, containsAllInOrder(FlutterVersion.officialChannels)); + expect(rows2, containsAllInOrder(kOfficialChannels)); // clear buffer for next process testLogger.clear(); @@ -127,7 +127,7 @@ void main() { // check if available official channels are in order of stability int prev = -1; int next = -1; - for (final String branch in FlutterVersion.officialChannels) { + for (final String branch in kOfficialChannels) { next = testLogger.statusText.indexOf(branch); if (next != -1) { expect(prev < next, isTrue); diff --git a/packages/flutter_tools/test/general.shard/version_test.dart b/packages/flutter_tools/test/general.shard/version_test.dart index c74a5fd03e..cb161e58bd 100644 --- a/packages/flutter_tools/test/general.shard/version_test.dart +++ b/packages/flutter_tools/test/general.shard/version_test.dart @@ -36,7 +36,15 @@ void main() { mockCache = MockCache(); }); - for (final String channel in FlutterVersion.officialChannels) { + testUsingContext('Channel enum and string transform to each other', () { + for (final Channel channel in Channel.values) { + expect(getNameForChannel(channel), kOfficialChannels.toList()[channel.index]); + } + expect(kOfficialChannels.toList().map((String str) => getChannelForName(str)).toList(), + Channel.values); + }); + + for (final String channel in kOfficialChannels) { DateTime getChannelUpToDateVersion() { return _testClock.ago(FlutterVersion.versionAgeConsideredUpToDate(channel) ~/ 2); }