diff --git a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java index 78fea9ad..9c84be1d 100644 --- a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java +++ b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java @@ -17,41 +17,72 @@ package com.palantir.javaformat.gradle; import static com.google.common.base.Preconditions.checkState; -import static java.util.Collections.singletonList; +import java.util.List; import javax.inject.Inject; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.gradle.api.provider.Provider; import org.gradle.api.provider.ProviderFactory; +import org.gradle.api.tasks.Nested; public abstract class NativeImageFormatProviderPlugin implements Plugin { + private final Logger log = Logging.getLogger(NativeImageFormatProviderPlugin.class); @Inject protected abstract ProviderFactory getProviderFactory(); + @Nested + protected abstract NativeImageSupport getNativeImageSupport(); + static final String NATIVE_CONFIGURATION_NAME = "palantirJavaFormatNative"; + /** The version of open-java-format-native that formats with {@code openjavaformat.native.formatter}. */ + static final String NATIVE_VERSION_PROPERTY = "openjavaformat.native.formatter.version"; + @Override public void apply(Project rootProject) { checkState( rootProject.equals(rootProject.getRootProject()), "May only apply dev.openjavaformat.java-format-provider to the root project"); - Provider platform = NativePlatform.current(getProviderFactory()); - String implementationVersion = JavaFormatExtension.class.getPackage().getImplementationVersion(); + Provider pluginVersion = getProviderFactory() + .provider(() -> JavaFormatExtension.class.getPackage().getImplementationVersion()); + + // The plugin's own version, unless the build names another: a release line that publishes no native image + // of its own, as the one for Java 17, formats with a native image of another line. + var version = + getProviderFactory().gradleProperty(NATIVE_VERSION_PROPERTY).orElse(pluginVersion); + + var platform = NativePlatform.current(getProviderFactory()); + var nativeImage = rootProject.getConfigurations().register(NATIVE_CONFIGURATION_NAME, conf -> { conf.setDescription("Internal configuration for resolving the open-java-format native image"); conf.setCanBeConsumed(false); conf.setCanBeResolved(true); + // Only the default image needs a version: a build that declares its own formats with it without one. + // Gradle runs this even when it only looks at the variants of the root project, as it does for the + // formatter's classpath, so it warns only when formatDiff is to run the image. conf.defaultDependencies(deps -> { - deps.addAllLater(platform.map(p -> singletonList(rootProject - .getDependencies() - .create(String.format( - "dev.openjavaformat:open-java-format-native:%s:%s@%s", - implementationVersion, p.classifier(), p.extension()))))); + if (!version.isPresent()) { + if (getNativeImageSupport().isNativeImageConfigured()) { + log.warn("Not able to determine the " + NATIVE_VERSION_PROPERTY + " value"); + } + + return; + } + + deps.addAllLater(platform.zip( + version, + (p, v) -> List.of(rootProject + .getDependencies() + .create("dev.openjavaformat:open-java-format-native:%s:%s@%s" + .formatted(v, p.classifier(), p.extension()))))); }); }); + // Resolved in each project that formats, through its own repositories; formatDiff copies the image it gets // into open-java-format-caches, executable, and runs it from there. FormatterClasspath.offerNativeImage(rootProject, nativeImage); diff --git a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java index b055bbf1..c2e241e0 100644 --- a/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java +++ b/gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java @@ -70,8 +70,7 @@ public void apply(Project project) { @DisableCachingByDefault( because = "Reformats whatever the working tree currently differs by; there is no reusable output") public abstract static class FormatDiffTask extends DefaultTask { - - private static Logger log = Logging.getLogger(FormatDiffTask.class); + private final Logger log = Logging.getLogger(FormatDiffTask.class); @org.gradle.api.tasks.Optional @InputFile @@ -106,6 +105,7 @@ public final void formatDiff() throws IOException, InterruptedException { var projectDir = getProjectDirectory().get().getAsFile().toPath(); if (getNativeImage().isPresent()) { log.info("Using the native-image formatter"); + FormatDiff.formatDiff( projectDir, new NativeImageFormatterService(NativeImageAtomicCopy.copyToCache( @@ -113,6 +113,7 @@ public final void formatDiff() throws IOException, InterruptedException { getGradleUserHome().get().getAsFile()))); } else { log.info("Using the Java-based formatter"); + FormatDiff.formatDiff( projectDir, FormatterServiceLoader.load(BundledFormatter.jarsUnlessOverridden( diff --git a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java index 7c0f05b2..48cbfe6a 100644 --- a/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java +++ b/gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java @@ -60,6 +60,8 @@ final class NativeImageResolutionTest { private static final String MAIN_JAVA = "src/main/java/Main.java"; private static final String UNFORMATTED = "class Main {\n int x;\n}\n"; private static final String FORMATTED = "class Main {\n int x;\n}\n"; + private static final String NO_VERSION_WARNING = + "Not able to determine the openjavaformat.native.formatter.version value"; @Parameter private boolean configurationCache; @@ -74,7 +76,9 @@ void formatDiff_runs_the_native_image_from_the_repositories_of_the_subprojects() var result = project.succeeds("formatDiff", "--info"); - assertThat(result.getOutput()).contains("Using the native-image formatter"); + assertThat(result.getOutput()) + .contains("Using the native-image formatter") + .doesNotContain(NO_VERSION_WARNING); assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); assertRunFromTheCaches(result); } @@ -89,21 +93,89 @@ void formatDiff_runs_the_native_image_in_a_parallel_build() throws Exception { assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); } + /** + * A build that declares no image and names its version in openjavaformat.native.formatter.version formats with the + * image of that version, not of the plugin's: the release line for Java 17 publishes none of its own. + */ + @Test + void formatDiff_runs_the_native_image_of_the_version_the_build_names() throws Exception { + var project = projectNamingTheVersion(); + commitThenChange(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + + var result = project.succeeds("formatDiff", "--info"); + + assertThat(result.getOutput()) + .contains("Using the native-image formatter") + .doesNotContain(NO_VERSION_WARNING); + assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + } + + /** + * With the native formatter on, a build that declares no image and names no version, with a plugin that has no + * version of its own, as TestKit runs it from its classes: the plugin warns that it cannot tell which image to take. + */ + @Test + void formatDiff_warns_when_the_native_formatter_is_on_and_nothing_names_its_version() throws Exception { + var project = project(""); + commitThenChange(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + + var result = project.fails("formatDiff"); + + assertThat(result.getOutput()).contains(NO_VERSION_WARNING); + } + + /** With the native formatter off, nothing needs the version of its image, and the plugin says nothing of it. */ + @Test + void formatDiff_says_nothing_of_the_version_when_the_native_formatter_is_off() throws Exception { + var project = projectWithTheNativeFormatterOff(); + commitThenChange(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + + var result = project.succeeds("formatDiff", "--info"); + + assertThat(result.getOutput()) + .contains("Using the Java-based formatter") + .doesNotContain(NO_VERSION_WARNING); + assertFormatted(project, "a/" + MAIN_JAVA, "b/" + MAIN_JAVA); + } + /** * The layout of the docs, with the repositories in the subprojects only, and the image declared in the root project * as a release's would be by default. Prints the Gradle user home, where the image is copied to. */ private GradleTestProject project() throws IOException { var platform = currentPlatform(); - writeNativeImage(platform); + return project(""" + dependencies { + palantirJavaFormatNative 'dev.openjavaformat:open-java-format-native:%s:%s@%s' + } + """.formatted(VERSION, platform.classifier(), platform.extension())); + } + + /** The same layout, with no image declared: the plugin asks for the version the build names. */ + private GradleTestProject projectNamingTheVersion() throws IOException { + return project("").gradleProperties("openjavaformat.native.formatter.version=" + VERSION); + } + + /** + * The same layout, with no image declared and the native formatter off: formatDiff formats with the formatter + * inside the plugin, which needs javac exported to the Gradle JVM. + */ + private GradleTestProject projectWithTheNativeFormatterOff() throws IOException { + return layout("").withJavacInternalExports(); + } + + private GradleTestProject project(String rootDependencies) throws IOException { + return layout(rootDependencies).gradleProperties("openjavaformat.native.formatter=true"); + } + + private GradleTestProject layout(String rootDependencies) throws IOException { + writeNativeImage(currentPlatform()); return new GradleTestProject(projectDir) .configurationCache(configurationCache) .withoutRepositories() .pluginsNotApplied("dev.openjavaformat.java-format") .subprojects("a", "b") - .gradleProperties("openjavaformat.native.formatter=true") - .buildGradle( - """ + .buildGradle(""" subprojects { apply plugin: 'java' apply plugin: 'dev.openjavaformat.java-format' @@ -111,15 +183,9 @@ private GradleTestProject project() throws IOException { maven { url = rootProject.file("%s") } } } - dependencies { - palantirJavaFormatNative 'dev.openjavaformat:open-java-format-native:%s:%s@%s' - } + %s println "gradleUserHome=${gradle.gradleUserHomeDir}" - """, - NATIVE_REPO.toAbsolutePath().toString().replace('\\', '/'), - VERSION, - platform.classifier(), - platform.extension()); + """, NATIVE_REPO.toAbsolutePath().toString().replace('\\', '/'), rootDependencies); } private static void assertFormatted(GradleTestProject project, String... files) {