Skip to content

Commit d1fb248

Browse files
authored
Merge pull request #583 from andrewparmet/ktfmt-kotlin-gradle-extension
Add ktfmt option to KotlinGradleExtension
2 parents aff9d66 + 325f697 commit d1fb248

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Diff for: plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
* Support for ktfmt in KotlinGradleExtension ([#583](https://github.com/diffplug/spotless/pull/583))
67

78
## [4.0.1] - 2020-05-21
89
### Fixed

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java

+28
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.diffplug.common.collect.ImmutableSortedMap;
2323
import com.diffplug.spotless.FormatterStep;
2424
import com.diffplug.spotless.kotlin.KtLintStep;
25+
import com.diffplug.spotless.kotlin.KtfmtStep;
2526

2627
public class KotlinGradleExtension extends FormatExtension {
2728
private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts";
@@ -65,6 +66,33 @@ private FormatterStep createStep() {
6566
}
6667
}
6768

69+
/** Uses the [ktfmt](https://github.com/facebookincubator/ktfmt) jar to format source code. */
70+
public KtfmtConfig ktfmt() {
71+
return ktfmt(KtfmtStep.defaultVersion());
72+
}
73+
74+
/**
75+
* Uses the given version of [ktfmt](https://github.com/facebookincubator/ktfmt) to format source
76+
* code.
77+
*/
78+
public KtfmtConfig ktfmt(String version) {
79+
Objects.requireNonNull(version);
80+
return new KtfmtConfig(version);
81+
}
82+
83+
public class KtfmtConfig {
84+
final String version;
85+
86+
KtfmtConfig(String version) {
87+
this.version = Objects.requireNonNull(version);
88+
addStep(createStep());
89+
}
90+
91+
private FormatterStep createStep() {
92+
return KtfmtStep.create(version, GradleProvisioner.fromProject(getProject()));
93+
}
94+
}
95+
6896
@Override
6997
protected void setupTask(SpotlessTask task) {
7098
if (target == null) {

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.gradle.testkit.runner.BuildResult;
2323
import org.junit.Test;
2424

25+
import com.diffplug.spotless.JreVersion;
26+
2527
public class KotlinGradleExtensionTest extends GradleIntegrationTest {
2628
@Test
2729
public void integration() throws IOException {
@@ -109,6 +111,28 @@ public void indentStep() throws IOException {
109111
assertThat(result.getOutput()).contains("Unexpected indentation (4) (it should be 6)");
110112
}
111113

114+
@Test
115+
public void integration_ktfmt() throws IOException {
116+
if (JreVersion.thisVm() == JreVersion._8) {
117+
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
118+
return;
119+
}
120+
setFile("build.gradle").toLines(
121+
"plugins {",
122+
" id 'nebula.kotlin' version '1.0.6'",
123+
" id 'com.diffplug.gradle.spotless'",
124+
"}",
125+
"repositories { mavenCentral() }",
126+
"spotless {",
127+
" kotlinGradle {",
128+
" ktfmt()",
129+
" }",
130+
"}");
131+
setFile("configuration.gradle.kts").toResource("kotlin/ktfmt/basic.dirty");
132+
gradleRunner().withArguments("spotlessApply").build();
133+
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktfmt/basic.clean");
134+
}
135+
112136
@Test
113137
public void integration_lint_script_files_without_top_level_declaration() throws IOException {
114138
setFile("build.gradle").toLines(

0 commit comments

Comments
 (0)