1 package org.simantics.district.maps.server;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.URISyntaxException;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.Arrays;
10 import java.util.concurrent.TimeoutException;
12 import org.simantics.district.maps.server.utils.EnvUtil;
13 import org.zeroturnaround.exec.InvalidExitValueException;
14 import org.zeroturnaround.exec.ProcessExecutor;
15 import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
19 public static final String NODEJS_VERSION = "v4.8.0";
21 private static final String NODE = "node";
23 private static Path nodeJSFolder() throws IOException, URISyntaxException {
24 StringBuilder sb = new StringBuilder();
25 sb.append(NODE).append("-").append(NODEJS_VERSION).append("-").append(getOs()).append("-").append(getArch());
26 return Activator.getNodeJSRoot().resolve(sb.toString());
29 public static Path executable() throws IOException, URISyntaxException {
30 String nodeJSexecutable = NODE;
31 if (EnvUtil.calculateOS() == "win")
32 nodeJSexecutable = nodeJSexecutable + ".exe";
33 return nodeJSFolder().resolve(nodeJSexecutable).toAbsolutePath().normalize();
36 private static Path npmExecutable() throws IOException, URISyntaxException {
37 String npmExecutable = "npm";
38 if (EnvUtil.calculateOS() == "win")
39 npmExecutable = npmExecutable + ".cmd";
40 return nodeJSFolder().resolve(npmExecutable).toAbsolutePath().normalize();
43 private static String getArch() {
44 return EnvUtil.calculateArch();
47 private static String getOs() {
48 return EnvUtil.calculateOS();
51 public static int npm(OutputStream output, String... args) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException, URISyntaxException {
52 List<String> actualArgs = new ArrayList<>();
53 actualArgs.add(npmExecutable().toString());
54 actualArgs.addAll(Arrays.asList(args));
55 ProcessExecutor exec = new ProcessExecutor().command(actualArgs).redirectOutput(Slf4jStream.ofCaller().asInfo()).destroyOnExit();
57 exec.redirectOutputAlsoTo(output);
58 return exec.execute().getExitValue();
62 public static void main(String[] args) throws Exception {
63 TileserverMapnik server = TileserverMapnikInstance.get();