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

[tool] Add Android dependency (gradle) option to update dependencies command #4757

Merged
merged 41 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dec3d69
Merge remote-tracking branch 'upstream/main' into camx_occ
camsim99 May 1, 2023
0e0333b
Merge remote-tracking branch 'upstream/main'
camsim99 May 2, 2023
bd7ac99
Merge remote-tracking branch 'upstream/main'
camsim99 May 3, 2023
5c3363b
Merge remote-tracking branch 'upstream/main'
camsim99 May 10, 2023
fed9621
Undo changes
camsim99 May 10, 2023
5aabe34
Merge remote-tracking branch 'upstream/main'
camsim99 May 12, 2023
2b9a352
Merge remote-tracking branch 'upstream/main'
camsim99 May 25, 2023
a1173da
Merge remote-tracking branch 'upstream/main'
camsim99 May 30, 2023
cbc3d6b
Merge remote-tracking branch 'upstream/main'
camsim99 May 30, 2023
cae5a4c
Merge remote-tracking branch 'upstream/main'
camsim99 Jun 1, 2023
72283db
Merge remote-tracking branch 'upstream/main'
camsim99 Jun 5, 2023
166a77c
Merge remote-tracking branch 'upstream/main'
camsim99 Jun 5, 2023
399780e
Merge remote-tracking branch 'upstream/main'
camsim99 Jun 14, 2023
8d5d0e7
Merge remote-tracking branch 'upstream/main'
camsim99 Jun 26, 2023
084d960
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 12, 2023
d2a59ac
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 17, 2023
a1422bf
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 17, 2023
bdd87a6
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 18, 2023
137a28b
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 19, 2023
bc0db5a
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 20, 2023
d04b466
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 21, 2023
a9cfe87
Merge remote-tracking branch 'upstream/main'
camsim99 Jul 24, 2023
a32def1
Merge remote-tracking branch 'upstream/main'
camsim99 Aug 2, 2023
4785148
Merge remote-tracking branch 'upstream/main'
camsim99 Aug 14, 2023
7a8fc69
Merge remote-tracking branch 'upstream/main'
camsim99 Aug 15, 2023
b02e15f
Merge remote-tracking branch 'upstream/main'
camsim99 Aug 15, 2023
c6e5868
Merge remote-tracking branch 'upstream/main'
camsim99 Aug 17, 2023
1a1859d
Add command for updating Gradle
camsim99 Aug 22, 2023
423b293
Modify regex
camsim99 Aug 22, 2023
0c62587
Add tests
camsim99 Aug 24, 2023
39fb01c
Merge remote-tracking branch 'upstream/main' into gradle_8
camsim99 Aug 24, 2023
7f61cbe
Start self review
camsim99 Aug 24, 2023
e110e35
Self review
camsim99 Aug 29, 2023
414a160
Add missing test
camsim99 Aug 29, 2023
668f3c1
Add fix for rewritten check
camsim99 Aug 29, 2023
98704e6
Merge remote-tracking branch 'upstream/main' into gradle_8
camsim99 Aug 29, 2023
4aae8a7
Start addressing review
camsim99 Aug 31, 2023
e5ef8d1
Address review
camsim99 Sep 1, 2023
121b7e6
Nits
camsim99 Sep 7, 2023
0664e93
Merge remote-tracking branch 'upstream/main' into gradle_8
camsim99 Sep 7, 2023
bde7428
Test fix
camsim99 Sep 7, 2023
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
113 changes: 110 additions & 3 deletions script/tool/lib/src/update_dependency_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'common/repository_package.dart';

const int _exitIncorrectTargetDependency = 3;
const int _exitNoTargetVersion = 4;
const int _exitInvalidTargetVersion = 5;

/// A command to update a dependency in packages.
///
Expand All @@ -38,23 +39,34 @@ class UpdateDependencyCommand extends PackageLoopingCommand {
_pubPackageFlag,
help: 'A pub package to update.',
);
argParser.addOption(_androidDependency,
help: 'An Android dependency to update.',
allowed: <String>[
'gradle',
],
allowedHelp: <String, String>{
'gradle': 'Updates Gradle version used in plugin example apps.',
});
argParser.addOption(
_versionFlag,
help: 'The version to update to.\n\n'
'- For pub, defaults to the latest published version if not '
'provided. This can be any constraint that pubspec.yaml allows; a '
'specific version will be treated as the exact version for '
'dependencies that are alread pinned, or a ^ range for those that '
'are unpinned.',
'are unpinned.\n'
'- For Android dependencies, a version must be provided.',
);
}

static const String _pubPackageFlag = 'pub-package';
static const String _androidDependency = 'android-dependency';
static const String _versionFlag = 'version';

final PubVersionFinder _pubVersionFinder;

late final String? _targetPubPackage;
late final String? _targetAndroidDependency;
late final String _targetVersion;

@override
Expand All @@ -72,14 +84,19 @@ class UpdateDependencyCommand extends PackageLoopingCommand {

@override
Future<void> initializeRun() async {
const Set<String> targetFlags = <String>{_pubPackageFlag};
const Set<String> targetFlags = <String>{
_pubPackageFlag,
_androidDependency
};
final Set<String> passedTargetFlags =
targetFlags.where((String flag) => argResults![flag] != null).toSet();
if (passedTargetFlags.length != 1) {
printError(
'Exactly one of the target flags must be provided: (${targetFlags.join(', ')})');
throw ToolExit(_exitIncorrectTargetDependency);
}

// Setup for updating pub dependency.
_targetPubPackage = getNullableStringArg(_pubPackageFlag);
if (_targetPubPackage != null) {
final String? version = getNullableStringArg(_versionFlag);
Expand All @@ -102,6 +119,33 @@ ${response.httpResponse.body}
}
} else {
_targetVersion = version;
return;
}
}

// Setup for updating Android dependency.
_targetAndroidDependency = getNullableStringArg(_androidDependency);
if (_targetAndroidDependency != null) {
final String? version = getNullableStringArg(_versionFlag);
if (version == null) {
printError('A version must be provided to update this dependency.');
throw ToolExit(_exitNoTargetVersion);
} else if (_targetAndroidDependency == 'gradle') {
final RegExp validGradleVersionPattern = RegExp(r'^\d+(?:\.\d+){1,2}$');
final bool isValidGradleVersion =
validGradleVersionPattern.stringMatch(version) == version;
stuartmorgan marked this conversation as resolved.
Show resolved Hide resolved
if (!isValidGradleVersion) {
printError(
'A version with a valid format (maximum 2-3 numbers separated by period) must be provided.');
throw ToolExit(_exitInvalidTargetVersion);
}
_targetVersion = version;
return;
} else {
// TODO(camsim99): Add other supported Android dependencies like the Android SDK and AGP.
printError(
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
'Target Android dependency $_targetAndroidDependency is unrecognized.');
throw ToolExit(_exitIncorrectTargetDependency);
}
}
}
Expand All @@ -116,7 +160,11 @@ ${response.httpResponse.body}
if (_targetPubPackage != null) {
return _runForPubDependency(package, _targetPubPackage!);
}
// TODO(stuartmorgan): Add othe dependency types here (e.g., maven).
if (_targetAndroidDependency != null) {
return _runForAndroidDependency(package);
}

// TODO(stuartmorgan): Add other dependency types here (e.g., maven).

return PackageResult.fail();
}
Expand Down Expand Up @@ -181,6 +229,65 @@ ${response.httpResponse.body}
return PackageResult.success();
}

/// Handles all of the updates for [package] when the target dependency is
/// an Android dependency.
Future<PackageResult> _runForAndroidDependency(
RepositoryPackage package) async {
if (_targetAndroidDependency == 'gradle') {
final Iterable<RepositoryPackage> packageExamples = package.getExamples();
bool updateRanForExamples = false;
for (final RepositoryPackage example in packageExamples) {
if (!example.platformDirectory(FlutterPlatform.android).existsSync()) {
continue;
}

updateRanForExamples = true;
Directory gradleWrapperPropertiesDirectory =
example.platformDirectory(FlutterPlatform.android);
if (gradleWrapperPropertiesDirectory
.childDirectory('app')
.childDirectory('gradle')
.existsSync()) {
gradleWrapperPropertiesDirectory =
gradleWrapperPropertiesDirectory.childDirectory('app');
}
final File gradleWrapperPropertiesFile =
gradleWrapperPropertiesDirectory
.childDirectory('gradle')
.childDirectory('wrapper')
.childFile('gradle-wrapper.properties');

final String gradleWrapperPropertiesContents =
gradleWrapperPropertiesFile.readAsStringSync();
final RegExp validGradleDistributionUrl =
RegExp(r'^\s*distributionUrl\s*=\s*.*\.zip', multiLine: true);
if (!validGradleDistributionUrl
.hasMatch(gradleWrapperPropertiesContents)) {
return PackageResult.fail(<String>[
'Unable to find a "distributionUrl" entry to update for ${package.displayName}.'
]);
}

print(
'${indentation}Updating ${getRelativePosixPath(example.directory, from: package.directory)} to "$_targetVersion"');
final String newGradleWrapperPropertiesContents =
gradleWrapperPropertiesContents.replaceFirst(
validGradleDistributionUrl,
'distributionUrl=https\\://services.gradle.org/distributions/gradle-$_targetVersion-all.zip');
// TODO(camsim99): Validate current AGP version against target Gradle
// version: https://github.com/flutter/flutter/issues/133887.
gradleWrapperPropertiesFile
.writeAsStringSync(newGradleWrapperPropertiesContents);
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
}
return updateRanForExamples
? PackageResult.success()
: PackageResult.skip('No example apps run on Android.');
}
return PackageResult.fail(<String>[
'Target Android dependency $_androidDependency is unrecognized.'
]);
}

/// Returns information about the current dependency of [package] on
/// the package named [dependencyName], or null if there is no dependency.
_PubDependencyInfo? _getPubDependencyInfo(
Expand Down
Loading