| /* |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| */ |
| |
| package org.apache.flex.compiler.internal.codegen.mxml.flexjs; |
| |
| import org.apache.commons.io.FilenameUtils; |
| import org.apache.commons.io.IOUtils; |
| import org.apache.flex.compiler.clients.problems.ProblemQuery; |
| import org.apache.flex.compiler.config.Configuration; |
| import org.apache.flex.compiler.internal.codegen.js.jsc.JSCPublisher; |
| import org.apache.flex.compiler.internal.driver.js.goog.JSGoogConfiguration; |
| import org.apache.flex.compiler.internal.graph.GoogDepsWriter; |
| import org.apache.flex.compiler.internal.graph.GoogDepsWriterCordova; |
| import org.apache.flex.compiler.internal.projects.FlexJSProject; |
| import org.apache.flex.swc.ISWC; |
| |
| import java.io.BufferedReader; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.io.OutputStream; |
| import java.io.StringWriter; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class MXMLFlexJSCordovaPublisher extends MXMLFlexJSPublisher |
| { |
| public MXMLFlexJSCordovaPublisher(Configuration config, FlexJSProject project) |
| { |
| super(project, config); |
| } |
| |
| private String cordova = "cordova"; |
| private String[] pathEnv = new String[3]; |
| |
| private StringBuilder log = new StringBuilder(); |
| private String newline = "\n"; |
| |
| private boolean needNewProject; |
| |
| @Override |
| protected void setupOutputFolder() |
| { |
| if (!outputFolder.exists()) |
| needNewProject = true; |
| super.setupOutputFolder(); |
| } |
| |
| @Override |
| public File getOutputFolder() |
| { |
| File newOutputFolder = super.getOutputFolder(); |
| |
| String osName = System.getProperty("os.name"); |
| if (osName.contains("Windows")) |
| { |
| newline = "\r\n"; |
| pathEnv = new String[7]; |
| cordova = "cordova.cmd"; |
| String home = System.getenv("HOME"); |
| if (home == null) |
| home = System.getenv("USERPROFILE"); |
| pathEnv[0] = "USERPROFILE=" + home; |
| String path = System.getenv("PATH"); |
| String java = System.getenv("JAVA_HOME"); |
| String android = System.getenv("ANDROID_HOME"); |
| String appdata = System.getenv("APPDATA"); |
| String programFiles = System.getenv("ProgramFiles"); |
| if (!path.contains("\\Program Files\\nodejs\\node_modules\\npm\\bin")) |
| { |
| path += ";" + programFiles + "\\nodejs\\node_modules\\npm\\bin"; |
| } |
| pathEnv[1] = "PATH=" + path; |
| pathEnv[2] = "JAVA_HOME=" + java; |
| pathEnv[3] = "ANDROID_HOME=" + android; |
| pathEnv[4] = "HOME=" + home; |
| pathEnv[5] = "APPDATA=" + appdata; |
| pathEnv[6] = "ProgramFiles=" + programFiles; |
| log.append(pathEnv[0]).append(newline); |
| log.append(pathEnv[1]).append(newline); |
| log.append(pathEnv[2]).append(newline); |
| log.append(pathEnv[3]).append(newline); |
| log.append(pathEnv[4]).append(newline); |
| log.append(pathEnv[5]).append(newline); |
| log.append(pathEnv[6]).append(newline); |
| } |
| else |
| { |
| File c = new File("/usr/local/bin/cordova"); |
| if (c.exists()) |
| { |
| cordova = "/usr/local/bin/cordova"; |
| String home = System.getenv("HOME"); |
| pathEnv[0] = "HOME=" + home; |
| File bash = new File(home + File.separator + ".bash_login"); |
| String path = System.getenv("PATH"); |
| String java = System.getenv("JAVA_HOME"); |
| if (path == null || !path.contains("node_modules") || java == null) |
| { |
| try |
| { |
| BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(bash), "UTF8")); |
| |
| String line = in.readLine(); |
| |
| while (line != null) |
| { |
| if (line.startsWith("export JAVA_HOME=") && java == null) |
| { |
| java = line.substring(17); |
| } |
| else if (line.startsWith("export PATH=")) |
| { |
| if (path == null) |
| path = ""; |
| String oldPath = path; |
| path = line.substring(12); |
| if (path.contains("$PATH")) |
| { |
| path = path.replace("$PATH", oldPath); |
| } |
| } |
| line = in.readLine(); |
| } |
| |
| in.close(); |
| if (!path.contains("/usr/local/bin")) |
| { |
| path += ":/usr/local/bin"; |
| } |
| } |
| catch (Exception e) |
| { |
| // nothing to see, move along... |
| } |
| } |
| pathEnv[1] = "PATH=" + path; |
| pathEnv[2] = "JAVA_HOME=" + java; |
| log.append(pathEnv[0]).append(newline); |
| log.append(pathEnv[1]).append(newline); |
| log.append(pathEnv[2]).append(newline); |
| } |
| } |
| |
| final String projectName = FilenameUtils.getBaseName(configuration.getTargetFile()); |
| |
| if (needNewProject) |
| { |
| /* useful for diagnosing path/environment issues |
| String[] npmParts = new String[2]; |
| npmParts[0] = "npm.cmd"; |
| npmParts[1] = "-v"; |
| try { |
| Process p = Runtime.getRuntime().exec(npmParts, pathEnv, newOutputFolder); |
| String line; |
| BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| while ((line = input.readLine()) != null) { |
| System.out.println(line); |
| } |
| input.close(); |
| BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
| while ((line = error.readLine()) != null) { |
| System.out.println(line); |
| } |
| int ret = p.exitValue(); |
| System.out.println("npm -v returned " + ret); |
| } catch (IOException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| */ |
| |
| String[] execParts = new String[5]; |
| execParts[0] = cordova; |
| execParts[1] = "create"; |
| execParts[2] = "app"; |
| execParts[3] = googConfiguration.getCordovaId(); |
| execParts[4] = projectName; |
| log.append(execParts[0]).append(newline); |
| log.append(execParts[1]).append(newline); |
| log.append(execParts[2]).append(newline); |
| log.append(execParts[3]).append(newline); |
| log.append(execParts[4]).append(newline); |
| try { |
| Process p = Runtime.getRuntime().exec(execParts, pathEnv, newOutputFolder); |
| String line; |
| BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| while ((line = input.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| input.close(); |
| BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
| while ((line = error.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| int ret = p.exitValue(); |
| System.out.println("cordova create returned " + ret); |
| } catch (IOException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| } |
| newOutputFolder = new File(newOutputFolder, "app"); |
| newOutputFolder = new File(newOutputFolder, "www"); |
| outputFolder = newOutputFolder; |
| return newOutputFolder; |
| } |
| |
| @Override |
| public boolean publish(ProblemQuery problems) throws IOException |
| { |
| if (super.publish(problems)) |
| { |
| cordovaPublish(); |
| File logFile = new File(outputFolder.getParentFile().getParentFile(), "cordova.log"); |
| writeFile(logFile, log.toString(), false); |
| } |
| |
| return true; |
| } |
| |
| private void cordovaPublish() |
| { |
| // The "intermediate" is the "js-debug" output. |
| final File intermediateDir = outputFolder; |
| final String projectName = FilenameUtils.getBaseName(configuration.getTargetFile()); |
| File projectDir = intermediateDir.getParentFile(); |
| |
| // The "release" is the "js-release" directory. |
| File releaseDir = new File(outputParentFolder, FLEXJS_RELEASE_DIR_NAME); |
| |
| List<String> platforms = googConfiguration.getCordovaPlatforms(); |
| for (String platform : platforms) |
| { |
| File platformDir = new File(intermediateDir, "platforms" + File.separator + platform); |
| if (!platformDir.exists()) |
| { |
| String[] execParts = new String[4]; |
| execParts[0] = cordova; |
| execParts[1] = "platform"; |
| execParts[2] = "add"; |
| execParts[3] = platform; |
| log.append(execParts[0]).append(newline); |
| log.append(execParts[1]).append(newline); |
| log.append(execParts[2]).append(newline); |
| log.append(execParts[3]).append(newline); |
| try { |
| Process p = Runtime.getRuntime().exec(execParts, pathEnv, outputFolder.getParentFile()); |
| String line; |
| BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| while ((line = input.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| input.close(); |
| BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
| while ((line = error.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| int ret = p.exitValue(); |
| log.append("cordova platform add returned " + ret).append(newline); |
| } catch (IOException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| |
| } |
| } |
| |
| for (String plugin : plugins) |
| { |
| try { |
| String[] execParts = new String[4]; |
| execParts[0] = cordova; |
| execParts[1] = "plugin"; |
| execParts[2] = "add"; |
| execParts[3] = plugin; |
| log.append(execParts[0]).append(newline); |
| log.append(execParts[1]).append(newline); |
| log.append(execParts[2]).append(newline); |
| log.append(execParts[3]).append(newline); |
| Process p = Runtime.getRuntime().exec(execParts, pathEnv, outputFolder.getParentFile()); |
| String line; |
| BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| while ((line = input.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| input.close(); |
| BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
| while ((line = error.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| int ret = p.exitValue(); |
| log.append("cordova plugin returned " + ret).append(newline); |
| } catch (IOException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| } |
| |
| try { |
| String[] execParts = new String[2]; |
| execParts[0] = cordova; |
| execParts[1] = "build"; |
| log.append(execParts[0]).append(newline); |
| log.append(execParts[1]).append(newline); |
| Process p = Runtime.getRuntime().exec(execParts, pathEnv, outputFolder.getParentFile()); |
| String line; |
| BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| while ((line = input.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| input.close(); |
| BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
| while ((line = error.readLine()) != null) { |
| log.append(line).append(newline); |
| } |
| int ret = p.exitValue(); |
| log.append("cordova build returned " + ret).append(newline);; |
| } catch (IOException e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| } |
| |
| private ArrayList<String> plugins; |
| |
| protected GoogDepsWriter getGoogDepsWriter(File intermediateDir, |
| String projectName, |
| JSGoogConfiguration googConfiguration, |
| List<ISWC> swcs) |
| { |
| plugins = new ArrayList<String>(); |
| return new GoogDepsWriterCordova(intermediateDir, projectName, googConfiguration, swcs, plugins); |
| } |
| |
| } |