Search in sources :

Example 1 with Engine

use of com.twinsoft.convertigo.engine.Engine in project convertigo by convertigo.

the class BuildLocally method runBuild.

public Status runBuild(String option, boolean run, String target) {
    try {
        File cordovaDir = getCordovaDir();
        File wwwDir = new File(cordovaDir, "www");
        wwwDir.mkdirs();
        if (!wwwDir.exists()) {
            throw new EngineException("Cannot create the build folder '" + wwwDir.getAbsolutePath() + "', check the current user can create files or change the Eclipse preference: 'Convertigo/Studio/Local Build Folder'.");
        }
        // Cordova environment is already created, we have to build
        // Step 1: Call Mobile packager to prepare the source package
        MobileResourceHelper mobileResourceHelper = new MobileResourceHelper(mobilePlatform, wwwDir.getAbsolutePath());
        wwwDir = mobileResourceHelper.preparePackage();
        // Step 2: Add platform and read config.xml to copy needed icons and splash resources
        String cordovaPlatform = mobilePlatform.getCordovaPlatform();
        // 
        FileUtils.copyFile(new File(wwwDir, "config.xml"), new File(cordovaDir, "config.xml"));
        FileUtils.deleteQuietly(new File(wwwDir, "config.xml"));
        if (iosProvisioningProfileUUID != null) {
            boolean release = "release".equals(option);
            JSONObject json = new JSONObject();
            if (iosSignIdentity != null) {
                json.put("codeSignIdentity", iosSignIdentity);
            } else {
                json.put("codeSignIdentity", release ? "iPhone Distribution" : "iPhone Developer");
            }
            json.put("provisioningProfile", iosProvisioningProfileUUID);
            json = new JSONObject().put(release ? "release" : "debug", json);
            json = new JSONObject().put("ios", json);
            FileUtils.write(new File(cordovaDir, "build.json"), json.toString(2), "utf-8");
        }
        if (androidKeystore != null) {
            boolean release = "release".equals(option);
            JSONObject json = new JSONObject();
            json.put("keystore", androidKeystore.getAbsolutePath());
            json.put("storePassword", androidKeystorePassword);
            json.put("alias", androidAlias);
            json.put("password", androidPassword);
            json.put("keystoreType", "");
            json = new JSONObject().put(release ? "release" : "debug", json);
            json = new JSONObject().put("android", json);
            FileUtils.write(new File(cordovaDir, "build.json"), json.toString(2), "utf-8");
        }
        processConfigXMLResources(wwwDir, cordovaDir);
        List<String> commandsList = new LinkedList<String>();
        if (mobilePlatform instanceof Windows) {
            File configFile = new File(cordovaDir, "config.xml");
            Document doc = XMLUtils.loadXml(configFile);
            TwsCachedXPathAPI xpathApi = new TwsCachedXPathAPI();
            Element singleElement = (Element) xpathApi.selectSingleNode(doc, "/widget/engine[@name='windows']");
            if (singleElement == null) {
                throw new Exception("The tag 'engine' is not specified in the file config.xml.");
            }
            String appx = singleElement.getAttribute("appx");
            String archs = singleElement.getAttribute("archs");
            if (appx == null || archs == null || appx.isEmpty() || archs.isEmpty()) {
                throw new Exception("The attributes 'appx' and 'archs' are not specified in the tag engine.");
            }
            commandsList.add("--");
            commandsList.add("--appx=" + appx);
            commandsList.add("--archs=" + archs);
        }
        jdk8Dir = null;
        if ("android".equals(mobilePlatform.getCordovaPlatform())) {
            Engine.logEngine.info("Check or install for the JDK8 to build the Android application");
            jdk8Dir = ProcessUtils.getJDK8((pBytesRead, pContentLength, pItems) -> {
                Engine.logEngine.info("download JDK8: " + Math.round(100f * pBytesRead / pContentLength) + "% [" + pBytesRead + "/" + pContentLength + "]");
            });
            androidSdkDir = ProcessUtils.getAndroidSDK(preferedAndroidBuildTools, (pBytesRead, pContentLength, pItems) -> {
                Engine.logEngine.info("download Android SDK: " + Math.round(100f * pBytesRead / pContentLength) + "% [" + pBytesRead + "/" + pContentLength + "]");
            });
            gradleDir = ProcessUtils.getGradle((pBytesRead, pContentLength, pItems) -> {
                Engine.logEngine.info("download Gradle: " + Math.round(100f * pBytesRead / pContentLength) + "% [" + pBytesRead + "/" + pContentLength + "]");
            });
        }
        runCordovaCommand(cordovaDir, "prepare", cordovaPlatform);
        // Step 3: Build or Run using Cordova the specific platform.
        if (run) {
            commandsList.add(0, "run");
            commandsList.add(1, cordovaPlatform);
            commandsList.add(2, "--" + option);
            commandsList.add(3, "--" + target);
            runCordovaCommand(cordovaDir, commandsList);
        } else {
            commandsList.add(0, "build");
            commandsList.add(1, cordovaPlatform);
            commandsList.add(2, "--" + option);
            commandsList.add(3, "--" + target);
            String out = runCordovaCommand(cordovaDir, commandsList);
            Matcher m = Pattern.compile("[^\\s]+(?:\\.apk|/build/device)").matcher(out);
            if (m.find()) {
                File pkg;
                do {
                    pkg = new File(m.group());
                    if (pkg.exists()) {
                        if (pkg.getName().equals("device")) {
                            for (File f : pkg.listFiles()) {
                                if (f.getName().endsWith(".ipa")) {
                                    pkg = f;
                                    break;
                                }
                            }
                        }
                        mobilePackage = pkg;
                    }
                } while (m.find());
            }
            // Step 4: Show dialog with path to apk/ipa/xap
            if (!processCanceled) {
                showLocationInstallFile(mobilePlatform, process.exitValue(), errorLines, option);
            }
        }
        return Status.OK;
    } catch (Throwable e) {
        logException(e, "Error when processing Cordova build: " + e);
        return Status.CANCEL;
    }
}
Also used : TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) Attr(org.w3c.dom.Attr) Matcher(java.util.regex.Matcher) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) NamedNodeMap(org.w3c.dom.NamedNodeMap) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) Engine(com.twinsoft.convertigo.engine.Engine) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) WindowsPhone8(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8) File(java.io.File) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) List(java.util.List) Element(org.w3c.dom.Element) NodeIterator(org.w3c.dom.traversal.NodeIterator) MobileResourceHelper(com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper) XMLUtils(com.twinsoft.convertigo.engine.util.XMLUtils) MobilePlatform(com.twinsoft.convertigo.beans.core.MobilePlatform) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) EngineException(com.twinsoft.convertigo.engine.EngineException) Collections(java.util.Collections) ProcessUtils(com.twinsoft.convertigo.engine.util.ProcessUtils) Matcher(java.util.regex.Matcher) Element(org.w3c.dom.Element) EngineException(com.twinsoft.convertigo.engine.EngineException) Windows(com.twinsoft.convertigo.beans.mobileplatforms.Windows) MobileResourceHelper(com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONObject(org.codehaus.jettison.json.JSONObject) File(java.io.File) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Aggregations

MobileApplication (com.twinsoft.convertigo.beans.core.MobileApplication)1 MobilePlatform (com.twinsoft.convertigo.beans.core.MobilePlatform)1 Android (com.twinsoft.convertigo.beans.mobileplatforms.Android)1 IOs (com.twinsoft.convertigo.beans.mobileplatforms.IOs)1 Windows (com.twinsoft.convertigo.beans.mobileplatforms.Windows)1 WindowsPhone8 (com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhone8)1 Engine (com.twinsoft.convertigo.engine.Engine)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 MobileResourceHelper (com.twinsoft.convertigo.engine.admin.services.mobiles.MobileResourceHelper)1 ProcessUtils (com.twinsoft.convertigo.engine.util.ProcessUtils)1 TwsCachedXPathAPI (com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)1 XMLUtils (com.twinsoft.convertigo.engine.util.XMLUtils)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1