Skip to content

Commit

Permalink
Use default nodejs path if not setted #17
Browse files Browse the repository at this point in the history
  • Loading branch information
philippefichet committed Oct 10, 2021
1 parent 8170567 commit a43b4df
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
<artifactId>sonarlint-core</artifactId>
<version>6.3.0.36253</version>
</dependency>
<dependency>
<groupId>org.sonarsource.javascript</groupId>
<artifactId>nodejs-utils</artifactId>
<version>8.3.0.16208</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* sonarlint4netbeans: SonarLint integration for Apache Netbeans
* Copyright (C) 2020 Philippe FICHET.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.github.philippefichet.sonarlint4netbeans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.sonarsource.nodejs.ProcessWrapper;
import org.sonarsource.nodejs.ProcessWrapperImpl;

/**
* Used to intercept nodejs path in NodeCommandBuilderImpl
* @author FICHET Philippe &lt;[email protected]&gt;
*/
public class NodeProcessWrapper implements ProcessWrapper {

private final ProcessWrapperImpl processWrapper = new ProcessWrapperImpl();
private List<String> commandLineUsed = null;

@Override
public Process startProcess(List<String> commandLine, Map<String, String> env, Consumer<String> outputConsumer, Consumer<String> errorConsumer) throws IOException {
commandLineUsed = new ArrayList<>(commandLine);
return processWrapper.startProcess(commandLine, env, outputConsumer, errorConsumer);
}

@Override
public boolean waitFor(Process process, long timeout, TimeUnit unit) throws InterruptedException {
return processWrapper.waitFor(process, timeout, unit);
}

@Override
public void interrupt() {
processWrapper.interrupt();
}

@Override
public void destroyForcibly(Process process) {
processWrapper.destroyForcibly(process);
}

@Override
public boolean isMac() {
return processWrapper.isMac();
}

@Override
public boolean isWindows() {
return processWrapper.isWindows();
}

@Override
public String getenv(String name) {
return processWrapper.getenv(name);
}

@Override
public int exitValue(Process process) {
return processWrapper.exitValue(process);
}

public Optional<List<String>> getCommandLineUsed() {
return Optional.ofNullable(commandLineUsed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.github.philippefichet.sonarlint4netbeans;

import com.google.gson.Gson;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
Expand All @@ -35,6 +36,9 @@
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import org.openide.util.NbPreferences;
import org.sonarsource.nodejs.NodeCommand;
import org.sonarsource.nodejs.NodeCommandBuilderImpl;
import org.sonarsource.nodejs.NodeCommandException;
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl;
import org.sonarsource.sonarlint.core.client.api.common.Language;
import org.sonarsource.sonarlint.core.client.api.common.LogOutput;
Expand Down Expand Up @@ -109,17 +113,40 @@ private void createInternalEngine() {
StandaloneGlobalConfiguration.Builder configBuilder = StandaloneGlobalConfiguration.builder()
.addEnabledLanguages(Language.values())
.addPlugins(pluginURLs.values().toArray(new URL[pluginURLs.values().size()]));
getNodeJSPath().ifPresent(nodeJSPath -> {
getNodeJSVersion().ifPresent(nodeJSVersion -> {
Path nodeJS = Paths.get(nodeJSPath);
configBuilder.setNodeJs(nodeJS, nodeJSVersion);
});
});
if (getNodeJSPath().isPresent() && getNodeJSVersion().isPresent()) {
String nodeJSPath = getNodeJSPath().get();
Version nodeJSVersion = getNodeJSVersion().get();
Path nodeJS = Paths.get(nodeJSPath);
configBuilder.setNodeJs(nodeJS, nodeJSVersion);
} else {
tryToSetDefaultNodeJS(configBuilder);
}
standaloneSonarLintEngineImpl = new StandaloneSonarLintEngineImpl(configBuilder.build());
consumerWaitingInitialization.forEach(consumer -> consumer.accept(this));
consumerWaitingInitialization.clear();
}).start();
}

private void tryToSetDefaultNodeJS(StandaloneGlobalConfiguration.Builder configBuilder) {
try {
NodeProcessWrapper nodeProcessWrapper = new NodeProcessWrapper();
NodeCommand nodeCommandVersion = new NodeCommandBuilderImpl(nodeProcessWrapper)
.nodeJsArgs("--version")
.build();
nodeCommandVersion.start();
if (nodeCommandVersion.waitFor() == 0 && nodeProcessWrapper.getCommandLineUsed().isPresent()) {
String nodeJSPath = nodeProcessWrapper.getCommandLineUsed().get().get(0);
Optional<Version> detectNodeJSVersion = SonarLintUtils.detectNodeJSVersion(nodeJSPath);
if (detectNodeJSVersion.isPresent()) {
configBuilder.setNodeJs(Paths.get(nodeJSPath), detectNodeJSVersion.get());
Logger.getLogger(SonarLintEngineImpl.class.getName()).log(Level.SEVERE, "Use default nodejs path");
}
}
} catch (NodeCommandException | IOException ex) {
Logger.getLogger(SonarLintEngineImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}


@Override
public Optional<String> getNodeJSPath() {
Expand Down

0 comments on commit a43b4df

Please sign in to comment.