#!/usr/bin/env ruby require 'rubygems' require 'rake' require 'haml' begin require 'fileutils' rescue LoadError require 'ftools' end require 'pathname' MOBOX_ROOT = Pathname(File.expand_path('..', File.dirname(__FILE__))) require MOBOX_ROOT.join('lib', 'mobox') PROJECT_ROOT = Pathname(FileUtils.pwd).ascend{|d| h=d+'.mobox'; break Pathname(File.dirname(h)) if h.file?} if PROJECT_ROOT WWW_SOURCE_DIR = Pathname(PROJECT_ROOT).join("src") ASSETS_SOURCE_DIR = WWW_SOURCE_DIR.join "assets" BUILD_DIR = Pathname(PROJECT_ROOT).join("build") WWW_BUILD_DIR = BUILD_DIR.join("www") IOS_BUILD_DIR = BUILD_DIR.join("ios") ANDROID_BUILD_DIR = BUILD_DIR.join("android") SPROCKETS_BUNDLES = %w[ application.css application.js ] end def ask_user prompt print prompt STDIN.gets.chomp.strip end def make_environment(platform, app_name, app_namespace) executable = MOBOX_ROOT.join('vendor', 'phonegap', 'lib', platform, 'bin', 'create') `#{executable} #{platform_build_path(platform)} #{app_namespace} #{app_name}` end def platform_build_path(platform) {'ios' => IOS_BUILD_DIR, 'android' => ANDROID_BUILD_DIR}[platform.to_s] end def clean_directory opts={} directory = Pathname( opts[:path] || '.' ) keep = opts[:keep] || [] Dir.glob(directory.join('**', '*')).each do |filename| if keep.size > 0 pattern_matches = keep.map{|match_string| filename =~ Regexp.new( directory.join(match_string).to_s )} unless (pattern_matches[0] or not pattern_matches.reduce(&:==)) FileUtils.remove_entry(filename) if File.exist?(filename) or Dir.exist?(filename) end else FileUtils.remove_entry(filename) if File.exist?(filename) or Dir.exist?(filename) end end end Rake.application.init('mobox') # Add Tasks below: desc "Create a new Mobox project" task :new, :directory do |taskname, args| directory = args[:directory] unless directory print "Enter a directory name for your Mobox app: " directory = STDIN.gets.chomp.strip end print "Creating new mobox project in '#{directory}'... " # put in the actual copying stuff FileUtils.mkdir_p File.join(FileUtils.pwd, directory) FileUtils.cp_r Dir[MOBOX_ROOT.join('template', '*')], File.join(FileUtils.pwd, directory) FileUtils.cp MOBOX_ROOT.join('template', '.mobox'), File.join(FileUtils.pwd, directory) puts "done." end task :locate_root do unless PROJECT_ROOT puts "No .mobox file found." exit 1 end end namespace :build do namespace :www do task :clean => :locate_root do print "Cleaning www build... " FileUtils.mkdir_p WWW_BUILD_DIR Dir.glob File.join(WWW_BUILD_DIR, "**", "*") do |path| FileUtils.rm_rf path if File.exist? path end puts "done." end task :build => [:locate_root, "build:html", "build:assets", "build:images"] namespace :build do task :html => :locate_root do print "Building index.html... " if(File.exist?(WWW_SOURCE_DIR.join("index.haml"))) render_scope = Mobox::RenderScope.new(WWW_SOURCE_DIR) FileUtils.mkdir_p WWW_BUILD_DIR if File.exist? WWW_BUILD_DIR.join("index.html") File.delete WWW_BUILD_DIR.join("index.html") end File.open(File.join(WWW_BUILD_DIR, "index.html"), "w") do |file| file.write Haml::Engine.new( File.open(WWW_SOURCE_DIR.join("index.haml")).read ).render(render_scope) file.close end end puts "done." end task :assets => :locate_root do print "Compiling static assets... " FileUtils.mkdir_p WWW_BUILD_DIR.join('javascripts') FileUtils.mkdir_p WWW_BUILD_DIR.join('stylesheets') FileUtils.mkdir_p WWW_BUILD_DIR.join('log') sprockets = Sprockets::Environment.new(WWW_SOURCE_DIR) do |env| env.logger = Logger.new( File.open( WWW_BUILD_DIR.join('log', 'sprockets_build.log'), "w") ) end sprockets.context_class.instance_eval { include Mobox::Helpers } sprockets.append_path( WWW_SOURCE_DIR.join('assets', 'javascripts') ) sprockets.append_path( WWW_SOURCE_DIR.join('assets', 'stylesheets') ) SPROCKETS_BUNDLES.each do |bundle| assets = sprockets.find_asset(bundle) prefix, basename = assets.pathname.to_s.split('/')[-2..-1] FileUtils.mkpath WWW_BUILD_DIR.join(prefix) realname = assets.pathname.basename.to_s.split(".")[0..1].join(".") assets.write_to(WWW_BUILD_DIR.join(prefix, realname)) end puts "done." end task :images => :locate_root do print "Copying images... " img_build_dir = WWW_BUILD_DIR.join("images") img_src_dir = WWW_SOURCE_DIR.join("assets", "images") FileUtils.mkdir_p img_build_dir if Dir.exist? img_src_dir FileUtils.cp_r Dir[img_src_dir.join("*")], img_build_dir end puts "done." end end end namespace :ios do task :check_ios_sim do if `which ios-sim`.chomp == "" puts "Please install ios-sim." puts "> brew install ios-sim" exit 1 end end desc "Create build environment for iOS" task :create do app_namespace = ask_user("Project namespace? [com.example.projectname]: ") app_name = ask_user("Project name? [MyApp]: ") print "Creating iOS build environment... " make_environment('ios', app_name, app_namespace) puts "done." end task :check_install => :check_ios_sim do unless IOS_BUILD_DIR.directory? and Dir.glob(IOS_BUILD_DIR.join('*')).size > 1 puts "No iOS build found. Please create one with `mobox build:ios:create`" exit 1 end end desc "Clean iOS web assets" task :clean => :check_install do print "Cleaning #{IOS_BUILD_DIR.join('www').to_s.gsub(PROJECT_ROOT.to_s, '')[1..-1]}... " clean_directory :keep => %w[res/* config.xml], :path => IOS_BUILD_DIR.join('www') puts "done." end desc "Build iOS application" task :build => :check_install do print "Copying static assets to build/ios/www... " FileUtils.cp_r Dir[WWW_BUILD_DIR.join('*')], IOS_BUILD_DIR.join('www') puts "done." print "Compiling ios app... " FileUtils.mkdir_p PROJECT_ROOT.join('log') system "#{IOS_BUILD_DIR.join('cordova', 'debug')} > #{PROJECT_ROOT.join('log','ios_build.log')} 2> #{PROJECT_ROOT.join('log', 'ios_build.err.log')}" puts "done." end end namespace :android do desc "Create build environment for Android" task :create do app_namespace = ask_user("Project namespace? [com.example.projectname]: ") app_name = ask_user("Project name? [MyApp]: ") print "Creating Android build environment... " make_environment('android', app_name, app_namespace) puts "done." end task :check_android_sdk do if `which android`.chomp == "" or `which adb`.chomp == "" puts "Please install the Android SDK and put the tools in your $PATH." exit 1 end end task :check_install => :check_android_sdk do unless ANDROID_BUILD_DIR.directory? and Dir.glob(ANDROID_BUILD_DIR.join('*')).size > 1 puts "No Android build found. Please create one with `mobox build:android:create`" exit 1 end end task :check_emulator do unless `#{ANDROID_BUILD_DIR.join('cordova', 'cordova')} check_devices`.chomp.strip == "0" puts "Android emulator not running. Please start a valid emulator." exit 1 end end desc "Clean Android web assets" task :clean => :check_install do print "Cleaning #{ANDROID_BUILD_DIR.join('assets', 'www').to_s.gsub(PROJECT_ROOT.to_s, '')[1..-1]}... " clean_directory :path => ANDROID_BUILD_DIR.join('assets', 'www') puts "done." end desc "Build Android application" task :build => [:check_install] do print "Copying static assets to build/android/assets/www... " FileUtils.cp_r Dir[WWW_BUILD_DIR.join('*')], ANDROID_BUILD_DIR.join('assets', 'www') puts "done." print "Compiling Android app... " FileUtils.mkdir_p PROJECT_ROOT.join('log') system "#{ANDROID_BUILD_DIR.join('cordova', 'debug')} > #{PROJECT_ROOT.join('log','ios_build.log')} 2> #{PROJECT_ROOT.join('log', 'ios_build.err.log')}" puts "done." print "Launching Android app... " `#{ANDROID_BUILD_DIR.join('cordova', 'cordova')} launch` puts "done." end end desc "Build static HTML application" task :www => ["www:clean", "www:build"] desc "Build iOS application" task :ios => ["ios:check_install", "ios:clean", :www, "ios:build"] desc "Build Android application" task :android => ["android:check_install", "android:check_emulator", :www, "android:clean", "android:build"] end desc "Run server on 0.0.0.0:3000" task :server => :locate_root do Mobox::Server.new(PROJECT_ROOT).run! end task :default # End task declarations Rake.application.top_level