#!/usr/bin/env ruby require 'fileutils' # ./droidgap /Users/brianleroux/Code/android-sdk-mac MyApp com.westcoastlogic example /Users/brianleroux/Desktop/MyApp class Build attr_reader :android_sdk_path, :name, :pkg, :www, :path, :dir def initialize(*a) @android_sdk_path, @name, @pkg, @www, @path = a @s = File::SEPARATOR @dir = Dir.pwd + @s end # runs the build script def run build_jar create_android include_www generate_manifest copy_libs add_name_to_strings write_java puts "Complete!" end # removes local.properties and recreates based on android_sdk_path # then generates framework/phonegap.jar def build_jar puts "Building the 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(@dir) end # runs android create project # TODO need to allow more flexible SDK targetting # TODO validate Android SDK def create_android target_id = 5 # `android list targets` =~ /id:\s*(\d+).*android-5/ ? $1 : 5 puts "Creating Android project... #{ target_id }" `android create project -t #{ target_id } -k #{ @pkg } -a #{ @name } -n #{ @name } -p #{ @path }` end def include_www puts "Adding www folder to project..." 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 puts "Generating manifest..." manifest = "" open(@dir + 'framework/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 }\"" manifest.gsub! 'android:minSdkVersion="5"', 'android:minSdkVersion="3"' end open("#{ @path }#{@s}AndroidManifest.xml", 'w') { |x| x.puts manifest } end # copies stuff from framework into the project # TODO need to allow for www import inc icon def copy_libs puts "Copying over libraries and assets and creating phonegap.js..." FileUtils.cp "#{ @dir }framework#{@s}phonegap.jar", "#{ @path }#{@s}libs" FileUtils.cp "#{ @dir }framework#{@s}res#{@s}values#{@s}strings.xml", "#{ @path }#{@s}res#{@s}values#{@s}strings.xml" FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}main.xml", "#{ @path }#{@s}res#{@s}layout#{@s}main.xml" FileUtils.cp "#{ @dir }framework#{@s}res#{@s}layout#{@s}preview.xml", "#{ @path }#{@s}res#{@s}layout#{@s}preview.xml" %w(drawable-hdpi drawable-ldpi drawable-mdpi).each do |e| FileUtils.cp "#{ @dir }framework#{@s}res#{@s}drawable#{@s}icon.png", "#{ @path }#{@s}res#{@s}#{ e }#{@s}icon.png" end # concat JS and put into www folder. 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", "phonegap.js"), 'w') {|f| f.write(phonegapjs) } end # puts app name in strings def add_name_to_strings puts "Adding some application name to strings.xml..." x = " #{ @name } Snap " open("#{ @path }#{@s}res#{@s}values#{@s}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 puts "Writing application Java code..." j = " package #{ @pkg }; import android.app.Activity; import android.os.Bundle; import com.phonegap.*; public class #{ @name } extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl(\"file:///android_asset/www/index.html\"); } } " dir = "#{ @path }#{@s}src#{@s}#{ @pkg.gsub '.', '/' }"; cls = "#{ @name }.java" pth = File.join(dir,cls) open(pth,'w') { |f| f.puts j.gsub(' ','') } end # end if ARGV.length == 5 Build.new(*ARGV).run else puts <<-EOF DroidGap: PhoneGap/Android Project Generator ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Creates a fresh app for hybrid mobile web hacking. Delicious robot! Usage: ./droidgap Params: 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 Android application. EOF end