1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.maven.plugins.nodejs.mojo;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.logging.Log;
30 import org.codehaus.plexus.util.Os;
31
32 import io.wcm.maven.plugins.nodejs.installation.NodeInstallationInformation;
33
34
35
36
37 public class Task {
38
39 private static final String PATH_VARIABLE_NAME = "PATH";
40
41
42
43
44 protected File workingDirectory;
45
46 private Log log;
47
48
49
50
51
52
53 public void execute(NodeInstallationInformation information) throws MojoExecutionException {
54 ProcessBuilder processBuilder = new ProcessBuilder(getCommand(information));
55 if (workingDirectory != null) {
56 if (!workingDirectory.exists()) {
57 workingDirectory.mkdir();
58 }
59 processBuilder.directory(workingDirectory);
60 }
61 else if (isWorkingDirectoryMandatory()) {
62 throw new MojoExecutionException("workingDirectory parameter missing for "
63 + StringUtils.uncapitalize(getClass().getSimpleName()));
64 }
65 setNodePath(processBuilder, information);
66 startProcess(processBuilder);
67 }
68
69 private void startProcess(ProcessBuilder processBuilder) throws MojoExecutionException {
70 try {
71 final Process process = processBuilder.start();
72 getLog().info("Running process: " + StringUtils.join(processBuilder.command(), " "));
73 initLogging(process);
74 int result = process.waitFor();
75 if (result != 0) {
76 throw new MojoExecutionException("Process: " + StringUtils.join(processBuilder.command(), " ") + " terminated with " + result);
77 }
78 }
79 catch (IOException ex) {
80 throw new MojoExecutionException("Error executing process: " + StringUtils.join(processBuilder.command(), " "), ex);
81 }
82 catch (InterruptedException ex) {
83 throw new MojoExecutionException("Error executing process: " + StringUtils.join(processBuilder.command(), " "), ex);
84 }
85 }
86
87 private void initLogging(final Process process) throws InterruptedException {
88 final Thread infoLogThread = new NodejsOutputStreamHandler(process.getInputStream(), getLog());
89 final Thread errorLogThread = new NodejsOutputStreamHandler(process.getErrorStream(), getLog());
90
91 infoLogThread.start();
92 errorLogThread.start();
93 infoLogThread.join();
94 errorLogThread.join();
95 }
96
97 private void setNodePath(ProcessBuilder pbuilder, NodeInstallationInformation information) {
98 final Map<String, String> environment = pbuilder.environment();
99 String pathVariableName = PATH_VARIABLE_NAME;
100 String pathValue = environment.get(pathVariableName);
101 if (Os.isFamily(Os.FAMILY_WINDOWS) || Os.isFamily(Os.FAMILY_WIN9X)) {
102 for (String key : environment.keySet()) {
103 if (PATH_VARIABLE_NAME.equalsIgnoreCase(key)) {
104 pathVariableName = key;
105 pathValue = environment.get(key);
106 }
107 }
108 }
109 if (pathValue == null) {
110 environment.put(pathVariableName, information.getNodeExecutable().getParent());
111 }
112 else {
113 environment.put(pathVariableName, information.getNodeExecutable().getParent() + File.pathSeparator + pathValue);
114 }
115 }
116
117
118
119
120
121
122 protected List<String> getCommand(NodeInstallationInformation information) throws MojoExecutionException {
123 return null;
124 }
125
126 public Log getLog() {
127 return log;
128 }
129
130 public void setLog(Log log) {
131 this.log = log;
132 }
133
134 protected boolean isWorkingDirectoryMandatory() {
135 return false;
136 }
137
138 }