Applied Gradle Plugins Declaratively for path_provider (#7822)

Updated applying gradle plugins from usage of imperative apply to usage
of declarative blocks {} apply. Intending on updating all android
example apps under packages. Did one more as a proof of concept before
doing more.

More information on Flutter Gradle Plugin Apply
[here](https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply)

Partially addresses
[#152656](https://github.com/flutter/flutter/issues/152656)
## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] page, which explains my
responsibilities.
- [x] I read and followed the [relevant style guides] and ran the
auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages
repo does use `dart format`.)
- [x] I signed the [CLA].
- [x] The title of the PR starts with the name of the package surrounded
by square brackets, e.g. `[shared_preferences]`
- [x] I [linked to at least one issue that this PR fixes] in the
description above.
- [x] I updated `pubspec.yaml` with an appropriate new version according
to the [pub versioning philosophy], or this PR is [exempt from version
changes].
- [x] I updated `CHANGELOG.md` to add a description of the change,
[following repository CHANGELOG style], or this PR is [exempt from
CHANGELOG changes].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]:
https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[CLA]: https://cla.developers.google.com/
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[pub versioning philosophy]: https://dart.dev/tools/pub/versioning
[exempt from version changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version
[following repository CHANGELOG style]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[exempt from CHANGELOG changes]:
https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog
[test-exempt]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests

---------

Co-authored-by: Reid Baker <reidbaker@google.com>
This commit is contained in:
jesswrd
2024-10-30 12:38:55 -07:00
committed by GitHub
parent bce9735f1c
commit 4feddffcfa
7 changed files with 225 additions and 61 deletions

View File

@ -174,6 +174,7 @@ ${warningsConfigured ? warningConfig : ''}
void writeFakeExampleTopLevelSettingsGradle(
RepositoryPackage package, {
bool includeArtifactHub = true,
bool includeArtifactDocumentation = true,
}) {
final File settingsGradle = package
.platformDirectory(FlutterPlatform.android)
@ -196,10 +197,58 @@ plugins.each { name, path ->
include ":\$name"
project(":\$name").projectDir = pluginDirectory
}
${includeArtifactDocumentation ? '// See ${GradleCheckCommand.artifactHubDocumentationString} for more info.' : ''}
${includeArtifactHub ? GradleCheckCommand.exampleRootSettingsArtifactHubString : ''}
''');
}
/// Writes a fake android/build.gradle file for an example [package] with the
/// given options.
void writeFakeExampleSettingsGradle(
RepositoryPackage package, {
bool includeArtifactHub = true,
bool includeArtifactDocumentation = true,
}) {
final File settingsGradle = package
.platformDirectory(FlutterPlatform.android)
.childFile('settings.gradle');
settingsGradle.createSync(recursive: true);
/// String printed as a valid example of settings.gradle repository
/// configuration without the artifact hub env variable.
/// GP stands for the gradle plugin method of flutter tooling inclusion.
const String exampleSettingsWithoutArtifactHubStringGP = '''
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
// ...other plugins
}
''';
settingsGradle.writeAsStringSync('''
pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}()
includeBuild("\$flutterSdkPath/packages/flutter_tools/gradle")
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
${includeArtifactDocumentation ? '// See ${GradleCheckCommand.artifactHubDocumentationString} for more info.' : ''}
${includeArtifactHub ? GradleCheckCommand.exampleSettingsArtifactHubStringGP : exampleSettingsWithoutArtifactHubStringGP}
include ":app"
''');
}
/// Writes a fake android/app/build.gradle file for an example [package] with
/// the given options.
void writeFakeExampleAppBuildGradle(
@ -258,6 +307,7 @@ dependencies {
String? kotlinVersion,
bool includeBuildArtifactHub = true,
bool includeSettingsArtifactHub = true,
bool includeSettingsDocumentationArtifactHub = true,
}) {
writeFakeExampleTopLevelBuildGradle(
package,
@ -271,6 +321,34 @@ dependencies {
writeFakeExampleTopLevelSettingsGradle(
package,
includeArtifactHub: includeSettingsArtifactHub,
includeArtifactDocumentation: includeSettingsDocumentationArtifactHub,
);
}
void writeFakeExampleBuildGradleGP(
RepositoryPackage package, {
required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool warningsConfigured = true,
String? kotlinVersion,
required bool includeBuildArtifactHub,
required bool includeSettingsArtifactHub,
required bool includeSettingsDocumentationArtifactHub,
}) {
writeFakeExampleTopLevelBuildGradle(
package,
pluginName: pluginName,
warningsConfigured: warningsConfigured,
kotlinVersion: kotlinVersion,
includeArtifactHub: includeBuildArtifactHub,
);
writeFakeExampleAppBuildGradle(package,
includeNamespace: includeNamespace, commentNamespace: commentNamespace);
writeFakeExampleSettingsGradle(
package,
includeArtifactHub: includeSettingsArtifactHub,
includeArtifactDocumentation: includeSettingsDocumentationArtifactHub,
);
}
@ -670,14 +748,14 @@ dependencies {
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradles(
example,
pluginName: packageName,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
// ignore: avoid_redundant_argument_values
includeSettingsArtifactHub: true,
);
writeFakeExampleBuildGradles(example,
pluginName: packageName,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
// ignore: avoid_redundant_argument_values
includeSettingsArtifactHub: true,
// ignore: avoid_redundant_argument_values
includeSettingsDocumentationArtifactHub: true);
writeFakeManifest(example, isApp: true);
final List<String> output =
@ -789,6 +867,71 @@ dependencies {
isNot(contains(GradleCheckCommand.exampleRootGradleArtifactHubString)),
);
});
test('prints error for declarative method of applying gradle plugins',
() async {
const String packageName = 'a_package';
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradleGP(example,
pluginName: packageName,
includeBuildArtifactHub: true,
includeSettingsArtifactHub: false,
includeSettingsDocumentationArtifactHub: true);
writeFakeManifest(example, isApp: true);
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['gradle-check'], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(GradleCheckCommand.exampleSettingsArtifactHubStringGP),
]),
);
expect(
output,
isNot(
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
);
});
test('error message is printed when documentation link is missing',
() async {
const String packageName = 'a_package';
final RepositoryPackage package =
createFakePackage('a_package', packagesDir);
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradleGP(example,
pluginName: packageName,
includeBuildArtifactHub: true,
includeSettingsArtifactHub: true,
includeSettingsDocumentationArtifactHub: false);
writeFakeManifest(example, isApp: true);
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['gradle-check'], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(GradleCheckCommand.artifactHubDocumentationString),
]),
);
});
});
group('Kotlin version check', () {