From d2f34aeebb3fc19cca754496b628e6229d9a7903 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Mon, 11 Oct 2021 17:16:21 +0200 Subject: [PATCH 1/3] Make NativeCmd step available in Gradle plugin Maven support had been added in #949. --- .../gradle/spotless/FormatExtension.java | 6 +++ .../spotless/NativeCmdIntegrationTest.java | 45 +++++++++++++++++++ .../src/main/resources/native_cmd/clean.txt | 2 + .../src/main/resources/native_cmd/dirty.txt | 2 + 4 files changed, 55 insertions(+) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NativeCmdIntegrationTest.java create mode 100644 testlib/src/main/resources/native_cmd/clean.txt create mode 100644 testlib/src/main/resources/native_cmd/dirty.txt diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index a30f4edeb8..92b9e20f3a 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -53,6 +53,7 @@ import com.diffplug.spotless.generic.IndentStep; import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode; +import com.diffplug.spotless.generic.NativeCmdStep; import com.diffplug.spotless.generic.PipeStepPair; import com.diffplug.spotless.generic.ReplaceRegexStep; import com.diffplug.spotless.generic.ReplaceStep; @@ -394,6 +395,11 @@ public void indentWithTabs() { addStep(IndentStep.Type.TAB.create()); } + /** Ensures formatting of files via native binary. */ + public void nativeCmd(String name, String pathToExe, List arguments) { + addStep(NativeCmdStep.create(name, new File(pathToExe), arguments)); + } + /** * Created by {@link FormatExtension#licenseHeader(String, String)} or {@link FormatExtension#licenseHeaderFile(Object, String)}. * For most language-specific formats (e.g. java, scala, etc.) you can omit the second {@code delimiter} argument, because it is supplied diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NativeCmdIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NativeCmdIntegrationTest.java new file mode 100644 index 0000000000..a4f8d6205c --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NativeCmdIntegrationTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2021 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.gradle.spotless; + +import static org.assertj.core.api.Assumptions.assumeThat; + +import java.io.File; +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +class NativeCmdIntegrationTest extends GradleIntegrationHarness { + @Test + void nativeCmd() throws IOException { + // This will only work if /usr/bin/sed is available + assumeThat(new File("/usr/bin/sed")).exists(); + + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " format 'test', {", + " target '**/*.txt'", + " nativeCmd('sed', '/usr/bin/sed', ['s/placeholder/replaced/g'])", + " }", + "}"); + setFile("test.txt").toResource("native_cmd/dirty.txt"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("test.txt").sameAsResource("native_cmd/clean.txt"); + } +} diff --git a/testlib/src/main/resources/native_cmd/clean.txt b/testlib/src/main/resources/native_cmd/clean.txt new file mode 100644 index 0000000000..fc38ad97dd --- /dev/null +++ b/testlib/src/main/resources/native_cmd/clean.txt @@ -0,0 +1,2 @@ +replaced foo +bar replaced diff --git a/testlib/src/main/resources/native_cmd/dirty.txt b/testlib/src/main/resources/native_cmd/dirty.txt new file mode 100644 index 0000000000..0792f35aca --- /dev/null +++ b/testlib/src/main/resources/native_cmd/dirty.txt @@ -0,0 +1,2 @@ +placeholder foo +bar placeholder From 7ad985390be981022a650b24a122c9f5a5477563 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Mon, 11 Oct 2021 17:27:30 +0200 Subject: [PATCH 2/3] Update docs regarding native cmd for gradle plugin --- README.md | 4 ++-- plugin-gradle/README.md | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1efb4f0247..c9119fd0ca 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('generic.Jsr223Step') +'{{no}} | {{yes}} | {{no}} | {{no}} |', lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', -lib('generic.NativeCmdStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |', +lib('generic.NativeCmdStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', @@ -87,7 +87,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`generic.Jsr223Step`](lib/src/main/java/com/diffplug/spotless/generic/Jsr223Step.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: | | [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :+1: | :white_large_square: | -| [`generic.NativeCmdStep`](lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: | +| [`generic.NativeCmdStep`](lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 078e4eff15..cffce71ffd 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -77,6 +77,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [eclipse web tools platform](#eclipse-web-tools-platform) - css, html, js, json, xml - **Language independent** + - [Generic steps](#generic-steps) - [License header](#license-header) ([slurp year from git](#retroactively-slurp-years-from-git-history)) - [How can I enforce formatting gradually? (aka "ratchet")](#ratchet) - [`spotless:off` and `spotless:on`](#spotlessoff-and-spotlesson) @@ -715,6 +716,20 @@ Unlike Eclipse, Spotless WTP ignores per default external URIs in schema locatio external entities. To allow the access of external URIs, set the property `resolveExternalURI` to true. +## Generic steps + +[Prettier](#prettier), [eclipse wtp](#eclipse-web-tools-platform), and [license header](#license-header) are available in every format, and they each have their own section. As mentioned in the [quickstart](#quickstart), there are a variety of simple generic steps which are also available in every format, here are examples of these: + +```gradle +spotless { + // run a native binary + format 'terraform', { + target 'src/**/*.tf', 'src/**/*.tfvars' // you have to set the target manually + nativeCmd('terraform', '/opt/homebrew/bin/terraform', ['fmt', '-']) // name, path to binary, additional arguments + } +} +``` + ## License header From b844080b0e876764ecdf510cb14e9629d716cc99 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Mon, 11 Oct 2021 17:39:18 +0200 Subject: [PATCH 3/3] Update changelog regardig gradle plugin native cmd --- plugin-gradle/CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index aa5878dfe6..5a8b72975f 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* Added support for calling local binary formatters ([#963](https://github.com/diffplug/spotless/pull/963)) + ### Fixed * [module-info formatting](https://github.com/diffplug/spotless/pull/958) in `eclipse-jdt` versions `4.20` and `4.21`. Note that the problem also affects older versions.