Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Created July 31, 2009 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmccray/159480 to your computer and use it in GitHub Desktop.
Save mattmccray/159480 to your computer and use it in GitHub Desktop.
#
# Titanium Rakefile
#
# Contains the following tasks:
#
# build:all # Build all source files
# build:auto # Autobuild all source files
# build:compress # Compresses and munges output JS using YUI Compressor
# build:css # Build CSS files from SASS source
# build:js # Assemble output javascript file(s)
# compile # Builds, then Runs the application
# run # Run the application via the Titanium SDK
# SETTINGS: - - - - - - - -
# Update for your platform:
TITANIUM_PATH = "/Library/Application Support/Titanium"
SDK_PATH = "#{TITANIUM_PATH}/sdk/osx/0.6.0"
# Build Parameters:
SRC_ROOT = "Source/"
CSS_SRC = "#{SRC_ROOT}theme/"
CSS_OUT = "Resources/theme/"
JS_SRC = ["#{SRC_ROOT}scripts/", "lib"]
JS_OUT = "Resources/scripts/"
# Each of these will get a separate 'concatenation' object created, and will be compressed separately.
JS_FILES = ['taskthis.js', 'klass.js', 'tarmac.js']
# Options to send the Sass engine
SASS_OPTS = {
:style => :nested,
:load_paths => [CSS_SRC]
}
# Set to true to skip the YUI compression phase
SKIP_COMPRESSION = true
# Set to true to remove the source js file after it's been compressed
RM_AFTER_COMPRESS = false
# Set to true to enable growl notifications... Requires 'growlnotify' to be in your path
ENABLE_GROWL = true
# TASKS: - - - - - - - -
desc "Run the application via the Titanium SDK"
task :run do
puts "Launching Titanium SDK..."
cmd = %Q|python "#{SDK_PATH}/tibuild.py" -d dist -s "#{TITANIUM_PATH}" -r -a "#{SDK_PATH}" .|
puts cmd
puts `#{cmd}`
end
desc "Builds, then Runs the application"
task :compile =>['build:all', 'run']
namespace :build do
# I use SASS to build the CSS
desc "Build CSS files from SASS source"
task :css do
require 'sass'
Dir[File.join(CSS_SRC, '*.sass')].each do |src_file|
short_name = src_file.gsub(CSS_SRC, '')
if short_name[0..0] != '_'
css_name = short_name.gsub('.sass', '.css')
puts " - #{css_name}..."
File.open(src_file, 'r') do |src|
File.open(File.join(CSS_OUT, css_name), 'w') do |out_file|
content = Sass::Engine.new(src.read, SASS_OPTS).render
out_file.write content
end
end
end
end
`growlnotify -H localhost -t "Titanium Build" -m "CSS files have been built"`
end
# Used by the auto builder task...
task :sass do
Rake::Task[ "build:css" ].execute
end
# I use Sprocket to assemble the source JavaScript
desc "Assemble output javascript file(s)"
task :js do
require 'sprockets'
JS_FILES.each do |src_file|
puts " - #{src_file}..."
js_loadpath = [JS_SRC].flatten
src_path = File.join(js_loadpath.first, src_file)
secretary = Sprockets::Secretary.new(
:load_path => js_loadpath,
:source_files => [src_path].flatten
)
# Generate a Sprockets::Concatenation object from the source files
concatenation = secretary.concatenation
# Write the concatenation to disk
concatenation.save_to( File.join(JS_OUT, src_file) )
end
`growlnotify -H localhost -t "Titanium Build" -m "JavaScript files have been built"`
# Now compress the generated js files...
Rake::Task[ "build:compress" ].execute
end
# I use yui-compressor to compress and munge the app js
desc "Compresses and munges output JS using YUI Compressor"
task :compress do
unless SKIP_COMPRESSION
require "yui/compressor"
JS_FILES.each do |src_file|
puts " ][ #{src_file}..."
compressor = YUI::JavaScriptCompressor.new(:munge => true)
# Open src file...
File.open(File.join(JS_OUT, src_file), "r") do |source|
output_file = src_file.gsub(/\.js$/, '.min.js')
# Open output file...
File.open(File.join(JS_OUT, output_file), "w") do |f|
compressor.compress(source) do |compressed|
# Write out the compressed content...
f.write compressed.read
end
end
end
if RM_AFTER_COMPRESS
File.unlink( File.join(JS_OUT, src_file) )
end
end
`growlnotify -H localhost -t "Titanium Build" -m "JavaScript files have been compressed"`
end
end
desc "Build all source files"
task :all do
Rake::Task[ "build:css" ].execute
Rake::Task[ "build:js" ].execute
end
# Uses directory_watcher for auto building...
desc "Autobuild all source files"
task :auto do
require 'directory_watcher'
builder = AutoBuilder.new
builder.run
end
end
class AutoBuilder
def initialize
@watcher = DirectoryWatcher.new( '.', :interval => 2 )
@watcher.add_observer self
glob = []
glob << File.join(CSS_SRC, '**', '*')
[JS_SRC].flatten.each do |js_src|
glob << File.join(js_src, '**', '*')
end
@watcher.glob = glob
end
def update( *events )
update = []
events.each do |evt|
update << File.extname( evt.path )[1..-1]
end
if update.length > 0
puts "\n[#{Time.now.strftime('%y/%m/%d %H:%M:%S')}] Source file changed..."
update.flatten.uniq.each do |type|
Rake::Task[ "build:#{type}" ].execute
end
puts " : Waiting..."
end
rescue => err
puts err
end
def run
Signal.trap('INT') {
@watcher.stop
}
@watcher.start
@watcher.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment