]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/src/org/simantics/district/maps/server/NodeJS.java
Adding pkg-precompiled tileserver-mapnik to avoid npm install
[simantics/district.git] / org.simantics.maps.server / src / org / simantics / district / maps / server / NodeJS.java
1 package org.simantics.district.maps.server;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.URISyntaxException;
6 import java.nio.charset.StandardCharsets;
7 import java.nio.file.Path;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11 import java.util.Optional;
12 import java.util.concurrent.TimeoutException;
13
14 import org.simantics.district.maps.server.utils.EnvUtil;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.zeroturnaround.exec.InvalidExitValueException;
18 import org.zeroturnaround.exec.ProcessExecutor;
19 import org.zeroturnaround.exec.stream.slf4j.Slf4jInfoOutputStream;
20
21 public class NodeJS {
22
23     public static final String NODEJS_VERSION = "v4.8.0";
24
25     private static final Logger LOGGER = LoggerFactory.getLogger(NodeJS.class);
26     private static final String NODE = "node";
27     
28     private static Path nodeJSFolder() throws IOException, URISyntaxException {
29         StringBuilder sb = new StringBuilder();
30         sb.append(NODE).append("-").append(NODEJS_VERSION).append("-").append(getOs()).append("-").append(getArch());
31         return Activator.getNodeJSRoot().resolve(sb.toString());
32     }
33     
34     public static Path executable() throws IOException, URISyntaxException {
35         String nodeJSexecutable = NODE;
36         if (EnvUtil.calculateOS() == "win")
37             nodeJSexecutable = nodeJSexecutable + ".exe";
38         return nodeJSFolder().resolve(nodeJSexecutable).toAbsolutePath().normalize();
39     }
40     
41     private static Path npmExecutable() throws IOException, URISyntaxException {
42         String npmExecutable = "npm";
43         if (EnvUtil.calculateOS() == "win")
44             npmExecutable = npmExecutable + ".cmd";
45         return nodeJSFolder().resolve(npmExecutable).toAbsolutePath().normalize();
46     }
47     
48     private static String getArch() {
49         return EnvUtil.calculateArch();
50     }
51
52     private static String getOs() {
53         return EnvUtil.calculateOS();
54     }
55
56     public static int npm(OutputStream output, String... args) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException, URISyntaxException {
57         LOGGER.info("Executing npm with args {}", Arrays.toString(args));
58         List<String> actualArgs = new ArrayList<>();
59         actualArgs.add(npmExecutable().toString());
60         actualArgs.addAll(Arrays.asList(args));
61         ProcessExecutor exec = new ProcessExecutor().command(actualArgs).redirectOutput(new Slf4jInfoOutputStream(LOGGER) {
62             
63             @Override
64             protected void processLine(String line) {
65                 // Convert to UTF-8 string
66                 String utf8Line = new String(line.getBytes(), StandardCharsets.UTF_8);
67                 log.info(utf8Line);
68             }
69         }).destroyOnExit();
70         
71         if (output != null)
72             exec.redirectOutputAlsoTo(output);
73         return exec.execute().getExitValue();
74     }
75     
76
77     public static void main(String[] args) throws Exception {
78         TileserverMapnik server = TileserverMapnikInstance.get();
79         server.start(Optional.empty());
80     }
81 }