Skip to content

Commit

Permalink
Less checkstyle.xml when palantir-java-format is enabled (#987)
Browse files Browse the repository at this point in the history
checkstyle Indentation rule is disabled when palantir-java-format is enabled
  • Loading branch information
iamdanfox authored and bulldozer-bot[bot] committed Oct 21, 2019
1 parent 31653ea commit f124d2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-987.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: checkstyle Indentation rule is disabled when palantir-java-format is
enabled
links:
- https://github.com/palantir/gradle-baseline/pull/987
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

package com.palantir.baseline.plugins;

import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import org.gradle.api.Action;
import org.gradle.api.Project;
Expand Down Expand Up @@ -69,16 +75,39 @@ public void execute(Task task) {
+ "'baseline' configuration, found: " + configuration.getFiles());
}

Path configDir = Paths.get(BaselineConfig.this.getConfigDir());
rootProject.copy(copySpec -> {
copySpec.from(rootProject.zipTree(configuration.getSingleFile()));
copySpec.into(BaselineConfig.this.getConfigDir());
copySpec.into(configDir);
copySpec.exclude("**/scalastyle_config.xml");
copySpec.setIncludeEmptyDirs(false);

if (!BaselineFormat.eclipseFormattingEnabled(task.getProject())) {
copySpec.exclude("**/spotless/eclipse.xml");
}
});

if (BaselineFormat.palantirJavaFormatterEnabled(rootProject)) {
Path checkstyleXml = configDir.resolve("checkstyle/checkstyle.xml");

try {
String contents = new String(Files.readAllBytes(checkstyleXml), StandardCharsets.UTF_8);
String replaced = contents.replace(
" <module name=\"Indentation\"> "
+ "<!-- Java Style Guide: Block indentation: +4 spaces -->\n"
+ " <property name=\"arrayInitIndent\" value=\"8\"/>\n"
+ " <property name=\"lineWrappingIndentation\" value=\"8\"/>\n"
+ " </module>\n",
"");
Preconditions.checkState(
!contents.equals(replaced),
"Patching checkstyle.xml must make a change");
Files.write(checkstyleXml, replaced.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException("Unable to patch " + checkstyleXml, e);
}
}

if (rootProject
.getAllprojects()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest {
""".stripIndent()

then:
with('--stacktrace', '--info', 'baselineUpdateConfig', '-Pcom.palantir.baseline-format.eclipse').build()
with('--stacktrace', '--info', 'baselineUpdateConfig',
'-Pcom.palantir.baseline-format.eclipse',
'-Pcom.palantir.baseline-format.palantir-java-format').build()
directory('.baseline').list().toList().toSet() == [
'checkstyle', 'copyright', 'eclipse', 'idea', 'spotless'
].toSet()
Expand Down

0 comments on commit f124d2c

Please sign in to comment.