2010-09-06 05:32:16 +08:00
# Create
2010-08-31 06:57:07 +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
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?
2010-09-06 05:32:16 +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' )
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 " )
@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'
2010-08-31 06:57:07 +08:00
2010-09-06 02:38:48 +08:00
# stop executation on errors
2010-10-11 20:30:54 +08:00
raise 'Expected index.html in the following folder #{ path }.\nThe 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' )
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' )
if File . exists? ( config_file )
require 'rexml/document'
2010-09-06 02:38:48 +08:00
f = File . new config_file
doc = REXML :: Document . new ( f )
@config = { }
@config [ :id ] = doc . root . attributes [ " id " ]
2010-08-31 06:57:07 +08:00
@config [ :version ] = doc . root . attributes [ " version " ]
doc . root . elements . each do | n |
2010-09-22 22:13:25 +08:00
@config [ :name ] = n . text . gsub ( '-' , '' ) . gsub ( ' ' , '' ) if n . name == 'name'
2010-08-31 06:57:07 +08:00
@config [ :description ] = n . text if n . name == 'description'
@config [ :icon ] = n . attributes [ " src " ] if n . name == 'icon'
@config [ :content ] = n . attributes [ " src " ] if n . name == 'content'
if n . name == " preference " && n . attributes [ " name " ] == 'javascript_folder'
@config [ :js_dir ] = n . attributes [ " value " ]
end
end
# 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
@icon = File . join ( @www , @config [ :icon ] )
# 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'
end
end
end