Skip to content

Convert GJF to compile only dependency #1630

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

Merged
merged 15 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changes
* **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))

## [2.37.0] - 2023-03-13
### Added
Expand Down
16 changes: 15 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ apply from: rootProject.file('gradle/java-publish.gradle')
def NEEDS_GLUE = [
'sortPom',
'palantirJavaFormat',
'googleJavaFormat',
'ktfmt',
'ktlint',
'flexmark',
Expand All @@ -20,11 +21,21 @@ def NEEDS_GLUE = [
'cleanthat'
]
for (glue in NEEDS_GLUE) {
sourceSets.register(glue) {
// main glue
def mainGlue = sourceSets.register(glue) {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
java {}
}
// test glue
sourceSets.register("test${glue.capitalize()}") {
compileClasspath += mainGlue.get().compileClasspath + mainGlue.get().output + sourceSets.test.output
runtimeClasspath += mainGlue.get().compileClasspath + mainGlue.get().output + sourceSets.test.output
java {}
}
configurations.named("test${glue.capitalize()}Implementation").configure {
extendsFrom(configurations.named("testImplementation").get())
}
}

versionCompatibility {
Expand Down Expand Up @@ -70,6 +81,9 @@ dependencies {

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

googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.8' // minimum required version due to api changes before then
testGoogleJavaFormatImplementation "com.diffplug.durian:durian-core:$VER_DURIAN"

// used jackson-based formatters
jacksonCompileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
jacksonCompileOnly 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.java;

import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug;

import java.util.Objects;

import javax.annotation.Nonnull;

import com.google.googlejavaformat.java.Formatter;
import com.google.googlejavaformat.java.FormatterException;
import com.google.googlejavaformat.java.ImportOrderer;
import com.google.googlejavaformat.java.JavaFormatterOptions;
import com.google.googlejavaformat.java.RemoveUnusedImports;
import com.google.googlejavaformat.java.StringWrapper;

import com.diffplug.spotless.FormatterFunc;

public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

@Nonnull
private final Formatter formatter;

@Nonnull
private final String version;
@Nonnull
private final JavaFormatterOptions.Style formatterStyle;
private final boolean reflowStrings;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
this.version = Objects.requireNonNull(version);
this.formatterStyle = JavaFormatterOptions.Style.valueOf(Objects.requireNonNull(style));
this.reflowStrings = reflowStrings;

this.formatter = new Formatter(JavaFormatterOptions.builder()
.style(formatterStyle)
.build());
}

@Override
@Nonnull
public String apply(@Nonnull String input) throws Exception {
String formatted = formatter.formatSource(input);
String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted);
String sortedImports = ImportOrderer.reorderImports(removedUnused, formatterStyle);
String reflowedLongStrings = reflowLongStrings(sortedImports);
return fixWindowsBug(reflowedLongStrings, version);
}

private String reflowLongStrings(String input) throws FormatterException {
if (reflowStrings) {
return StringWrapper.wrap(input, formatter);
} else {
return input;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.java;

import static com.diffplug.spotless.glue.java.GoogleJavaFormatUtils.fixWindowsBug;

import java.util.Objects;

import javax.annotation.Nonnull;

import com.google.googlejavaformat.java.RemoveUnusedImports;

import com.diffplug.spotless.FormatterFunc;

public class GoogleJavaFormatRemoveUnusedImporterFormatterFunc implements FormatterFunc {

@Nonnull
private final String version;

public GoogleJavaFormatRemoveUnusedImporterFormatterFunc(@Nonnull String version) {
this.version = Objects.requireNonNull(version);
}

@Override
@Nonnull
public String apply(@Nonnull String input) throws Exception {
String removedUnused = RemoveUnusedImports.removeUnusedImports(input);
return fixWindowsBug(removedUnused, version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.java;

import com.diffplug.spotless.LineEnding;

public class GoogleJavaFormatUtils {

private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n");

/**
* google-java-format-1.1's removeUnusedImports does *wacky* stuff on Windows.
* The beauty of normalizing all line endings to unix!
*/
static String fixWindowsBug(String input, String version) {
if (IS_WINDOWS && version.equals("1.1")) {
int firstImport = input.indexOf("\nimport ");
if (firstImport == 0) {
return input;
} else if (firstImport > 0) {
int numToTrim = 0;
char prevChar;
do {
++numToTrim;
prevChar = input.charAt(firstImport - numToTrim);
} while (Character.isWhitespace(prevChar) && (firstImport - numToTrim) > 0);
if (firstImport - numToTrim == 0) {
// import was the very first line, and we'd like to maintain a one-line gap
++numToTrim;
} else if (prevChar == ';' || prevChar == '/') {
// import came after either license or a package declaration
--numToTrim;
}
if (numToTrim > 0) {
return input.substring(0, firstImport - numToTrim + 2) + input.substring(firstImport + 1);
}
}
}
return input;
}
}
Loading