-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aabd04b
commit e97832a
Showing
9 changed files
with
120 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: improvement | ||
improvement: | ||
description: When using the gradle plugin, users will receive a notification to | ||
install the IntelliJ plugin | ||
links: | ||
- https://github.com/palantir/palantir-java-format/pull/26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,9 @@ gradlePlugin { | |
} | ||
} | ||
} | ||
|
||
idea { | ||
module { | ||
sourceDirs += sourceSets.main.groovy.srcDirs | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...rmat/src/main/groovy/com/palantir/javaformat/gradle/ConfigureExternalDependenciesXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.palantir.javaformat.gradle; | ||
|
||
import groovy.util.Node; | ||
|
||
public class ConfigureExternalDependenciesXml extends UpdateIntellijXmlTask { | ||
public ConfigureExternalDependenciesXml() { | ||
getXmlFile().set(getProject().file(".idea/externalDependencies.xml")); | ||
} | ||
|
||
@Override | ||
protected final void configure(Node rootNode) { | ||
ConfigureJavaFormatterXml.configureExternalDependencies(rootNode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 6 additions & 44 deletions
50
...format/src/main/groovy/com/palantir/javaformat/gradle/ConfigurePalantirJavaFormatXml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,29 @@ | ||
package com.palantir.javaformat.gradle; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.io.Files; | ||
import groovy.util.Node; | ||
import groovy.util.XmlNodePrinter; | ||
import groovy.util.XmlParser; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.net.URI; | ||
import java.nio.charset.Charset; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Classpath; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.xml.sax.SAXException; | ||
|
||
public class ConfigurePalantirJavaFormatXml extends DefaultTask { | ||
public class ConfigurePalantirJavaFormatXml extends UpdateIntellijXmlTask { | ||
private final Property<Configuration> implConfiguration = getProject().getObjects().property(Configuration.class); | ||
|
||
@Classpath | ||
Property<Configuration> getImplConfiguration() { | ||
return implConfiguration; | ||
} | ||
|
||
@PathSensitive(PathSensitivity.RELATIVE) | ||
@OutputFile | ||
File getOutputFile() { | ||
return getProject().file(".idea/palantir-java-format.xml"); | ||
public ConfigurePalantirJavaFormatXml() { | ||
getXmlFile().set(getProject().file(".idea/palantir-java-format.xml")); | ||
} | ||
|
||
@TaskAction | ||
public void run() { | ||
File configurationFile = getOutputFile(); | ||
Node rootNode; | ||
if (configurationFile.isFile()) { | ||
try { | ||
rootNode = new XmlParser().parse(configurationFile); | ||
} catch (IOException | SAXException | ParserConfigurationException e) { | ||
throw new RuntimeException("Couldn't parse existing configuration file: " + configurationFile, e); | ||
} | ||
} else { | ||
rootNode = new Node(null, "project", ImmutableMap.of("version", "4")); | ||
} | ||
|
||
@Override | ||
protected final void configure(Node rootNode) { | ||
List<URI> uris = implConfiguration.get().getFiles().stream().map(File::toURI).collect(Collectors.toList()); | ||
ConfigureJavaFormatterXml.configure(rootNode, uris); | ||
|
||
try (BufferedWriter writer = Files.newWriter(configurationFile, Charset.defaultCharset()); | ||
PrintWriter printWriter = new PrintWriter(writer)) { | ||
XmlNodePrinter nodePrinter = new XmlNodePrinter(printWriter); | ||
nodePrinter.setPreserveWhitespace(true); | ||
nodePrinter.print(rootNode); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write back to configuration file: " + configurationFile, e); | ||
} | ||
ConfigureJavaFormatterXml.configureJavaFormat(rootNode, uris); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...tir-java-format/src/main/groovy/com/palantir/javaformat/gradle/UpdateIntellijXmlTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.palantir.javaformat.gradle; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.io.Files; | ||
import groovy.util.Node; | ||
import groovy.util.XmlNodePrinter; | ||
import groovy.util.XmlParser; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.Charset; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.xml.sax.SAXException; | ||
|
||
public abstract class UpdateIntellijXmlTask extends DefaultTask { | ||
private final Property<File> xmlFile = getProject().getObjects().property(File.class); | ||
static final String INTELLIJ_TASK_GROUP = "intellij"; | ||
|
||
protected abstract void configure(Node rootNode); | ||
|
||
public UpdateIntellijXmlTask() { | ||
setGroup(INTELLIJ_TASK_GROUP); | ||
} | ||
|
||
@PathSensitive(PathSensitivity.RELATIVE) | ||
@OutputFile | ||
protected final Property<File> getXmlFile() { | ||
return xmlFile; | ||
} | ||
|
||
@TaskAction | ||
public final void run() { | ||
File configurationFile = getXmlFile().get(); | ||
Node rootNode; | ||
if (configurationFile.isFile()) { | ||
try { | ||
rootNode = new XmlParser().parse(configurationFile); | ||
} catch (IOException | SAXException | ParserConfigurationException e) { | ||
throw new RuntimeException("Couldn't parse existing configuration file: " + configurationFile, e); | ||
} | ||
} else { | ||
rootNode = new Node(null, "project", ImmutableMap.of("version", "4")); | ||
} | ||
|
||
configure(rootNode); | ||
|
||
try (BufferedWriter writer = Files.newWriter(configurationFile, Charset.defaultCharset()); | ||
PrintWriter printWriter = new PrintWriter(writer)) { | ||
XmlNodePrinter nodePrinter = new XmlNodePrinter(printWriter); | ||
nodePrinter.setPreserveWhitespace(true); | ||
nodePrinter.print(rootNode); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write back to configuration file: " + configurationFile, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters