Skip to content

Commit 9452472

Browse files
authored
Update Gradle Command Test to Only Accept Gradle Declarative Apply (#8325)
Currently the tests accept both the old gradle imperative apply and the gradle declarative apply. Since all plugins have been migrated to the gradle declarative apply, we want all exmple app plugins to reflect that and only accept gradle declarative apply. Fixes flutter/flutter#152656 Fixes flutter/flutter#157660
1 parent 864e6be commit 9452472

File tree

5 files changed

+46
-154
lines changed

5 files changed

+46
-154
lines changed

.ci/legacy_project/all_packages/android/app/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
plugins {
2+
id "com.android.application"
3+
id "org.jetbrains.kotlin.android"
4+
id "dev.flutter.flutter-gradle-plugin"
5+
}
6+
17
def localProperties = new Properties()
28
def localPropertiesFile = rootProject.file('local.properties')
39
if (localPropertiesFile.exists()) {
@@ -6,11 +12,6 @@ if (localPropertiesFile.exists()) {
612
}
713
}
814

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
13-
1415
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1516
if (flutterVersionCode == null) {
1617
flutterVersionCode = '1'
@@ -21,9 +22,6 @@ if (flutterVersionName == null) {
2122
flutterVersionName = '1.0'
2223
}
2324

24-
apply plugin: 'com.android.application'
25-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
26-
2725
android {
2826
namespace = "com.example.all_packages"
2927
compileSdkVersion 33

.ci/legacy_project/all_packages/android/build.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
buildscript {
2-
repositories {
3-
google()
4-
mavenCentral()
5-
}
6-
7-
dependencies {
8-
classpath 'com.android.tools.build:gradle:7.0.0'
9-
}
10-
}
11-
121
allprojects {
132
repositories {
143
google()
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
4-
def properties = new Properties()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
511

6-
assert localPropertiesFile.exists()
7-
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
17+
}
818

9-
def flutterSdkPath = properties.getProperty("flutter.sdk")
10-
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
11-
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
19+
// See https://github.com/flutter/flutter/blob/master/docs/ecosystem/Plugins-and-Packages-repository-structure.md#gradle-structure for more info.
20+
plugins {
21+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
22+
id "com.android.application" version "7.0.0" apply false
23+
id "org.jetbrains.kotlin.android" version "1.9.0" apply false
24+
}
25+
26+
include ":app"

script/tool/lib/src/gradle_check_command.dart

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,10 @@ class GradleCheckCommand extends PackageLoopingCommand {
198198
return succeeded;
199199
}
200200

201-
/// String printed as example of valid example root settings.gradle repository
202-
/// configuration that enables artifact hub env variable.
203-
@visibleForTesting
204-
static String exampleRootSettingsArtifactHubString = '''
205-
buildscript {
206-
repositories {
207-
maven {
208-
url "https://plugins.gradle.org/m2/"
209-
}
210-
}
211-
dependencies {
212-
classpath "gradle.plugin.com.google.cloud.artifactregistry:artifactregistry-gradle-plugin:2.2.1"
213-
}
214-
}
215-
apply plugin: "com.google.cloud.artifactregistry.gradle-plugin"
216-
''';
217-
218201
/// String printed as a valid example of settings.gradle repository
219202
/// configuration that enables artifact hub env variable.
220-
/// GP stands for the gradle plugin method of flutter tooling inclusion.
221203
@visibleForTesting
222-
static String exampleSettingsArtifactHubStringGP = '''
204+
static String exampleSettingsArtifactHubString = '''
223205
plugins {
224206
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
225207
// ...other plugins
@@ -235,32 +217,15 @@ plugins {
235217
RepositoryPackage example, List<String> gradleLines) {
236218
final RegExp documentationPresentRegex = RegExp(
237219
r'github\.com.*flutter.*blob.*Plugins-and-Packages-repository-structure.*gradle-structure');
238-
final RegExp artifactRegistryDefinitionRegex = RegExp(
239-
r'classpath.*gradle\.plugin\.com\.google\.cloud\.artifactregistry:artifactregistry-gradle-plugin');
240220
final RegExp artifactRegistryPluginApplyRegex = RegExp(
241-
r'apply.*plugin.*com\.google\.cloud\.artifactregistry\.gradle-plugin');
242-
final RegExp artifactRegistryPluginApplyRegexGP = RegExp(
243221
r'id.*com\.google\.cloud\.artifactregistry\.gradle-plugin.*version.*\b\d+\.\d+\.\d+\b');
244-
final RegExp artifactRegistryPluginApplyDeclarativeRegex =
245-
RegExp(r'\bpluginManagement\b');
246222

247223
final bool documentationPresent = gradleLines
248224
.any((String line) => documentationPresentRegex.hasMatch(line));
249-
final bool artifactRegistryDefined = gradleLines
250-
.any((String line) => artifactRegistryDefinitionRegex.hasMatch(line));
251-
final bool artifactRegistryPluginApplied = gradleLines
225+
final bool declarativeArtifactRegistryApplied = gradleLines
252226
.any((String line) => artifactRegistryPluginApplyRegex.hasMatch(line));
253-
final bool declarativeArtifactRegistryApplied = gradleLines.any(
254-
(String line) => artifactRegistryPluginApplyRegexGP.hasMatch(line));
255-
final bool declarativePluginBlockApplied = gradleLines.any((String line) =>
256-
artifactRegistryPluginApplyDeclarativeRegex.hasMatch(line));
257-
258-
final bool imperativeArtifactRegistryApplied =
259-
artifactRegistryDefined && artifactRegistryPluginApplied;
260-
261-
final bool validArtifactConfiguration = documentationPresent &&
262-
(imperativeArtifactRegistryApplied ||
263-
declarativeArtifactRegistryApplied);
227+
final bool validArtifactConfiguration =
228+
documentationPresent && declarativeArtifactRegistryApplied;
264229

265230
if (!validArtifactConfiguration) {
266231
printError('Failed Artifact Hub validation.');
@@ -269,14 +234,9 @@ plugins {
269234
'The link to the Artifact Hub documentation is missing. Include the following in '
270235
'example root settings.gradle:\n// See $artifactHubDocumentationString for more info.');
271236
}
272-
if (artifactRegistryDefined ||
273-
artifactRegistryPluginApplied ||
274-
!declarativePluginBlockApplied) {
275-
printError('Include the following in '
276-
'example root settings.gradle:\n$exampleRootSettingsArtifactHubString');
277-
} else if (!declarativeArtifactRegistryApplied) {
237+
if (!declarativeArtifactRegistryApplied) {
278238
printError('Include the following in '
279-
'example root settings.gradle:\n$exampleSettingsArtifactHubStringGP');
239+
'example root settings.gradle:\n$exampleSettingsArtifactHubString');
280240
}
281241
}
282242
return validArtifactConfiguration;

script/tool/test/gradle_check_command_test.dart

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -169,39 +169,6 @@ ${warningsConfigured ? warningConfig : ''}
169169
''');
170170
}
171171

172-
/// Writes a fake android/build.gradle file for an example [package] with the
173-
/// given options.
174-
void writeFakeExampleTopLevelSettingsGradle(
175-
RepositoryPackage package, {
176-
bool includeArtifactHub = true,
177-
bool includeArtifactDocumentation = true,
178-
}) {
179-
final File settingsGradle = package
180-
.platformDirectory(FlutterPlatform.android)
181-
.childFile('settings.gradle');
182-
settingsGradle.createSync(recursive: true);
183-
184-
settingsGradle.writeAsStringSync('''
185-
include ':app'
186-
187-
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
188-
189-
def plugins = new Properties()
190-
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
191-
if (pluginsFile.exists()) {
192-
pluginsFile.withInputStream { stream -> plugins.load(stream) }
193-
}
194-
195-
plugins.each { name, path ->
196-
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
197-
include ":\$name"
198-
project(":\$name").projectDir = pluginDirectory
199-
}
200-
${includeArtifactDocumentation ? '// See ${GradleCheckCommand.artifactHubDocumentationString} for more info.' : ''}
201-
${includeArtifactHub ? GradleCheckCommand.exampleRootSettingsArtifactHubString : ''}
202-
''');
203-
}
204-
205172
/// Writes a fake android/build.gradle file for an example [package] with the
206173
/// given options.
207174
void writeFakeExampleSettingsGradle(
@@ -216,8 +183,7 @@ ${includeArtifactHub ? GradleCheckCommand.exampleRootSettingsArtifactHubString :
216183

217184
/// String printed as a valid example of settings.gradle repository
218185
/// configuration without the artifact hub env variable.
219-
/// GP stands for the gradle plugin method of flutter tooling inclusion.
220-
const String exampleSettingsWithoutArtifactHubStringGP = '''
186+
const String exampleSettingsWithoutArtifactHubString = '''
221187
plugins {
222188
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
223189
// ...other plugins
@@ -244,7 +210,7 @@ pluginManagement {
244210
}
245211
246212
${includeArtifactDocumentation ? '// See ${GradleCheckCommand.artifactHubDocumentationString} for more info.' : ''}
247-
${includeArtifactHub ? GradleCheckCommand.exampleSettingsArtifactHubStringGP : exampleSettingsWithoutArtifactHubStringGP}
213+
${includeArtifactHub ? GradleCheckCommand.exampleSettingsArtifactHubString : exampleSettingsWithoutArtifactHubString}
248214
include ":app"
249215
''');
250216
}
@@ -310,36 +276,6 @@ dependencies {
310276
bool includeBuildArtifactHub = true,
311277
bool includeSettingsArtifactHub = true,
312278
bool includeSettingsDocumentationArtifactHub = true,
313-
}) {
314-
writeFakeExampleTopLevelBuildGradle(
315-
package,
316-
pluginName: pluginName,
317-
warningsConfigured: warningsConfigured,
318-
kotlinVersion: kotlinVersion,
319-
includeArtifactHub: includeBuildArtifactHub,
320-
);
321-
writeFakeExampleAppBuildGradle(package,
322-
includeNamespace: includeNamespace,
323-
commentNamespace: commentNamespace,
324-
includeNameSpaceAsDeclaration: includeNameSpaceAsDeclaration);
325-
writeFakeExampleTopLevelSettingsGradle(
326-
package,
327-
includeArtifactHub: includeSettingsArtifactHub,
328-
includeArtifactDocumentation: includeSettingsDocumentationArtifactHub,
329-
);
330-
}
331-
332-
void writeFakeExampleBuildGradleGP(
333-
RepositoryPackage package, {
334-
required String pluginName,
335-
bool includeNamespace = true,
336-
bool commentNamespace = false,
337-
bool includeNameSpaceAsDeclaration = false,
338-
bool warningsConfigured = true,
339-
String? kotlinVersion,
340-
required bool includeBuildArtifactHub,
341-
required bool includeSettingsArtifactHub,
342-
required bool includeSettingsDocumentationArtifactHub,
343279
}) {
344280
writeFakeExampleTopLevelBuildGradle(
345281
package,
@@ -827,7 +763,7 @@ dependencies {
827763
output,
828764
containsAllInOrder(<Matcher>[
829765
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
830-
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
766+
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
831767
]),
832768
);
833769
});
@@ -859,11 +795,6 @@ dependencies {
859795
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
860796
]),
861797
);
862-
expect(
863-
output,
864-
isNot(
865-
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
866-
);
867798
});
868799

869800
test('fails settings.gradle artifact hub check when missing', () async {
@@ -890,7 +821,7 @@ dependencies {
890821
expect(
891822
output,
892823
containsAllInOrder(<Matcher>[
893-
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
824+
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
894825
]),
895826
);
896827
expect(
@@ -907,10 +838,12 @@ dependencies {
907838
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
908839
writeFakeManifest(package);
909840
final RepositoryPackage example = package.getExamples().first;
910-
writeFakeExampleBuildGradleGP(example,
841+
writeFakeExampleBuildGradles(example,
911842
pluginName: packageName,
843+
// ignore: avoid_redundant_argument_values
912844
includeBuildArtifactHub: true,
913845
includeSettingsArtifactHub: false,
846+
// ignore: avoid_redundant_argument_values
914847
includeSettingsDocumentationArtifactHub: true);
915848
writeFakeManifest(example, isApp: true);
916849

@@ -924,14 +857,9 @@ dependencies {
924857
expect(
925858
output,
926859
containsAllInOrder(<Matcher>[
927-
contains(GradleCheckCommand.exampleSettingsArtifactHubStringGP),
860+
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
928861
]),
929862
);
930-
expect(
931-
output,
932-
isNot(
933-
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
934-
);
935863
});
936864

937865
test('error message is printed when documentation link is missing',
@@ -942,9 +870,11 @@ dependencies {
942870
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
943871
writeFakeManifest(package);
944872
final RepositoryPackage example = package.getExamples().first;
945-
writeFakeExampleBuildGradleGP(example,
873+
writeFakeExampleBuildGradles(example,
946874
pluginName: packageName,
875+
// ignore: avoid_redundant_argument_values
947876
includeBuildArtifactHub: true,
877+
// ignore: avoid_redundant_argument_values
948878
includeSettingsArtifactHub: true,
949879
includeSettingsDocumentationArtifactHub: false);
950880
writeFakeManifest(example, isApp: true);

0 commit comments

Comments
 (0)