Skip to content

Commit 0a9f080

Browse files
committed
comments
1 parent 05c1e2a commit 0a9f080

File tree

6 files changed

+34
-61
lines changed

6 files changed

+34
-61
lines changed

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/NativeImageAtomicCopy.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.javaformat.gradle;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.net.URI;
2122
import java.nio.channels.FileChannel;
@@ -34,9 +35,9 @@ public class NativeImageAtomicCopy {
3435

3536
private static Logger logger = Logging.getLogger(PalantirJavaFormatIdeaPlugin.class);
3637

37-
public static URI copyToCacheDir(URI srcUri) {
38+
public static URI copyToCacheDir(URI srcUri, File cacheDir) {
3839
Path src = Paths.get(srcUri);
39-
Path dst = getGradleCacheNativeImagesDir().resolve(src.getFileName());
40+
Path dst = cacheDir.toPath().resolve(src.getFileName());
4041
if (Files.exists(dst)) {
4142
logger.info("Native image at path {} already exists", dst);
4243
return dst.toUri();
@@ -78,12 +79,6 @@ public static URI copyToCacheDir(URI srcUri) {
7879
}
7980
}
8081

81-
private static Path getGradleCacheNativeImagesDir() {
82-
Path nativeImagesCacheDir = getGradleCacheDir().resolve("palantir-java-format-caches");
83-
nativeImagesCacheDir.toFile().mkdirs();
84-
return nativeImagesCacheDir;
85-
}
86-
8782
private static Path getGradleCacheDir() {
8883
return Path.of(Optional.ofNullable(System.getenv("GRADLE_USER_HOME"))
8984
.orElseGet(() -> System.getenv("HOME") + "/.gradle"));

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPlugin.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,13 @@ public void apply(Project rootProject) {
4545
.getTasks()
4646
.register("updatePalantirJavaFormatXml", UpdatePalantirJavaFormatIdeaXmlFile.class, task -> {
4747
task.getOutputFile().set(rootProject.file(".idea/palantir-java-format.xml"));
48-
task.getShouldCreateOutputIfAbsent().set(true);
49-
});
50-
51-
TaskProvider<UpdatePalantirJavaFormatIdeaXmlFile> updatePalantirJavaFormatIpr = rootProject
52-
.getTasks()
53-
.register("updatePalantirJavaFormatIpr", UpdatePalantirJavaFormatIdeaXmlFile.class, task -> {
54-
task.getOutputFile().set(rootProject.file(rootProject.getName() + ".ipr"));
55-
task.getShouldCreateOutputIfAbsent().set(false);
56-
});
57-
58-
rootProject
59-
.getTasks()
60-
.withType(UpdatePalantirJavaFormatIdeaXmlFile.class)
61-
.configureEach(task -> {
48+
task.getCacheDir()
49+
.set(rootProject
50+
.getGradle()
51+
.getGradleUserHomeDir()
52+
.toPath()
53+
.resolve("palantir-java-format-caches")
54+
.toFile());
6255
task.getImplementationConfig()
6356
.from(rootProject
6457
.getConfigurations()
@@ -71,23 +64,11 @@ public void apply(Project rootProject) {
7164
.getTasks()
7265
.register("updateWorkspaceXml", UpdateWorkspaceXmlFile.class, task -> {
7366
task.getOutputFile().set(rootProject.file(".idea/workspace.xml"));
74-
task.getShouldCreateOutputIfAbsent().set(true);
75-
});
76-
77-
TaskProvider<UpdateWorkspaceXmlFile> updateWorkspaceIwsXml = rootProject
78-
.getTasks()
79-
.register("updateWorkspaceIwsXml", UpdateWorkspaceXmlFile.class, task -> {
80-
task.getOutputFile().set(rootProject.file(rootProject.getName() + ".iws"));
81-
task.getShouldCreateOutputIfAbsent().set(false);
8267
});
8368

8469
// Add the task to the Gradle start parameters so it executes automatically.
8570
StartParameter startParameter = rootProject.getGradle().getStartParameter();
86-
List<String> updateTasks = Stream.of(
87-
updatePalantirJavaFormatXml,
88-
updatePalantirJavaFormatIpr,
89-
updateWorkspaceXml,
90-
updateWorkspaceIwsXml)
71+
List<String> updateTasks = Stream.of(updatePalantirJavaFormatXml, updateWorkspaceXml)
9172
.map(taskProvider -> String.format(":%s", taskProvider.getName()))
9273
.toList();
9374
List<String> taskNames = ImmutableList.<String>builder()

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdatePalantirJavaFormatIdeaXmlFile.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
import org.gradle.api.DefaultTask;
2424
import org.gradle.api.file.ConfigurableFileCollection;
2525
import org.gradle.api.file.RegularFileProperty;
26-
import org.gradle.api.provider.Property;
2726
import org.gradle.api.tasks.Classpath;
28-
import org.gradle.api.tasks.Input;
2927
import org.gradle.api.tasks.InputFiles;
3028
import org.gradle.api.tasks.Optional;
29+
import org.gradle.api.tasks.OutputDirectory;
3130
import org.gradle.api.tasks.OutputFile;
3231
import org.gradle.api.tasks.TaskAction;
3332

@@ -45,8 +44,8 @@ public abstract class UpdatePalantirJavaFormatIdeaXmlFile extends DefaultTask {
4544
@OutputFile
4645
public abstract RegularFileProperty getOutputFile();
4746

48-
@Input
49-
public abstract Property<Boolean> getShouldCreateOutputIfAbsent();
47+
@OutputDirectory
48+
public abstract RegularFileProperty getCacheDir();
5049

5150
@TaskAction
5251
public final void updateXml() {
@@ -55,10 +54,10 @@ public final void updateXml() {
5554
java.util.Optional<URI> nativeUri = getNativeImageConfig().isEmpty()
5655
? java.util.Optional.empty()
5756
: java.util.Optional.of(getNativeImageConfig().getSingleFile().toURI())
58-
.map(NativeImageAtomicCopy::copyToCacheDir);
57+
.map(uri -> NativeImageAtomicCopy.copyToCacheDir(
58+
uri, getCacheDir().getAsFile().get()));
5959
XmlUtils.updateIdeaXmlFile(
6060
getOutputFile().getAsFile().get(),
61-
node -> ConfigureJavaFormatterXml.configureJavaFormat(node, uris, nativeUri),
62-
getShouldCreateOutputIfAbsent().get());
61+
node -> ConfigureJavaFormatterXml.configureJavaFormat(node, uris, nativeUri));
6362
}
6463
}

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateWorkspaceXmlFile.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import org.gradle.api.DefaultTask;
2020
import org.gradle.api.file.RegularFileProperty;
21-
import org.gradle.api.provider.Property;
22-
import org.gradle.api.tasks.Input;
2321
import org.gradle.api.tasks.Optional;
2422
import org.gradle.api.tasks.OutputFile;
2523
import org.gradle.api.tasks.TaskAction;
@@ -30,14 +28,8 @@ public abstract class UpdateWorkspaceXmlFile extends DefaultTask {
3028
@OutputFile
3129
public abstract RegularFileProperty getOutputFile();
3230

33-
@Input
34-
public abstract Property<Boolean> getShouldCreateOutputIfAbsent();
35-
3631
@TaskAction
3732
public final void updateXml() {
38-
XmlUtils.updateIdeaXmlFile(
39-
getOutputFile().getAsFile().get(),
40-
ConfigureJavaFormatterXml::configureWorkspaceXml,
41-
getShouldCreateOutputIfAbsent().get());
33+
XmlUtils.updateIdeaXmlFile(getOutputFile().getAsFile().get(), ConfigureJavaFormatterXml::configureWorkspaceXml);
4234
}
4335
}

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/XmlUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
public class XmlUtils {
3434

35-
public static void updateIdeaXmlFile(File configurationFile, Consumer<Node> configure, boolean createIfAbsent) {
35+
public static void updateIdeaXmlFile(File configurationFile, Consumer<Node> configure) {
3636
Node rootNode;
3737
if (configurationFile.isFile()) {
3838
try {
@@ -41,9 +41,6 @@ public static void updateIdeaXmlFile(File configurationFile, Consumer<Node> conf
4141
throw new RuntimeException("Couldn't parse existing configuration file: " + configurationFile, e);
4242
}
4343
} else {
44-
if (!createIfAbsent) {
45-
return;
46-
}
4744
rootNode = new Node(null, "project", ImmutableMap.of("version", "4"));
4845
}
4946

gradle-palantir-java-format/src/test/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatIdeaPluginTest.groovy

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PalantirJavaFormatIdeaPluginTest extends IntegrationTestKitSpec {
2323
private static final NATIVE_IMAGE_FILE = new File("build/nativeImage.path")
2424
private static final NATIVE_CONFIG = String.format("palantirJavaFormatNative files(\"%s\")", NATIVE_IMAGE_FILE.text)
2525

26-
def "idea_configuresIpr"() {
26+
def "idea_configuresXmlFiles"() {
2727
file('gradle.properties') << extraGradleProperties
2828

2929
buildFile << """
@@ -42,15 +42,24 @@ class PalantirJavaFormatIdeaPluginTest extends IntegrationTestKitSpec {
4242
runTasks('idea')
4343

4444
then:
45-
def iprFile = new File(projectDir, "${moduleName}.ipr")
46-
def ipr = new XmlSlurper().parse(iprFile)
47-
def settings = ipr.component.findAll { it.@name == "PalantirJavaFormatSettings" }
45+
def pjfXmlFile = new File(projectDir, ".idea/palantir-java-format.xml")
46+
def xmlContent = new XmlSlurper().parse(pjfXmlFile)
47+
def settings = xmlContent.component.findAll { it.@name == "PalantirJavaFormatSettings" }
4848
!settings.isEmpty()
49-
def implementationClassPathOption = ipr.component.option.find { it.@name == 'implementationClassPath' }
49+
def implementationClassPathOption = xmlContent.component.option.find { it.@name == 'implementationClassPath' }
5050
!implementationClassPathOption.isEmpty()
51-
def nativeImageClassPathOption = ipr.component.option.find { it.@name == 'nativeImageClassPath' }
51+
def nativeImageClassPathOption = xmlContent.component.option.find { it.@name == 'nativeImageClassPath' }
5252
extraGradleProperties.contains("palantir.native.formatter=true") ? !nativeImageClassPathOption.isEmpty() : true
5353

54+
def workspaceXmlFile = new File(projectDir, ".idea/workspace.xml")
55+
def workspaceContent = new XmlSlurper().parse(workspaceXmlFile)
56+
57+
def formatOnSave = workspaceContent.component.findAll { it.@name == "FormatOnSaveOptions" }
58+
!formatOnSave.isEmpty()
59+
60+
def optimizeOnSave = workspaceContent.component.findAll { it.@name == "OptimizeOnSaveOptions" }
61+
!formatOnSave.isEmpty()
62+
5463
where:
5564
extraGradleProperties | extraDependencies
5665
"" | ""

0 commit comments

Comments
 (0)