From 437daa368ad10d544959ff4a32f12c4f9b10de44 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 10 Sep 2013 16:31:42 -0400 Subject: [PATCH] [CB-4782] Convert ApplicationInfo.java -> appinfo.js --- bin/lib/create.js | 12 +--- .../ApplicationInfo/ApplicationInfo.java | 61 ------------------- bin/templates/cordova/lib/appinfo.js | 41 +++++++++++++ bin/templates/cordova/lib/device.js | 14 ++--- bin/templates/cordova/lib/emulator.js | 13 ++-- bin/update | 8 --- bin/update.js | 13 ---- 7 files changed, 50 insertions(+), 112 deletions(-) delete mode 100644 bin/templates/cordova/ApplicationInfo/ApplicationInfo.java create mode 100644 bin/templates/cordova/lib/appinfo.js diff --git a/bin/lib/create.js b/bin/lib/create.js index 955de1a3..8754f971 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -126,22 +126,11 @@ module.exports.run = function(project_path, package_name, project_name, project_ replaceInFile(manifest_path, /__APILEVEL__/, target_api.split('-')[1]); var cordova_path = path.join(ROOT, 'bin', 'templates', 'cordova'); - // create app info jar - if(!fs.existsSync(path.join(cordova_path, 'appinfo.jar'))) { - console.log('Creating appinfo.jar...'); - var cwd = process.cwd(); - process.chdir(path.join(cordova_path, 'ApplicationInfo')); - exec('javac ApplicationInfo.java'); - exec('jar -cfe ' + path.join(cordova_path, 'appinfo.jar') + ' ApplicationInfo ApplicationInfo.class'); - process.chdir(cwd); - } - // creating cordova folder and copying run/build/log/launch/check_reqs scripts var lib_path = path.join(cordova_path, 'lib'); shell.mkdir(path.join(project_path, 'cordova')); shell.mkdir(path.join(project_path, 'cordova', 'lib')); - shell.cp(path.join(cordova_path, 'appinfo.jar'), path.join(project_path, 'cordova', 'appinfo.jar')); shell.cp(path.join(cordova_path, 'build'), path.join(project_path, 'cordova', 'build')); shell.chmod(755, path.join(project_path, 'cordova', 'build')); shell.cp(path.join(cordova_path, 'clean'), path.join(project_path, 'cordova', 'clean')); @@ -155,6 +144,7 @@ module.exports.run = function(project_path, package_name, project_name, project_ shell.cp(path.join(ROOT, 'bin', 'check_reqs'), path.join(project_path, 'cordova', 'check_reqs')); shell.chmod(755, path.join(project_path, 'cordova', 'check_reqs')); + shell.cp(path.join(lib_path, 'appinfo.js'), path.join(project_path, 'cordova', 'lib', 'appinfo.js')); shell.cp(path.join(lib_path, 'build.js'), path.join(project_path, 'cordova', 'lib', 'build.js')); shell.cp(path.join(ROOT, 'bin', 'lib', 'check_reqs.js'), path.join(project_path, 'cordova', 'lib', 'check_reqs.js')); shell.cp(path.join(lib_path, 'clean.js'), path.join(project_path, 'cordova', 'lib', 'clean.js')); diff --git a/bin/templates/cordova/ApplicationInfo/ApplicationInfo.java b/bin/templates/cordova/ApplicationInfo/ApplicationInfo.java deleted file mode 100644 index c344b651..00000000 --- a/bin/templates/cordova/ApplicationInfo/ApplicationInfo.java +++ /dev/null @@ -1,61 +0,0 @@ -// 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. - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import org.xml.sax.SAXException; -import java.io.IOException; - -public class ApplicationInfo { - private static void parseAndroidManifest(String path) { - // System.out.println(path); - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - Document dom; - try { - DocumentBuilder db = dbf.newDocumentBuilder(); - dom = db.parse(path); - - // getting package information - Element manifest = dom.getDocumentElement(); - String pakkage = manifest.getAttribute("package"); - - // getting activity name - String activity = ((Element)dom.getElementsByTagName("activity").item(0)).getAttribute("android:name"); - System.out.println(String.format("%s/.%s", pakkage, activity.replace(".", ""))); - } catch(ParserConfigurationException pce) { - pce.printStackTrace(); - } catch(SAXException se) { - se.printStackTrace(); - } catch(IOException ioe) { - ioe.printStackTrace(); - } - - } - - public static void main(String[] args) { - String path; - if(args.length > 0) { - path = args[0]; - } else { - path = System.getProperty("user.dir") + "/../AndroidManifest.xml"; - } - parseAndroidManifest(path); - } -} diff --git a/bin/templates/cordova/lib/appinfo.js b/bin/templates/cordova/lib/appinfo.js new file mode 100644 index 00000000..1f8ebe2c --- /dev/null +++ b/bin/templates/cordova/lib/appinfo.js @@ -0,0 +1,41 @@ +#!/usr/bin/env node + +/* + 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. +*/ + +var path = require('path'); +var fs = require('fs'); +var cachedAppInfo = null; + +function readAppInfoFromManifest() { + var manifestPath = path.join(__dirname, '..', '..', 'AndroidManifest.xml'); + var manifestData = fs.readFileSync(manifestPath, {encoding:'utf8'}); + var packageName = /\bpackage\s*=\s*"(.+?)"/.exec(manifestData); + if (!packageName) throw new Error('Could not find package name within ' + manifestPath); + var activityTag = //.exec(manifestData); + if (!activityTag) throw new Error('Could not find within ' + manifestPath); + var activityName = /\bandroid:name\s*=\s*"(.+?)"/.exec(activityTag); + if (!activityName) throw new Error('Could not find android:name within ' + manifestPath); + + return packageName[1] + '/.' + activityName[1]; +} + +exports.getActivityName = function() { + return cachedAppInfo = cachedAppInfo || readAppInfoFromManifest(); +}; diff --git a/bin/templates/cordova/lib/device.js b/bin/templates/cordova/lib/device.js index 900baf43..46686b68 100644 --- a/bin/templates/cordova/lib/device.js +++ b/bin/templates/cordova/lib/device.js @@ -22,6 +22,7 @@ var shell = require('shelljs'), path = require('path'), build = require('./build'), + appinfo = require('./appinfo'), exec = require('child_process').exec, ROOT = path.join(__dirname, '..', '..'); @@ -57,14 +58,7 @@ module.exports.install = function(target) { target = typeof target !== 'undefined' ? target : device_list[0]; if (device_list.indexOf(target) > -1) { var apk_path = build.get_apk(); - var cmd = 'java -jar ' + path.join(ROOT, 'cordova', 'appinfo.jar') + ' ' + path.join(ROOT, 'AndroidManifest.xml'); - var launch_name = shell.exec(cmd, {silent:true, async:false}); - if (launch_name.error) { - console.log('ERROR : Failed to get application name from appinfo.jar + AndroidManifest : '); - console.log("Output : " + launch_name.output); - process.exit(2); - } - + var launchName = appinfo.getActivityName(); console.log('Installing app on device...'); cmd = 'adb -s ' + target + ' install -r ' + apk_path; var install = shell.exec(cmd, {silent:false, async:false}); @@ -80,7 +74,7 @@ module.exports.install = function(target) { // launch the application console.log('Launching application...'); - cmd = 'adb -s ' + target + ' shell am start -W -a android.intent.action.MAIN -n ' + launch_name.output.replace('\r', '').replace('\n', ''); + cmd = 'adb -s ' + target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName; var launch = shell.exec(cmd, {silent:true, async:false}); if(launch.code > 0) { console.error('ERROR : Failed to launch application on emulator : ' + launch.error); @@ -98,4 +92,4 @@ module.exports.install = function(target) { console.error('ERROR : Failed to deploy to device, no devices found.'); process.exit(2); } -} \ No newline at end of file +} diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js index 579c898f..6f8a7dd5 100644 --- a/bin/templates/cordova/lib/emulator.js +++ b/bin/templates/cordova/lib/emulator.js @@ -21,6 +21,7 @@ var shell = require('shelljs'), path = require('path'), + appinfo = require('./appinfo'), build = require('./build'), ROOT = path.join(__dirname, '..', '..'), new_emulator = 'cordova_emulator'; @@ -317,14 +318,8 @@ module.exports.install = function(target) { // launch the application console.log('Launching application...'); - cmd = 'java -jar ' + path.join(ROOT, 'cordova', 'appinfo.jar') + ' ' + path.join(ROOT, 'AndroidManifest.xml'); - var launch_name = shell.exec(cmd, {silent:true, async:false}); - if (launch_name.error) { - console.log('ERROR : Failed to get application name from appinfo.jar + AndroidManifest : '); - console.log("Output : " + launch_name.output); - process.exit(2); - } - cmd = 'adb -s ' + target + ' shell am start -W -a android.intent.action.MAIN -n ' + launch_name.output.replace('\r', '').replace('\n', ''); + var launchName = appinfo.getActivityName(); + cmd = 'adb -s ' + target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName; console.log(cmd); var launch = shell.exec(cmd, {silent:false, async:false}); if(launch.code > 0) { @@ -339,4 +334,4 @@ module.exports.install = function(target) { console.error('Failed to deploy to emulator.'); process.exit(2); } -} \ No newline at end of file +} diff --git a/bin/update b/bin/update index 8e7503db..3fe7f9cc 100755 --- a/bin/update +++ b/bin/update @@ -51,13 +51,6 @@ function on_exit { fi } -function createAppInfoJar { - (cd "$BUILD_PATH"/bin/templates/cordova/ApplicationInfo && - javac ApplicationInfo.java && - jar -cfe ../appinfo.jar ApplicationInfo ApplicationInfo.class - ) -} - function on_error { echo "An unexpected error occurred: $previous_command exited with $?" exit 1 @@ -119,7 +112,6 @@ then mkdir "$PROJECT_PATH"/cordova mkdir "$PROJECT_PATH"/cordova/lib fi -cp "$BUILD_PATH"/bin/templates/cordova/appinfo.jar "$PROJECT_PATH"/cordova/appinfo.jar cp "$BUILD_PATH"/bin/templates/cordova/build "$PROJECT_PATH"/cordova/build cp "$BUILD_PATH"/bin/templates/cordova/clean "$PROJECT_PATH"/cordova/clean cp "$BUILD_PATH"/bin/templates/cordova/log "$PROJECT_PATH"/cordova/log diff --git a/bin/update.js b/bin/update.js index 8396d6fe..e73d2f7f 100644 --- a/bin/update.js +++ b/bin/update.js @@ -76,17 +76,6 @@ function exec(command) { } } -function createAppInfoJar() { - if(!fso.FileExists(ROOT+"\\bin\\templates\\cordova\\appinfo.jar")) { - WScript.Echo("Creating appinfo.jar..."); - var cur = shell.CurrentDirectory; - shell.CurrentDirectory = ROOT+"\\bin\\templates\\cordova\\ApplicationInfo"; - exec("javac ApplicationInfo.java"); - exec("jar -cfe ..\\appinfo.jar ApplicationInfo ApplicationInfo.class"); - shell.CurrentDirectory = cur; - } -} - function cleanup() { if(fso.FileExists(ROOT + '\\framework\\cordova-'+VERSION+'.jar')) { fso.DeleteFile(ROOT + '\\framework\\cordova-'+VERSION+'.jar'); @@ -138,9 +127,7 @@ if(fso.FolderExists(ROOT + '\\framework')) { } // update cordova scripts -createAppInfoJar(); WScript.Echo("Copying cordova command tools..."); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\appinfo.jar ' + PROJECT_PATH + '\\cordova\\appinfo.jar /Y'); exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\cordova.bat ' + PROJECT_PATH + '\\cordova\\cordova.bat /Y'); exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\clean.bat ' + PROJECT_PATH + '\\cordova\\clean.bat /Y'); exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\build.bat ' + PROJECT_PATH + '\\cordova\\build.bat /Y');