-
Notifications
You must be signed in to change notification settings - Fork 460
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
Make it easy to integrate native formatters #672
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4ed50e6
Bump gradle from 6.3 to 6.6
nedtwigg b3cfb03
Infrastructure for running third-party processes.
nedtwigg 7176ce4
Add BlackStep.
nedtwigg 9208477
Add gradle-integration to BlackStep.
nedtwigg 38f6ef4
Refactor part of BlackStep into ForeignExe
nedtwigg 153cd0e
Add ClangFormatStep.
nedtwigg f9a023a
Add gradle-integration to ClangFormatStep.
nedtwigg cf740ea
Add docs for clang-format to plugin-gradle.
nedtwigg 9901ab8
Add docs for python-black to plugin-gradle.
nedtwigg eceb244
Add tags for black and clang-format
nedtwigg 1b1abed
Add categories to keep clang and black out of the standard tests.
nedtwigg aa89c1f
FIx spotbugs warnings.
nedtwigg 59953e7
Improve the error messages.
nedtwigg 56f1636
Refactor ForeignExe to a better, more future-proof API.
nedtwigg 425a2a6
Improve ProcessRunner performance with reusable buffers.
nedtwigg f673500
Improve the ProcessRunner API.
nedtwigg 7f16ecc
Fix resource leak in our new native steps.
nedtwigg 7e1f261
Fix {versionFound} tags in ClangFormatStep and BlackStep.
nedtwigg 18c10f9
Refactored FormatterFunc to encourage a safer resource usage pattern.
nedtwigg 550acac
Deprecate the old FormatterFunc.Closeable.of(), and rename all its us…
nedtwigg ab212c5
Improve the ergonomics of error messages.
nedtwigg a114eb6
Update all changelogs and the contributing guide.
nedtwigg 800d1d5
Minor improvement to clang-format error messages.
nedtwigg e106d38
Add integration tests for clang-format and python/black.
nedtwigg 608e128
Merge branch 'main' into feat/foreign
nedtwigg 52453ae
Link the changelogs to the PR.
nedtwigg c786acd
Link clang-format binary management to its tracking issue #673.
nedtwigg 55d45e8
Link python black package management to its tracking issue #674.
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def special = [ | ||
'Npm', | ||
'Black', | ||
'Clang' | ||
] | ||
|
||
tasks.named('test') { | ||
useJUnit { excludeCategories special.collect { "com.diffplug.spotless.category.${it}Test" } as String[] } | ||
} | ||
|
||
special.forEach { | ||
def category = "com.diffplug.spotless.category.${it}Test" | ||
tasks.register("${it}Test", Test) { | ||
useJUnit { includeCategories category } | ||
} | ||
} | ||
135 changes: 135 additions & 0 deletions
135
lib/src/main/java/com/diffplug/spotless/ForeignExe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2020 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.spotless; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Finds a foreign executable and checks its version. | ||
* If either part of that fails, it shows you why | ||
* and helps you fix it. | ||
* | ||
* Usage: `ForeignExe.nameAndVersion("grep", "2.5.7").confirmVersionAndGetAbsolutePath()` | ||
* will find grep, confirm that it is version 2.5.7, and then return. | ||
*/ | ||
public class ForeignExe { | ||
private @Nullable String pathToExe; | ||
private String versionFlag = "--version"; | ||
private Pattern versionRegex = Pattern.compile("version (\\S*)"); | ||
private @Nullable String fixCantFind, fixWrongVersion; | ||
|
||
// MANDATORY | ||
private String name; | ||
private String version; | ||
|
||
/** The name of the executable, used by "where" (win) and "which" (unix). */ | ||
public static ForeignExe nameAndVersion(String exeName, String version) { | ||
ForeignExe foreign = new ForeignExe(); | ||
foreign.name = Objects.requireNonNull(exeName); | ||
foreign.version = Objects.requireNonNull(version); | ||
return foreign; | ||
} | ||
|
||
/** The flag which causes the exe to print its version (defaults to --version). */ | ||
public ForeignExe versionFlag(String versionFlag) { | ||
this.versionFlag = Objects.requireNonNull(versionFlag); | ||
return this; | ||
} | ||
|
||
/** A regex which can parse the version out of the output of the {@link #versionFlag(String)} command (defaults to `version (\\S*)`) */ | ||
public ForeignExe versionRegex(Pattern versionRegex) { | ||
this.versionRegex = Objects.requireNonNull(versionRegex); | ||
return this; | ||
} | ||
|
||
/** Use {version} anywhere you would like to inject the actual version string. */ | ||
public ForeignExe fixCantFind(String msg) { | ||
this.fixCantFind = msg; | ||
return this; | ||
} | ||
|
||
/** Use {version} or {versionFound} anywhere you would like to inject the actual version strings. */ | ||
public ForeignExe fixWrongVersion(String msg) { | ||
this.fixWrongVersion = msg; | ||
return this; | ||
} | ||
|
||
/** Path to the executable. If null, will search for the executable on the system path. */ | ||
public ForeignExe pathToExe(@Nullable String pathToExe) { | ||
this.pathToExe = pathToExe; | ||
return this; | ||
} | ||
|
||
/** | ||
* Searches for the executable and confirms that it has the expected version. | ||
* If it can't find the executable, or if it doesn't have the correct version, | ||
* throws an exception with a message describing how to fix. | ||
*/ | ||
public String confirmVersionAndGetAbsolutePath() throws IOException, InterruptedException { | ||
try (ProcessRunner runner = new ProcessRunner()) { | ||
String exeAbsPath; | ||
if (pathToExe != null) { | ||
exeAbsPath = pathToExe; | ||
} else { | ||
ProcessRunner.Result cmdWhich = runner.shellWinUnix("where " + name, "which " + name); | ||
if (cmdWhich.exitNotZero()) { | ||
throw cantFind("Unable to find " + name + " on path", cmdWhich); | ||
} else { | ||
exeAbsPath = cmdWhich.assertExitZero(Charset.defaultCharset()).trim(); | ||
} | ||
} | ||
ProcessRunner.Result cmdVersion = runner.exec(exeAbsPath, versionFlag); | ||
if (cmdVersion.exitNotZero()) { | ||
throw cantFind("Unable to run " + exeAbsPath, cmdVersion); | ||
} | ||
Matcher versionMatcher = versionRegex.matcher(cmdVersion.assertExitZero(Charset.defaultCharset())); | ||
if (!versionMatcher.find()) { | ||
throw cantFind("Unable to parse version with /" + versionRegex + "/", cmdVersion); | ||
} | ||
String versionFound = versionMatcher.group(1); | ||
if (!versionFound.equals(version)) { | ||
throw wrongVersion("You specified version " + version + ", but Spotless found " + versionFound, cmdVersion, versionFound); | ||
} | ||
return exeAbsPath; | ||
} | ||
} | ||
|
||
private RuntimeException cantFind(String message, ProcessRunner.Result cmd) { | ||
return exceptionFmt(message, cmd, fixCantFind == null ? null : fixCantFind.replace("{version}", version)); | ||
} | ||
|
||
private RuntimeException wrongVersion(String message, ProcessRunner.Result cmd, String versionFound) { | ||
return exceptionFmt(message, cmd, fixWrongVersion == null ? null : fixWrongVersion.replace("{version}", version).replace("{versionFound}", versionFound)); | ||
} | ||
|
||
private RuntimeException exceptionFmt(String msgPrimary, ProcessRunner.Result cmd, @Nullable String msgFix) { | ||
StringBuilder errorMsg = new StringBuilder(); | ||
errorMsg.append(msgPrimary); | ||
errorMsg.append('\n'); | ||
if (msgFix != null) { | ||
errorMsg.append(msgFix); | ||
errorMsg.append('\n'); | ||
} | ||
errorMsg.append(cmd.toString()); | ||
return new RuntimeException(errorMsg.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, these new
Test
tasks are not being run on CircleCI yet (although I'd love to be corrected on this!), and regardless I think we'd need Node.js, Python and Clang installed on our CircleCI setup for those tests to work, and I can't see any CircleCI-related changes in this PR, so I'm a little confused. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another good catch! None of this is being exercised on CircleCI right now, which is a problem. I did extensive testing on my personal win and mac machines, but this should definitely be tested better.
It's easy to write a cross-platform "machine preparation script" for
black
, but it's a little bit trickier forclang-format
. On mac,brew
gives you10.0.1
and its hard to get anything else. On windows,choco
gives you10.0.0
, and it's hard to get anything else. And those will both change over time - the llvm website seems to advertise for12.0
, which apparently hasn't made it into package managers yet. If your team is all on the same platform (fairly common), then none of this is a problem: "just runbrew install clang-format
"It would be easy to make a docker image which has been prepared for specific versions for running
blackTest
andclangTest
. However, if a contributor wants to run these tests, they'll have to do that preparation manually, which is a pain. When I went back and forth between win and mac, I just changed the versions in the tests, which isn't great.Testing would be a lot easier if #673 and #674 were implemented. So my testing plan was: when someone has a good idea/PR for improving either of those issues, then that's when we'll run the tests on CI, since it'll be a lot easier at that point.
In the meantime, I'll commit to be being the human-test-runner for PRs which affect these native formatters, and I'm very open to a PR which adds a prepared docker image to offload that burden onto CI :D