Skip to content

Commit

Permalink
Add new windows create script for android as a cscript script. Also c…
Browse files Browse the repository at this point in the history
…reated an ant xml that should work on mac too.
  • Loading branch information
davejohnson committed Aug 20, 2011
1 parent 4a9fbb6 commit 28b972b
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/create.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cscript create.js
88 changes: 88 additions & 0 deletions bin/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* create a phonegap/android project
*
* USAGE
* ./create [path package activity]
*/

function read(filename) {
var fso=WScript.CreateObject("Scripting.FileSystemObject");
var f=fso.OpenTextFile(filename, 1, true);
var s=f.ReadAll();
f.Close();
return s;
}
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(s) {
var o=shell.Exec(s);
}

var args = WScript.Arguments, PROJECT_PATH="example",
PACKAGE="com.phonegap.example", ACTIVITY="PhoneGapExample",
shell=WScript.CreateObject("WScript.Shell");

if (args.Count() == 3) {
WScript.Echo('Found expected arguments');
PROJECT_PATH=args(0);
PACKAGE=args(1);
ACTIVITY=args(2);
}

var PACKAGE_AS_PATH=PACKAGE.replace(/\./g, '\\');
var ACTIVITY_PATH=PROJECT_PATH+'\\src\\'+PACKAGE_AS_PATH+'\\'+ACTIVITY+'.java';
var MANIFEST_PATH=PROJECT_PATH+'\\AndroidManifest.xml';
var TARGET=shell.Exec('android.bat list targets').StdOut.ReadAll().match(/id:\s([0-9]).*/)[1];
var VERSION=read('VERSION').replace(/\r\n/,'').replace(/\n/,'');

// clobber any existing example

/*
if [ $# -eq 0 ]
then
rm -rf $PROJECT_PATH
fi
*/

// create the project
exec('android.bat create project --target '+TARGET+' --path '+PROJECT_PATH+' --package '+PACKAGE+' --activity '+ACTIVITY);

// update the phonegap framework project to a target that exists on this machine
exec('android.bat update project --target '+TARGET+' --path framework');

// compile phonegap.js and phonegap.jar
// if you see an error about "Unable to resolve target" then you may need to
// update your android tools or install an additional Android platform version
exec('ant.bat -v -f framework\\build.xml jar');

// copy in the project template
exec('cmd /c xcopy bin\\templates\\project '+PROJECT_PATH+' /S /Y');

// copy in phonegap.js
exec('cmd /c copy framework\\assets\\www\\phonegap-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\phonegap-'+VERSION+'.js /Y');

// copy in phonegap.jar
exec('cmd /c copy framework\\phonegap-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\phonegap-'+VERSION+'.jar /Y');

// copy in default activity
exec('cmd /c copy bin\\templates\\Activity.java '+ACTIVITY_PATH+' /Y');

// interpolate the activity name and package
replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY);
replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE);

replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY);
replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE);

/*
# leave the id for launching
touch $PROJECT_PATH/package-activity
echo $PACKAGE/$PACKAGE.$ACTIVITY > $PROJECT_PATH/package-activity
*/
81 changes: 81 additions & 0 deletions bin/create.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="PhoneGap" default="create" basedir="../">

<property name="project.path" value="${basedir}/example"/>
<property name="package" value="com.phonegap.example"/>
<property name="activity" value="PhoneGapExample"/>

<target name="create">

<echo file="tmp/package.tmp">package-as-path=${package}</echo>
<replace file="tmp/package.tmp" token="." value="\\" />
<property file="tmp/package.tmp" />
<property name="activity.path" value="${project.path}/src/${package-as-path}/${activity}.java" />
<property name="manifest.path" value="${project.path}/AndroidManifest.xml" />

<!-- get the highest target on this machine -->
<exec executable="cmd" osfamily="windows" output="target.list.tmp">
<arg line="/c android.bat list targets"/>
</exec>
<exec executable="android" osfamily="mac,unix">
<arg line="list targets"/>
</exec>
<replaceregexp file="tmp/target.list.tmp" match=".*id:\s([0-9]).*" replace="target=\1" flags="s" />
<property file="tmp/target.list.tmp" />

<!-- var VERSION=read('VERSION').replace(/\r\n/,'').replace(/\n/,''); -->
<copy file="VERSION" tofile="tmp/VERSION.tmp" overwrite="true" />
<replaceregexp file="tmp/VERSION.tmp" match="^" replace="version=" />
<replaceregexp file="tmp/VERSION.tmp" match="\r\n" replace="" />
<property file="tmp/VERSION.tmp" />

<!-- clobber any existing example -->

<!-- create the project -->
<exec executable="cmd" osfamily="windows">
<arg line="/c android.bat create project --target ${target} --path ${project.path} --package ${package} --activity ${activity}"/>
</exec>
<exec executable="android" osfamily="mac,unix">
<arg line="create project --target ${target} --path ${project.path} --package ${package} --activity ${activity}"/>
</exec>

<!-- update the framework dir -->
<exec executable="cmd" osfamily="windows">
<arg line="/c android.bat update project --target ${target} --path ${basedir}/framework"/>
</exec>
<exec executable="android" osfamily="mac,unix">
<arg line="update project --target ${target} --path ${basedir}/framework"/>
</exec>

<!-- compile phonegap.js and phonegap.jar -->
<!-- // if you see an error about "Unable to resolve target" then you may need to
// update your android tools or install an additional Android platform version -->
<exec executable="cmd" osfamily="windows">
<arg line="ant.bat -v -f ${basedir}/framework/build.xml jar"/>
</exec>
<exec executable="ant" osfamily="mac,unix">
<arg line="-v -f ${basedir}/framework/build.xml jar"/>
</exec>

<!-- copy in the project template -->
<copy todir="${project.path}" overwrite="true">
<fileset dir="${basedir}/bin/templates/project"/>
</copy>

<!-- copy in phonegap.js -->
<copy file="${basedir}/framework/assets/www/phonegap-${version}.js" todir="${project.path}/assets/www/" />

<!-- copy in phonegap.jar -->
<copy file="${basedir}/framework/phonegap-${version}.jar" todir="${project.path}/libs/" />

<!-- copy in default activity -->
<copy file="${basedir}/bin/templates/Activity.java" tofile="${activity.path}" overwrite="true" />

<!-- interpolate the activity name and package -->
<replaceregexp file="${activity.path}" match="__ACTIVITY__" replace="${activity}" />
<replaceregexp file="${activity.path}" match="__ID__" replace="${package}" />

<replaceregexp file="${manifest.path}" match="__ACTIVITY__" replace="${activity}" />
<replaceregexp file="${manifest.path}" match="__PACKAGE__" replace="${package}" />
</target>
</project>

0 comments on commit 28b972b

Please sign in to comment.