Skip to content

Commit

Permalink
Recommend intellij plugin (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferozco authored and dansanduleac committed Oct 16, 2019
1 parent aabd04b commit e97832a
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 54 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import com.palantir.javaformat.java.*

buildscript {
repositories {
maven { url "https://palantir.bintray.com/releases" }
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-26.v2.yml
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
6 changes: 6 additions & 0 deletions gradle-palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ gradlePlugin {
}
}
}

idea {
module {
sourceDirs += sourceSets.main.groovy.srcDirs
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.palantir.javaformat.gradle

class ConfigureJavaFormatterXml {
static void configure(Node rootNode, List<URI> uris) {
static void configureJavaFormat(Node rootNode, List<URI> uris) {
def settings = matchOrCreateChild(rootNode, 'component', [name: 'PalantirJavaFormatSettings'])
// enable
matchOrCreateChild(settings, 'option', [name: 'enabled']).attributes().put('value', 'true')
Expand All @@ -14,6 +14,11 @@ class ConfigureJavaFormatterXml {
}
}

static void configureExternalDependencies(Node rootNode) {
def externalDependencies = matchOrCreateChild(rootNode, 'component', [name: 'ExternalDependencies'])
matchOrCreateChild(externalDependencies, 'plugin', [id: 'palantir-java-format'])
}

private static Node matchOrCreateChild(Node base, String name, Map attributes = [:], Map defaults = [:]) {
def child = base[name].find { it.attributes().entrySet().containsAll(attributes.entrySet()) }
if (child) {
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.util.stream.Collectors;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.provider.Provider;
import org.gradle.plugins.ide.idea.model.IdeaModel;
import org.jetbrains.gradle.ext.TaskTriggersConfig;

Expand Down Expand Up @@ -68,21 +70,31 @@ private static void configureLegacyIdea(Project project, Configuration implConfi
ideaModel.getProject().getIpr().withXml(xmlProvider -> {
// this block is lazy
List<URI> uris = implConfiguration.getFiles().stream().map(File::toURI).collect(Collectors.toList());
ConfigureJavaFormatterXml.configure(xmlProvider.asNode(), uris);
ConfigureJavaFormatterXml.configureJavaFormat(xmlProvider.asNode(), uris);
ConfigureJavaFormatterXml.configureExternalDependencies(xmlProvider.asNode());
});
}

private static void configureIntelliJImport(Project project, Configuration implConfiguration) {
project.getPluginManager().apply("org.jetbrains.gradle.plugin.idea-ext");

ConfigurePalantirJavaFormatXml fixPalantirJavaFormatXmlTask = project.getTasks()
.create("fixPalantirJavaFormatXml", ConfigurePalantirJavaFormatXml.class, task -> {
Provider<? extends Task> configurePalantirJavaFormatXmlTask = project.getTasks()
.register("configurePalantirJavaFormatXml", ConfigurePalantirJavaFormatXml.class, task -> {
task.getImplConfiguration().set(implConfiguration);
});

Provider<? extends Task> configurePalantirJavaFormatPluginDependencyXml = project.getTasks()
.register("configurePalantirJavaFormatPluginDependencyXml", ConfigureExternalDependenciesXml.class);

Task palantirJavaFormatIntellij = project.getTasks().create("palantirJavaFormatIntellij", task -> {
task.setDescription("Configure IntelliJ directory-based repository after importing");
task.setGroup(UpdateIntellijXmlTask.INTELLIJ_TASK_GROUP);
task.dependsOn(configurePalantirJavaFormatXmlTask, configurePalantirJavaFormatPluginDependencyXml);
});

ExtensionAware ideaProject = (ExtensionAware) project.getExtensions().getByType(IdeaModel.class).getProject();
ExtensionAware settings = (ExtensionAware) ideaProject.getExtensions().getByName("settings");
TaskTriggersConfig taskTriggers = settings.getExtensions().getByType(TaskTriggersConfig.class);
taskTriggers.afterSync(fixPalantirJavaFormatXmlTask);
taskTriggers.beforeSync(palantirJavaFormatIntellij);
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ConfigureJavaFormatterXmlTest extends Specification {
def node = new XmlParser().parseText(MISSING_ENTIRE_BLOCK)

when:
ConfigureJavaFormatterXml.configure(node, ['foo', 'bar'].collect { URI.create(it) })
ConfigureJavaFormatterXml.configureJavaFormat(node, ['foo', 'bar'].collect { URI.create(it) })

then:
xmlToString(node) == EXPECTED
Expand All @@ -60,7 +60,7 @@ class ConfigureJavaFormatterXmlTest extends Specification {
def node = new XmlParser().parseText(MISSING_CLASS_PATH)

when:
ConfigureJavaFormatterXml.configure(node, ['foo', 'bar'].collect { URI.create(it) })
ConfigureJavaFormatterXml.configureJavaFormat(node, ['foo', 'bar'].collect { URI.create(it) })

then:
xmlToString(node) == """\
Expand All @@ -83,7 +83,7 @@ class ConfigureJavaFormatterXmlTest extends Specification {
def node = new XmlParser().parseText(EXISTING_CLASS_PATH)

when:
ConfigureJavaFormatterXml.configure(node, ['foo', 'bar'].collect { URI.create(it) })
ConfigureJavaFormatterXml.configureJavaFormat(node, ['foo', 'bar'].collect { URI.create(it) })

then:
xmlToString(node) == EXPECTED
Expand Down

0 comments on commit e97832a

Please sign in to comment.