Skip to content

Commit 14122d0

Browse files
committed
Add Gradle compatibility
1 parent a5c8b4d commit 14122d0

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
3535
import com.diffplug.spotless.extra.java.EclipseJdtFormatterStep;
3636
import com.diffplug.spotless.generic.LicenseHeaderStep;
37+
import com.diffplug.spotless.java.CleanthatJavaStep;
3738
import com.diffplug.spotless.java.FormatAnnotationsStep;
3839
import com.diffplug.spotless.java.GoogleJavaFormatStep;
3940
import com.diffplug.spotless.java.ImportOrderStep;
@@ -270,6 +271,77 @@ private FormatterStep createStep() {
270271
}
271272
}
272273

274+
/** Apply CleanThat refactoring rules. */
275+
public CleanthatJavaConfig cleanthat() {
276+
return new CleanthatJavaConfig();
277+
}
278+
279+
public class CleanthatJavaConfig {
280+
private String groupArtifact = CleanthatJavaStep.defaultGroupArtifact();
281+
282+
private String version = CleanthatJavaStep.defaultVersion();
283+
284+
private String sourceJdk = CleanthatJavaStep.defaultJdkVersion();
285+
286+
private List<String> mutators = CleanthatJavaStep.defaultMutators();
287+
288+
private List<String> excludedMutators = CleanthatJavaStep.defaultExcludedMutators();
289+
290+
CleanthatJavaConfig() {
291+
addStep(createStep());
292+
}
293+
294+
public CleanthatJavaConfig groupArtifact(String groupArtifact) {
295+
Objects.requireNonNull(groupArtifact);
296+
this.groupArtifact = groupArtifact;
297+
replaceStep(createStep());
298+
return this;
299+
}
300+
301+
public CleanthatJavaConfig version(String version) {
302+
Objects.requireNonNull(version);
303+
this.version = version;
304+
replaceStep(createStep());
305+
return this;
306+
}
307+
308+
public CleanthatJavaConfig sourceJdk(String sourceJdk) {
309+
Objects.requireNonNull(sourceJdk);
310+
this.sourceJdk = sourceJdk;
311+
replaceStep(createStep());
312+
return this;
313+
}
314+
315+
// Especially useful to clear default mutators
316+
public CleanthatJavaConfig clearMutators() {
317+
this.mutators.clear();
318+
replaceStep(createStep());
319+
return this;
320+
}
321+
322+
// The fully qualified name of a class implementing eu.solven.cleanthat.engine.java.refactorer.meta.IMutator
323+
// or '*' to include all default mutators
324+
public CleanthatJavaConfig addMutator(String mutator) {
325+
this.mutators.add(mutator);
326+
replaceStep(createStep());
327+
return this;
328+
}
329+
330+
// useful to exclude a mutator amongst the default list of mutators
331+
public CleanthatJavaConfig excludeMutator(String mutator) {
332+
this.excludedMutators.add(mutator);
333+
replaceStep(createStep());
334+
return this;
335+
}
336+
337+
private FormatterStep createStep() {
338+
return CleanthatJavaStep.create(
339+
groupArtifact,
340+
version,
341+
sourceJdk, mutators, excludedMutators, provisioner());
342+
}
343+
}
344+
273345
/** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */
274346
@Override
275347
protected void setupTask(SpotlessTask task) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2022-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.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class CleanthatJavaIntegrationTest extends GradleIntegrationHarness {
23+
@Test
24+
void integration() throws IOException {
25+
setFile("build.gradle").toLines(
26+
"plugins {",
27+
" id 'com.diffplug.spotless'",
28+
"}",
29+
"repositories { mavenCentral() }",
30+
"",
31+
"spotless {",
32+
" java {",
33+
" target file('test.java')",
34+
" cleanthat().sourceJdk('11')",
35+
" }",
36+
"}");
37+
38+
setFile("test.java").toResource("java/cleanthat/MultipleMutators.dirty.java");
39+
gradleRunner().withArguments("spotlessApply").build();
40+
assertFile("test.java").sameAsResource("java/cleanthat/MultipleMutators.clean.java");
41+
}
42+
}

0 commit comments

Comments
 (0)