CLI: Fixed a bug where error occured during packaging native project on Windows 23/16423/3
authorshingil.kang <shingil.kang@samsung.com>
Mon, 17 Feb 2014 08:30:25 +0000 (17:30 +0900)
committershingil.kang <shingil.kang@samsung.com>
Wed, 19 Feb 2014 06:35:20 +0000 (15:35 +0900)
Added the function which generate packaging windows command
Added the exception handling codes

Change-Id: Id15141cdc69d5ae32d80ebcefadb3bedfe37c110
Signed-off-by: shingil.kang <shingil.kang@samsung.com>
org.tizen.ncli.ide/src/org/tizen/ncli/ide/subcommands/PackageNativeCLICommand.java

index bc764bc..acc8cd0 100644 (file)
@@ -1,7 +1,14 @@
 package org.tizen.ncli.ide.subcommands;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -13,6 +20,7 @@ import javax.xml.xpath.XPathFactory;
 import org.tizen.common.core.application.InstallPathConfig;
 import org.tizen.common.util.FileUtil;
 import org.tizen.common.util.HostUtil;
+import org.tizen.common.util.IOUtil;
 import org.tizen.common.util.OSChecker;
 import org.tizen.nativecommon.build.SmartBuildInterface;
 import org.tizen.nativecommon.build.exception.SBIException;
@@ -26,7 +34,13 @@ public class PackageNativeCLICommand extends AbstractSubCommand<PackageNativeCLI
 {
     private static final String PATH_SBI = FileUtil.appendPath(InstallPathConfig.getSDKPath(), "tools/smart-build-interface/bin");
     private static final String CMD_CLI_NATIVE_PACK_LINUX = FileUtil.appendPath(PATH_SBI, "sbi");
-    private static final String CMD_CLI_NATIVE_PACK_WINDOWS = FileUtil.appendPath(PATH_SBI, "sbi.bat");
+    private static final String CMD_CLI_NATIVE_PACK_WINDOWS = FileUtil.appendPath(PATH_SBI, "sbi.exe");
+    private static final String SBI_COMMAND_ACTION = "action";
+    private static final String SBI_COMMAND_BUILD_PACKAGE = "buildpackage";
+
+    // The SBI CLI uses dependency library on Windows
+    private static final String MINGW_BIN_PATH = FileUtil.appendPath(InstallPathConfig.getSDKPath(), "tools/mingw/bin");
+    private static final String MSYS_BIN_PATH = FileUtil.appendPath(InstallPathConfig.getSDKPath(), "tools/mingw/msys/1.0/bin");
 
     protected PackageNativeCLICommandData data = new PackageNativeCLICommandData();
 
@@ -49,35 +63,155 @@ public class PackageNativeCLICommand extends AbstractSubCommand<PackageNativeCLI
     protected String packageType = "TPK";
     protected String projectName;
     protected String packageName;
-    protected String version;
+    protected String packageVersion;
 
     XPath xpath = XPathFactory.newInstance().newXPath();
 
     @Override
     protected PackageNativeCLICommandData call()
     {
-        if (!setEnv())
-            return null;
+        if (setEnv())
+        {
+            try
+            {
+                // create documents
+                cprojectDoc = getDocument(cprojectPath);
+                projectDoc = getDocument(projectFilePath);
+                manifestDoc = getDocument(manifestPath);
+
+                // get target ID
+                targetID = getTargetID(cprojectDoc);
+                if (targetID == null)
+                {
+                    progressLog.error("Error occured during getting target ID");
+                    return null;
+                }
+
+            } catch (Exception e)
+            {
+                progressLog.error("Error occured during creating project file", e.getMessage());
+                return null;
+            }
+
+            sbi = SmartBuildInterface.getInstance();
+
+            if (sbi != null)
+            {
+                try
+                {
+                    arch = SBIModel.getBaseArchName(sbi.getRootstrapArchitecture(targetID));
+                } catch (Exception e)
+                {
+                    progressLog.error("Error occured during getting target architecture");
+                    return null;
+                }
+
+                packageName = getPackageName(manifestDoc);
+                projectName = getProjectName(projectDoc);
+                packageVersion = getVersion(manifestDoc);
+                PackageOption packageOption = new PackageOption(workingProjectConfigurationPath, arch, packageType, packageName, projectName, projectName, packageVersion);
+
+                if (!OSChecker.isWindows())
+                {
+                    String command = CMD_CLI_NATIVE_PACK_LINUX + " " + getSBIAction(targetID) + " " + packageOption.toString();
+                    progressLog.info("COMMAND = " + command);
+
+                    // run packing command
+                    progressLog.info(HostUtil.returnExecute(command));
+
+                    // set TPK path
+                    setTpkPath();
+
+                    return data;
+                }
+                else
+                {
+                    // create packing command
+                    List<String> command = new ArrayList<String>();
+                    command.add(CMD_CLI_NATIVE_PACK_WINDOWS);
+                    command.add(SBI_COMMAND_ACTION);
+                    command.add(targetID);
+                    command.add("--");
+                    command.add(SBI_COMMAND_BUILD_PACKAGE);
+
+                    command = addPackageOptionToList(packageOption, command);
+
+                    if (command != null)
+                    {
+                        // run packing command
+                        runPackageCLIonWindows(command);
+
+                        // set TPK path
+                        setTpkPath();
+
+                        return data;
+                    }
+                    else
+                    {
+                        progressLog.error("Error occured during getting packaging option");
+                    }
+                }
+            }
+        }
+        else
+        {
+            progressLog.error("Error occured during setting enviroment");
+        }
+        return null;
+    }
 
-        sbi = SmartBuildInterface.getInstance();
+    public void runPackageCLIonWindows(List<String> command)
+    {
+        // get system enviroment
+        Map<String, String> env = System.getenv();
 
-        // create documents
-        cprojectDoc = getDocument(cprojectPath);
-        projectDoc = getDocument(projectFilePath);
-        manifestDoc = getDocument(manifestPath);
+        if (env != null)
+        {
+            ProcessBuilder pb = new ProcessBuilder(command);
 
-        // create command
-        String option = getPackageOption();
-        String command = getCLICommand() + getSBIAction(targetID) + option;
-        progressLog.info("COMMAND = " + command);
+            Map<String, String> processEnv = pb.environment();
+            if (processEnv == null)
+            {
+                processEnv = new HashMap<String, String>();
+            }
 
-        // run command
-        progressLog.info(HostUtil.returnExecute(command));
+            // set dependency library path
+            for (Entry<String, String> entry : env.entrySet())
+            {
+                if (entry.getKey().equalsIgnoreCase("PATH"))
+                {
+                    String newPath = entry.getValue();
+                    newPath = MINGW_BIN_PATH + ";" + MSYS_BIN_PATH + ";" + newPath;
+                    processEnv.put(entry.getKey(), newPath);
+                }
+                else
+                {
+                    processEnv.put(entry.getKey(), entry.getValue());
+                }
+            }
+
+            Process proc = null;
+            BufferedReader in = null;
+            try
+            {
+                proc = pb.start();
+                in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+                String resultLine = in.readLine();
+                while (resultLine != null)
+                {
+                    progressLog.info(resultLine);
+                    resultLine = in.readLine();
+                }
 
-        // set TPK path
-        setTpkPath();
+            } catch (Exception e)
+            {
+                progressLog.error("Error occured during running packaging command", e.getMessage());
+            } finally
+            {
+                IOUtil.tryClose(in);
+            }
 
-        return data;
+        }
     }
 
     public void setConfiguration(String configuration)
@@ -118,86 +252,58 @@ public class PackageNativeCLICommand extends AbstractSubCommand<PackageNativeCLI
 
     private void setTpkPath()
     {
-        String tpkPath = workingProjectConfigurationPath + File.separatorChar + packageName + "-" + version + "-" + arch + ".tpk";
+        String tpkPath = workingProjectConfigurationPath + File.separatorChar + packageName + "-" + packageVersion + "-" + arch + ".tpk";
         data.setTpkPath(tpkPath);
         log.debug("Package path: {}", tpkPath);
     }
 
-    // private void setWorkingProjectConfigurationPath(String projectPath)
-    // {
-    // if (new File(projectPath + File.separatorChar + "Debug").isDirectory())
-    // workingProjectConfigurationPath = projectPath + File.separatorChar +
-    // "Debug";
-    // else if (new File(projectPath + File.separatorChar +
-    // "Release").isDirectory())
-    // workingProjectConfigurationPath = projectPath + File.separatorChar +
-    // "Release";
-    // else if (new File(projectPath + File.separatorChar +
-    // "Dynamic Analyzer").isDirectory())
-    // workingProjectConfigurationPath = projectPath + File.separatorChar +
-    // "Dynamic Analyzer";
-    // }
-
-    private String getCLICommand()
+    private String getSBIAction(String targetID)
     {
-        if (OSChecker.isWindows())
+        return String.format("%s %s -- %s", SBI_COMMAND_ACTION, targetID, SBI_COMMAND_BUILD_PACKAGE);
+    }
+
+    private List<String> addPackageOptionToList(PackageOption packageOption, List<String> packageOptionList)
+    {
+        if (packageOption != null)
         {
-            return CMD_CLI_NATIVE_PACK_WINDOWS;
+            packageOptionList.add(packageOption.getBuildTargetPathWithOption());
+            packageOptionList.add(packageOption.getArchWithOption());
+            packageOptionList.add(packageOption.getPackageTypeWithOption());
+            packageOptionList.add(packageOption.getPackageNameWithOption());
+            packageOptionList.add(packageOption.getProjectNameWithOption());
+            packageOptionList.add(packageOption.getArtifactNameWithOption());
+            packageOptionList.add(packageOption.getPackageVersionWithOption());
         }
         else
         {
-            return CMD_CLI_NATIVE_PACK_LINUX;
+            progressLog.error("Packaging option is null");
+            return null;
         }
+        return packageOptionList;
     }
 
-    private String getSBIAction(String targetId)
+    private PackageOption getPackageOption(String workingProjectConfigurationPath, String targetID, Document projectDoc, Document manifestDoc)
     {
-        return String.format(" action %s -- buildpackage", targetId);
-    }
-
-    public String getPackageOption()
-    {
-        targetID = getTargetID(cprojectDoc);
-        String options = null;
+        PackageOption packageOption = null;
 
         try
         {
-            options = String.format(" -BUILD_DIR=\"%s\"" + //$NON-NLS-1$
-                    " -ARCH=%s" + //$NON-NLS-1$
-                    " -PKG_TYPE=%s" + //$NON-NLS-1$
-                    " -PKG_NAME=%s" + //$NON-NLS-1$
-                    " -PRJ_NAME=%s" + //$NON-NLS-1$
-                    " -ARTIFACT_NAME=%s" + //$NON-NLS-1$
-                    " -PKG_VER=%s", //$NON-NLS-1$
-
-                    workingProjectConfigurationPath, arch = SBIModel.getBaseArchName(sbi.getRootstrapArchitecture(targetID)), packageType, packageName = getPackageName(manifestDoc), projectName = getProjectName(projectDoc), projectName, version = getVersion(manifestDoc));
+            packageOption = new PackageOption(workingProjectConfigurationPath, SBIModel.getBaseArchName(sbi.getRootstrapArchitecture(targetID)), packageType, getPackageName(manifestDoc), projectName = getProjectName(projectDoc), projectName, getVersion(manifestDoc));
+
         } catch (SBIException e)
         {
-            e.printStackTrace();
+            progressLog.error("Error occured during getting SBI options", e.getMessage());
+            return null;
         }
-        return options;
+        return packageOption;
     }
 
-    // get document from xml file path
-    private Document getDocument(String path)
+    // get document from file path
+    private Document getDocument(String filePath) throws SAXException, IOException, ParserConfigurationException
     {
-        File cprojectFile = new File(path);
-        try
-        {
-            // create document
-            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(cprojectFile);
-            return document;
-        } catch (SAXException e)
-        {
-            e.printStackTrace();
-        } catch (IOException e)
-        {
-            e.printStackTrace();
-        } catch (ParserConfigurationException e)
-        {
-            e.printStackTrace();
-        }
-        return null;
+        File file = new File(filePath);
+        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
+        return document;
     }
 
     // get target id from document
@@ -279,4 +385,91 @@ public class PackageNativeCLICommand extends AbstractSubCommand<PackageNativeCLI
         }
         return null;
     }
-}
+
+    class PackageOption
+    {
+        private String buildTargetPath;
+        private String arch;
+        private String packageType;
+        private String packageName;
+        private String projectName;
+        private String artifactName;
+        private String packageVersion;
+
+        private final String BUILD_TARGET_PATH_OPTION = "-BUILD_DIR=\"%s\"";
+        private String ARCH_OPTION = "-ARCH=%s";
+        private String PACKAGE_TYPE_OPTION = "-PKG_TYPE=%s";
+        private String PACKAGE_NAME_OPTION = "-PKG_NAME=%s";
+        private String PROJECT_NAME_OPTION = "-PRJ_NAME=%s";
+        private String ARTIFACT_NAME_OPTION = "-ARTIFACT_NAME=%s";
+        private String PACKAGE_VERSION_OPTION = "-PKG_VER=%s";
+
+        private String buildTargetPathWithOption;
+        private String archWithOption;
+        private String packageTypeWithOption;
+        private String packageNameWithOption;
+        private String projectNameWithOption;
+        private String artifactNameWithOption;
+        private String packageVersionWithOption;
+
+        PackageOption(String buildTargetPath, String arch, String packageType, String packageName, String projectName, String artifactName, String packageVersion)
+        {
+            this.buildTargetPath = buildTargetPath;
+            this.arch = arch;
+            this.packageType = packageType;
+            this.packageName = packageName;
+            this.projectName = projectName;
+            this.artifactName = artifactName;
+            this.packageVersion = packageVersion;
+
+            buildTargetPathWithOption = String.format(BUILD_TARGET_PATH_OPTION, buildTargetPath);
+            archWithOption = String.format(ARCH_OPTION, arch);
+            packageTypeWithOption = String.format(PACKAGE_TYPE_OPTION, packageType);
+            packageNameWithOption = String.format(PACKAGE_NAME_OPTION, packageName);
+            projectNameWithOption = String.format(PROJECT_NAME_OPTION, projectName);
+            artifactNameWithOption = String.format(ARTIFACT_NAME_OPTION, artifactName);
+            packageVersionWithOption = String.format(PACKAGE_VERSION_OPTION, packageVersion);
+        }
+
+        public String getBuildTargetPathWithOption()
+        {
+            return buildTargetPathWithOption;
+        }
+
+        public String getArchWithOption()
+        {
+            return archWithOption;
+        }
+
+        public String getPackageTypeWithOption()
+        {
+            return packageTypeWithOption;
+        }
+
+        public String getPackageNameWithOption()
+        {
+            return packageNameWithOption;
+        }
+
+        public String getProjectNameWithOption()
+        {
+            return projectNameWithOption;
+        }
+
+        public String getArtifactNameWithOption()
+        {
+            return artifactNameWithOption;
+        }
+
+        public String getPackageVersionWithOption()
+        {
+            return packageVersionWithOption;
+        }
+
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            return sb.append(buildTargetPathWithOption).append(" ").append(archWithOption).append(" ").append(packageTypeWithOption).append(" ").append(packageNameWithOption).append(" ").append(projectNameWithOption).append(" ").append(artifactNameWithOption).append(" ").append(packageVersionWithOption).toString();
+        }
+    }
+}
\ No newline at end of file