diff --git a/bin/create b/bin/create index 9c4586ca..020bf862 100755 --- a/bin/create +++ b/bin/create @@ -18,15 +18,19 @@ specific language governing permissions and limitations under the License. */ -var path = require('path'); -var args = process.argv; +var path = require('path'); var create = require('./lib/create'); +var args = process.argv; // Support basic help commands if(args.length < 3 || (args[2] == '--help' || args[2] == '/?' || args[2] == '-h' || args[2] == 'help' || args[2] == '-help' || args[2] == '/help')) { - create.help(); + console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'create')) + ' '); + console.log(' : Path to your new Cordova Android project'); + console.log(' : Package name, following reverse-domain style convention'); + console.log(' : Project name'); + process.exit(1); } else { - create.run(args[2], args[3], args[4], args[5]); + create.createProject(args[2], args[3], args[4], args[5]); } diff --git a/bin/lib/create.js b/bin/lib/create.js index 3a486f3e..fc1f8da2 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -24,11 +24,6 @@ var shell = require('shelljs'), check_reqs = require('./check_reqs'), ROOT = path.join(__dirname, '..', '..'); - -/* - * HELPER FUNCTIONS - */ - function exec(command) { var result; try { @@ -47,6 +42,13 @@ function exec(command) { } } +function setShellFatal(value, func) { + var oldVal = shell.config.fatal; + shell.config.fatal = value; + func(); + shell.config.fatal = oldVal; +} + function ensureJarIsBuilt(version, target_api) { if (!fs.existsSync(path.join(ROOT, 'framework', 'cordova-' + version + '.jar')) && fs.existsSync(path.join(ROOT, 'framework'))) { var valid_target = check_reqs.get_target(); @@ -59,7 +61,41 @@ function ensureJarIsBuilt(version, target_api) { exec('ant jar'); process.chdir(cwd); } -}; +} + +function copyJsAndJar(projectPath, version) { + shell.cp(path.join(ROOT, 'framework', 'assets', 'www', 'cordova.js'), path.join(projectPath, 'assets', 'www', 'cordova.js')); + // Don't fail if there are no old jars. + setShellFatal(false, function() { + shell.ls(path.join(projectPath, 'libs', 'cordova-*.jar')).forEach(function(oldJar) { + shell.rm('-f', path.join(projectPath, 'libs', oldJar)); + }); + }); + shell.cp(path.join(ROOT, 'framework', 'cordova-' + version + '.jar'), path.join(projectPath, 'libs', 'cordova-' + version + '.jar')); +} + +function copyScripts(projectPath) { + var srcScriptsDir = path.join(ROOT, 'bin', 'templates', 'cordova'); + var destScriptsDir = path.join(projectPath, 'cordova'); + // Delete old scripts directory if this is an update. + shell.rm('-rf', destScriptsDir); + // Copy in the new ones. + shell.cp('-r', srcScriptsDir, projectPath); + shell.cp('-r', path.join(ROOT, 'bin', 'node_modules'), destScriptsDir); + shell.cp(path.join(ROOT, 'bin', 'check_reqs'), path.join(destScriptsDir, 'check_reqs')); + shell.cp(path.join(ROOT, 'bin', 'lib', 'check_reqs.js'), path.join(projectPath, 'cordova', 'lib', 'check_reqs.js')); + + if (!/^win/.test(process.platform)) { + // Ensure they are all executable and delete .bat files. + shell.find(destScriptsDir).forEach(function(p) { + if (/\.bat$/.test(p)) { + shell.rm(p); + } else { + shell.chmod(755, p); + } + }); + } +} /** * $ create [options] @@ -74,12 +110,12 @@ function ensureJarIsBuilt(version, target_api) { * - 'project_template_dir' {String} Path to project template (override). */ -module.exports.run = function(project_path, package_name, project_name, project_template_dir) { - - var VERSION = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf-8'); +exports.createProject = function(project_path, package_name, project_name, project_template_dir) { + var VERSION = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf-8').trim(); // Set default values for path, package and name project_path = typeof project_path !== 'undefined' ? project_path : "CordovaExample"; + project_path = path.relative(process.cwd(), project_path); package_name = typeof package_name !== 'undefined' ? package_name : 'my.cordova.project'; project_name = typeof project_name !== 'undefined' ? project_name : 'CordovaExample'; project_template_dir = typeof project_template_dir !== 'undefined' ? @@ -112,7 +148,7 @@ module.exports.run = function(project_path, package_name, project_name, project_ // Log the given values for the project console.log('Creating Cordova project for the Android platform :'); - console.log('\tPath : ' + path.relative(process.cwd(), project_path)); + console.log('\tPath : ' + project_path); console.log('\tPackage : ' + package_name); console.log('\tName : ' + project_name); console.log('\tAndroid target : ' + target_api); @@ -121,118 +157,47 @@ module.exports.run = function(project_path, package_name, project_name, project_ ensureJarIsBuilt(VERSION, target_api); // create new android project - var create_cmd = 'android create project --target "'+target_api+'" --path "'+path.relative(process.cwd(), project_path)+'" --package "'+package_name+'" --activity "'+safe_activity_name+'"'; + var create_cmd = 'android create project --target "'+target_api+'" --path "'+ project_path+'" --package "'+package_name+'" --activity "'+safe_activity_name+'"'; exec(create_cmd); console.log('Copying template files...'); - // Automatically fail if any commands fail. - shell.config.fatal = true; - // copy project template - shell.cp('-r', path.join(project_template_dir, 'assets'), project_path); - shell.cp('-r', path.join(project_template_dir, 'res'), project_path); + setShellFatal(true, function() { + // copy project template + shell.cp('-r', path.join(project_template_dir, 'assets'), project_path); + shell.cp('-r', path.join(project_template_dir, 'res'), project_path); - // copy cordova.js, cordova.jar and res/xml - if(fs.existsSync(path.join(ROOT, 'framework'))) { + // copy cordova.js, cordova.jar and res/xml shell.cp('-r', path.join(ROOT, 'framework', 'res', 'xml'), path.join(project_path, 'res')); - shell.cp(path.join(ROOT, 'framework', 'assets', 'www', 'cordova.js'), path.join(project_path, 'assets', 'www', 'cordova.js')); - shell.cp(path.join(ROOT, 'framework', 'cordova-' + VERSION + '.jar'), path.join(project_path, 'libs', 'cordova-' + VERSION + '.jar')); - } else { - shell.cp('-r', path.join(ROOT, 'xml'), path.join(project_path, 'res')); - shell.cp(path.join(ROOT, 'cordova.js'), path.join(project_path, 'assets', 'www', 'cordova.js')); - shell.cp(path.join(ROOT, 'cordova-' + VERSION + '.jar'), path.join(project_path, 'libs', 'cordova-' + VERSION + '.jar')); - } + copyJsAndJar(project_path, VERSION); - // interpolate the activity name and package - shell.mkdir('-p', activity_dir); - shell.cp('-f', path.join(project_template_dir, 'Activity.java'), activity_path); - shell.sed('-i', /__ACTIVITY__/, safe_activity_name, activity_path); - shell.sed('-i', /__ID__/, package_name, activity_path); + // interpolate the activity name and package + shell.mkdir('-p', activity_dir); + shell.cp('-f', path.join(project_template_dir, 'Activity.java'), activity_path); + shell.sed('-i', /__ACTIVITY__/, safe_activity_name, activity_path); + shell.sed('-i', /__ID__/, package_name, activity_path); - // interpolate the app name into strings.xml - shell.sed('-i', />Cordova' + project_name + '<', strings_path); + // interpolate the app name into strings.xml + shell.sed('-i', />Cordova' + project_name + '<', strings_path); - shell.cp('-f', path.join(project_template_dir, 'AndroidManifest.xml'), manifest_path); - shell.sed('-i', /__ACTIVITY__/, safe_activity_name, manifest_path); - shell.sed('-i', /__PACKAGE__/, package_name, manifest_path); - shell.sed('-i', /__APILEVEL__/, target_api.split('-')[1], manifest_path); - - var cordova_path = path.join(ROOT, 'bin', 'templates', 'cordova'); - // 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, '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')); - shell.chmod(755, path.join(project_path, 'cordova', 'clean')); - shell.cp(path.join(cordova_path, 'log'), path.join(project_path, 'cordova', 'log')); - shell.chmod(755, path.join(project_path, 'cordova', 'log')); - shell.cp(path.join(cordova_path, 'run'), path.join(project_path, 'cordova', 'run')); - shell.chmod(755, path.join(project_path, 'cordova', 'run')); - shell.cp(path.join(cordova_path, 'version'), path.join(project_path, 'cordova', 'version')); - shell.chmod(755, path.join(project_path, 'cordova', 'version')); - 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')); - shell.cp(path.join(lib_path, 'device.js'), path.join(project_path, 'cordova', 'lib', 'device.js')); - shell.cp(path.join(lib_path, 'emulator.js'), path.join(project_path, 'cordova', 'lib', 'emulator.js')); - shell.cp(path.join(lib_path, 'log.js'), path.join(project_path, 'cordova', 'lib', 'log.js')); - shell.cp(path.join(lib_path, 'run.js'), path.join(project_path, 'cordova', 'lib', 'run.js')); - shell.cp(path.join(lib_path, 'install-device'), path.join(project_path, 'cordova', 'lib', 'install-device')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'install-device')); - shell.cp(path.join(lib_path, 'install-emulator'), path.join(project_path, 'cordova', 'lib', 'install-emulator')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'install-emulator')); - shell.cp(path.join(lib_path, 'list-devices'), path.join(project_path, 'cordova', 'lib', 'list-devices')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'list-devices')); - shell.cp(path.join(lib_path, 'list-emulator-images'), path.join(project_path, 'cordova', 'lib', 'list-emulator-images')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'list-emulator-images')); - shell.cp(path.join(lib_path, 'list-started-emulators'), path.join(project_path, 'cordova', 'lib', 'list-started-emulators')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'list-started-emulators')); - shell.cp(path.join(lib_path, 'start-emulator'), path.join(project_path, 'cordova', 'lib', 'start-emulator')); - shell.chmod(755, path.join(project_path, 'cordova', 'lib', 'start-emulator')); - - // if on windows, copy .bat scripts - // TODO : make these not nessesary, they clutter the scripting folder. - if(process.platform == 'win32' || process.platform == 'win64') { - shell.cp(path.join(cordova_path, 'build.bat'), path.join(project_path, 'cordova', 'build.bat')); - shell.cp(path.join(cordova_path, 'clean.bat'), path.join(project_path, 'cordova', 'clean.bat')); - shell.cp(path.join(cordova_path, 'log.bat'), path.join(project_path, 'cordova', 'log.bat')); - shell.cp(path.join(cordova_path, 'run.bat'), path.join(project_path, 'cordova', 'run.bat')); - shell.cp(path.join(cordova_path, 'version.bat'), path.join(project_path, 'cordova', 'version.bat')); - shell.cp(path.join(ROOT, 'bin', 'check_reqs.bat'), path.join(project_path, 'cordova', 'check_reqs.bat')); - - // lib scripts - shell.cp(path.join(lib_path, 'install-device.bat'), path.join(project_path, 'cordova', 'lib', 'install-device.bat')); - shell.cp(path.join(lib_path, 'install-emulator.bat'), path.join(project_path, 'cordova', 'lib', 'install-emulator.bat')); - shell.cp(path.join(lib_path, 'list-devices.bat'), path.join(project_path, 'cordova', 'lib', 'list-devices.bat')); - shell.cp(path.join(lib_path, 'list-emulator-images.bat'), path.join(project_path, 'cordova', 'lib', 'list-emulator-images.bat')); - shell.cp(path.join(lib_path, 'list-started-emulators.bat'), path.join(project_path, 'cordova', 'lib', 'list-started-emulators.bat')); - shell.cp(path.join(lib_path, 'start-emulator.bat'), path.join(project_path, 'cordova', 'lib', 'start-emulator.bat')); - } - - // copy node related files - shell.cp(path.join(ROOT, 'bin', 'package.json'), path.join(project_path, 'cordova', 'package.json')); - shell.cp('-r', path.join(ROOT, 'bin', 'node_modules'), path.join(project_path, 'cordova')); + shell.cp('-f', path.join(project_template_dir, 'AndroidManifest.xml'), manifest_path); + shell.sed('-i', /__ACTIVITY__/, safe_activity_name, manifest_path); + shell.sed('-i', /__PACKAGE__/, package_name, manifest_path); + shell.sed('-i', /__APILEVEL__/, target_api.split('-')[1], manifest_path); + copyScripts(project_path); + }); } -/** - * Usage information. - **/ - -module.exports.help = function() { - console.log('Usage: ' + path.relative(process.cwd(), path.join(ROOT, 'bin', 'create')) + ' '); - console.log('Make sure the Android SDK tools folder is in your PATH!'); - console.log(' : Path to your new Cordova Android project'); - console.log(' : Package name, following reverse-domain style convention'); - console.log(' : Project name'); - process.exit(0); -} - - +exports.updateProject = function(projectPath) { + // Check that requirements are met and proper targets are installed + if (!check_reqs.run()) { + process.exit(2); + } + var version = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf-8').trim(); + var target_api = check_reqs.get_target(); + ensureJarIsBuilt(version, target_api); + copyJsAndJar(projectPath, version); + copyScripts(projectPath); + console.log('Android project is now at version ' + version); +}; diff --git a/bin/update b/bin/update index 3fe7f9cc..aabe7dbe 100755 --- a/bin/update +++ b/bin/update @@ -1,125 +1,33 @@ -#! /bin/bash -# 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. -# -# update a cordova/android project's command line tools -# -# USAGE -# ./update [path] -# +#!/usr/bin/env node -set -e +/* + 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 -if [ -z "$1" ] || [ "$1" == "-h" ] -then - echo 'usage: update path' - echo "Make sure the Android SDK tools folder is in your PATH!" - exit 0 -fi + http://www.apache.org/licenses/LICENSE-2.0 -BUILD_PATH="$( cd "$( dirname "$0" )/.." && pwd )" -VERSION=$(cat "$BUILD_PATH"/VERSION) + 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 args = process.argv; +var create = require('./lib/create'); -PROJECT_PATH="${1:-'./example'}" - -if [ ! -d "$PROJECT_PATH" ] -then - echo "The project path has to exist for it to be updated" - exit 0 -fi - - -# cleanup after exit and/or on error -function on_exit { - if [ -f "$BUILD_PATH"/framework/cordova-$VERSION.jar ] - then - rm "$BUILD_PATH"/framework/cordova-$VERSION.jar - fi +// Support basic help commands +if(args.length < 3 || (args[2] == '--help' || args[2] == '/?' || args[2] == '-h' || + args[2] == 'help' || args[2] == '-help' || args[2] == '/help')) { + console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'update')) + ' '); + process.exit(1); +} else { + create.updateProject(args[2]); } -function on_error { - echo "An unexpected error occurred: $previous_command exited with $?" - exit 1 -} - -function replace { - local pattern=$1 - local filename=$2 - # Mac OS X requires -i argument - if [[ "$OSTYPE" =~ "darwin" ]] - then - /usr/bin/sed -i '' -e $pattern "$filename" - elif [[ "$OSTYPE" =~ "linux" ]] - then - /bin/sed -i -e $pattern "$filename" - fi -} - -# we do not want the script to silently fail -trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG -trap on_error ERR -trap on_exit EXIT - -ANDROID_BIN="${ANDROID_BIN:=$( which android )}" - -TARGET=$("$ANDROID_BIN" list targets | grep id: | tail -1 | cut -f 2 -d ' ' ) -API_LEVEL=$("$ANDROID_BIN" list target | grep "API level:" | tail -n 1 | cut -f 2 -d ':' | tr -d ' ') - -# check that build targets exist -if [ -z "$TARGET" ] || [ -z "$API_LEVEL" ] -then - echo "No Android Targets are installed. Please install at least one via the android SDK" - exit 1 -fi - -# if this a distribution release no need to build a jar -if [ ! -e "$BUILD_PATH"/cordova-$VERSION.jar ] && [ -d "$BUILD_PATH"/framework ] -then -# update the cordova-android framework for the desired target - "$ANDROID_BIN" update project --target $TARGET --path "$BUILD_PATH"/framework &> /dev/null - -# compile cordova.js and cordova.jar - (cd "$BUILD_PATH"/framework && ant jar &> /dev/null ) -fi - -# copy cordova.js, cordova.jar and res/xml -if [ -d "$BUILD_PATH"/framework ] -then - cp "$BUILD_PATH"/framework/assets/www/cordova.js "$PROJECT_PATH"/assets/www/cordova.js - cp "$BUILD_PATH"/framework/cordova-$VERSION.jar "$PROJECT_PATH"/libs/cordova-$VERSION.jar -else - cp "$BUILD_PATH"/cordova.js "$PROJECT_PATH"/assets/www/cordova.js - cp "$BUILD_PATH"/cordova-$VERSION.jar "$PROJECT_PATH"/libs/cordova-$VERSION.jar -fi - -# creating cordova folder and copying run/build/log/launch scripts -if [ ! -e "$PROJECT_PATH/cordova" ] -then - mkdir "$PROJECT_PATH"/cordova - mkdir "$PROJECT_PATH"/cordova/lib -fi -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 -cp "$BUILD_PATH"/bin/templates/cordova/run "$PROJECT_PATH"/cordova/run -cp "$BUILD_PATH"/bin/templates/cordova/lib/cordova.js "$PROJECT_PATH"/cordova/lib/cordova.js -cp "$BUILD_PATH"/bin/templates/cordova/lib/install-device "$PROJECT_PATH"/cordova/lib/install-device -cp "$BUILD_PATH"/bin/templates/cordova/lib/install-emulator "$PROJECT_PATH"/cordova/lib/install-emulator -cp "$BUILD_PATH"/bin/templates/cordova/lib/list-devices "$PROJECT_PATH"/cordova/lib/list-devices -cp "$BUILD_PATH"/bin/templates/cordova/lib/list-emulator-images "$PROJECT_PATH"/cordova/lib/list-emulator-images -cp "$BUILD_PATH"/bin/templates/cordova/lib/list-started-emulators "$PROJECT_PATH"/cordova/lib/list-started-emulators -cp "$BUILD_PATH"/bin/templates/cordova/lib/start-emulator "$PROJECT_PATH"/cordova/lib/start-emulator diff --git a/bin/update.bat b/bin/update.bat index a61fd269..d0aa7a01 100644 --- a/bin/update.bat +++ b/bin/update.bat @@ -16,17 +16,11 @@ :: under the License. @ECHO OFF -IF NOT DEFINED JAVA_HOME GOTO MISSING -FOR %%X in (java.exe javac.exe ant.bat android.bat) do ( - SET FOUND=%%~$PATH:X - IF NOT DEFINED FOUND GOTO MISSING +SET script_path="%~dp0update" +IF EXIST %script_path% ( + node %script_path% %* +) ELSE ( + ECHO. + ECHO ERROR: Could not find 'update' script in 'bin' folder, aborting...>&2 + EXIT /B 1 ) -cscript "%~dp0\update.js" %* -GOTO END -:MISSING -ECHO Missing one of the following: -ECHO JDK: http://java.oracle.com -ECHO Android SDK: http://developer.android.com -ECHO Apache ant: http://ant.apache.org -EXIT /B 1 -:END diff --git a/bin/update.js b/bin/update.js deleted file mode 100644 index e73d2f7f..00000000 --- a/bin/update.js +++ /dev/null @@ -1,144 +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. -*/ - -/* - * create a cordova/android project - * - * USAGE - * ./update [path] - */ - -var fso = WScript.CreateObject('Scripting.FileSystemObject'); - -function read(filename) { - var fso=WScript.CreateObject("Scripting.FileSystemObject"); - var f=fso.OpenTextFile(filename, 1); - var s=f.ReadAll(); - f.Close(); - return s; -} - -function checkTargets(targets) { - if(!targets) { - WScript.Echo("You do not have any android targets setup. Please create at least one target with the `android` command"); - WScript.Quit(69); - } -} - -function setTarget() { - var targets = shell.Exec('android.bat list targets').StdOut.ReadAll().match(/id:\s\d+/g); - checkTargets(targets); - return targets[targets.length - 1].replace(/id: /, ""); // TODO: give users the option to set their target -} - -function setApiLevel() { - var targets = shell.Exec('android.bat list targets').StdOut.ReadAll().match(/API level:\s\d+/g); - checkTargets(targets); - return targets[targets.length - 1].replace(/API level: /, ""); -} - -function write(filename, contents) { - var fso=WScript.CreateObject("Scripting.FileSystemObject"); - var f=fso.OpenTextFile(filename, 2, true); - f.Write(contents); - f.Close(); -} - -function replaceInFile(filename, regexp, replacement) { - write(filename, read(filename).replace(regexp, replacement)); -} - -function exec(command) { - var oShell=shell.Exec(command); - while (oShell.Status == 0) { - if(!oShell.StdOut.AtEndOfStream) { - var line = oShell.StdOut.ReadLine(); - // XXX: Change to verbose mode - // WScript.StdOut.WriteLine(line); - } - WScript.sleep(100); - } -} - -function cleanup() { - if(fso.FileExists(ROOT + '\\framework\\cordova-'+VERSION+'.jar')) { - fso.DeleteFile(ROOT + '\\framework\\cordova-'+VERSION+'.jar'); - } - if(fso.FileExists(ROOT + '\\framework\\assets\\www\\cordova-'+VERSION+'.js')) { - fso.DeleteFile(ROOT + '\\framework\\assets\\www\\cordova-'+VERSION+'.js'); - } -} - -var args = WScript.Arguments, PROJECT_PATH="example", - shell=WScript.CreateObject("WScript.Shell"); - -// working dir -var ROOT = WScript.ScriptFullName.split('\\bin\\update.js').join(''); - -if (args.Count() == 1) { - PROJECT_PATH=args(0); -} - -if(!fso.FolderExists(PROJECT_PATH)) { - WScript.Echo("Project doesn't exist!"); - WScript.Quit(1); -} - -var TARGET=setTarget(); -var API_LEVEL=setApiLevel(); -var VERSION=read(ROOT+'\\VERSION').replace(/\r\n/,'').replace(/\n/,''); - -// build from source. distro should have these files -if (!fso.FileExists(ROOT+'\\cordova-'+VERSION+'.jar') && - !fso.FileExists(ROOT+'\\cordova-'+VERSION+'.js')) { - WScript.Echo("Building jar and js files..."); - // update the cordova framework project to a target that exists on this machine - exec('android.bat update project --target '+TARGET+' --path '+ROOT+'\\framework'); - exec('ant.bat -f \"'+ ROOT +'\\framework\\build.xml\" jar'); -} - -// check if we have the source or the distro files -WScript.Echo("Copying js, jar & config.xml files..."); -if(fso.FolderExists(ROOT + '\\framework')) { - exec('%comspec% /c copy "'+ROOT+'"\\framework\\assets\\www\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y'); - exec('%comspec% /c copy "'+ROOT+'"\\framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y'); -} else { - // copy in cordova.js - exec('%comspec% /c copy "'+ROOT+'"\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y'); - // copy in cordova.jar - exec('%comspec% /c copy "'+ROOT+'"\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y'); - // copy in xml -} - -// update cordova scripts -WScript.Echo("Copying cordova command tools..."); -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'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\log.bat ' + PROJECT_PATH + '\\cordova\\log.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\run.bat ' + PROJECT_PATH + '\\cordova\\run.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\cordova.js ' + PROJECT_PATH + '\\cordova\\lib\\cordova.js /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\install-device.bat ' + PROJECT_PATH + '\\cordova\\lib\\install-device.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\install-emulator.bat ' + PROJECT_PATH + '\\cordova\\lib\\install-emulator.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\list-emulator-images.bat ' + PROJECT_PATH + '\\cordova\\lib\\list-emulator-images.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\list-devices.bat ' + PROJECT_PATH + '\\cordova\\lib\\list-devices.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\list-started-emulators.bat ' + PROJECT_PATH + '\\cordova\\lib\\list-started-emulators.bat /Y'); -exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\lib\\start-emulator.bat ' + PROJECT_PATH + '\\cordova\\lib\\start-emulator.bat /Y'); - -cleanup();