Skip to content

Commit 4dd5b5f

Browse files
author
Dimitrii Lipiridi
committed
feat: Introduce "spotlessIdeHook" argument for maven plugin
1 parent 060a202 commit 4dd5b5f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
66
### Added
77
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
88
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
9+
* `-DspotlessIdeHook` provides the ability to apply Spotless exclusively to a specified file. It accepts the absolute path of the file.
910
### Fixed
1011
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
1112
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))

plugin-maven/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,14 +1601,23 @@ By default, `spotless:check` is bound to the `verify` phase. You might want to
16011601

16021602
## Can I apply Spotless to specific files?
16031603

1604-
You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns:
1604+
There are two options:
16051605

1606+
### Pattern
1607+
You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns:
16061608
```
1607-
cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pattern.java
1609+
cmd> mvn spotless:apply -DspotlessFiles="my/file/pattern\.java, more/generic/.*-pattern\.java"
16081610
```
1609-
16101611
The patterns are matched using `String#matches(String)` against the absolute file path.
16111612

1613+
### IDE Hook
1614+
You can specify a single file by providing the `spotlessIdeHook` argument, which accepts the absolute path of the file.
1615+
```
1616+
cmd> mvn spotless:apply -DspotlessIdeHook="C:\my-project\src\main\java\com\example\Application.java"
1617+
1618+
```
1619+
This option ignores the ratchetFrom property.
1620+
16121621
<a name="examples"></a>
16131622

16141623
## Example configurations (from real-world projects)

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
187187
@Parameter(property = "spotlessFiles")
188188
private String filePatterns;
189189

190+
@Parameter(property = "spotlessIdeHook")
191+
private String spotlessIdeHook;
192+
190193
@Parameter(property = LicenseHeaderStep.spotlessSetLicenseHeaderYearsFromGitHistory)
191194
private String setLicenseHeaderYearsFromGitHistory;
192195

@@ -251,6 +254,10 @@ private boolean shouldSkip() {
251254
}
252255

253256
private List<File> collectFiles(FormatterFactory formatterFactory, FormatterConfig config) {
257+
if (!(spotlessIdeHook == null || spotlessIdeHook.isEmpty())) {
258+
return fetchFileFromIdeHook();
259+
}
260+
254261
Optional<String> ratchetFrom = formatterFactory.ratchetFrom(config);
255262
try {
256263
final List<File> files;
@@ -279,6 +286,15 @@ private List<File> collectFiles(FormatterFactory formatterFactory, FormatterConf
279286
}
280287
}
281288

289+
private List<File> fetchFileFromIdeHook() {
290+
File file = new File(spotlessIdeHook);
291+
if (!file.isAbsolute()) {
292+
throw new PluginException("Argument passed to spotlessIdeHook must be an absolute path");
293+
}
294+
295+
return List.of(file);
296+
}
297+
282298
private List<File> collectFilesFromGit(FormatterFactory formatterFactory, String ratchetFrom) {
283299
MatchPatterns includePatterns = MatchPatterns.from(
284300
withNormalizedFileSeparators(getIncludes(formatterFactory)));

0 commit comments

Comments
 (0)