-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Use inferred flutter version #5207
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
Merged
mctofu
merged 19 commits into
dependabot:main
from
sigurdm:use_preferred_flutter_version
Jun 13, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c5eb881
Infer the flutter version from pubspec.yaml
sigurdm 4841bee
Test of infer_sdk_versions tool
sigurdm a0d214e
Update pub/README.md
sigurdm f1149cb
lints
sigurdm 6ba8da1
Remove commented code
sigurdm 473fddc
merge to main
sigurdm 3f9cc76
Scoping
sigurdm c68388f
Work around https://github.com/flutter/flutter/issues/54014
sigurdm 9d368da
Actually use the flutter downloaded
sigurdm 7d43821
Typo
sigurdm a43dc29
Handle empty url given
sigurdm 37065ae
Pull fixtures from right location
sigurdm fbcf171
trailing newline in .gitignore
sigurdm d08618d
Delete stray files
sigurdm 65ebd08
Final newline in pubspec
sigurdm befa583
Typo
sigurdm be3113d
Typo
sigurdm a5f0c57
Typo
sigurdm d3fe6ce
Markdown formatting
sigurdm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ | |
| /tmp | ||
| /dependabot-*.gem | ||
| Gemfile.lock | ||
| .dart_tool/ | ||
| .packages | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /// Support for automated upgrades. | ||
| library dependency_services; | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:args/args.dart'; | ||
| import 'package:args/command_runner.dart'; | ||
| import 'package:pub/src/command.dart'; | ||
| import 'package:pub/src/command/dependency_services.dart'; | ||
| import 'package:pub/src/exit_codes.dart' as exit_codes; | ||
| import 'package:pub/src/io.dart'; | ||
| import 'package:pub/src/log.dart' as log; | ||
|
|
||
| class _DependencyServicesCommandRunner extends CommandRunner<int> | ||
| implements PubTopLevel { | ||
| @override | ||
| String? get directory => argResults['directory']; | ||
|
|
||
| @override | ||
| bool get captureStackChains => argResults['verbose']; | ||
|
|
||
| @override | ||
| bool get trace => argResults['verbose']; | ||
|
|
||
| ArgResults? _argResults; | ||
|
|
||
| /// The top-level options parsed by the command runner. | ||
| @override | ||
| ArgResults get argResults { | ||
| final a = _argResults; | ||
| if (a == null) { | ||
| throw StateError( | ||
| 'argResults cannot be used before Command.run is called.'); | ||
| } | ||
| return a; | ||
| } | ||
|
|
||
| _DependencyServicesCommandRunner() | ||
| : super('dependency_services', 'Support for automatic upgrades', | ||
| usageLineLength: lineLength) { | ||
| argParser.addFlag('verbose', | ||
| abbr: 'v', negatable: false, help: 'Shortcut for "--verbosity=all".'); | ||
| argParser.addOption( | ||
| 'directory', | ||
| abbr: 'C', | ||
| help: 'Run the subcommand in the directory<dir>.', | ||
| defaultsTo: '.', | ||
| valueHelp: 'dir', | ||
| ); | ||
|
|
||
| addCommand(DependencyServicesListCommand()); | ||
| addCommand(DependencyServicesReportCommand()); | ||
| addCommand(DependencyServicesApplyCommand()); | ||
| } | ||
|
|
||
| @override | ||
| Future<int> run(Iterable<String> args) async { | ||
| try { | ||
| _argResults = parse(args); | ||
| return await runCommand(argResults) ?? exit_codes.SUCCESS; | ||
| } on UsageException catch (error) { | ||
| log.exception(error); | ||
| return exit_codes.USAGE; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void printUsage() { | ||
| log.message(usage); | ||
| } | ||
|
|
||
| @override | ||
| log.Verbosity get verbosity => log.Verbosity.normal; | ||
| } | ||
|
|
||
| Future<void> main(List<String> arguments) async { | ||
| await flushThenExit(await _DependencyServicesCommandRunner().run(arguments)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:args/args.dart'; | ||
| import 'package:collection/collection.dart'; | ||
| import 'package:http/http.dart'; | ||
| import 'package:http/retry.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
| import 'package:pub_semver/pub_semver.dart'; | ||
| import 'package:path/path.dart' as p; | ||
|
|
||
| Never fail(String message) { | ||
| stderr.writeln(message); | ||
| exit(-1); | ||
| } | ||
|
|
||
| final client = RetryClient(Client()); | ||
|
|
||
| ArgResults parseArgs(List<String> args) { | ||
| final argParser = ArgParser() | ||
| ..addOption( | ||
| 'directory', | ||
| abbr: 'C', | ||
| defaultsTo: '.', | ||
| help: 'The directory containing the pubspec.yaml of the package.', | ||
| ) | ||
| ..addOption('flutter-releases-url', | ||
| help: | ||
| 'The url to retrieve the list of available flutter releases from.') | ||
| ..addFlag('help', help: 'Display the usage message.'); | ||
| final results; | ||
| try { | ||
| results = argParser.parse(args); | ||
| if (results['help'] as bool) { | ||
| stdout.writeln( | ||
| 'Infers the newest available flutter sdk to use for a package.'); | ||
| stdout.writeln(argParser.usage); | ||
| exit(0); | ||
| } | ||
| return results; | ||
| } on FormatException catch (e) { | ||
| stderr.writeln(e.message); | ||
| stderr.writeln(argParser.usage); | ||
| exit(-1); | ||
| } | ||
| } | ||
|
|
||
| Map<String, VersionConstraint> parseSdkConstraints(dynamic pubspec) { | ||
| final dartConstraint = | ||
| VersionConstraint.parse(pubspec['environment']?['sdk'] ?? 'any'); | ||
| final flutterConstraint = | ||
| VersionConstraint.parse(pubspec['environment']?['flutter'] ?? 'any'); | ||
| return { | ||
| 'dart': dartConstraint, | ||
| 'flutter': flutterConstraint, | ||
| }; | ||
| } | ||
|
|
||
| Future<void> main(List<String> args) async { | ||
| try { | ||
| final argResults = parseArgs(args); | ||
| var url = argResults['flutter-releases-url']; | ||
| if (url == null || url.isEmpty) { | ||
| url = flutterReleasesUrl; | ||
| } | ||
| final flutterReleases = await retrieveFlutterReleases(url); | ||
|
|
||
| final pubspecPath = p.join(argResults['directory'], 'pubspec.yaml'); | ||
| final pubspec = loadYaml(File(pubspecPath).readAsStringSync(), | ||
| sourceUrl: Uri.file(pubspecPath)); | ||
|
|
||
| final bestFlutterRelease = | ||
| inferBestFlutterRelease(parseSdkConstraints(pubspec), flutterReleases); | ||
| if (bestFlutterRelease == null) { | ||
| fail( | ||
| 'No flutter release matching sdk constraints.', | ||
| ); | ||
| } | ||
| stdout.writeln(JsonEncoder.withIndent(' ').convert({ | ||
| 'flutter': bestFlutterRelease.flutterVersion.toString(), | ||
| 'dart': bestFlutterRelease.dartVersion.toString(), | ||
| 'channel': { | ||
| Channel.stable: 'stable', | ||
| Channel.beta: 'beta', | ||
| Channel.dev: 'dev' | ||
| }[bestFlutterRelease.channel], | ||
| })); | ||
| } on FormatException catch (e) { | ||
| fail(e.message); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| } | ||
|
|
||
| String get flutterReleasesUrl => | ||
| 'https://storage.googleapis.com/flutter_infra_release/releases/releases_linux.json'; | ||
|
|
||
| // Retrieves all released versions of Flutter. | ||
| Future<List<FlutterRelease>> retrieveFlutterReleases(String url) async { | ||
| final response = await client.get(Uri.parse(url)); | ||
| final decoded = jsonDecode(response.body); | ||
| if (decoded is! Map) throw FormatException('Bad response - should be a Map'); | ||
| final releases = decoded['releases']; | ||
| if (releases is! List) | ||
| throw FormatException('Bad response - releases should be a list.'); | ||
| final result = <FlutterRelease>[]; | ||
| for (final release in releases) { | ||
| final channel = { | ||
| 'beta': Channel.beta, | ||
| 'stable': Channel.stable, | ||
| 'dev': Channel.dev | ||
| }[release['channel']]; | ||
| if (channel == null) throw FormatException('Release with bad channel'); | ||
| final dartVersion = release['dart_sdk_version']; | ||
| // Some releases don't have an associated dart version, ignore. | ||
| if (dartVersion is! String) continue; | ||
| final flutterVersion = release['version']; | ||
| if (flutterVersion is! String) throw FormatException('Not a string'); | ||
| result.add(FlutterRelease( | ||
| flutterVersion: Version.parse(flutterVersion), | ||
| dartVersion: Version.parse(dartVersion.split(' ').first), | ||
| channel: channel, | ||
| )); | ||
| } | ||
| return result | ||
| // Sort releases by channel and version. | ||
| .sorted((a, b) { | ||
| final compareChannels = b.channel.index - a.channel.index; | ||
| if (compareChannels != 0) return compareChannels; | ||
| return a.flutterVersion.compareTo(b.flutterVersion); | ||
| }) | ||
| // Newest first. | ||
| .reversed | ||
| .toList(); | ||
| } | ||
|
|
||
| /// The "best" Flutter release for a given set of constraints is the first one | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does "first" mean: oldest or newest Flutter version?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the first in the list passed to this function. I tried to rephrase a bit. |
||
| /// in [flutterReleases] that matches both the flutter and dart constraint. | ||
| FlutterRelease? inferBestFlutterRelease( | ||
| Map<String, VersionConstraint> sdkConstraints, | ||
| List<FlutterRelease> flutterReleases) { | ||
| return flutterReleases.firstWhereOrNull((release) => | ||
| (sdkConstraints['flutter'] ?? VersionConstraint.any) | ||
| .allows(release.flutterVersion) && | ||
| (sdkConstraints['dart'] ?? VersionConstraint.any) | ||
| .allows(release.dartVersion)); | ||
| } | ||
|
|
||
| enum Channel { | ||
| stable, | ||
| beta, | ||
| dev, | ||
| } | ||
|
|
||
| /// A version of the Flutter SDK and its related Dart SDK. | ||
| class FlutterRelease { | ||
| final Version flutterVersion; | ||
| final Version dartVersion; | ||
| final Channel channel; | ||
| FlutterRelease({ | ||
| required this.flutterVersion, | ||
| required this.dartVersion, | ||
| required this.channel, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Precompiles the helper scripts for the pub integration. | ||
|
|
||
| set -e | ||
|
|
||
| if [ -z "$DEPENDABOT_NATIVE_HELPERS_PATH" ]; then | ||
| echo "Unable to build, DEPENDABOT_NATIVE_HELPERS_PATH is not set" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Retrieve the dependencies | ||
| dart pub get -C "$DEPENDABOT_NATIVE_HELPERS_PATH/pub/helpers" | ||
|
|
||
| # Compile the helpers | ||
| dart compile exe "$DEPENDABOT_NATIVE_HELPERS_PATH/pub/helpers/bin/dependency_services.dart" -o "$DEPENDABOT_NATIVE_HELPERS_PATH/pub/dependency_services" | ||
| dart compile exe "$DEPENDABOT_NATIVE_HELPERS_PATH/pub/helpers/bin/infer_sdk_versions.dart" -o "$DEPENDABOT_NATIVE_HELPERS_PATH/pub/infer_sdk_versions" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inferring is a good start, but dependabot should allow pointing to an exact git-ref (
tag,sha1).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm - do you have an idea for a good place to put this configuration?
What are scenarios where you would need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scenario: We use
flutterwto pin the flutter version. Others usefvmor custom solutions.Tree options pop into my mind:
dependabot.flutter_versionproperty topubspec.yamlof each package and read itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @jonasfj what do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it sufficient that we allow pinning the Flutter SDK constraint in
environment.flutter.Generally, the
dart pubclient will ignore the upper-bound inenvironment.flutter, as a result of a decision made in Flutter 2 that Flutter wouldn't break backwards compatibility.But in dependabot, I think we made it respect the upper-bound, thus, the
environment.flutterconstraint can be used to pin a Flutter version. You can't pin to arbitrary sha or tag, but you can pin to stable and beta releases of Flutter.My two cents is that this is a reasonable compromise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the lower bound of
environment.flutterwould work for us. But it could prevent dependabot from adding the latest versions.Using the upper bound wouldn't be practical. We'd have to change it every time we upgrade in all packages of our mono repo.