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

Less checkstyle.xml when palantir-java-format is enabled #987

Merged
merged 2 commits into from
Oct 21, 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-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