2010-09-06 05:32:16 +08:00
# Create
2011-03-31 00:14:21 +08:00
#
2010-09-06 05:32:16 +08:00
# Generates an Android project from a valid WWW directory and puts it in ../[PROJECT NAME]_android
2010-08-31 06:57:07 +08:00
#
2010-09-06 05:32:16 +08:00
class Create < Classic
2010-08-31 06:57:07 +08:00
def initialize ( path )
2010-09-06 05:32:16 +08:00
guess_paths ( path )
2010-09-06 02:38:48 +08:00
read_config
2010-09-06 05:32:16 +08:00
build
2010-08-31 06:57:07 +08:00
end
2011-03-31 00:14:21 +08:00
2010-09-06 05:32:16 +08:00
def guess_paths ( path )
2010-08-31 06:57:07 +08:00
# if no path is supplied uses current directory for project
path = FileUtils . pwd if path . nil?
2011-03-31 00:14:21 +08:00
2010-08-31 06:57:07 +08:00
# if a www is found use it for the project
path = File . join ( path , 'www' ) if File . exists? File . join ( path , 'www' )
2011-03-31 00:14:21 +08:00
2010-09-06 02:38:48 +08:00
# defaults
@name = path . split ( " / " ) . last . gsub ( '-' , '' ) . gsub ( ' ' , '' ) # no dashses nor spaces
@path = File . join ( path , '..' , " #{ @name } _android " )
2011-03-31 00:14:21 +08:00
@www = path
@pkg = " com.phonegap. #{ @name } "
2010-10-09 02:39:56 +08:00
@android_sdk_path = Dir . getwd [ 0 , 1 ] != " / " ? ` android-sdk-path.bat android.bat ` . gsub ( '\\tools' , '' ) . gsub ( '\\' , '\\\\\\\\' ) : ` which android ` . gsub ( / \/ tools \/ android$ / , '' ) . chomp
2010-08-31 06:57:07 +08:00
@android_dir = File . expand_path ( File . dirname ( __FILE__ ) . gsub ( 'lib' , '' ) )
@framework_dir = File . join ( @android_dir , " framework " )
2010-09-06 02:38:48 +08:00
@icon = File . join ( @www , 'icon.png' )
@app_js_dir = ''
@content = 'index.html'
2011-03-31 00:14:21 +08:00
2010-09-06 02:38:48 +08:00
# stop executation on errors
2011-03-31 00:14:21 +08:00
raise " Expected index.html in the following folder #{ path } . \n The path is expected to be the directory droidgap create is run from or specified as a command line arg like droidgap create my_path. " unless File . exists? File . join ( path , 'index.html' )
2010-10-11 20:30:54 +08:00
raise 'Could not find android in your PATH!' if @android_sdk_path . empty?
2010-09-06 02:38:48 +08:00
end
# reads in a config.xml file
def read_config
2010-08-31 06:57:07 +08:00
config_file = File . join ( @www , 'config.xml' )
2011-03-31 00:14:21 +08:00
2010-08-31 06:57:07 +08:00
if File . exists? ( config_file )
require 'rexml/document'
2010-09-06 02:38:48 +08:00
f = File . new config_file
2011-03-05 03:14:40 +08:00
doc = REXML :: Document . new ( f )
2011-03-04 10:15:12 +08:00
@config = { }
2010-09-06 02:38:48 +08:00
@config [ :id ] = doc . root . attributes [ " id " ]
2010-08-31 06:57:07 +08:00
@config [ :version ] = doc . root . attributes [ " version " ]
2011-03-04 10:15:12 +08:00
@config [ :icons ] = { }
2011-03-05 03:14:40 +08:00
defaultIconSize = 0
2010-08-31 06:57:07 +08:00
doc . root . elements . each do | n |
2011-03-31 00:14:21 +08:00
@config [ :name ] = n . text . gsub ( '-' , '' ) . gsub ( ' ' , '' ) if n . name == 'name'
@config [ :description ] = n . text if n . name == 'description'
@config [ :content ] = n . attributes [ " src " ] if n . name == 'content'
2011-03-04 10:15:12 +08:00
if n . name == 'icon'
if n . attributes [ " width " ] == '72' && n . attributes [ " height " ] == '72'
@config [ :icons ] [ " drawable-hdpi " . to_sym ] = n . attributes [ " src " ]
2011-03-05 03:14:40 +08:00
if 72 > defaultIconSize
@config [ :icon ] = n . attributes [ " src " ]
defaultIconSize = 72
end
2011-03-04 10:15:12 +08:00
elsif n . attributes [ " width " ] == '48' && n . attributes [ " height " ] == '48'
@config [ :icons ] [ " drawable-mdpi " . to_sym ] = n . attributes [ " src " ]
2011-03-05 03:14:40 +08:00
if 48 > defaultIconSize
@config [ :icon ] = n . attributes [ " src " ]
defaultIconSize = 48
end
2011-03-04 10:15:12 +08:00
elsif n . attributes [ " width " ] == '36' && n . attributes [ " height " ] == '36'
@config [ :icons ] [ " drawable-ldpi " . to_sym ] = n . attributes [ " src " ]
2011-03-05 03:14:40 +08:00
if 36 > defaultIconSize
@config [ :icon ] = n . attributes [ " src " ]
defaultIconSize = 36
end
2011-03-04 10:15:12 +08:00
else
2011-03-05 03:14:40 +08:00
@config [ :icon ] = n . attributes [ " src " ]
2011-03-04 10:15:12 +08:00
end
end
2011-03-31 00:14:21 +08:00
2010-08-31 06:57:07 +08:00
if n . name == " preference " && n . attributes [ " name " ] == 'javascript_folder'
@config [ :js_dir ] = n . attributes [ " value " ]
2011-03-31 00:14:21 +08:00
end
end
2010-08-31 06:57:07 +08:00
# extract android specific stuff
@config [ :versionCode ] = doc . elements [ " //android:versionCode " ] ? doc . elements [ " //android:versionCode " ] . text : 3
@config [ :minSdkVersion ] = doc . elements [ " //android:minSdkVersion " ] ? doc . elements [ " //android:minSdkVersion " ] . text : 1
# will change the name from the directory to the name element text
@name = @config [ :name ] if @config [ :name ]
# set the icon from the config
2011-02-05 03:20:22 +08:00
@icon = File . join ( @www , @config [ :icon ] ) if @config [ :icon ]
2011-03-04 10:15:12 +08:00
@icons = @config [ :icons ] if @config [ :icons ] . length > 0
2010-08-31 06:57:07 +08:00
# sets the app js dir where phonegap.js gets copied
@app_js_dir = @config [ :js_dir ] ? @config [ :js_dir ] : ''
# sets the start page
@content = @config [ :content ] ? @config [ :content ] : 'index.html'
2011-03-31 00:14:21 +08:00
end
end
2010-08-31 06:57:07 +08:00
end