Skip to content

Commit 8b24b22

Browse files
committed
use compile-only sourceset for gjf
1 parent a92c7b0 commit 8b24b22

File tree

7 files changed

+258
-173
lines changed

7 files changed

+258
-173
lines changed

lib/build.gradle

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apply from: rootProject.file('gradle/java-publish.gradle')
1010
def NEEDS_GLUE = [
1111
'sortPom',
1212
'palantirJavaFormat',
13+
'googleJavaFormat',
1314
'ktfmt',
1415
'ktlint',
1516
'flexmark',
@@ -20,11 +21,21 @@ def NEEDS_GLUE = [
2021
'cleanthat'
2122
]
2223
for (glue in NEEDS_GLUE) {
23-
sourceSets.register(glue) {
24+
// main glue
25+
def mainGlue = sourceSets.register(glue) {
2426
compileClasspath += sourceSets.main.output
2527
runtimeClasspath += sourceSets.main.output
2628
java {}
2729
}
30+
// test glue
31+
sourceSets.register("test${glue.capitalize()}") {
32+
compileClasspath += mainGlue.get().compileClasspath + mainGlue.get().output + sourceSets.test.output
33+
runtimeClasspath += mainGlue.get().compileClasspath + mainGlue.get().output + sourceSets.test.output
34+
java {}
35+
}
36+
configurations.named("test${glue.capitalize()}Implementation").configure {
37+
extendsFrom(configurations.named("testImplementation").get())
38+
}
2839
}
2940

3041
versionCompatibility {
@@ -70,6 +81,9 @@ dependencies {
7081

7182
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' // this version needs to stay compilable against Java 8 for CI Job testNpm
7283

84+
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.8' // minimum required version for jdk 11
85+
testGoogleJavaFormatImplementation "com.diffplug.durian:durian-core:$VER_DURIAN"
86+
7387
// used jackson-based formatters
7488
jacksonCompileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
7589
jacksonCompileOnly 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2'
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.java;
17+
18+
import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug;
19+
20+
import java.util.Objects;
21+
22+
import javax.annotation.Nonnull;
23+
24+
import com.google.googlejavaformat.java.Formatter;
25+
import com.google.googlejavaformat.java.FormatterException;
26+
import com.google.googlejavaformat.java.ImportOrderer;
27+
import com.google.googlejavaformat.java.JavaFormatterOptions;
28+
import com.google.googlejavaformat.java.RemoveUnusedImports;
29+
import com.google.googlejavaformat.java.StringWrapper;
30+
31+
import com.diffplug.spotless.FormatterFunc;
32+
33+
public class GoogleJavaFormatFormatterFunc implements FormatterFunc {
34+
35+
@Nonnull
36+
private final Formatter formatter;
37+
38+
@Nonnull
39+
private final String version;
40+
@Nonnull
41+
private final JavaFormatterOptions.Style formatterStyle;
42+
private final boolean reflowStrings;
43+
44+
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
45+
this.version = Objects.requireNonNull(version);
46+
this.formatterStyle = JavaFormatterOptions.Style.valueOf(Objects.requireNonNull(style));
47+
this.reflowStrings = reflowStrings;
48+
49+
this.formatter = new Formatter(JavaFormatterOptions.builder()
50+
.style(formatterStyle)
51+
.build());
52+
}
53+
54+
@Override
55+
@Nonnull
56+
public String apply(@Nonnull String input) throws Exception {
57+
String formatted = formatter.formatSource(input);
58+
String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted);
59+
String sortedImports = ImportOrderer.reorderImports(removedUnused, formatterStyle);
60+
String reflowedLongStrings = reflowLongStrings(sortedImports);
61+
return fixWindowsBug(reflowedLongStrings, version);
62+
}
63+
64+
private String reflowLongStrings(String input) throws FormatterException {
65+
if (reflowStrings) {
66+
return StringWrapper.wrap(input, formatter);
67+
} else {
68+
return input;
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.java;
17+
18+
import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug;
19+
20+
import java.util.Objects;
21+
22+
import javax.annotation.Nonnull;
23+
24+
import com.google.googlejavaformat.java.RemoveUnusedImports;
25+
26+
import com.diffplug.spotless.FormatterFunc;
27+
28+
public class GoogleJavaFormatRemoveUnusedImporterFormatterFunc implements FormatterFunc {
29+
30+
@Nonnull
31+
private final String version;
32+
33+
public GoogleJavaFormatRemoveUnusedImporterFormatterFunc(@Nonnull String version) {
34+
this.version = Objects.requireNonNull(version);
35+
}
36+
37+
@Override
38+
@Nonnull
39+
public String apply(@Nonnull String input) throws Exception {
40+
String removedUnused = RemoveUnusedImports.removeUnusedImports(input);
41+
return fixWindowsBug(removedUnused, version);
42+
}
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.java;
17+
18+
import com.diffplug.spotless.LineEnding;
19+
20+
public class GoogleJavaFormatUtils {
21+
22+
private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n");
23+
24+
/**
25+
* google-java-format-1.1's removeUnusedImports does *wacky* stuff on Windows.
26+
* The beauty of normalizing all line endings to unix!
27+
*/
28+
static String fixWindowsBug(String input, String version) {
29+
if (IS_WINDOWS && version.equals("1.1")) {
30+
int firstImport = input.indexOf("\nimport ");
31+
if (firstImport == 0) {
32+
return input;
33+
} else if (firstImport > 0) {
34+
int numToTrim = 0;
35+
char prevChar;
36+
do {
37+
++numToTrim;
38+
prevChar = input.charAt(firstImport - numToTrim);
39+
} while (Character.isWhitespace(prevChar) && (firstImport - numToTrim) > 0);
40+
if (firstImport - numToTrim == 0) {
41+
// import was the very first line, and we'd like to maintain a one-line gap
42+
++numToTrim;
43+
} else if (prevChar == ';' || prevChar == '/') {
44+
// import came after either license or a package declaration
45+
--numToTrim;
46+
}
47+
if (numToTrim > 0) {
48+
return input.substring(0, firstImport - numToTrim + 2) + input.substring(firstImport + 1);
49+
}
50+
}
51+
}
52+
return input;
53+
}
54+
}

0 commit comments

Comments
 (0)