From adede05fc19d73b30a267cf27c5bb699dd9b1f20 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Thu, 6 Apr 2023 18:05:37 +0200 Subject: [PATCH] Fix lookup of metadata repository This commit fixes the URI which is constructed when only a version is set in the metadata repository. Before this change, whatever version was used in the configuration, we would ignore it, because we were testing against the wrong URI to figure out if the user had configured something explicitly or not. Now, we will prioritize the user URI, then construct a GitHub release URI from the version which is configured, and eventually use Maven Central if that URI is the same as the default one. Fixes #424 --- .../buildtools/gradle/NativeImagePlugin.java | 24 ++++--- .../gradle/NativeImagePluginTest.groovy | 68 +++++++++++++++++++ 2 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 native-gradle-plugin/src/test/groovy/org/graalvm/buildtools/gradle/NativeImagePluginTest.groovy diff --git a/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java b/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java index ef7eb1baf..c09a9842c 100644 --- a/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java +++ b/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java @@ -132,6 +132,7 @@ import java.util.Objects; import java.util.Set; import java.util.function.BiFunction; +import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.regex.Pattern; @@ -169,6 +170,7 @@ public class NativeImagePlugin implements Plugin { private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED = "junit.platform.listeners.uid.tracking.enabled"; private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR = "junit.platform.listeners.uid.tracking.output.dir"; private static final String REPOSITORY_COORDINATES = "org.graalvm.buildtools:graalvm-reachability-metadata:" + VersionInfo.NBT_VERSION + ":repository@zip"; + private static final String DEFAULT_URI = String.format(METADATA_REPO_URL_TEMPLATE, VersionInfo.METADATA_REPO_VERSION); private GraalVMLogger logger; @@ -405,19 +407,26 @@ private Provider graalVMReachabilityMetadata LogLevel logLevel = determineLogLevel(); spec.getMaxParallelUsages().set(1); spec.getParameters().getLogLevel().set(logLevel); - spec.getParameters().getUri().set(repositoryExtension.getUri().map(serializableTransformerOf(configuredUri -> computeMetadataRepositoryUri(project, repositoryExtension, configuredUri, GraalVMLogger.of(project.getLogger()))))); + spec.getParameters().getUri().set(repositoryExtension.getUri().map(serializableTransformerOf(configuredUri -> computeMetadataRepositoryUri(project, repositoryExtension, m -> logFallbackToDefaultUri(m, logger))))); spec.getParameters().getCacheDir().set( new File(project.getGradle().getGradleUserHomeDir(), "native-build-tools/repositories")); }); } - private static URI computeMetadataRepositoryUri(Project project, - GraalVMReachabilityMetadataRepositoryExtension repositoryExtension, - URI configuredUri, - GraalVMLogger logger) { + private static void logFallbackToDefaultUri(URI defaultUri, GraalVMLogger logger) { + logger.warn("Unable to find the GraalVM reachability metadata repository in Maven repository. " + + "Falling back to the default repository at " + defaultUri); + } + + static URI computeMetadataRepositoryUri(Project project, + GraalVMReachabilityMetadataRepositoryExtension repositoryExtension, + Consumer onFallback) { + URI configuredUri = repositoryExtension.getUri().getOrNull(); + URI githubReleaseUri; URI defaultUri; try { - defaultUri = new URI(String.format(METADATA_REPO_URL_TEMPLATE, repositoryExtension.getVersion().get())); + defaultUri = new URI(DEFAULT_URI); + githubReleaseUri = new URI(String.format(METADATA_REPO_URL_TEMPLATE, repositoryExtension.getVersion().get())); } catch (URISyntaxException e) { throw new RuntimeException("Unable to convert repository path to URI", e); } @@ -434,8 +443,7 @@ private static URI computeMetadataRepositoryUri(Project project, if (files.size() == 1) { return files.iterator().next().toURI(); } else { - logger.warn("Unable to find the GraalVM reachability metadata repository in Maven repository. " + - "Falling back to the default repository at " + defaultUri); + onFallback.accept(githubReleaseUri); } } return configuredUri; diff --git a/native-gradle-plugin/src/test/groovy/org/graalvm/buildtools/gradle/NativeImagePluginTest.groovy b/native-gradle-plugin/src/test/groovy/org/graalvm/buildtools/gradle/NativeImagePluginTest.groovy new file mode 100644 index 000000000..3e8ae2bd4 --- /dev/null +++ b/native-gradle-plugin/src/test/groovy/org/graalvm/buildtools/gradle/NativeImagePluginTest.groovy @@ -0,0 +1,68 @@ +package org.graalvm.buildtools.gradle + +import org.graalvm.buildtools.gradle.dsl.GraalVMExtension +import org.graalvm.buildtools.gradle.dsl.GraalVMReachabilityMetadataRepositoryExtension +import org.gradle.api.Project +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Issue +import spock.lang.Specification + +import static org.graalvm.buildtools.VersionInfo.METADATA_REPO_VERSION + +class NativeImagePluginTest extends Specification { + + private static final String DEFAULT_GITHUB_RELEASES_METADATA_URI = "https://github.com/oracle/graalvm-reachability-metadata/releases/download/${METADATA_REPO_VERSION}/graalvm-reachability-metadata-${METADATA_REPO_VERSION}.zip" + + private Project project + private GraalVMReachabilityMetadataRepositoryExtension reachabilityMetadataRepositoryExtension + + + private URI resultUri + private URI fallbackUri + + def setup() { + project = ProjectBuilder.builder().build() + project.repositories.mavenCentral() + project.plugins.apply(NativeImagePlugin) + reachabilityMetadataRepositoryExtension = project.extensions + .findByType(GraalVMExtension) + .extensions + .findByType(GraalVMReachabilityMetadataRepositoryExtension) + } + + @Issue("https://github.com/graalvm/native-build-tools/issues/424") + def "can set the version of the repository"() { + when: + repositoryUriFor(configuredUri, version) + + then: + resultUri == new URI(expectedUri) + fallbackUri == (expectedFallbackUri == null ? null : new URI(expectedFallbackUri)) + + where: + configuredUri | version | expectedUri | expectedFallbackUri + null | null | "https://lookup.on.maven.central" | DEFAULT_GITHUB_RELEASES_METADATA_URI + DEFAULT_GITHUB_RELEASES_METADATA_URI | null | "https://lookup.on.maven.central" | DEFAULT_GITHUB_RELEASES_METADATA_URI + "https://custom.uri" | null | 'https://custom.uri' | null + null | '155' | 'https://github.com/oracle/graalvm-reachability-metadata/releases/download/155/graalvm-reachability-metadata-155.zip' | null + null | METADATA_REPO_VERSION | 'https://lookup.on.maven.central' | DEFAULT_GITHUB_RELEASES_METADATA_URI + "https://custom.uri" | 'ignored' | 'https://custom.uri' | null + } + + private void repositoryUriFor(String configuredUri, String version) { + if (configuredUri != null) { + reachabilityMetadataRepositoryExtension.uri.set(new URI(configuredUri)) + } + if (version != null) { + reachabilityMetadataRepositoryExtension.version.set(version) + } + fallbackUri = null + resultUri = NativeImagePlugin.computeMetadataRepositoryUri(project, reachabilityMetadataRepositoryExtension) { + fallbackUri = it + } + if (fallbackUri != null) { + // if we have a fallback uri, then it means we tried to look on Maven Central + resultUri = new URI("https://lookup.on.maven.central") + } + } +}