Skip to content

Commit

Permalink
Implement native cmd for gradle plugin (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Oct 12, 2021
2 parents d21a7bc + b844080 commit f22977d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}} |',
Expand Down Expand Up @@ -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: |
Expand Down
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 15 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
```
<a name="license-header-options"></a>
## License header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 2 additions & 0 deletions testlib/src/main/resources/native_cmd/clean.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
replaced foo
bar replaced
2 changes: 2 additions & 0 deletions testlib/src/main/resources/native_cmd/dirty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
placeholder foo
bar placeholder

0 comments on commit f22977d

Please sign in to comment.