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;
}
}
Aggregations