This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
335 lines (290 loc) · 9.8 KB
/
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require 'rubygems'
require 'bundler/setup'
Bundler.require
require 'yaml'
require 'sass/css'
require File.realpath('.lib.rb')
class String
def partition_all(reg)
r = self.partition(reg)
if reg =~ r[2]
r[2] = r[2].partition_all(reg)
end
return r.flatten
end
end
# build files
# 1. load(download & copy) files to .source
# 2. convert files type to .sass, .js and image types
# 3. merge sass and js files into one css file and one js file
# 4. generate lib.js and minfy files
desc 'build files from config.yml'
task :build, :config do |task, args|
args = {
:config => 'example'
}.merge(args.to_hash)
args[:config] = args[:config] + '.yml'
unless File.exists?(args[:config])
p args[:config] + ' is not exists!'
exit!
end
p '== Starting Build @' + args[:config]
# Get config.yml
DATA = YAML.load(File.read(args[:config]))
# Merge default config
@config = {
'src' => 'src/example',
'download' => false,
'minify' => true
}.merge(DATA['config'])
@config['url'] = @config['src'] unless @config.has_key?('url')
system('mkdir ' + @config['src']) unless File.exists?(@config['src'])
['source'].each do |path|
@config['src/' + path] = File.join(@config['src'], '.' + path) unless @config.has_key?('src/' + path)
system('mkdir ' + @config['src/' + path]) unless File.exists?(@config['src/' + path])
end
['javascripts', 'stylesheets', 'images'].each do |path|
@config['url/' + path] = @config['url'] + '/' + path unless @config.has_key?('url/' + path)
@config['src/' + path] = File.join(@config['src'], path) unless @config.has_key?('src/' + path)
system('mkdir ' + @config['src/' + path]) unless File.exists?(@config['src/' + path])
end
# Merge default libs
@libs = {
'lazyload' => 'https://raw.github.com/rgrove/lazyload/master/lazyload.js'
}.merge(DATA['libs'])
@bundle = DATA['bundle']
@preload = DATA['preload']
if @config.has_key?('before')
load @config['before']
end
p '== [1/2] Starting Progress Source =='
length = @libs.length
num = 0
@libs.each do |name, urls|
num = num + 1
p "[#{num}/#{length}] #{name}"
urls = [urls] unless urls.class == Array
lib = []
urls.each do |url|
if @libs.has_key?(url)
lib.push(url)
else
path = File.join(@config['src/source'], name, File.basename(url))
dir = File.dirname(path)
system('mkdir ' + dir) unless File.exists?(dir)
download url, path
case get_filetype(path)
when 'css'
css = css_import(url, dir)
File.open(path, 'w'){ |f| f.write(css) }
images = download_images(name, url, path)
if images.length > 0
lib.push images
end
when 'rb'
script = eval(File.read(path))
rb_path = path
css = ''
js = ''
script.each do | type, content |
case type
when :css
css << content
when :js
js << content
end
end
if css != ''
path = File.join(dir, File.basename(path, '.rb') << '.css')
File.open(path, 'w'){ |f| f.write("/* @import #{rb_path} */\n" + css) }
elsif js != ''
path = File.join(dir, File.basename(path, '.rb') << '.js')
File.open(path, 'w'){ |f| f.write("/* @import #{rb_path} */\n" + js) }
end
when 'sass'
options = { :syntax => :sass, :cache => false }.merge(Compass.sass_engine_options)
options[:load_paths].push File.dirname(path), File.dirname(url)
css = "/* @import #{path} */\n" + Sass::Engine.new(File.read(path), options).render
path = File.join(dir, File.basename(path, '.sass') << '.css')
File.open(path, 'w'){ |f| f.write(css) }
when 'scss'
options = { :syntax => :scss, :cache => false }.merge(Compass.sass_engine_options)
options[:load_paths].push File.dirname(path), File.dirname(url)
css = "/* @import #{path} */\n" + Sass::Engine.new(File.read(path), options).render
path = File.join(dir, File.basename(path, '.sass') << '.css')
File.open(path, 'w'){ |f| f.write(css) }
when 'coffee'
js = "/* @import #{path} */\n" + CoffeeScript.compile(File.read(path))
path = File.join(dir, File.basename(path, '.coffee') << '.js')
File.open(path, 'w'){ |f| f.write(js) }
else
lib.push url
end
lib.push(path)
end
end
lib = lib.flatten
css = ''
js = ''
lib = lib.map{ |file|
if File.exists?(file)
content = "/* @import #{file} */\n" + File.read(file)
case File.extname(file)
when '.css'
css << content
file = nil
when '.js'
js << content << ';'
file = nil
end
end
file
}.compact
if css != ''
file = File.join(@config['src/source'], name + '.css')
File.open(file, 'w'){ |f| f.write(css) }
lib.push(file)
end
if js != ''
file = File.join(@config['src/source'], name + '.js')
File.open(file, 'w'){ |f| f.write(js) }
lib.push(file)
end
@libs[name] = lib.map{ |file|
if File.exists?(file)
case File.extname(file)
when '.js'
type = 'javascripts'
when '.css'
type = 'stylesheets'
else
type = 'images'
end
path = File.join(@config['src/' + type], File.basename(file))
p '=> ' + path
system('cp ' + file + ' ' + path)
reg = /url\("?'?([^'")]+)'?"?\)/
if type == 'stylesheets' && @config['changeImageUrl'] && reg =~ File.read(path)
css = File.read(path).partition_all(reg).map{ |f|
if reg =~ f
if @config['url'] == @config['src']
f = 'url("../images/' << File.basename(f.match(reg)[1]) << '")'
else
f = 'url("' + @config['url/images'] + File.basename(f.match(reg)[1]) << '")'
end
end
f
}.join('')
File.open(path, 'w'){ |f| f.write(css) }
end
if type == 'images'
path = nil
end
if @config['minify']
if type == 'stylesheets'
min = minify(File.read(path), :css)
File.open(path, 'w'){ |f| f.write(min) }
end
if type == 'javascripts'
min = minify(File.read(path), :js)
File.open(path, 'w'){ |f| f.write(min) }
end
end
else
path = @libs[file]
end
path
}.compact.flatten.uniq
@libs[name] = @libs[name][0] if @libs[name].length == 1
end
p '== [2/2] Generate lib.js =='
libjs = File.read(@libs['lazyload']) << ';'
if @config['minify']
libjs << minify(CoffeeScript.compile(File.read('lib.coffee')), :js)
else
libjs << CoffeeScript.compile(File.read('lib.coffee'))
end
@urls = {}
@libs.each do |lib, path|
path = [path] unless path.class == Array
path = path.map{ |url|
case File.extname(url)
when '.css'
url = @config['url/stylesheets'] + '/' + File.basename(url)
when '.js'
url = @config['url/javascripts'] + '/' + File.basename(url)
else
url = nil
end
url
}.compact.uniq
@urls[lib] = path
end
libjs << "\n/* libs */\nlib.libs(#{@urls.to_json});"
if @bundle != nil && @bundle.length > 0
@bundle.each do |name, libs|
css = ''
js = ''
files = []
libs.each do |lib|
lib = @libs[lib] if @libs.has_key?(lib)
lib = [lib] unless lib.class == Array
lib.each do |file|
files.push(file)
case File.extname(file)
when '.css'
css << File.read(file)
when '.js'
js << File.read(file) << ';'
end
end
end
path = []
if css != ''
file = File.join(@config['src/stylesheets'], name + '.css')
File.open(file, 'w'){ |f| f.write(css) }
path.push(@config['url/stylesheets'] + '/' + File.basename(file))
end
if js != ''
files_url = files.map{ |f| @config['url/javascripts'] + '/' + File.basename(f) }.join("','")
js << "\nif(typeof lib === 'function'){lib.loaded('add', '#{files_url}');}"
file = File.join(@config['src/javascripts'], name + '.js')
File.open(file, 'w'){ |f| f.write(js) }
path.push(@config['url/javascripts'] + '/' + File.basename(file))
end
if path.length > 0
path = path[0] if path.length == 0
@bundle[name] = path
else
@bundle.delete(name)
end
end
libjs << "\n/* bundle */\nlib.libs(#{@bundle.to_json});"
end
if @preload.class == Array && @preload.length > 0
libjs << "\n/* preload */\nlib('#{@preload.join(' ')}');"
end
File.open(File.join(@config['src/javascripts'], 'lib.js'), 'w'){ |f| f.write(libjs) }
if @config.has_key?('after')
load @config['after']
end
p '== End Build =='
end
desc 'watch files changes and auto build'
task :watch, :config do |task, args|
args = {
:config => 'example'
}.merge(args.to_hash)
config = args[:config]
args[:config] = args[:config] + '.yml'
unless File.exists?(args[:config])
p args[:config] + ' is not exists!'
exit!
end
p path = File.realpath(YAML.load(File.read(args[:config]))['config']['watchr']) + '/*'
script = Watchr::Script.new
script.watch(path){ Rake::Task['build'].invoke(config) }
contrl = Watchr::Controller.new(script, Watchr.handler.new)
contrl.run
p '== Starting Watch, You can use Ctrl+C to close it'
end