[All] Expand artifact hub to all plugins (#4645)

- Adds artifact hub check to gradle command
- Add tests for build.gradle and settings.gradle check
- Update all example build.gradle and settings.gradle files

flutter/flutter/issues/120119

Expansion of https://github.com/flutter/packages/pull/4567
This commit is contained in:
Reid Baker
2023-08-07 11:33:26 -07:00
committed by GitHub
parent fa86f8a4e1
commit 6cb2a32b7f
84 changed files with 1089 additions and 8 deletions

View File

@ -66,6 +66,12 @@ class GradleCheckCommand extends PackageLoopingCommand {
if (!_validateExampleTopLevelBuildGradle(package, topLevelGradleFile)) {
succeeded = false;
}
final File topLevelSettingsGradleFile =
_getSettingsGradleFile(androidDir);
if (!_validateExampleTopLevelSettingsGradle(
package, topLevelSettingsGradleFile)) {
succeeded = false;
}
final File appGradleFile =
_getBuildGradleFile(androidDir.childDirectory('app'));
@ -82,6 +88,10 @@ class GradleCheckCommand extends PackageLoopingCommand {
// Returns the gradle file in the given directory.
File _getBuildGradleFile(Directory dir) => dir.childFile('build.gradle');
// Returns the settings gradle file in the given directory.
File _getSettingsGradleFile(Directory dir) =>
dir.childFile('settings.gradle');
// Returns the main/AndroidManifest.xml file for the given package.
File _getMainAndroidManifest(RepositoryPackage package,
{required bool isExample}) {
@ -120,6 +130,124 @@ class GradleCheckCommand extends PackageLoopingCommand {
return succeeded;
}
/// Documentation url for Artifact hub implementation in flutter repo's.
@visibleForTesting
static const String artifactHubDocumentationString =
r'https://github.com/flutter/flutter/wiki/Plugins-and-Packages-repository-structure#gradle-structure';
/// String printed as example of valid example root build.gradle repository
/// configuration that enables artifact hub env variable.
@visibleForTesting
static const String exampleRootGradleArtifactHubString = '''
// See $artifactHubDocumentationString for more info.
def artifactRepoKey = 'ARTIFACT_HUB_REPOSITORY'
if (System.getenv().containsKey(artifactRepoKey)) {
println "Using artifact hub"
maven { url System.getenv(artifactRepoKey) }
}
''';
/// Validates that [gradleLines] reads and uses a artifiact hub repository
/// when ARTIFACT_HUB_REPOSITORY is set.
///
/// Required in root gradle file.
bool _validateArtifactHubUsage(
RepositoryPackage example, List<String> gradleLines) {
// Gradle variable name used to hold environment variable string.
const String keyVariable = 'artifactRepoKey';
final RegExp keyPresentRegex =
RegExp('$keyVariable' r"\s+=\s+'ARTIFACT_HUB_REPOSITORY'");
final RegExp documentationPresentRegex = RegExp(
r'github\.com.*wiki.*Plugins-and-Packages-repository-structure.*gradle-structure');
final RegExp keyReadRegex =
RegExp(r'if.*System\.getenv.*\.containsKey.*' '$keyVariable');
final RegExp keyUsedRegex =
RegExp(r'maven.*url.*System\.getenv\(' '$keyVariable');
final bool keyPresent =
gradleLines.any((String line) => keyPresentRegex.hasMatch(line));
final bool documentationPresent = gradleLines
.any((String line) => documentationPresentRegex.hasMatch(line));
final bool keyRead =
gradleLines.any((String line) => keyReadRegex.hasMatch(line));
final bool keyUsed =
gradleLines.any((String line) => keyUsedRegex.hasMatch(line));
if (!(documentationPresent && keyPresent && keyRead && keyUsed)) {
printError('Failed Artifact Hub validation. Include the following in '
'example root build.gradle:\n$exampleRootGradleArtifactHubString');
}
return keyPresent && documentationPresent && keyRead && keyUsed;
}
/// Validates the top-level settings.gradle for an example app (e.g.,
/// some_package/example/android/settings.gradle).
bool _validateExampleTopLevelSettingsGradle(
RepositoryPackage package, File gradleSettingsFile) {
print('${indentation}Validating '
'${getRelativePosixPath(gradleSettingsFile, from: package.directory)}.');
final String contents = gradleSettingsFile.readAsStringSync();
final List<String> lines = contents.split('\n');
// This is tracked as a variable rather than a sequence of &&s so that all
// failures are reported at once, not just the first one.
bool succeeded = true;
if (!_validateArtifactHubSettingsUsage(package, lines)) {
succeeded = false;
}
return succeeded;
}
/// String printed as example of valid example root settings.gradle repository
/// configuration that enables artifact hub env variable.
@visibleForTesting
static String exampleRootSettingsArtifactHubString = '''
// See $artifactHubDocumentationString for more info.
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"
''';
/// Validates that [gradleLines] reads and uses a artifiact hub repository
/// when ARTIFACT_HUB_REPOSITORY is set.
///
/// Required in root gradle file.
bool _validateArtifactHubSettingsUsage(
RepositoryPackage example, List<String> gradleLines) {
final RegExp documentationPresentRegex = RegExp(
r'github\.com.*wiki.*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 bool documentationPresent = gradleLines
.any((String line) => documentationPresentRegex.hasMatch(line));
final bool artifactRegistryDefined = gradleLines
.any((String line) => artifactRegistryDefinitionRegex.hasMatch(line));
final bool artifactRegistryPluginApplied = gradleLines
.any((String line) => artifactRegistryPluginApplyRegex.hasMatch(line));
if (!(documentationPresent &&
artifactRegistryDefined &&
artifactRegistryPluginApplied)) {
printError('Failed Artifact Hub validation. Include the following in '
'example root settings.gradle:\n$exampleRootSettingsArtifactHubString');
}
return documentationPresent &&
artifactRegistryDefined &&
artifactRegistryPluginApplied;
}
/// Validates the top-level build.gradle for an example app (e.g.,
/// some_package/example/android/build.gradle).
bool _validateExampleTopLevelBuildGradle(
@ -138,6 +266,9 @@ class GradleCheckCommand extends PackageLoopingCommand {
if (!_validateKotlinVersion(package, lines)) {
succeeded = false;
}
if (!_validateArtifactHubUsage(package, lines)) {
succeeded = false;
}
return succeeded;
}