Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lookup of metadata repository #425

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}