Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

baseline-idea: configures IntelliJ EclipseCodeFormatter plugin #794

Merged
merged 6 commits into from
Sep 9, 2019
Merged
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-794.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: Automatically configure the [Intellij Eclipse format plugin](https://plugins.jetbrains.com/plugin/6546-eclipse-code-formatter)
to use the eclipse formatter
links:
- https://github.com/palantir/gradle-baseline/pull/794
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.diffplug.gradle.spotless.SpotlessExtension;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.ConfigurableFileCollection;
Expand All @@ -35,10 +34,9 @@ class BaselineFormat extends AbstractBaselinePlugin {
public void apply(Project project) {
this.project = project;

Path eclipseXml = Paths.get(getConfigDir(), "spotless/eclipse.xml");

project.getPluginManager().withPlugin("java", plugin -> {
project.getPluginManager().apply("com.diffplug.gradle.spotless");
Path eclipseXml = eclipseConfigFile(project);

project.getExtensions().getByType(SpotlessExtension.class).java(java -> {
// Configure a lazy FileCollection then pass it as the target
Expand Down Expand Up @@ -78,4 +76,8 @@ public void apply(Project project) {
static boolean eclipseFormattingEnabled(Project project) {
return project.hasProperty(ECLIPSE_FORMATTING);
}

static Path eclipseConfigFile(Project project) {
return project.getRootDir().toPath().resolve(".baseline/spotless/eclipse.xml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.palantir.baseline.plugins
import groovy.transform.CompileStatic
import groovy.xml.XmlUtil
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.transport.URIish
Expand All @@ -27,7 +28,6 @@ import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileTreeElement
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.plugins.ide.idea.GenerateIdeaModule
import org.gradle.plugins.ide.idea.GenerateIdeaProject
import org.gradle.plugins.ide.idea.GenerateIdeaWorkspace
Expand All @@ -51,6 +51,7 @@ class BaselineIdea extends AbstractBaselinePlugin {
addCodeStyle(node)
addCopyright(node)
addCheckstyle(node)
addEclipseFormat(node)
addGit(node)
addInspectionProjectProfile(node)
addJavacSettings(node)
Expand Down Expand Up @@ -136,6 +137,41 @@ class BaselineIdea extends AbstractBaselinePlugin {
copyrightManager.@default = lastFileName
}

private void addEclipseFormat(node) {
def baselineFormat = project.plugins.findPlugin(BaselineFormat)
if (baselineFormat == null) {
project.logger.debug "Baseline: Skipping IDEA eclipse format configuration since baseline-format not applied"
return
}

if (!BaselineFormat.eclipseFormattingEnabled(project)) {
project.logger.debug "Baseline: Not configuring EclipseCodeFormatter because com.palantir.baseline-format.eclipse is not enabled in gradle.properties"
return;
}

Path formatterConfig = BaselineFormat.eclipseConfigFile(project)
if (!Files.exists(formatterConfig)) {
project.logger.warn "Please run ./gradlew baselineUpdateConfig to create eclipse formatter config: " + formatterConfig
return
}

project.logger.debug "Baseline: Configuring EclipseCodeFormatter plugin for Idea"
node.append(new XmlParser().parseText("""
<component name="EclipseCodeFormatterProjectSettings">
<option name="projectSpecificProfile">
<ProjectSpecificProfile>
<option name="formatter" value="ECLIPSE" />
<option name="importOrder" value="" />
<option name="pathToConfigFileJava" value="\$PROJECT_DIR\$/.baseline/spotless/eclipse.xml" />
<option name="selectedJavaProfile" value="PalantirStyle" />
</ProjectSpecificProfile>
</option>
</component>
"""))
def externalDependencies = matchOrCreateChild(node, 'component', [name: 'ExternalDependencies'])
matchOrCreateChild(externalDependencies, 'plugin', [id: 'EclipseCodeFormatter'])
}

private void addCheckstyle(node) {
def checkstyle = project.plugins.findPlugin(BaselineCheckstyle)
if (checkstyle == null) {
Expand All @@ -161,7 +197,6 @@ class BaselineIdea extends AbstractBaselinePlugin {
</option>
</component>
""".stripIndent()))

def externalDependencies = matchOrCreateChild(node, 'component', [name: 'ExternalDependencies'])
matchOrCreateChild(externalDependencies, 'plugin', [id: 'CheckStyle-IDEA'])
}
Expand Down