cordova-android/lib/create.rb

98 lines
4.1 KiB
Ruby
Raw Normal View History

# Create
#
# Generates an Android project from a valid WWW directory and puts it in ../[PROJECT NAME]_android
2010-08-31 06:57:07 +08:00
#
class Create < Classic
2010-08-31 06:57:07 +08:00
def initialize(path)
guess_paths(path)
2010-09-06 02:38:48 +08:00
read_config
build
2010-08-31 06:57:07 +08:00
end
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-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-09-06 02:38:48 +08:00
# stop executation on errors
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')
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
doc = REXML::Document.new(f)
@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"]
@config[:icons] = {}
defaultIconSize = 0
2010-08-31 06:57:07 +08:00
doc.root.elements.each do |n|
@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'
if n.name == 'icon'
if n.attributes["width"] == '72' && n.attributes["height"] == '72'
@config[:icons]["drawable-hdpi".to_sym] = n.attributes["src"]
if 72 > defaultIconSize
@config[:icon] = n.attributes["src"]
defaultIconSize = 72
end
elsif n.attributes["width"] == '48' && n.attributes["height"] == '48'
@config[:icons]["drawable-mdpi".to_sym] = n.attributes["src"]
if 48 > defaultIconSize
@config[:icon] = n.attributes["src"]
defaultIconSize = 48
end
elsif n.attributes["width"] == '36' && n.attributes["height"] == '36'
@config[:icons]["drawable-ldpi".to_sym] = n.attributes["src"]
if 36 > defaultIconSize
@config[:icon] = n.attributes["src"]
defaultIconSize = 36
end
else
@config[:icon] = n.attributes["src"]
end
end
2010-08-31 06:57:07 +08:00
if n.name == "preference" && n.attributes["name"] == 'javascript_folder'
@config[:js_dir] = n.attributes["value"]
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
@icon = File.join(@www, @config[:icon]) if @config[:icon]
@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'
end
end
2010-08-31 06:57:07 +08:00
end