Skip to content

Commit

Permalink
Release command
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Jun 22, 2020
1 parent f2fe3ec commit fd86c94
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
30 changes: 17 additions & 13 deletions lib/commands/releases.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// import 'dart:io';

// import 'package:io/ansi.dart';
import 'package:args/command_runner.dart';
import 'package:console/console.dart';
// import 'package:fvm/utils/print.dart';

import 'package:fvm/utils/releases_helper.dart';
// import 'package:fvm/utils/print.dart';
// import 'package:fvm/constants.dart';
import 'package:fvm/utils/version_installer.dart';

/// List installed SDK Versions
class ReleasesCommand extends Command {
Expand All @@ -25,17 +20,26 @@ class ReleasesCommand extends Command {
@override
void run() async {
final flutterReleases = await fetchReleases();
final channels = flutterReleases.currentRelease.toMap();

final list = <String>[];

channels.forEach((key, value) {
list.add('$key: $value');
});

final releases = flutterReleases.releases;
final outputReleases = releases.map((release) {
return '${release.version}';
}).toList();
var chooser = Chooser<String>(
outputReleases,
message: 'Select a version: ',
list,
message: 'Select a release: ',
);

var version = chooser.chooseSync();
print('You chose $version.');

channels.forEach((key, value) {
if (version == '$key: $value') {
installFlutterVersion(value as String);
}
});
print('You chose $version');
}
}
22 changes: 20 additions & 2 deletions lib/utils/releases_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ Future<FlutterReleases> fetchReleases() async {
}
}

Map<String, dynamic> filterCurrentReleases(Map<String, dynamic> json) {
final currentRelease = json['current_release'] as Map<String, dynamic>;
final releases = json['releases'] as List<dynamic>;
// Hashes of current releases
final hashMap = currentRelease.map((key, value) => MapEntry(value, key));

// Filter out channel/currentRelease versions
releases.forEach((r) {
// Check if release hash is in hashmap
final channel = hashMap[r['hash']];
if (channel != null) {
currentRelease[channel] = r['version'];
}
});

return currentRelease;
}

FlutterReleases flutterReleasesFromMap(String str) =>
FlutterReleases.fromMap(jsonDecode(str) as Map<String, dynamic>);

Expand All @@ -37,10 +55,10 @@ class FlutterReleases {
final List<Release> releases;

factory FlutterReleases.fromMap(Map<String, dynamic> json) {
final currentRelease = filterCurrentReleases(json);
return FlutterReleases(
baseUrl: json['base_url'] as String,
currentRelease: CurrentRelease.fromMap(
json['current_release'] as Map<String, dynamic>),
currentRelease: CurrentRelease.fromMap(currentRelease),
releases: List<Release>.from(json['releases']
.map((x) => Release.fromMap(x as Map<String, dynamic>))
as Iterable<dynamic>),
Expand Down

0 comments on commit fd86c94

Please sign in to comment.