Skip to content

Commit

Permalink
Merge pull request #583 from andrewparmet/ktfmt-kotlin-gradle-extension
Browse files Browse the repository at this point in the history
Add ktfmt option to KotlinGradleExtension
  • Loading branch information
nedtwigg authored May 28, 2020
2 parents aff9d66 + 325f697 commit d1fb248
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

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

## [4.0.1] - 2020-05-21
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.diffplug.common.collect.ImmutableSortedMap;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.kotlin.KtLintStep;
import com.diffplug.spotless.kotlin.KtfmtStep;

public class KotlinGradleExtension extends FormatExtension {
private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts";
Expand Down Expand Up @@ -65,6 +66,33 @@ private FormatterStep createStep() {
}
}

/** Uses the [ktfmt](https://github.com/facebookincubator/ktfmt) jar to format source code. */
public KtfmtConfig ktfmt() {
return ktfmt(KtfmtStep.defaultVersion());
}

/**
* Uses the given version of [ktfmt](https://github.com/facebookincubator/ktfmt) to format source
* code.
*/
public KtfmtConfig ktfmt(String version) {
Objects.requireNonNull(version);
return new KtfmtConfig(version);
}

public class KtfmtConfig {
final String version;

KtfmtConfig(String version) {
this.version = Objects.requireNonNull(version);
addStep(createStep());
}

private FormatterStep createStep() {
return KtfmtStep.create(version, GradleProvisioner.fromProject(getProject()));
}
}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.gradle.testkit.runner.BuildResult;
import org.junit.Test;

import com.diffplug.spotless.JreVersion;

public class KotlinGradleExtensionTest extends GradleIntegrationTest {
@Test
public void integration() throws IOException {
Expand Down Expand Up @@ -109,6 +111,28 @@ public void indentStep() throws IOException {
assertThat(result.getOutput()).contains("Unexpected indentation (4) (it should be 6)");
}

@Test
public void integration_ktfmt() throws IOException {
if (JreVersion.thisVm() == JreVersion._8) {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
return;
}
setFile("build.gradle").toLines(
"plugins {",
" id 'nebula.kotlin' version '1.0.6'",
" id 'com.diffplug.gradle.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlinGradle {",
" ktfmt()",
" }",
"}");
setFile("configuration.gradle.kts").toResource("kotlin/ktfmt/basic.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktfmt/basic.clean");
}

@Test
public void integration_lint_script_files_without_top_level_declaration() throws IOException {
setFile("build.gradle").toLines(
Expand Down

0 comments on commit d1fb248

Please sign in to comment.