From 0417a9873b72b5461a12cfdad94a1bda3783e95c Mon Sep 17 00:00:00 2001 From: brianleroux Date: Sun, 5 Sep 2010 14:32:16 -0700 Subject: [PATCH] added classic mode for droidgap and implmented test command first pass --- bin/droidgap | 48 ++++++++++++---- lib/classic.rb | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/create.rb | 148 +++--------------------------------------------- lib/test.rb | 6 ++ 4 files changed, 202 insertions(+), 151 deletions(-) create mode 100644 lib/classic.rb create mode 100644 lib/test.rb diff --git a/bin/droidgap b/bin/droidgap index ce22c062..7c51f959 100755 --- a/bin/droidgap +++ b/bin/droidgap @@ -2,9 +2,11 @@ ROOT = File.expand_path(File.dirname(__FILE__).gsub('bin','')) require 'fileutils' require File.join(ROOT, "lib", "generate.rb") +require File.join(ROOT, "lib", "classic.rb") require File.join(ROOT, "lib", "create.rb") require File.join(ROOT, "lib", "run.rb") require File.join(ROOT, "lib", "update.rb") +require File.join(ROOT, "lib", "test.rb") # ---------------------------------------------------------- # # # @@ -15,6 +17,9 @@ require File.join(ROOT, "lib", "update.rb") # droidgap gen [app name] Generate.new(ARGV[1]) if ARGV.first == 'gen' +# droidgap classic (for windows users mostly) +Classic.new(ARGV[1..-1]) if ARGV.first == 'classic' + # droidgap create [path to phonegap project] Create.new(ARGV[1]) if ARGV.first == 'create' @@ -34,10 +39,11 @@ if ARGV.first == 'log' end end +# droidgap test +Test.new if ARGV.first == 'test' + # TODO implement these! puts "droidgap ship not implemented" if ARGV.first == 'ship' -puts "droidgap test not implemented" if ARGV.first == 'test' - if ARGV.first.nil? || ARGV.first == 'help' help = <<-EOF @@ -53,19 +59,22 @@ if ARGV.first.nil? || ARGV.first == 'help' Commands: - help ..... See this message. Type help to see specific help topics. - gen ...... Generate an example PhoneGap application to current directory. - create ... Creates an Android compatible project from a www folder. Careful, this clobbers previous packaging. - run ...... Installs a valid PhoneGap Project to first device found. - log ...... Attach a logger that listens for console.log statements. - update ... Copy a fresh phonegap.jar and phonegap.js into a valid PhoneGap/Android project. - test ..... Gets a copy of mobile-spec and runs in first device or emulator attached. - ship ..... Build and sign an APK suitable for submission to an Android Marketplace. + help ...... See this message. Type help [command name] to see specific help topics. + gen ....... Generate an example PhoneGap application to current directory. + create .... Creates an Android compatible project from a www folder. Careful, this clobbers previous packaging. + classic ... Backwards support for droidgap script. Run "droidgap help classic" for more info. + run ....... Installs a valid PhoneGap Project to first device found. + log ....... Attach a logger that listens for console.log statements. + update .... Copy a fresh phonegap.jar and phonegap.js into a valid PhoneGap/Android project. + test ...... Gets edge copy of mobile-spec and runs in first device or emulator attached. + ship ...... Build and sign an APK suitable for submission to an Android Marketplace. Quickstart: $ droidgap gen example $ cd example + $ droidgap create + $ cd ../example_android $ droidgap run Now you can launch your app and optionally start a logger with: @@ -148,5 +157,24 @@ if ARGV.first.nil? || ARGV.first == 'help' EOF + classic = <<-EOF + + DroidGap Classic + ~~~~~~~~~~~~-~~~ + + Compatability for older droidgap scripts. + + Usage: + + droidgap classic [android_sdk_path] [name] [package_name] [www] [path] + + android_sdk_path ... The path to your Android SDK install. + name ............... The name of your application. + package_name ....... The name of your package (For example: com.nitobi.demo) + www ................ The path to your www folder. (Wherein your HTML, CSS and JS app is.) + path ............... The path to generate the application. + + EOF + puts ARGV[1].nil? ? help : eval(ARGV[1]) end diff --git a/lib/classic.rb b/lib/classic.rb new file mode 100644 index 00000000..2e218e8d --- /dev/null +++ b/lib/classic.rb @@ -0,0 +1,151 @@ +class Classic + attr_reader :android_sdk_path, :name, :pkg, :www, :path + + def initialize(a) + @android_sdk_path, @name, @pkg, @www, @path = a + build + end + + def build + setup + clobber + build_jar + create_android + include_www + generate_manifest + copy_libs + add_name_to_strings + write_java + end + + def setup + @android_dir = File.expand_path(File.dirname(__FILE__).gsub('lib','')) + @framework_dir = File.join(@android_dir, "framework") + @icon = File.join(@www, 'icon.png') + @app_js_dir = '' + @content = 'index.html' + end + + # replaces @path with new android project + def clobber + FileUtils.rm_r(@path) if File.exists? @path + FileUtils.mkdir_p @path + end + + # removes local.properties and recreates based on android_sdk_path + # then generates framework/phonegap.jar + def build_jar + %w(local.properties phonegap.js phonegap.jar).each do |f| + FileUtils.rm File.join(@framework_dir, f) if File.exists? File.join(@framework_dir, f) + end + open(File.join(@framework_dir, "local.properties"), 'w') do |f| + f.puts "sdk.dir=#{ @android_sdk_path }" + end + Dir.chdir(@framework_dir) + `ant jar` + Dir.chdir(@android_dir) + end + + # runs android create project + # TODO need to allow more flexible SDK targetting via config.xml + def create_android + target_id = `android list targets | grep id:`.split("\n").last.match(/\d/).to_a.first + `android create project -t #{ target_id } -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` + end + + # copies the project/www folder into tmp/android/www + def include_www + FileUtils.mkdir_p File.join(@path, "assets", "www") + FileUtils.cp_r File.join(@www, "."), File.join(@path, "assets", "www") + end + + # creates an AndroidManifest.xml for the project + def generate_manifest + manifest = "" + open(File.join(@framework_dir, "AndroidManifest.xml"), 'r') do |old| + manifest = old.read + manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' + manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" + manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name.gsub(' ','') }\"" + manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' + end + open(File.join(@path, "AndroidManifest.xml"), 'w') { |x| x.puts manifest } + end + + # copies stuff from src directory into the android project directory (@path) + def copy_libs + framework_res_dir = File.join(@framework_dir, "res") + app_res_dir = File.join(@path, "res") + # copies in the jar + FileUtils.mkdir_p File.join(@path, "libs") + FileUtils.cp File.join(@framework_dir, "phonegap.jar"), File.join(@path, "libs") + # copies in the strings.xml + FileUtils.mkdir_p File.join(app_res_dir, "values") + FileUtils.cp File.join(framework_res_dir, "values","strings.xml"), File.join(app_res_dir, "values", "strings.xml") + # drops in the layout files: main.xml and preview.xml + FileUtils.mkdir_p File.join(app_res_dir, "layout") + %w(main.xml).each do |f| + FileUtils.cp File.join(framework_res_dir, "layout", f), File.join(app_res_dir, "layout", f) + end + # icon file copy + # if it is not in the www directory use the default one in the src dir + @icon = File.join(framework_res_dir, "drawable", "icon.png") unless File.exists?(@icon) + %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| + FileUtils.mkdir_p(File.join(app_res_dir, e)) + FileUtils.cp(@icon, File.join(app_res_dir, e, "icon.png")) + end + # concat JS and put into www folder. this can be overridden in the config.xml via @app_js_dir + js_dir = File.join(@framework_dir, "assets", "js") + phonegapjs = IO.read(File.join(js_dir, 'phonegap.js.base')) + Dir.new(js_dir).entries.each do |script| + next if script[0].chr == "." or script == "phonegap.js.base" + phonegapjs << IO.read(File.join(js_dir, script)) + phonegapjs << "\n\n" + end + File.open(File.join(@path, "assets", "www", @app_js_dir, "phonegap.js"), 'w') {|f| f.write(phonegapjs) } + end + + # puts app name in strings + def add_name_to_strings + x = " + + #{ @name } + Snap + + " + open(File.join(@path, "res", "values", "strings.xml"), 'w') do |f| + f.puts x.gsub(' ','') + end + end + + # this is so fucking unholy yet oddly beautiful + # not sure if I should thank Ruby or apologize for this abusive use of string interpolation + def write_java + j = " + package #{ @pkg }; + + import android.app.Activity; + import android.os.Bundle; + import com.phonegap.*; + + public class #{ @name.gsub(' ','') } extends DroidGap + { + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + super.loadUrl(\"file:///android_asset/www/#{ @content }\"); + } + } + " + code_dir = File.join(@path, "src", @pkg.gsub('.', File::SEPARATOR)) + FileUtils.mkdir_p(code_dir) + open(File.join(code_dir, "#{ @name }.java"),'w') { |f| f.puts j } + end + + # friendly output for now + def msg + puts "Created #{ @path }" + end + # +end diff --git a/lib/create.rb b/lib/create.rb index 824e448e..e99babdf 100644 --- a/lib/create.rb +++ b/lib/create.rb @@ -1,29 +1,18 @@ -# Package +# Create # -# Generates an Android project from a valid PhoneGap project directory and puts it in ../[PROJECT NAME]-android +# Generates an Android project from a valid WWW directory and puts it in ../[PROJECT NAME]_android # -# TODO ensure the phonegap.js file is overwritten every single time into the correct tmp dir -# -class Create - attr_reader :name, :pkg, :www, :path - +class Create < Classic def initialize(path) - set_paths(path) + guess_paths(path) read_config - clobber - build_jar - create_android - include_www - generate_manifest - copy_libs - add_name_to_strings - write_java - msg + build end - def set_paths(path) + def guess_paths(path) # if no path is supplied uses current directory for project path = FileUtils.pwd if path.nil? + # if a www is found use it for the project path = File.join(path, 'www') if File.exists? File.join(path, 'www') @@ -80,127 +69,4 @@ class Create @content = @config[:content] ? @config[:content] : 'index.html' end end - - # kills and replaces tmp/android - def clobber - FileUtils.rm_r(@path) if File.exists? @path - FileUtils.mkdir_p @path - end - - # removes local.properties and recreates based on android_sdk_path - # then generates framework/phonegap.jar - def build_jar - %w(local.properties phonegap.js phonegap.jar).each do |f| - FileUtils.rm File.join(@framework_dir, f) if File.exists? File.join(@framework_dir, f) - end - open(File.join(@framework_dir, "local.properties"), 'w') do |f| - f.puts "sdk.dir=#{ @android_sdk_path }" - end - Dir.chdir(@framework_dir) - `ant jar` - Dir.chdir(@android_dir) - end - - # runs android create project - # TODO need to allow more flexible SDK targetting via config.xml - def create_android - target_id = `android list targets | grep id:`.split("\n").last.match(/\d/).to_a.first - `android create project -t #{ target_id } -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` - end - - # copies the project/www folder into tmp/android/www - def include_www - FileUtils.mkdir_p File.join(@path, "assets", "www") - FileUtils.cp_r File.join(@www, "."), File.join(@path, "assets", "www") - end - - # creates an AndroidManifest.xml for the project - def generate_manifest - manifest = "" - open(File.join(@framework_dir, "AndroidManifest.xml"), 'r') do |old| - manifest = old.read - manifest.gsub! 'android:versionCode="5"', 'android:versionCode="1"' - manifest.gsub! 'package="com.phonegap"', "package=\"#{ @pkg }\"" - manifest.gsub! 'android:name=".StandAlone"', "android:name=\".#{ @name.gsub(' ','') }\"" - manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' - end - open(File.join(@path, "AndroidManifest.xml"), 'w') { |x| x.puts manifest } - end - - # copies stuff from src directory into the android project directory (@path) - def copy_libs - framework_res_dir = File.join(@framework_dir, "res") - app_res_dir = File.join(@path, "res") - # copies in the jar - FileUtils.mkdir_p File.join(@path, "libs") - FileUtils.cp File.join(@framework_dir, "phonegap.jar"), File.join(@path, "libs") - # copies in the strings.xml - FileUtils.mkdir_p File.join(app_res_dir, "values") - FileUtils.cp File.join(framework_res_dir, "values","strings.xml"), File.join(app_res_dir, "values", "strings.xml") - # drops in the layout files: main.xml and preview.xml - FileUtils.mkdir_p File.join(app_res_dir, "layout") - %w(main.xml).each do |f| - FileUtils.cp File.join(framework_res_dir, "layout", f), File.join(app_res_dir, "layout", f) - end - # icon file copy - # if it is not in the www directory use the default one in the src dir - @icon = File.join(framework_res_dir, "drawable", "icon.png") unless File.exists?(@icon) - %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| - FileUtils.mkdir_p(File.join(app_res_dir, e)) - FileUtils.cp(@icon, File.join(app_res_dir, e, "icon.png")) - end - # concat JS and put into www folder. this can be overridden in the config.xml via @app_js_dir - js_dir = File.join(@framework_dir, "assets", "js") - phonegapjs = IO.read(File.join(js_dir, 'phonegap.js.base')) - Dir.new(js_dir).entries.each do |script| - next if script[0].chr == "." or script == "phonegap.js.base" - phonegapjs << IO.read(File.join(js_dir, script)) - phonegapjs << "\n\n" - end - File.open(File.join(@path, "assets", "www", @app_js_dir, "phonegap.js"), 'w') {|f| f.write(phonegapjs) } - end - - # puts app name in strings - def add_name_to_strings - x = " - - #{ @name } - Snap - - " - open(File.join(@path, "res", "values", "strings.xml"), 'w') do |f| - f.puts x.gsub(' ','') - end - end - - # this is so fucking unholy yet oddly beautiful - # not sure if I should thank Ruby or apologize for this abusive use of string interpolation - def write_java - j = " - package #{ @pkg }; - - import android.app.Activity; - import android.os.Bundle; - import com.phonegap.*; - - public class #{ @name.gsub(' ','') } extends DroidGap - { - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - super.loadUrl(\"file:///android_asset/www/#{ @content }\"); - } - } - " - code_dir = File.join(@path, "src", @pkg.gsub('.', File::SEPARATOR)) - FileUtils.mkdir_p(code_dir) - open(File.join(code_dir, "#{ @name }.java"),'w') { |f| f.puts j } - end - - # friendly output for now - def msg - puts "Created #{ @path }" - end - # end diff --git a/lib/test.rb b/lib/test.rb new file mode 100644 index 00000000..6c8b80ea --- /dev/null +++ b/lib/test.rb @@ -0,0 +1,6 @@ +# this is for implementors mostly +class Test + def initialize + `git clone git@github.com:phonegap/mobile-spec.git && cd mobile-spec && droidgap create && cd ../mobilespec_android && ant debug install` + end +end \ No newline at end of file