Skip to content

Commit

Permalink
Fix lookup of metadata repository
Browse files Browse the repository at this point in the history
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
  • Loading branch information
melix committed Apr 6, 2023
1 parent 0a858d2 commit adede05
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,6 +170,7 @@ public class NativeImagePlugin implements Plugin<Project> {
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;

Expand Down Expand Up @@ -405,19 +407,26 @@ private Provider<GraalVMReachabilityMetadataService> 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<? super URI> 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);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}
}

0 comments on commit adede05

Please sign in to comment.