From fa7424d40869e9e81c0ea6f09c0eb7afa9eecbea Mon Sep 17 00:00:00 2001 From: leoafarias Date: Wed, 6 Jul 2022 09:22:45 -0300 Subject: [PATCH 1/2] Filter releases based on architecture --- CHANGELOG.md | 4 ++ .../current_release_parser.dart | 42 +++++++++++++++++++ .../releases_service/releases_client.dart | 4 +- lib/src/version.dart | 2 +- pubspec.yaml | 2 +- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b4d07e2..73726289 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.4.1 + +- Filter out Mac releases based on architecture. + ## 2.4.0 - Upgraded minimum Dart version to 2.17.0 for better Flutter 3.0 compatibility diff --git a/lib/src/services/releases_service/current_release_parser.dart b/lib/src/services/releases_service/current_release_parser.dart index 6090ba5d..d3dc46c8 100644 --- a/lib/src/services/releases_service/current_release_parser.dart +++ b/lib/src/services/releases_service/current_release_parser.dart @@ -1,3 +1,10 @@ +import 'dart:io'; + +const arm = 'arm'; +const arm64 = 'arm64'; +const ia32 = 'ia32'; +const x64 = 'x64'; + /// Goes through the current_release payload. /// Finds the proper release base on the hash /// Assings to the current_release @@ -5,6 +12,17 @@ Map parseCurrentReleases(Map json) { final currentRelease = json['current_release'] as Map; final releases = json['releases'] as List; + final systemArch = _architecture(); + + if (Platform.isMacOS) { + // Filter out releases based on architecture + // Remove if architecture is not compatible + releases.removeWhere((release) { + final arch = release['dart_sdk_arch']; + return arch != systemArch && arch != null; + }); + } + // Filter out channel/currentRelease versions // Could be more efficient for (var release in releases) { @@ -19,3 +37,27 @@ Map parseCurrentReleases(Map json) { return currentRelease; } + +// https://stackoverflow.com/questions/45125516/possible-values-for-uname-m +final _unames = { + 'arm': arm, + 'arm64': arm64, + 'aarch64_be': arm64, + 'aarch64': arm64, + 'armv8b': arm64, + 'armv8l': arm64, + 'i386': ia32, + 'i686': ia32, + 'x86_64': x64, +}; + +String _architecture() { + final uname = Process.runSync('uname', ['-m']).stdout as String; + final trimmedName = uname.trim(); + final architecture = _unames[trimmedName]; + if (architecture == null) { + throw Exception('Unrecognized architecture: "$trimmedName"'); + } + + return architecture; +} diff --git a/lib/src/services/releases_service/releases_client.dart b/lib/src/services/releases_service/releases_client.dart index 702dc738..8bcb9360 100644 --- a/lib/src/services/releases_service/releases_client.dart +++ b/lib/src/services/releases_service/releases_client.dart @@ -41,6 +41,7 @@ Future fetchFlutterReleases({bool cache = true}) async { return Future.value(_cacheReleasesRes); } final response = await fetch(getReleasesUrl()); + _cacheReleasesRes = FlutterReleases.fromJson(response); return Future.value(_cacheReleasesRes); } on Exception catch (err) { @@ -50,7 +51,8 @@ Future fetchFlutterReleases({bool cache = true}) async { final response = await fetch(getGoogleReleaseUrl()); _cacheReleasesRes = FlutterReleases.fromJson(response); return Future.value(_cacheReleasesRes); - } on Exception { + } on Exception catch (err) { + logger.trace(err.toString()); throw FvmInternalError( 'Failed to retrieve the Flutter SDK from: ${getGoogleReleaseUrl()}\n' 'Fvm will use the value set on ' diff --git a/lib/src/version.dart b/lib/src/version.dart index cfb4a066..38a2ff8a 100644 --- a/lib/src/version.dart +++ b/lib/src/version.dart @@ -1,2 +1,2 @@ // Generated code. Do not modify. -const packageVersion = '2.4.0'; +const packageVersion = '2.4.1'; diff --git a/pubspec.yaml b/pubspec.yaml index 1e9d8b52..7c895ffa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: fvm description: A simple cli to manage Flutter SDK versions per project. Support channels, releases, and local cache for fast switching between versions. -version: 2.4.0 +version: 2.4.1 homepage: https://github.com/leoafarias/fvm environment: From 02aade469e102705f377c5cc9909818ef2a85407 Mon Sep 17 00:00:00 2001 From: leoafarias Date: Wed, 6 Jul 2022 11:37:10 -0300 Subject: [PATCH 2/2] Filter releases by architecture --- .../current_release_parser.dart | 66 ++++++++++++------- .../models/flutter_releases.model.dart | 8 +-- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/lib/src/services/releases_service/current_release_parser.dart b/lib/src/services/releases_service/current_release_parser.dart index d3dc46c8..cc2ecb95 100644 --- a/lib/src/services/releases_service/current_release_parser.dart +++ b/lib/src/services/releases_service/current_release_parser.dart @@ -1,27 +1,30 @@ import 'dart:io'; -const arm = 'arm'; -const arm64 = 'arm64'; -const ia32 = 'ia32'; -const x64 = 'x64'; +const _arm = 'arm'; +const _arm64 = 'arm64'; +const _ia32 = 'ia32'; +const _x64 = 'x64'; + +/// Parsed release model +class ParsedReleases { + /// Constructor + const ParsedReleases({required this.channels, required this.releases}); + + /// Channels + final Map channels; + + /// Releases + final List releases; +} /// Goes through the current_release payload. /// Finds the proper release base on the hash /// Assings to the current_release -Map parseCurrentReleases(Map json) { +ParsedReleases parseCurrentReleases(Map json) { final currentRelease = json['current_release'] as Map; final releases = json['releases'] as List; - final systemArch = _architecture(); - - if (Platform.isMacOS) { - // Filter out releases based on architecture - // Remove if architecture is not compatible - releases.removeWhere((release) { - final arch = release['dart_sdk_arch']; - return arch != systemArch && arch != null; - }); - } + final systemArch = 'x64'; // Filter out channel/currentRelease versions // Could be more efficient @@ -35,20 +38,33 @@ Map parseCurrentReleases(Map json) { } } - return currentRelease; + if (Platform.isMacOS) { + // Filter out releases based on architecture + // Remove if architecture is not compatible + releases.removeWhere((release) { + final arch = release['dart_sdk_arch']; + final isActiveChanel = release['activeChannel'] == true; + return arch != systemArch && arch != null && !isActiveChanel; + }); + } + + return ParsedReleases( + channels: currentRelease, + releases: releases, + ); } // https://stackoverflow.com/questions/45125516/possible-values-for-uname-m final _unames = { - 'arm': arm, - 'arm64': arm64, - 'aarch64_be': arm64, - 'aarch64': arm64, - 'armv8b': arm64, - 'armv8l': arm64, - 'i386': ia32, - 'i686': ia32, - 'x86_64': x64, + 'arm': _arm, + 'arm64': _arm64, + 'aarch64_be': _arm64, + 'aarch64': _arm64, + 'armv8b': _arm64, + 'armv8l': _arm64, + 'i386': _ia32, + 'i686': _ia32, + 'x86_64': _x64, }; String _architecture() { diff --git a/lib/src/services/releases_service/models/flutter_releases.model.dart b/lib/src/services/releases_service/models/flutter_releases.model.dart index b20f938d..377752f9 100644 --- a/lib/src/services/releases_service/models/flutter_releases.model.dart +++ b/lib/src/services/releases_service/models/flutter_releases.model.dart @@ -31,14 +31,14 @@ class FlutterReleases { /// Create FlutterRelease from a map of values factory FlutterReleases.fromMap(Map json) { - final currentRelease = parseCurrentReleases(json); + final parsedResults = parseCurrentReleases(json); return FlutterReleases( baseUrl: json['base_url'] as String, - channels: Channels.fromMap(currentRelease), + channels: Channels.fromMap(parsedResults.channels), releases: List.from( - json['releases'].map( + parsedResults.releases.map( (release) => Release.fromMap(release as Map), - ) as Iterable, + ), ), ); }