Skip to content
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

Add ktfmt option to KotlinGradleExtension #583

Merged
merged 3 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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