Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
extract commands to class.
Browse files Browse the repository at this point in the history
  • Loading branch information
juno committed Mar 29, 2012
1 parent 8726051 commit dad8fc7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 87 deletions.
10 changes: 6 additions & 4 deletions lib/alloy.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@


$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))


require "alloy/command"
require "alloy/config"
require "alloy/task"

module Alloy module Alloy


PERL_COLOR_FILTER = %Q{| perl -pe 's/^\\[DEBUG\\].*$/\\e[35m$&\\e[0m/g;s/^\\[INFO\\].*$/\\e[36m$&\\e[0m/g;s/^\\[WARN\\].*$/\\e[33m$&\\e[0m/g;s/^\\[ERROR\\].*$/\\e[31m$&\\e[0m/g;'} PERL_COLOR_FILTER = %Q{| perl -pe 's/^\\[DEBUG\\].*$/\\e[35m$&\\e[0m/g;s/^\\[INFO\\].*$/\\e[36m$&\\e[0m/g;s/^\\[WARN\\].*$/\\e[33m$&\\e[0m/g;s/^\\[ERROR\\].*$/\\e[31m$&\\e[0m/g;'}


end end

require "alloy/command/build"
require "alloy/command/clean"
require "alloy/command/coffee_script"
require "alloy/config"
require "alloy/task"
59 changes: 0 additions & 59 deletions lib/alloy/command.rb

This file was deleted.

47 changes: 47 additions & 0 deletions lib/alloy/command/build.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- encoding: utf-8 -*-

require 'colored'

module Alloy::Command

class Build
def initialize(platform)
@platform = platform
end

def execute
if @platform == 'iphone'
build :device => 'iphone'
elsif @platform == 'android'
build :device => 'android'
else
raise ArgumentError, "Platform '#{@platform}' is invalid"
end

true
end

private

def build(options = {})
options[:device] ||= 'iphone'

config = Alloy::Config.new

puts "Building with Titanium... (DEVICE_TYPE: #{options[:device]})".blue
case options[:device]
when 'android'
system %Q{bash -c "#{config.titanium_android_builder} run '#{config.project_root}/' #{config.android_sdk_path} #{config.app_id} #{config.app_name} #{options[:device]} " #{Alloy::PERL_COLOR_FILTER}}
when 'iphone'
begin
system %Q{bash -c "#{config.titanium_iphone_builder} run '#{config.project_root}/' #{config.iphone_sdk_version} #{config.app_id} #{config.app_name} #{options[:device]}" #{Alloy::PERL_COLOR_FILTER}}
rescue => e
puts e.message.red
system %Q{killall "iPhone Simulator"}
end
end
end

end

end
43 changes: 43 additions & 0 deletions lib/alloy/command/clean.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- encoding: utf-8 -*-

require 'colored'
require 'fileutils'

module Alloy::Command

class Clean
def initialize(platform)
@platform = platform
end

def execute
case @platform
when 'android'
clean :device => 'android'
when 'iphone'
clean :device => 'iphone'
when nil
clean :device => 'android'
clean :device => 'iphone'
else
raise ArgumentError, "Platform '#{@platform}' is invalid"
end

true
end

private

def clean(options = {})
case options[:device]
when 'android'
FileUtils.rm_r Dir.glob('build/android/*'), :force => true
puts "Cleaned build/android".blue
when 'iphone'
FileUtils.rm_r Dir.glob('build/iphone/*'), :force => true
puts "Cleaned build/iphone".blue
end
end
end

end
33 changes: 33 additions & 0 deletions lib/alloy/command/coffee_script.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- encoding: utf-8 -*-

require 'colored'

module Alloy::Command

class CoffeeScript
def execute
compile_coffee
end

private

def compile_coffee
puts "Compiling CoffeeScript".blue

failed = false
paths = `find app -name '*.coffee'`.split("\n")

paths.each do |path|
out_dir = path.gsub(/^app/, 'Resources').gsub(/\/[\w-]*\.coffee$/,"")
unless system "coffee -c -b -o #{out_dir} #{path}"
puts "Compilation failed: #{path}".red
failed = true
end
end

puts "Successfully compiled CoffeeScript".green unless failed
not failed
end
end

end
33 changes: 9 additions & 24 deletions lib/alloy/task.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,40 +7,25 @@ module Alloy
class Task < Thor class Task < Thor
desc 'coffee', 'Compile CoffeeScript into JavaScript' desc 'coffee', 'Compile CoffeeScript into JavaScript'
def coffee def coffee
Command.compile_coffee Command::CoffeeScript.new.execute
end end


desc 'build [PLATFORM]', 'Build the app and run in simulator or emulator' desc 'build [PLATFORM]', 'Build the app and run in simulator or emulator'
def build(platform = 'iphone') def build(platform = 'iphone')
if platform == 'iphone' return unless coffee
Command.build :device => 'iphone' Command::Build.new(platform).execute
elsif platform == 'android'
Command.build :device => 'android'
else
raise ArgumentError, "Platform '#{platform}' is invalid"
end
end end


desc 'clean [PLATFORM]', 'Clean build directory for specified platform' desc 'clean [PLATFORM]', 'Clean build directory for specified platform'
def clean(platform = nil) def clean(platform = nil)
case platform Command::Clean.new(platform).execute
when 'android'
Command.clean :device => 'android'
when 'iphone'
Command.clean :device => 'iphone'
when nil
Command.clean :device => 'android'
Command.clean :device => 'iphone'
else
raise ArgumentError, "Platform '#{platform}' is invalid"
end
end end


desc 'docco', 'Generate documents by Docco' # desc 'docco', 'Generate documents by Docco'
def docco # def docco
system %Q{bash -c "find app -name '*.coffee' | xargs docco"} # system %Q{bash -c "find app -name '*.coffee' | xargs docco"}
system %Q{bash -c "open docs/app.html"} # system %Q{bash -c "open docs/app.html"}
end # end
end end


end end

0 comments on commit dad8fc7

Please sign in to comment.