View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * General task implementation.
36   */
37  public class Task {
38  
39    private static final String PATH_VARIABLE_NAME = "PATH";
40  
41    /**
42     * Directory in which the should be executed.
43     */
44    protected File workingDirectory;
45  
46    private Log log;
47  
48    /**
49     * Executes the {@link Process} with commands returned by {@link #getCommand(NodeInstallationInformation)}.
50     * @param information Information
51     * @throws MojoExecutionException Mojo execution exception
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    * @param information about the node installation
119    * @return {@link List} of commands which will be executed by the task
120    * @throws MojoExecutionException Mojo execution exception
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 }