Skip to content
Open
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 @@ -32,24 +32,33 @@

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<NativePlatform> platform = NativePlatform.current(getProviderFactory());
String implementationVersion = 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.
Provider<String> version = getProviderFactory()
.gradleProperty(NATIVE_VERSION_PROPERTY)
.orElse(String.valueOf(JavaFormatExtension.class.getPackage().getImplementationVersion()));

Check warning on line 49 in gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/NativeImageFormatProviderPlugin.java

View workflow job for this annotation

GitHub Actions / build

[IdentityConversion] This method invocation appears redundant; remove it or suppress this warning and add a comment explaining its purpose
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);
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())))));
deps.addAllLater(platform.zip(
version,
(p, v) -> singletonList(rootProject
.getDependencies()
.create(String.format(
"dev.openjavaformat:open-java-format-native:%s:%s@%s",
v, p.classifier(), p.extension())))));
});
});
// Resolved in each project that formats, through its own repositories; formatDiff copies the image it gets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,58 @@ 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");
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(NativeImageFormatProviderPlugin.NATIVE_VERSION_PROPERTY + "=" + VERSION);
}

private GradleTestProject project(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'
repositories {
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) {
Expand Down
Loading