Skip to content

Commit

Permalink
Merge pull request #445 from fluttertools/feature/filter-architecture…
Browse files Browse the repository at this point in the history
…-mac

Filter releases based on architecture
  • Loading branch information
leoafarias authored Jul 6, 2022
2 parents dc0788a + 02aade4 commit 148857b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 60 additions & 2 deletions lib/src/services/releases_service/current_release_parser.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import 'dart:io';

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<String, dynamic> channels;

/// Releases
final List<dynamic> releases;
}

/// Goes through the current_release payload.
/// Finds the proper release base on the hash
/// Assings to the current_release
Map<String, dynamic> parseCurrentReleases(Map<String, dynamic> json) {
ParsedReleases parseCurrentReleases(Map<String, dynamic> json) {
final currentRelease = json['current_release'] as Map<String, dynamic>;
final releases = json['releases'] as List<dynamic>;

final systemArch = 'x64';

// Filter out channel/currentRelease versions
// Could be more efficient
for (var release in releases) {
Expand All @@ -17,5 +38,42 @@ Map<String, dynamic> parseCurrentReleases(Map<String, dynamic> 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,
};

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class FlutterReleases {

/// Create FlutterRelease from a map of values
factory FlutterReleases.fromMap(Map<String, dynamic> 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<Release>.from(
json['releases'].map(
parsedResults.releases.map(
(release) => Release.fromMap(release as Map<String, dynamic>),
) as Iterable<dynamic>,
),
),
);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/services/releases_service/releases_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Future<FlutterReleases> 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) {
Expand All @@ -50,7 +51,8 @@ Future<FlutterReleases> 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 '
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 148857b

Please sign in to comment.