X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.maps.server%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fmaps%2Fserver%2FNodeJS.java;fp=org.simantics.maps.server%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fmaps%2Fserver%2FNodeJS.java;h=5bbb5ec72a19e5d87f0ce5a7cc6f1fa5b99cd4a9;hb=2529be6d456deeb07c128603ce4971f1dc29b695;hp=0000000000000000000000000000000000000000;hpb=2636fc31c16c23711cf2b06a4ae8537bba9c1d35;p=simantics%2Fdistrict.git diff --git a/org.simantics.maps.server/src/org/simantics/district/maps/server/NodeJS.java b/org.simantics.maps.server/src/org/simantics/district/maps/server/NodeJS.java new file mode 100644 index 00000000..5bbb5ec7 --- /dev/null +++ b/org.simantics.maps.server/src/org/simantics/district/maps/server/NodeJS.java @@ -0,0 +1,66 @@ +package org.simantics.district.maps.server; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeoutException; + +import org.simantics.district.maps.server.utils.EnvUtil; +import org.zeroturnaround.exec.InvalidExitValueException; +import org.zeroturnaround.exec.ProcessExecutor; +import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; + +public class NodeJS { + + public static final String NODEJS_VERSION = "v4.8.0"; + + private static final String NODE = "node"; + + private static Path nodeJSFolder() throws IOException, URISyntaxException { + StringBuilder sb = new StringBuilder(); + sb.append(NODE).append("-").append(NODEJS_VERSION).append("-").append(getOs()).append("-").append(getArch()); + return Activator.getNodeJSRoot().resolve(sb.toString()); + } + + public static Path executable() throws IOException, URISyntaxException { + String nodeJSexecutable = NODE; + if (EnvUtil.calculateOS() == "win") + nodeJSexecutable = nodeJSexecutable + ".exe"; + return nodeJSFolder().resolve(nodeJSexecutable).toAbsolutePath().normalize(); + } + + private static Path npmExecutable() throws IOException, URISyntaxException { + String npmExecutable = "npm"; + if (EnvUtil.calculateOS() == "win") + npmExecutable = npmExecutable + ".cmd"; + return nodeJSFolder().resolve(npmExecutable).toAbsolutePath().normalize(); + } + + private static String getArch() { + return EnvUtil.calculateArch(); + } + + private static String getOs() { + return EnvUtil.calculateOS(); + } + + public static int npm(OutputStream output, String... args) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException, URISyntaxException { + List actualArgs = new ArrayList<>(); + actualArgs.add(npmExecutable().toString()); + actualArgs.addAll(Arrays.asList(args)); + ProcessExecutor exec = new ProcessExecutor().command(actualArgs).redirectOutput(Slf4jStream.ofCaller().asInfo()).destroyOnExit(); + if (output != null) + exec.redirectOutputAlsoTo(output); + return exec.execute().getExitValue(); + } + + + public static void main(String[] args) throws Exception { + TileserverMapnik server = TileserverMapnikInstance.get(); + server.start(); + } +}