-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
348 lines (285 loc) · 9.16 KB
/
Rakefile
File metadata and controls
348 lines (285 loc) · 9.16 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
require 'rake'
require 'yaml'
# Common Variables ---------------------------------------------------------------
PATH_SEP = ":"
BASE_DIR = File.dirname(__FILE__)
APP_NAME = "mingda"
APP_VERSION = "1.0.0"
APP_JAR = "#{APP_NAME}/mingda-#{APP_VERSION}.jar"
SRC_DIR = "src"
JAVA_SRC_DIR = "#{SRC_DIR}"
LIB_DIR = "lib"
BUILD_DIR = "build"
DEST_DIR = "dest"
COMPILE_PARAMS = %w( -g:none -1.5 -encoding UTF-8 -progress ).join(' ')
COMPILE_DEST_DIR = "#{BUILD_DIR}/app/WEB-INF/classes"
# Classpath ---------------------------------------------------------------------
LIB = YAML.load(File.open('lib/lib.yml'))
def build_classpath(classpath, *libs)
libs.flatten!
libs.each do |l|
jar = LIB[l]['jar']
jar = [jar] unless jar.class == Array
jar.each do |j|
path = File.join(LIB['dir'], LIB[l]['dir'], j)
classpath << PATH_SEP << path unless classpath.include? path
end
# add requirements
build_classpath(classpath, LIB[l]['require']) if LIB[l]['require']
end
classpath
end
def collect_app_jars(jars, *libs)
libs.flatten!
libs.each do |l|
jar = LIB[l]['jar']
jar = [jar] unless jar.class == Array
jar.each do |j|
path = File.join(LIB['dir'], LIB[l]['dir'], j)
jars << path unless jars.include? path
end
# add requirements
collect_app_jars(jars, LIB[l]['require']) if LIB[l]['require']
end
jars
end
COMPILE_CLASSPATH = build_classpath("", LIB['compile-classpath'])
JS_COMPRESSION_CLASSPATH = build_classpath("", LIB['js-compression-classpath'])
# Make directory structure ------------------------------------------------------
DIRS = [DEST_DIR, BUILD_DIR, "#{BUILD_DIR}/web", "#{BUILD_DIR}/app", "#{BUILD_DIR}/app/WEB-INF"]
desc "Make the directory structure for building"
task :make_dir_structure do
DIRS.each { |dir| mkdir_p dir }
end
# Task clean -------------------------------------------------------------------
CLEAN = FileList[DEST_DIR, BUILD_DIR]
desc "Remove any temporary products."
task :clean do
CLEAN.each { |fn| rm_rf fn rescue nil }
end
# Package Web files -----------------------------------------------------------
WEB_FILES = FileList[
'web/images/**/*',
'web/**/*.html',
'web/**/*.htm',
'web/**/*.css',
'web/**/*.htc',
'web/**/*.gif',
'web/**/*.jpg',
'web/**/*.jbf',
'web/**/*.png',
'web/**/*.swf',
'web/**/*.zip',
'web/favicon.ico',
'web/crossdomain.xml'
].exclude('*.jsp', '*.ftl', 'web/WEB-INF')
desc "Copy static web files..."
task :copy_web_files do
WEB_FILES.each do |f|
cp_file f, "#{BUILD_DIR}/#{f}"
end
end
def cp_file!(src, dest)
if File.file? src
mkdirs_if_not_exists dest
cp src, dest
end
end
def cp_file(src, dest)
if File.file? src
if not File.exists?(dest) or File.new(src).mtime > File.new(dest).mtime
mkdirs_if_not_exists dest
cp src, dest
end
end
end
def mkdirs_if_not_exists(file)
non_exists_dirs = []
dir = File.dirname(file)
while not File.exists?(dir)
non_exists_dirs << dir
dir = File.dirname(dir)
end
if not non_exists_dirs.empty?
non_exists_dirs.reverse!.each { |d| mkdir d }
end
end
JS_COMPRESS_EXCLUDE_FILES = FileList[
'web/scripts/common.js',
'web/scripts/lightedit.js',
'web/scripts/photo_upload.js',
'web/scripts/tinymce/**/*.js',
'web/**/lang*.js',
'web/scripts/calendar/**',
'web/scripts/calendar/lang/**',
'web/**/getcity.js'
]
JS_FILES = FileList[
'web/**/*.js'
].exclude(
# 'web/scripts/mt/mootools.v1.11.js',
#'web/scripts/calendar/**'
)
JS_RENAME_DICT = {
# 'web/scripts/mt/mootools.v1.11.compressed.js' => 'web/scripts/mt/mootools.v1.11.js',
# 'web/ext/ext-all-compressed.js' => 'web/ext/ext-all.js'
}
CSS_COMPRESS_EXCLUDE_FILES = FileList[
# 'web/scripts/common.js',
# 'web/scripts/lightedit.js',
# 'web/scripts/photo_upload.js',
# 'web/scripts/tinymce/**/*.js',
# 'web/**/lang*.js',
# 'web/**/getcity.js'
]
CSS_FILES = FileList[
'web/styles/*.css',
'web/styles/calendar/**/*.css',
'web/styles/main/*.css'
].exclude(
# 'web/scripts/mt/mootools.v1.11.js',
# 'web/ext/ext-all.js'
)
CSS_RENAME_DICT = {
# 'web/scripts/mt/mootools.v1.11.compressed.js' => 'web/scripts/mt/mootools.v1.11.js',
# 'web/ext/ext-all-compressed.js' => 'web/ext/ext-all.js'
}
desc "Compress javascript source files"
task :compress_js_files do
excludes = JS_COMPRESS_EXCLUDE_FILES.to_a
JS_FILES.each do |js|
if excludes.include?(js)
if JS_RENAME_DICT[js]
cp_file js, "#{BUILD_DIR}/#{JS_RENAME_DICT[js]}"
else
cp_file js, "#{BUILD_DIR}/#{js}"
end
else
# use java to compress js
if JS_RENAME_DICT[js]
compress_js js, JS_RENAME_DICT[js]
else
compress_js js, js
end
end
end
end
desc "Compress css source files"
task :compress_css_files do
excludes = CSS_COMPRESS_EXCLUDE_FILES.to_a
CSS_FILES.each do |css|
if excludes.include?(css)
if CSS_RENAME_DICT[css]
cp_file css, "#{BUILD_DIR}/#{CSS_RENAME_DICT[css]}"
else
cp_file css, "#{BUILD_DIR}/#{css}"
end
else
# use java to compress js
if CSS_RENAME_DICT[css]
compress_css css, CSS_RENAME_DICT[css]
else
compress_css css, css
end
end
end
end
def compress_js(js_file, to_file)
puts "compressing javascript: #{js_file}"
#sh "java -cp #{JS_COMPRESSION_CLASSPATH} com.yupoo.tools.JSCompressor -l #{js_file} #{BUILD_DIR}/#{to_file}"
sh "java -cp #{JS_COMPRESSION_CLASSPATH} com.yahoo.platform.yui.compressor.YUICompressor --type js --charset utf-8 #{js_file} > #{BUILD_DIR}/#{to_file}"
end
def compress_css(css_file, to_file)
puts "compressing css: #{css_file}"
sh "java -cp #{JS_COMPRESSION_CLASSPATH} com.yahoo.platform.yui.compressor.YUICompressor --type css --charset utf-8 #{css_file} > #{BUILD_DIR}/#{to_file}"
end
desc "Package static web files"
task :pkg_web do
puts "Archiving static web files..."
sh "tar -cf #{DEST_DIR}/web.tar.gz --directory=#{BUILD_DIR} web"
end
task :pkg_web => [ :make_dir_structure, :copy_web_files, :compress_js_files, :compress_css_files ]
# Package App files -----------------------------------------------------------
APP_FILES = FileList[
'web/**/*.ftl',
'web/**/*.jsp',
'web/**/*.xml',
'web/**/*.tld',
'web/**/*.properties'
].exclude('web/crossdomain.xml', 'web/WEB-INF/classes')
desc "Copy application files..."
task :copy_app_files do
APP_FILES.each do |f|
destfile = f.sub(/^web/, "app")
cp_file f, "#{BUILD_DIR}/#{destfile}"
end
end
task :copy_app_files => :make_dir_structure
desc "Compile java source files use ajc"
task :compile_java do
puts "compile java..."
sh "ajc -cp #{COMPILE_CLASSPATH} #{COMPILE_PARAMS} -sourceroots #{JAVA_SRC_DIR} -d #{COMPILE_DEST_DIR}"
end
task :compile_java => :make_dir_structure
desc "Copy application configuration files"
task :copy_app_conf do
FileList['src/conf/**/*'].each do |f|
destfile = f.sub(/^src\/conf\//, '')
cp_file f, "#{BUILD_DIR}/app/WEB-INF/classes/#{destfile}"
end
end
desc "Copy application configuration files"
task :copy_webapp_conf do
FileList['src/webapp/**/*.xml', 'src/webapp/**/*.properties', 'src/webapp/**/*.ftl'].each do |f|
destfile = f.sub(/^src\/webapp\/(conf|resources)\//, '')
cp_file f, "#{BUILD_DIR}/app/WEB-INF/classes/#{destfile}"
end
end
desc "Copy production environment configuration files"
task :copy_production_conf do
FileList['metadata/webapp/**/*'].each do |f|
destfile = f.sub(/^metadata\/webapp\//, '')
cp_file! f, "#{BUILD_DIR}/app/#{destfile}"
end
end
task :copy_libs do
libs = collect_app_jars([], LIB['deploy-classpath'])
libs.each do |jar|
cp_file jar, "#{BUILD_DIR}/app/WEB-INF/lib/#{File.basename(jar)}"
end
end
desc "Archive application files"
task :archive_app do
puts "Archiving application files..."
sh "tar cfz #{DEST_DIR}/app.tar.gz --directory=#{BUILD_DIR}/app ."
end
task :pkg_app => [ :make_dir_structure, :copy_app_files, :copy_app_conf, :copy_production_conf, :copy_libs, :compile_java, :copy_webapp_conf, :archive_app ]
task :pkg_app_nolib => [ :make_dir_structure, :copy_app_files, :copy_app_conf, :copy_production_conf, :compile_java, :copy_webapp_conf, :archive_app ]
task :pkg_lib do
puts "Archiving application libraries..."
sh "tar cf #{DEST_DIR}/lib.tar --directory=#{BUILD_DIR}/app/WEB-INF/lib ."
end
# Deploy application -----------------------------
# Invoke the given actions via Capistrano
def cap(*parameters)
begin
require 'rubygems'
rescue LoadError
# no rubygems to load, so we fail silently
end
require 'capistrano/cli'
STDERR.puts "Capistrano/Rake integration is deprecated."
STDERR.puts "Please invoke the 'cap' command directly: `cap #{parameters.join(" ")}'"
Capistrano::CLI.new(parameters.map { |param| param.to_s }).execute!
end
namespace :remote do
desc "Execute a specific action using capistrano"
task :exec do
unless ENV['ACTION']
raise "Please specify an action (or comma separated list of actions) via the ACTION environment variable"
end
actions = ENV['ACTION'].split(",")
actions.concat(ENV['PARAMS'].split(" ")) if ENV['PARAMS']
cap(*actions)
end
end