Skip to content

Commit

Permalink
[tool/ci] Add minimum supported SDK validation (#7028)
Browse files Browse the repository at this point in the history
Adds options to `pubspec.yaml` to check that the minimum supported SDK range for Flutter/Dart is at least a given version, to add CI enforcement that we're updating all of our support claims when we update our tested versions (rather than it being something we have to remember to do), and enables it in CI.

As part of enabling it, fixes some violations:
- path_provider_foundation had been temporarily dropped back to 2.10 as part of pushing out a regression fix.
- a number of examples were missing Flutter constraints even though they used Flutter.
- the non-Flutter `plugin_platform_interface` package hadn't been update since I hadn't thought about Dart-only constraints in the past.
  • Loading branch information
stuartmorgan committed Jan 26, 2023
1 parent e9406bc commit af065a6
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ task:
always:
format_script: ./script/tool_runner.sh format --fail-on-change
license_script: $PLUGIN_TOOL_COMMAND license-check
pubspec_script: ./script/tool_runner.sh pubspec-check
# The major and minor versions here should match the lowest version
# analyzed in legacy_version_analyze.
pubspec_script: ./script/tool_runner.sh pubspec-check --min-min-flutter-version=3.0.0 --min-min-dart-version=2.17.0
readme_script:
- ./script/tool_runner.sh readme-check
# Re-run with --require-excerpts, skipping packages that still need
Expand Down Expand Up @@ -171,6 +173,7 @@ task:
- name: legacy_version_analyze
depends_on: analyze
matrix:
# Change the arguments to pubspec-check when changing these values.
env:
CHANNEL: "3.0.5"
DART_VERSION: "2.17.6"
Expand Down
1 change: 1 addition & 0 deletions packages/file_selector/file_selector/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version: 1.0.0+1

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
file_selector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version: 1.0.0

environment:
sdk: ">=2.14.4 <3.0.0"
flutter: ">=3.0.0"

dependencies:
# The following adds the Cupertino Icons font to your application.
Expand All @@ -28,4 +29,4 @@ dev_dependencies:
sdk: flutter

flutter:
uses-material-design: true
uses-material-design: true
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version: 1.0.0+1

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
file_selector_linux:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_foundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* Updates minimum supported Flutter version to 3.0.

## 2.1.1

* Fixes a regression in the path retured by `getApplicationSupportDirectory` on iOS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version: 2.1.1

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=2.10.0"
flutter: ">=3.0.0"

flutter:
plugin:
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* Updates minimum supported Dart version.

## 2.1.3

* Minor fixes for new analysis options.
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+
version: 2.1.3

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"

dependencies:
meta: ^1.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ publish_to: none

environment:
sdk: ">=2.14.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ publish_to: none

environment:
sdk: ">=2.14.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=3.0.0"

dependencies:
flutter:
Expand Down
5 changes: 5 additions & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.13.4

* Adds the ability to validate minimum supported Dart/Flutter versions in
`pubspec-check`.

## 0.13.3

* Renames `podspecs` to `podspec-check`. The old name will continue to work.
Expand Down
76 changes: 75 additions & 1 deletion script/tool/lib/src/pubspec_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:file/file.dart';
import 'package:git/git.dart';
import 'package:platform/platform.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';

import 'common/core.dart';
Expand All @@ -29,7 +30,23 @@ class PubspecCheckCommand extends PackageLoopingCommand {
processRunner: processRunner,
platform: platform,
gitDir: gitDir,
);
) {
argParser.addOption(
_minMinDartVersionFlag,
help:
'The minimum Dart version to allow as the minimum SDK constraint.\n\n'
'This is only enforced for non-Flutter packages; Flutter packages '
'use --$_minMinFlutterVersionFlag',
);
argParser.addOption(
_minMinFlutterVersionFlag,
help:
'The minimum Flutter version to allow as the minimum SDK constraint.',
);
}

static const String _minMinDartVersionFlag = 'min-min-dart-version';
static const String _minMinFlutterVersionFlag = 'min-min-flutter-version';

// Section order for plugins. Because the 'flutter' section is critical
// information for plugins, and usually small, it goes near the top unlike in
Expand Down Expand Up @@ -100,6 +117,24 @@ class PubspecCheckCommand extends PackageLoopingCommand {
printError('$listIndentation${sectionOrder.join('\n$listIndentation')}');
}

final String minMinDartVersionString = getStringArg(_minMinDartVersionFlag);
final String minMinFlutterVersionString =
getStringArg(_minMinFlutterVersionFlag);
final String? minVersionError = _checkForMinimumVersionError(
pubspec,
package,
minMinDartVersion: minMinDartVersionString.isEmpty
? null
: Version.parse(minMinDartVersionString),
minMinFlutterVersion: minMinFlutterVersionString.isEmpty
? null
: Version.parse(minMinFlutterVersionString),
);
if (minVersionError != null) {
printError('$indentation$minVersionError');
passing = false;
}

if (isPlugin) {
final String? implementsError =
_checkForImplementsError(pubspec, package: package);
Expand Down Expand Up @@ -320,4 +355,43 @@ class PubspecCheckCommand extends PackageLoopingCommand {
final String suffix = packageName.substring(parentName.length);
return !nonImplementationSuffixes.contains(suffix);
}

/// Validates that a Flutter package has a minimum SDK version constraint of
/// at least [minMinFlutterVersion] (if provided), or that a non-Flutter
/// package has a minimum SDK version constraint of [minMinDartVersion]
/// (if provided).
///
/// Returns an error string if validation fails.
String? _checkForMinimumVersionError(
Pubspec pubspec,
RepositoryPackage package, {
Version? minMinDartVersion,
Version? minMinFlutterVersion,
}) {
final VersionConstraint? dartConstraint = pubspec.environment?['sdk'];
final VersionConstraint? flutterConstraint =
pubspec.environment?['flutter'];

if (flutterConstraint != null) {
// Validate Flutter packages against the Flutter requirement.
if (minMinFlutterVersion != null) {
final Version? constraintMin =
flutterConstraint is VersionRange ? flutterConstraint.min : null;
if ((constraintMin ?? Version(0, 0, 0)) < minMinFlutterVersion) {
return 'Minimum allowed Flutter version $constraintMin is less than $minMinFlutterVersion';
}
}
} else {
// Validate non-Flutter packages against the Dart requirement.
if (minMinDartVersion != null) {
final Version? constraintMin =
dartConstraint is VersionRange ? dartConstraint.min : null;
if ((constraintMin ?? Version(0, 0, 0)) < minMinDartVersion) {
return 'Minimum allowed Dart version $constraintMin is less than $minMinDartVersion';
}
}
}

return null;
}
}
2 changes: 1 addition & 1 deletion script/tool/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_plugin_tools
description: Productivity utils for flutter/plugins and flutter/packages
repository: https://github.com/flutter/plugins/tree/main/script/tool
version: 0.13.3
version: 0.13.4

dependencies:
args: ^2.1.0
Expand Down
Loading

0 comments on commit af065a6

Please sign in to comment.