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 32 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
107 changes: 104 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,32 @@ class UpdateDependencyCommand extends PackageLoopingCommand {
_pubPackageFlag,
help: 'A pub package to update.',
);
argParser.addOption(
_androidDependency,
help: 'An Android dependency to update.\n\n'
'Supported dependencies:\n'
'- "gradle", to update Gradle version used in plugin example apps',
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
);
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 +82,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 +117,32 @@ ${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 {
printError(
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
'Target Android dependency $_targetAndroidDependency is unrecognized.');
throw ToolExit(_exitIncorrectTargetDependency);
}
}
}
Expand All @@ -116,7 +157,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 +226,62 @@ ${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();
for (final RepositoryPackage example in packageExamples) {
if (!example.directory.childDirectory('android').existsSync()) {
return PackageResult.skip('Example app does not run on Android.');
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
}
File gradleWrapperPropertiesFile;
if (example.directory
.childDirectory('android')
.childDirectory('app')
.childDirectory('gradle')
.existsSync()) {
gradleWrapperPropertiesFile = example.directory
.childDirectory('android')
.childDirectory('app')
.childDirectory('gradle')
.childDirectory('wrapper')
.childFile('gradle-wrapper.properties');
} else {
gradleWrapperPropertiesFile = example.directory
.childDirectory('android')
.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>[
'Failed to update Gradle version in the example app for ${package.displayName}'
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
]);
}

final String newGradleWrapperPropertiesContents =
gradleWrapperPropertiesContents.replaceFirst(
validGradleDistributionUrl,
// ignore: unnecessary_string_escapes
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
'distributionUrl=https\://services.gradle.org/distributions/gradle-$_targetVersion-all.zip');
gradleWrapperPropertiesFile
.writeAsStringSync(newGradleWrapperPropertiesContents);
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
}
return PackageResult.success();
}
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
199 changes: 199 additions & 0 deletions script/tool/test/update_dependency_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Ignores escape characters used in testing gradle-wrapper.properties file
// formats.
// ignore_for_file: unnecessary_string_escapes

import 'dart:convert';

import 'package:args/command_runner.dart';
Expand Down Expand Up @@ -79,6 +83,9 @@ dev_dependencies:
);
});

// TODO(camsim99): Add test case
test('throws if multiple dependency specified', () async {});

group('pub dependencies', () {
test('throws if no version is given for an unpublished target', () async {
mockHttpResponse = (http.Request request) async {
Expand Down Expand Up @@ -584,4 +591,196 @@ dev_dependencies:
);
});
});

group('Android dependencies', () {
test('throws if Android dependency unrecognized', () async {
const String testDependency = 'testDependency';
Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'update-dependency',
'--android-dependency',
testDependency,
'--version',
'8.3.1',
], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(
'Target Android dependency $testDependency is unrecognized.'),
]),
);
});

test(
'throws if Android dependency is "gradle" but version format is invalid',
() async {
Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'update-dependency',
'--android-dependency',
'gradle',
'--version',
'83',
], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(
'A version with a valid format (maximum 2-3 numbers separated by period) must be provided.'),
]),
);
});

test('skips if example app does not run on Android', () async {
final RepositoryPackage package =
createFakePlugin('fake_plugin', packagesDir);

final List<String> output = await runCapturingPrint(runner, <String>[
'update-dependency',
'--packages',
package.displayName,
'--android-dependency',
'gradle',
'--version',
'8.8.8',
]);

expect(
output,
containsAllInOrder(<Matcher>[
contains('SKIPPING: Example app does not run on Android.'),
]),
);
});

test(
'throws if wrapper does not have distribution URL with expected format',
() async {
final RepositoryPackage package = createFakePlugin(
'fake_plugin', packagesDir, extraFiles: <String>[
'example/android/app/gradle/wrapper/gradle-wrapper.properties'
]);

final File gradleWrapperPropertiesFile = package.directory
.childDirectory('example')
.childDirectory('android')
.childDirectory('app')
.childDirectory('gradle')
.childDirectory('wrapper')
.childFile('gradle-wrapper.properties');

gradleWrapperPropertiesFile.writeAsStringSync('''
How is it even possible that I didn't specify a Gradle distribution?
''');

Error? commandError;
final List<String> output = await runCapturingPrint(runner, <String>[
'update-dependency',
'--packages',
package.displayName,
'--android-dependency',
'gradle',
'--version',
'8.8.8',
], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(
'Failed to update Gradle version in the example app for ${package.displayName}'),
]),
);
});

test('succeeds if example app has android/app directory', () async {
final RepositoryPackage package = createFakePlugin(
'fake_plugin', packagesDir, extraFiles: <String>[
'example/android/app/gradle/wrapper/gradle-wrapper.properties'
]);
const String newGradleVersion = '8.8.8';

final File gradleWrapperPropertiesFile = package.directory
.childDirectory('example')
.childDirectory('android')
.childDirectory('app')
.childDirectory('gradle')
.childDirectory('wrapper')
.childFile('gradle-wrapper.properties');

gradleWrapperPropertiesFile.writeAsStringSync('''
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
''');

await runCapturingPrint(runner, <String>[
'update-dependency',
'--packages',
package.displayName,
'--android-dependency',
'gradle',
'--version',
newGradleVersion,
]);

final String updatedGradleWrapperPropertiesContents =
gradleWrapperPropertiesFile.readAsStringSync();
expect(updatedGradleWrapperPropertiesContents.contains(newGradleVersion),
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
isTrue);
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
});

test('succeeds if example app does not have android/gradle directory',
() async {
final RepositoryPackage package = createFakePlugin(
'fake_plugin', packagesDir, extraFiles: <String>[
'example/android/gradle/wrapper/gradle-wrapper.properties'
]);
const String newGradleVersion = '9.9';

final File gradleWrapperPropertiesFile = package.directory
.childDirectory('example')
.childDirectory('android')
.childDirectory('gradle')
.childDirectory('wrapper')
.childFile('gradle-wrapper.properties');

gradleWrapperPropertiesFile.writeAsStringSync('''
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
''');

await runCapturingPrint(runner, <String>[
'update-dependency',
'--packages',
package.displayName,
'--android-dependency',
'gradle',
'--version',
newGradleVersion,
]);

final String updatedGradleWrapperPropertiesContents =
gradleWrapperPropertiesFile.readAsStringSync();
expect(updatedGradleWrapperPropertiesContents.contains(newGradleVersion),
isTrue);
});
});
}