darthapo (owner)

Revisions

gist: 159480 Download_button fork
public
Public Clone URL: git://gist.github.com/159480.git
Embed All Files: show embed
Rakefile #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#
# 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