Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
jesswrd committed Dec 18, 2024
1 parent 3515aba commit 46428e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 127 deletions.
52 changes: 6 additions & 46 deletions script/tool/lib/src/gradle_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,10 @@ class GradleCheckCommand extends PackageLoopingCommand {
return succeeded;
}

/// String printed as example of valid example root settings.gradle repository
/// configuration that enables artifact hub env variable.
@visibleForTesting
static String exampleRootSettingsArtifactHubString = '''
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.google.cloud.artifactregistry:artifactregistry-gradle-plugin:2.2.1"
}
}
apply plugin: "com.google.cloud.artifactregistry.gradle-plugin"
''';

/// String printed as a valid example of settings.gradle repository
/// configuration that enables artifact hub env variable.
/// GP stands for the gradle plugin method of flutter tooling inclusion.
@visibleForTesting
static String exampleSettingsArtifactHubStringGP = '''
static String exampleSettingsArtifactHubString = '''
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
// ...other plugins
Expand All @@ -235,32 +217,15 @@ plugins {
RepositoryPackage example, List<String> gradleLines) {
final RegExp documentationPresentRegex = RegExp(
r'github\.com.*flutter.*blob.*Plugins-and-Packages-repository-structure.*gradle-structure');
final RegExp artifactRegistryDefinitionRegex = RegExp(
r'classpath.*gradle\.plugin\.com\.google\.cloud\.artifactregistry:artifactregistry-gradle-plugin');
final RegExp artifactRegistryPluginApplyRegex = RegExp(
r'apply.*plugin.*com\.google\.cloud\.artifactregistry\.gradle-plugin');
final RegExp artifactRegistryPluginApplyRegexGP = RegExp(
r'id.*com\.google\.cloud\.artifactregistry\.gradle-plugin.*version.*\b\d+\.\d+\.\d+\b');
final RegExp artifactRegistryPluginApplyDeclarativeRegex =
RegExp(r'\bpluginManagement\b');

final bool documentationPresent = gradleLines
.any((String line) => documentationPresentRegex.hasMatch(line));
final bool artifactRegistryDefined = gradleLines
.any((String line) => artifactRegistryDefinitionRegex.hasMatch(line));
final bool artifactRegistryPluginApplied = gradleLines
final bool declarativeArtifactRegistryApplied = gradleLines
.any((String line) => artifactRegistryPluginApplyRegex.hasMatch(line));
final bool declarativeArtifactRegistryApplied = gradleLines.any(
(String line) => artifactRegistryPluginApplyRegexGP.hasMatch(line));
final bool declarativePluginBlockApplied = gradleLines.any((String line) =>
artifactRegistryPluginApplyDeclarativeRegex.hasMatch(line));

final bool imperativeArtifactRegistryApplied =
artifactRegistryDefined && artifactRegistryPluginApplied;

final bool validArtifactConfiguration = documentationPresent &&
(imperativeArtifactRegistryApplied ||
declarativeArtifactRegistryApplied);
final bool validArtifactConfiguration =
documentationPresent && declarativeArtifactRegistryApplied;

if (!validArtifactConfiguration) {
printError('Failed Artifact Hub validation.');
Expand All @@ -269,14 +234,9 @@ plugins {
'The link to the Artifact Hub documentation is missing. Include the following in '
'example root settings.gradle:\n// See $artifactHubDocumentationString for more info.');
}
if (artifactRegistryDefined ||
artifactRegistryPluginApplied ||
!declarativePluginBlockApplied) {
printError('Include the following in '
'example root settings.gradle:\n$exampleRootSettingsArtifactHubString');
} else if (!declarativeArtifactRegistryApplied) {
if (!declarativeArtifactRegistryApplied) {
printError('Include the following in '
'example root settings.gradle:\n$exampleSettingsArtifactHubStringGP');
'example root settings.gradle:\n$exampleSettingsArtifactHubString');
}
}
return validArtifactConfiguration;
Expand Down
92 changes: 11 additions & 81 deletions script/tool/test/gradle_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,6 @@ ${warningsConfigured ? warningConfig : ''}
''');
}

/// Writes a fake android/build.gradle file for an example [package] with the
/// given options.
void writeFakeExampleTopLevelSettingsGradle(
RepositoryPackage package, {
bool includeArtifactHub = true,
bool includeArtifactDocumentation = true,
}) {
final File settingsGradle = package
.platformDirectory(FlutterPlatform.android)
.childFile('settings.gradle');
settingsGradle.createSync(recursive: true);

settingsGradle.writeAsStringSync('''
include ':app'
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withInputStream { stream -> plugins.load(stream) }
}
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
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(
Expand All @@ -216,8 +183,7 @@ ${includeArtifactHub ? GradleCheckCommand.exampleRootSettingsArtifactHubString :

/// 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 = '''
const String exampleSettingsWithoutArtifactHubString = '''
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
// ...other plugins
Expand All @@ -244,7 +210,7 @@ pluginManagement {
}
${includeArtifactDocumentation ? '// See ${GradleCheckCommand.artifactHubDocumentationString} for more info.' : ''}
${includeArtifactHub ? GradleCheckCommand.exampleSettingsArtifactHubStringGP : exampleSettingsWithoutArtifactHubStringGP}
${includeArtifactHub ? GradleCheckCommand.exampleSettingsArtifactHubString : exampleSettingsWithoutArtifactHubString}
include ":app"
''');
}
Expand Down Expand Up @@ -310,36 +276,6 @@ dependencies {
bool includeBuildArtifactHub = true,
bool includeSettingsArtifactHub = true,
bool includeSettingsDocumentationArtifactHub = true,
}) {
writeFakeExampleTopLevelBuildGradle(
package,
pluginName: pluginName,
warningsConfigured: warningsConfigured,
kotlinVersion: kotlinVersion,
includeArtifactHub: includeBuildArtifactHub,
);
writeFakeExampleAppBuildGradle(package,
includeNamespace: includeNamespace,
commentNamespace: commentNamespace,
includeNameSpaceAsDeclaration: includeNameSpaceAsDeclaration);
writeFakeExampleTopLevelSettingsGradle(
package,
includeArtifactHub: includeSettingsArtifactHub,
includeArtifactDocumentation: includeSettingsDocumentationArtifactHub,
);
}

void writeFakeExampleBuildGradleGP(
RepositoryPackage package, {
required String pluginName,
bool includeNamespace = true,
bool commentNamespace = false,
bool includeNameSpaceAsDeclaration = false,
bool warningsConfigured = true,
String? kotlinVersion,
required bool includeBuildArtifactHub,
required bool includeSettingsArtifactHub,
required bool includeSettingsDocumentationArtifactHub,
}) {
writeFakeExampleTopLevelBuildGradle(
package,
Expand Down Expand Up @@ -827,7 +763,7 @@ dependencies {
output,
containsAllInOrder(<Matcher>[
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
]),
);
});
Expand Down Expand Up @@ -859,11 +795,6 @@ dependencies {
contains(GradleCheckCommand.exampleRootGradleArtifactHubString),
]),
);
expect(
output,
isNot(
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
);
});

test('fails settings.gradle artifact hub check when missing', () async {
Expand All @@ -890,7 +821,7 @@ dependencies {
expect(
output,
containsAllInOrder(<Matcher>[
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString),
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
]),
);
expect(
Expand All @@ -907,10 +838,12 @@ dependencies {
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradleGP(example,
writeFakeExampleBuildGradles(example,
pluginName: packageName,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
includeSettingsArtifactHub: false,
// ignore: avoid_redundant_argument_values
includeSettingsDocumentationArtifactHub: true);
writeFakeManifest(example, isApp: true);

Expand All @@ -924,14 +857,9 @@ dependencies {
expect(
output,
containsAllInOrder(<Matcher>[
contains(GradleCheckCommand.exampleSettingsArtifactHubStringGP),
contains(GradleCheckCommand.exampleSettingsArtifactHubString),
]),
);
expect(
output,
isNot(
contains(GradleCheckCommand.exampleRootSettingsArtifactHubString)),
);
});

test('error message is printed when documentation link is missing',
Expand All @@ -942,9 +870,11 @@ dependencies {
writeFakePluginBuildGradle(package, includeLanguageVersion: true);
writeFakeManifest(package);
final RepositoryPackage example = package.getExamples().first;
writeFakeExampleBuildGradleGP(example,
writeFakeExampleBuildGradles(example,
pluginName: packageName,
// ignore: avoid_redundant_argument_values
includeBuildArtifactHub: true,
// ignore: avoid_redundant_argument_values
includeSettingsArtifactHub: true,
includeSettingsDocumentationArtifactHub: false);
writeFakeManifest(example, isApp: true);
Expand Down

0 comments on commit 46428e6

Please sign in to comment.