Skip to content

Instantly share code, notes, and snippets.

@dnfield
Last active September 20, 2018 20:31
Show Gist options
  • Save dnfield/4285613d803807adf7444ab5bf988393 to your computer and use it in GitHub Desktop.
Save dnfield/4285613d803807adf7444ab5bf988393 to your computer and use it in GitHub Desktop.
Gets the versions of Flutter from GitHub

Flutter Version Check

This will print out the versions of the three major Flutter branches on GitHub.

$ dart flutter_version.dart
Flutter beta is at v0.4.4
Flutter dev is at v0.5.4
Flutter master is at <NO TAG>

It's also possible to check only one branch, and to print out more detail about the branch and tag that is found: dart flutter_version.dart beta or dart flutter_version master -v.

import 'dart:async';
import 'dart:convert';
import 'dart:io';
void main(List<String> args) async {
final String branchName =
args != null && args.length > 0 ? args[0].toLowerCase() : null;
final bool verbose = args.contains('--verbose') || args.contains('-v');
final List<Map<String, dynamic>> branches =
await getJson('https://api.github.com/repos/flutter/flutter/branches');
final List<Map<String, dynamic>> tags =
await getJson('https://api.github.com/repos/flutter/flutter/tags');
if (branchName != null && branchName.trim() != '') {
printBranchVersion(branches, tags, branchName, verbose);
} else {
Function printer =
(String n) => printBranchVersion(branches, tags, n, verbose);
['beta', 'dev', 'master'].forEach(printer);
}
}
void printBranchVersion(
List<dynamic> branches, List<dynamic> tags, String branchName,
[bool verbose]) {
final Map<String, dynamic> branch =
branches.firstWhere((x) => x['name'].toLowerCase() == branchName, orElse: () => null);
if (branch == null) {
print('No branch named $branchName');
return;
}
final String branchSha = branch['commit']['sha'];
if (verbose) {
print(branch);
}
final Map<String, dynamic> tag = tags
.firstWhere((x) => x['commit']['sha'] == branchSha, orElse: () => null);
final String tagName = tag == null ? '<NO TAG>' : tag['name'];
if (verbose) {
print(tag);
}
print('Flutter $branchName is at ${tagName}');
}
Future<List<Map<String, dynamic>>> getJson(String uri) async {
final HttpClient http = new HttpClient();
final HttpClientRequest req = await http.getUrl(Uri.parse(uri));
final HttpClientResponse resp = await req.close();
assert(resp.statusCode == HttpStatus.OK);
final List<dynamic> ret =
json.decode(utf8.decode(await resp.expand((x) => x).toList()));
http.close(force: true);
return ret.cast<Map<String, dynamic>>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment