-
Notifications
You must be signed in to change notification settings - Fork 472
Adding npm-based formatters typescript-formatter (tsfmt) and prettier #283
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
Merged
nedtwigg
merged 46 commits into
diffplug:master
from
simschla:feature/typescript-formatting-using-nodejs-in-j2v8
Sep 24, 2018
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
75ce9aa
initial implementation of nodebased steps
simschla dfe881d
Move tests for into lib-extra.
nedtwigg c0bb96f
Separate the `NpmTest` tests from the rest of the test suite.
nedtwigg 991457c
renaming package nodebased -> npm
simschla 164f047
move subpackages to npm top-level package
simschla 37fff43
restrict visibility
simschla ddfc882
revert the from-file feature for JarState as it is not used right now
simschla ef4d244
adapt to package renaming
simschla 4f16998
using map as config api for npm formatter steps
simschla 74dfaee
clearing up resources after prettier/tsfmt run
simschla e706531
spotbugs cleanup
simschla b8fb907
cleanup autorelease-code
simschla 11b32f4
restrict engine versions according to the npm packages used
simschla c02b3ef
dynamically select j2v8 lib based on platform
simschla 27dc6f9
add support for closing FormatterFunc
simschla 0b7f6a9
implement behavioral tests
simschla 4b25b7c
validate working config options for tsfmt
simschla 48430c8
normalized arch name (matching arch for for ci)
simschla ff1d3d3
validate multiple formatting filetypes are working
simschla d832ccd
test prettier configuration behavior
simschla ff9a3d2
auto-resolve npm binary (at least for tests very useful)
simschla 3dfe3c9
adding gradle-integration for prettier
simschla d21f2bc
adding gradle-integration for tsfmt
simschla 8b1cf97
Fix minor warnings in eclipse.
nedtwigg 18c7748
Made as much of the npm implementation package-private as possible.
nedtwigg 5550174
Fixed the gradle typescript extension.
nedtwigg 092a488
Make the npm tests run on CI.
nedtwigg 83178d1
Moved all of npm's test files into testlib so that they can be reused…
nedtwigg ded5885
readFileFromClasspath is now compatible with the gradle classpath
nedtwigg 4ee647e
Added an integration test for TypescriptExtension, but it doesn't pas…
nedtwigg 0b604ab
A super incomplete version of the docs, to plant the seed.
nedtwigg 2197c27
first documentation draft
simschla 732e27a
unify naming
simschla e400fa5
provide default basedir
simschla 1fb5838
step 1 on supporting inline-config for typescript-formatter
simschla b5e57e7
introduce inline-config for tsfmt step
simschla 2a21254
adding more integration tests for typescript extension
simschla 3af839f
integration tests for prettier formatter
simschla c3c8ae1
unify usage of NpmTest category
simschla f817309
PR feedback: extract into sub-methods
simschla bf16cab
update documentation for tsfmt config file part
simschla 3d3f304
smaller api for tsfmt config files
simschla 6e07578
moving npm based formatters from lib-extra to lib
simschla ade884a
fixes for running on windows
simschla 807bf7b
integrating PR feedback
simschla 60f2bcd
Merge branch 'master' into feature/typescript-formatting-using-nodejs…
simschla 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
24 changes: 24 additions & 0 deletions
24
lib/src/main/java/com/diffplug/spotless/npm/BlacklistedOptionException.java
This file contains hidden or 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,24 @@ | ||
/* | ||
* Copyright 2016 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.npm; | ||
|
||
class BlacklistedOptionException extends RuntimeException { | ||
private static final long serialVersionUID = -5876348893394153811L; | ||
|
||
public BlacklistedOptionException(String blacklistedOption) { | ||
super("The config option '" + blacklistedOption + "' is not supported."); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java
This file contains hidden or 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,106 @@ | ||
/* | ||
* Copyright 2016 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.npm; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
class NodeJSWrapper extends ReflectiveObjectWrapper { | ||
|
||
public static final String V8_RUNTIME_CLASS = "com.eclipsesource.v8.V8"; | ||
public static final String V8_VALUE_CLASS = "com.eclipsesource.v8.V8Value"; | ||
|
||
public static final String WRAPPED_CLASS = "com.eclipsesource.v8.NodeJS"; | ||
|
||
private static final AtomicBoolean flagsSet = new AtomicBoolean(false); | ||
|
||
public NodeJSWrapper(ClassLoader classLoader) { | ||
super(Reflective.withClassLoader(classLoader), | ||
reflective -> { | ||
final boolean firstRun = flagsSet.compareAndSet(false, true); | ||
if (firstRun) { | ||
reflective.invokeStaticMethod(V8_RUNTIME_CLASS, "setFlags", "-color=false"); // required to run prettier on windows | ||
} | ||
return reflective.invokeStaticMethod(WRAPPED_CLASS, "createNodeJS"); | ||
}); | ||
} | ||
|
||
public V8ObjectWrapper require(File npmModulePath) { | ||
Objects.requireNonNull(npmModulePath); | ||
Object v8Object = invoke("require", npmModulePath); | ||
return new V8ObjectWrapper(reflective(), v8Object); | ||
} | ||
|
||
public V8ObjectWrapper createNewObject() { | ||
Object v8Object = reflective().invokeConstructor(V8ObjectWrapper.WRAPPED_CLASS, nodeJsRuntime()); | ||
V8ObjectWrapper objectWrapper = new V8ObjectWrapper(reflective(), v8Object); | ||
return objectWrapper; | ||
} | ||
|
||
public V8ObjectWrapper createNewObject(Map<String, Object> values) { | ||
Objects.requireNonNull(values); | ||
V8ObjectWrapper obj = createNewObject(); | ||
values.forEach(obj::add); | ||
return obj; | ||
} | ||
|
||
public V8ArrayWrapper createNewArray(Object... elements) { | ||
final V8ArrayWrapper v8ArrayWrapper = this.createNewArray(); | ||
for (Object element : elements) { | ||
v8ArrayWrapper.push(element); | ||
} | ||
return v8ArrayWrapper; | ||
} | ||
|
||
public V8ArrayWrapper createNewArray() { | ||
Object v8Array = reflective().invokeConstructor(V8ArrayWrapper.WRAPPED_CLASS, nodeJsRuntime()); | ||
V8ArrayWrapper arrayWrapper = new V8ArrayWrapper(reflective(), v8Array); | ||
return arrayWrapper; | ||
} | ||
|
||
public V8FunctionWrapper createNewFunction(V8FunctionWrapper.WrappedJavaCallback callback) { | ||
Object v8Function = reflective().invokeConstructor(V8FunctionWrapper.WRAPPED_CLASS, | ||
reflective().typed( | ||
V8_RUNTIME_CLASS, | ||
nodeJsRuntime()), | ||
reflective().typed( | ||
V8FunctionWrapper.CALLBACK_WRAPPED_CLASS, | ||
V8FunctionWrapper.proxiedCallback(callback, reflective()))); | ||
V8FunctionWrapper functionWrapper = new V8FunctionWrapper(reflective(), v8Function); | ||
return functionWrapper; | ||
} | ||
|
||
public void handleMessage() { | ||
invoke("handleMessage"); | ||
} | ||
|
||
private Object nodeJsRuntime() { | ||
return invoke("getRuntime"); | ||
} | ||
|
||
public Object v8NullValue(Object value) { | ||
if (value == null) { | ||
return reflective().staticField(V8_VALUE_CLASS, "NULL"); | ||
} | ||
return value; | ||
} | ||
|
||
public boolean isV8NullValue(Object v8Object) { | ||
return reflective().staticField(V8_VALUE_CLASS, "NULL") == v8Object; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
lib/src/main/java/com/diffplug/spotless/npm/NpmConfig.java
This file contains hidden or 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,40 @@ | ||
/* | ||
* Copyright 2016 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.npm; | ||
|
||
import java.io.Serializable; | ||
|
||
class NpmConfig implements Serializable { | ||
|
||
private static final long serialVersionUID = -1866722789779160491L; | ||
|
||
private final String packageJsonContent; | ||
|
||
private final String npmModule; | ||
|
||
public NpmConfig(String packageJsonContent, String npmModule) { | ||
this.packageJsonContent = packageJsonContent; | ||
this.npmModule = npmModule; | ||
} | ||
|
||
public String getPackageJsonContent() { | ||
return packageJsonContent; | ||
} | ||
|
||
public String getNpmModule() { | ||
return npmModule; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
lib/src/main/java/com/diffplug/spotless/npm/NpmExecutableResolver.java
This file contains hidden or 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,108 @@ | ||
/* | ||
* Copyright 2016 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.npm; | ||
|
||
import static com.diffplug.spotless.npm.PlatformInfo.OS.WINDOWS; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Utility class to resolve an npm binary to be used by npm-based steps. | ||
* Tries to find an npm executable in the following order: | ||
* <ol> | ||
* <li>from System-Property {@code npm.exec} (unverified)</li> | ||
* <li>from Environment-Properties in the following order:</li> | ||
* <ol> | ||
* <li> from NVM_BIN environment variable, if available </li> | ||
* <li> from NVM_SYMLINK environment variable, if available </li> | ||
* <li> from NODE_PATH environment variable, if available </li> | ||
* <li>fallback: PATH environment variable</li> | ||
* </ol> | ||
* </ol> | ||
*/ | ||
class NpmExecutableResolver { | ||
|
||
private NpmExecutableResolver() { | ||
// no instance | ||
} | ||
|
||
static String npmExecutableName() { | ||
String npmName = "npm"; | ||
if (PlatformInfo.normalizedOS() == WINDOWS) { | ||
npmName += ".cmd"; | ||
} | ||
return npmName; | ||
} | ||
|
||
static Supplier<Optional<File>> systemProperty() { | ||
return () -> Optional.ofNullable(System.getProperty("npm.exec")) | ||
.map(File::new); | ||
} | ||
|
||
static Supplier<Optional<File>> environmentNvmBin() { | ||
return () -> Optional.ofNullable(System.getenv("NVM_BIN")) | ||
.map(File::new) | ||
.map(binDir -> new File(binDir, npmExecutableName())) | ||
.filter(File::exists) | ||
.filter(File::canExecute); | ||
} | ||
|
||
static Supplier<Optional<File>> environmentNvmSymlink() { | ||
return pathListFromEnvironment("NVM_SYMLINK"); | ||
} | ||
|
||
static Supplier<Optional<File>> environmentNodepath() { | ||
return pathListFromEnvironment("NODE_PATH"); | ||
} | ||
|
||
static Supplier<Optional<File>> environmentPath() { | ||
return pathListFromEnvironment("PATH"); | ||
} | ||
|
||
static Optional<File> tryFind() { | ||
return Stream.of(systemProperty(), | ||
environmentNvmBin(), | ||
environmentNvmSymlink(), | ||
environmentNodepath(), | ||
environmentPath()) | ||
.map(Supplier::get) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.findFirst(); | ||
} | ||
|
||
private static Supplier<Optional<File>> pathListFromEnvironment(String environmentPathListName) { | ||
return () -> { | ||
String pathList = System.getenv(environmentPathListName); | ||
if (pathList != null) { | ||
return Arrays.stream(pathList.split(System.getProperty("path.separator", ":"))) | ||
.map(File::new) | ||
.map(dir -> dir.getName().equalsIgnoreCase("node_modules") ? dir.getParentFile() : dir) | ||
.map(dir -> new File(dir, npmExecutableName())) | ||
.filter(File::exists) | ||
.filter(File::canExecute) | ||
.findFirst(); | ||
|
||
} | ||
return Optional.empty(); | ||
}; | ||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.