Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Flutter SDK location relative to Dart SDK #3045

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions lib/src/sdk/flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,67 @@ class FlutterSdk extends Sdk {
@override
Version get firstPubVersion => Version.parse('1.19.0');

static final bool _isAvailable =
Platform.environment.containsKey('FLUTTER_ROOT');
static final String _rootDirectory = Platform.environment['FLUTTER_ROOT'];
// We only consider the Flutter SDK to present if we find a root directory
// and the root directory contains a valid 'version' file.
static final bool _isAvailable = _rootDirectory != null && _version != null;
static final String _rootDirectory = () {
// If FLUTTER_ROOT is specified, then this always points to the Flutter SDK
if (Platform.environment.containsKey('FLUTTER_ROOT')) {
return Platform.environment['FLUTTER_ROOT'];
}

// We can try to find the Flutter SDK relative to the Dart SDK.
// We know that the Dart SDK is always present, this is found relative to
// the `dart` executable, for details see: lib/src/sdk/dart.dart
//
// Once we have the location of the Dart SDK, we can look at its parent
// directories, if going 3 levels-up and down `bin/cache/dart-sdk/` is equal
// to the Dart SDK root, then it's probably because we are located inside
// the Flutter SDK, at: `$FLUTTER_ROOT/bin/cache/dart-sdk`
final parts = p.split(sdk.rootDirectory);
if (parts.length > 3) {
// Go 3-levels up from the Dart SDK root
final flutterSdk = p.joinAll(parts.take(parts.length - 3));
// If going down 'bin/cache/dart-sdk/' yields the same path as the Dart
// SDK has, then it's probably because the Dart SDK is located inside
jonasfj marked this conversation as resolved.
Show resolved Hide resolved
// the Flutter SDK.
final dartRootFromFlutterSdk = p.join(
flutterSdk,
'bin',
'cache',
'dart-sdk',
);
if (p.equals(sdk.rootDirectory, dartRootFromFlutterSdk)) {
return flutterSdk;
}
}

return null;
}();
static final Version _version = () {
if (_rootDirectory == null) return null;

try {
return Version.parse(
readTextFile(p.join(_rootDirectory, 'version')).trim(),
);
} on IOException {
return null; // I guess the file doesn't exist
} on FormatException {
return null; // I guess the file has the wrong format
}
}();

@override
String get installMessage =>
'Flutter users should run `flutter pub get` instead of `pub get`.';
'Flutter users should run `flutter pub get` instead of `dart pub get`.';
jonasfj marked this conversation as resolved.
Show resolved Hide resolved

@override
Version get version {
if (!_isAvailable) return null;

_version ??=
Version.parse(readTextFile(p.join(_rootDirectory, 'version')).trim());
if (!isAvailable) return null;
return _version;
}

Version _version;

@override
String packagePath(String name) {
if (!isAvailable) return null;
Expand Down
2 changes: 1 addition & 1 deletion test/sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void main() {
Because myapp depends on foo any from sdk which doesn't exist (the
Flutter SDK is not available), version solving failed.

Flutter users should run `flutter pub get` instead of `pub
Flutter users should run `flutter pub get` instead of `dart pub
get`.
"""), exitCode: exit_codes.UNAVAILABLE);
});
Expand Down
6 changes: 3 additions & 3 deletions test/version_solver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ void sdkConstraint() {
await expectResolves(error: equalsIgnoringWhitespace('''
Because myapp requires the Flutter SDK, version solving failed.

Flutter users should run `flutter pub get` instead of `pub get`.
Flutter users should run `flutter pub get` instead of `dart pub get`.
'''));
});

Expand All @@ -1474,7 +1474,7 @@ void sdkConstraint() {
Because myapp depends on foo any which requires the Flutter SDK, version
solving failed.

Flutter users should run `flutter pub get` instead of `pub get`.
Flutter users should run `flutter pub get` instead of `dart pub get`.
'''));
});

Expand Down Expand Up @@ -1502,7 +1502,7 @@ void sdkConstraint() {
await expectResolves(error: equalsIgnoringWhitespace('''
Because myapp requires the Flutter SDK, version solving failed.

Flutter users should run `flutter pub get` instead of `pub get`.
Flutter users should run `flutter pub get` instead of `dart pub get`.
'''));
});
});
Expand Down