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

Use Eclipse formatter (behind -Pcom.palantir.baseline-format.eclipse feature flag) #707

Merged
merged 11 commits into from
Jul 31, 2019
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ spotless {
importOrder ''
trimTrailingWhitespace
indentWithSpaces 4
endWithNewline

// only enabled if you run `./gradlew format -Pcom.palantir.baseline-format.eclipse`
eclipse().configFile "$rootDir/.baseline/eclipse.xml"
}
}
```

We chose the Eclipse formatter because it can be run from the command line and from both IDEs (IntelliJ using the [Eclipse Code Formatter](https://plugins.jetbrains.com/plugin/6546-eclipse-code-formatter) plugin).

## com.palantir.baseline-reproducibility

This plugin is a shorthand for the following snippet, which opts-in to reproducible behaviour for all Gradle's Jar, Tar and Zip tasks. (Surprisingly, these tasks are not reproducible by default).
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-707.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Use the Eclipse formatter in Spotless
links:
- https://github.com/palantir/gradle-baseline/pull/707
318 changes: 318 additions & 0 deletions gradle-baseline-java-config/resources/spotless/eclipse.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public void execute(Task task) {
copySpec.into(BaselineConfig.this.getConfigDir());
copySpec.exclude("**/scalastyle_config.xml");
copySpec.setIncludeEmptyDirs(false);

if (!BaselineFormat.eclipseFormattingEnabled(task.getProject())) {
copySpec.exclude("**/spotless/eclipse.xml");
}
});
if (rootProject
.getAllprojects()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.palantir.baseline.plugins;

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 @@ -25,9 +28,15 @@

class BaselineFormat extends AbstractBaselinePlugin {

// TODO(dfox): remove this feature flag when we've refined the eclipse.xml sufficiently
private static final String ECLIPSE_FORMATTING = "com.palantir.baseline-format.eclipse";

@Override
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");

Expand All @@ -46,23 +55,26 @@ public void apply(Project project) {
// use empty string to specify one group for all non-static imports
java.importOrder("");
java.trimTrailingWhitespace();
java.indentWithSpaces(4);
java.endWithNewline();

// No empty lines at start of blocks
java.replaceRegex("Block starts with blank lines", "\\) \\{\n+", ") {\n");

// No dangling parentheses - closing paren must be on the same line as the expression
java.replaceRegex("Dangling closing parenthesis", "(\n\\s+)+\\)", ")");
if (eclipseFormattingEnabled(project)) {
java.eclipse().configFile(project.file(eclipseXml.toString()));
}
});

// necessary because SpotlessPlugin creates tasks in an afterEvaluate block
Task formatTask = project.task("format");
if (eclipseFormattingEnabled(project) && !Files.exists(eclipseXml)) {
formatTask.dependsOn(project.getTasks().findByPath(":baselineUpdateConfig"));
}
project.afterEvaluate(p -> {
Task spotlessApply = project.getTasks().getByName("spotlessApply");
formatTask.dependsOn(spotlessApply);
project.getTasks().withType(JavaCompile.class).configureEach(spotlessApply::mustRunAfter);
});
});
}

static boolean eclipseFormattingEnabled(Project project) {
return project.hasProperty(ECLIPSE_FORMATTING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.gradle.testkit.runner.TaskOutcome
* This test relies on running ./gradlew :gradle-baseline-java-config:publishToMavenLocal.
*/
class BaselineConfigIntegrationTest extends AbstractPluginTest {
def projectVersion = "git describe --tags --first-parent --dirty=.dirty".execute().text.trim()
def projectVersion = "git describe --tags --first-parent --dirty=.dirty --abbrev=7".execute().text.trim()
def standardBuildFile = """
plugins {
id 'com.palantir.baseline-config'
Expand All @@ -46,7 +46,9 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest {

then:
with('--stacktrace', '--info', 'baselineUpdateConfig').build()
directory('.baseline').list().toList().toSet() == ['checkstyle', 'copyright', 'eclipse', 'idea'].toSet()
directory('.baseline').list().toList().toSet() == [
'checkstyle', 'copyright', 'eclipse', 'idea', 'spotless'
].toSet()
directory('project').list().toList().isEmpty()
}

Expand All @@ -65,7 +67,9 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest {

then:
with('--stacktrace', '--info', 'baselineUpdateConfig').build()
directory('.baseline').list().toList().toSet() == ['checkstyle', 'copyright', 'eclipse', 'idea'].toSet()
directory('.baseline').list().toList().toSet() == [
'checkstyle', 'copyright', 'eclipse', 'idea', 'spotless'
].toSet()
directory('project').list().toList().toSet() == ['scalastyle_config.xml'].toSet()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

package com.palantir.baseline

import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome

class BaselineFormatIntegrationTest extends AbstractPluginTest {

def setup() {
FileUtils.copyDirectory(
new File("../gradle-baseline-java-config/resources"),
new File(projectDir, ".baseline"))
}

def standardBuildFile = '''
plugins {
id 'java'
Expand All @@ -36,14 +43,17 @@ class BaselineFormatIntegrationTest extends AbstractPluginTest {

def validJavaFile = '''
package test;
public class Test { void test() {
int x = 1;
System.out.println(
"Hello");
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
} }

public class Test {
void test() {
int x = 1;
System.out.println(
"Hello");
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
}
}
'''.stripIndent()

def invalidJavaFile = '''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class BaselineFormatTest extends Specification {

def setup() {
project = ProjectBuilder.builder().build()
project.buildscript {
repositories {
mavenCentral()
}
}
project.plugins.apply 'java'
project.plugins.apply BaselineFormat
project.evaluate()
Expand Down