1717package com .palantir .javaformat .gradle ;
1818
1919import com .palantir .javaformat .bootstrap .NativeImageFormatterService ;
20- import java .io .File ;
2120import java .io .IOException ;
2221import org .gradle .api .DefaultTask ;
2322import org .gradle .api .Plugin ;
2423import org .gradle .api .Project ;
25- import org .gradle .api .artifacts .Configuration ;
2624import org .gradle .api .file .ConfigurableFileCollection ;
2725import org .gradle .api .file .DirectoryProperty ;
2826import org .gradle .api .file .FileCollection ;
2927import org .gradle .api .file .RegularFileProperty ;
3028import org .gradle .api .logging .Logger ;
3129import org .gradle .api .logging .Logging ;
32- import org .gradle .api .provider .Provider ;
3330import org .gradle .api .tasks .Classpath ;
3431import org .gradle .api .tasks .InputFile ;
3532import org .gradle .api .tasks .Internal ;
@@ -53,31 +50,23 @@ public void apply(Project project) {
5350 project .getPlugins ().apply (PalantirJavaFormatSpotlessPlugin .class );
5451
5552 project .getPlugins ().withId ("java" , p -> {
53+ var formatterClasspath = FormatterClasspath .of (project );
54+ var nativeImage = FormatterClasspath .nativeImageOf (project );
5655
5756 // TODO(dfox): in the future we may want to offer a simple 'format' task so people don't need to use
5857 // spotless to try out our formatter
5958 project .getTasks ().register ("formatDiff" , FormatDiffTask .class , task -> {
6059 task .getProjectDirectory ().set (project .getLayout ().getProjectDirectory ());
60+ task .getGradleUserHome ().set (project .getGradle ().getGradleUserHomeDir ());
6161 if (getNativeImageSupport ().isNativeImageConfigured ()) {
62- task .getNativeImage ().fileProvider (getNativeImplConfiguration ( project ));
62+ task .getNativeImage ().fileProvider (nativeImage . map ( FileCollection :: getSingleFile ));
6363 } else {
64- task .getFormatterClasspath ().from (getImplConfiguration ( project ) );
64+ task .getFormatterClasspath ().from (formatterClasspath );
6565 }
6666 });
6767 });
6868 }
6969
70- private static Provider <Configuration > getImplConfiguration (Project project ) {
71- return project .getRootProject ().getConfigurations ().named (PalantirJavaFormatProviderPlugin .CONFIGURATION_NAME );
72- }
73-
74- private static Provider <File > getNativeImplConfiguration (Project project ) {
75- return (project .getRootProject ()
76- .getConfigurations ()
77- .named (NativeImageFormatProviderPlugin .NATIVE_CONFIGURATION_NAME )
78- .map (FileCollection ::getSingleFile ));
79- }
80-
8170 @ DisableCachingByDefault (
8271 because = "Reformats whatever the working tree currently differs by; there is no reusable output" )
8372 public abstract static class FormatDiffTask extends DefaultTask {
@@ -89,10 +78,17 @@ public abstract static class FormatDiffTask extends DefaultTask {
8978 @ PathSensitive (PathSensitivity .NAME_ONLY )
9079 abstract RegularFileProperty getNativeImage ();
9180
92- /** The jars of the Java-based formatter, which runs when the native image is not configured. */
81+ /**
82+ * The jars a build declared for the Java-based formatter, which runs when the native image is not configured.
83+ * Most builds declare none, and the task then runs the one inside the plugin.
84+ */
9385 @ Classpath
9486 abstract ConfigurableFileCollection getFormatterClasspath ();
9587
88+ /** Where the formatter inside the plugin is extracted to, and the native image copied to. */
89+ @ Internal
90+ abstract DirectoryProperty getGradleUserHome ();
91+
9692 /**
9793 * The project whose changed lines are formatted. The task reads it here rather than from {@code getProject()},
9894 * which a task must not call when it runs with the configuration cache.
@@ -112,13 +108,16 @@ public final void formatDiff() throws IOException, InterruptedException {
112108 log .info ("Using the native-image formatter" );
113109 FormatDiff .formatDiff (
114110 projectDir ,
115- new NativeImageFormatterService (
116- getNativeImage ().get ().getAsFile ().toPath ()));
111+ new NativeImageFormatterService (NativeImageAtomicCopy .copyToCache (
112+ getNativeImage ().get ().getAsFile ().toPath (),
113+ getGradleUserHome ().get ().getAsFile ())));
117114 } else {
118115 log .info ("Using the Java-based formatter" );
119116 FormatDiff .formatDiff (
120117 projectDir ,
121- FormatterServiceLoader .load (getFormatterClasspath ().getFiles ()));
118+ FormatterServiceLoader .load (BundledFormatter .jarsUnlessOverridden (
119+ getFormatterClasspath ().getFiles (),
120+ getGradleUserHome ().get ().getAsFile ())));
122121 }
123122 }
124123 }
0 commit comments