Skip to content

Commit

Permalink
fixes for running on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Sep 12, 2018
1 parent 6e07578 commit ade884a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
11 changes: 10 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/npm/NodeJSWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

class NodeJSWrapper extends ReflectiveObjectWrapper {

Expand All @@ -26,9 +27,17 @@ class NodeJSWrapper extends ReflectiveObjectWrapper {

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 -> reflective.invokeStaticMethod(WRAPPED_CLASS, "createNodeJS"));
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* <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>
Expand All @@ -45,7 +46,7 @@ private NpmExecutableResolver() {
static String npmExecutableName() {
String npmName = "npm";
if (PlatformInfo.normalizedOS() == WINDOWS) {
npmName += ".exe";
npmName += ".cmd";
}
return npmName;
}
Expand All @@ -55,14 +56,18 @@ static Supplier<Optional<File>> systemProperty() {
.map(File::new);
}

static Supplier<Optional<File>> environmentNvm() {
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");
}
Expand All @@ -72,7 +77,11 @@ static Supplier<Optional<File>> environmentPath() {
}

static Optional<File> tryFind() {
return Stream.of(systemProperty(), environmentNvm(), environmentNodepath(), environmentPath())
return Stream.of(systemProperty(),
environmentNvmBin(),
environmentNvmSymlink(),
environmentNodepath(),
environmentPath())
.map(Supplier::get)
.filter(Optional::isPresent)
.map(Optional::get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.diffplug.gradle.spotless;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
Expand Down Expand Up @@ -48,7 +47,7 @@ public void useInlineConfig() throws IOException {

@Test
public void useFileConfig() throws IOException {
File formattingFile = setFile(".prettierrc.yml").toResource("npm/prettier/config/.prettierrc.yml");
setFile(".prettierrc.yml").toResource("npm/prettier/config/.prettierrc.yml");
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
Expand All @@ -57,7 +56,7 @@ public void useFileConfig() throws IOException {
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier().configFile('" + formattingFile.getAbsolutePath() + "')",
" prettier().configFile('.prettierrc.yml')",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.diffplug.gradle.spotless;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
Expand Down Expand Up @@ -48,7 +47,7 @@ public void useTsfmtInlineConfig() throws IOException {

@Test
public void useTsfmtFileConfig() throws IOException {
File formattingFile = setFile("tsfmt.json").toLines(
setFile("tsfmt.json").toLines(
"{",
" \"indentSize\": 1,",
" \"convertTabsToSpaces\": true",
Expand All @@ -61,7 +60,7 @@ public void useTsfmtFileConfig() throws IOException {
"spotless {",
" typescript {",
" target 'test.ts'",
" tsfmt().tsfmtFile('" + formattingFile.getAbsolutePath() + "')",
" tsfmt().tsfmtFile('tsfmt.json')",
" }",
"}");
setFile("test.ts").toResource("npm/tsfmt/tsfmt/tsfmt.dirty");
Expand Down

0 comments on commit ade884a

Please sign in to comment.