forked from tantaman/strut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
249 lines (204 loc) · 6.09 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
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require 'rake'
require 'fileutils'
myDir = Dir.pwd
def setWinPath()
ENV['PATH'] = '.\\node_modules\\.bin;' + ENV['PATH']
end
def setNixPath()
ENV['PATH'] = './node_modules/.bin:' + ENV['PATH']
end
cmdPprefix = ""
if ENV['OS'] != nil
if ENV['OS']['Windows'] != nil
cmdPrefix = "powershell "
setWinPath()
else
setNixPath()
end
else
setNixPath()
end
task :updateCoffeeIgnore do
system "./updateCoffeeIgnore.sh"
end
task :coffee, :watch do |t,args|
watch = ""
if args[:watch]
watch = "--watch"
end
system %{coffee #{watch} -b --compile --output web/scripts/ src/main/coffee}
end
# TODO: add the ability to watch templates for changes
task :templates, :pretty do |t, args|
FileList[myDir + "/src/main/resources/ui/**/templates"].each do |filename|
pretty = args[:pretty]
puts "Processing: #{filename}"
compiledTemplates = '''
define(["vendor/amd/Handlebars"], function(Handlebars) {
return {
'''
first = true
FileList["#{filename}/*.bars"].each do |fname|
pipe = IO.popen("handlebars -s #{fname}")
result = pipe.readlines
pipe.close
joined = result.join
if not pretty
joined = joined.gsub(/\\r\\n|\n|\\n/, "");
end
templateFileName = File.basename(fname, ".bars");
if first
compiledTemplates += "\n\"#{templateFileName}\": Handlebars.template(#{joined})"
first = false
else
compiledTemplates += ",\n\"#{templateFileName}\": Handlebars.template(#{joined})"
end
end
destination = filename.sub("/src/main/resources/ui/", "/web/scripts/ui/").sub("/templates", "")
FileUtils.mkdir_p destination
puts "#{destination}/Templates.js"
File.open("#{destination}/Templates.js", 'w') {|f|
f.write(compiledTemplates)
f.write("\n}});");
}
end
end
task :copyjs, :watch do |t, args|
puts "Copying intial js files"
FileList["src/main/js/**/*.js"].each do |fname|
dest = File.dirname(fname).sub("src/main/js", "web/scripts")
FileUtils.mkdir_p dest
FileUtils.cp fname, dest
end
FileList["src/vendor/**/*.js"].each do |fname|
dest = File.dirname(fname).sub("src/vendor", "web/scripts/vendor")
FileUtils.mkdir_p dest
FileUtils.cp fname, dest
end
end
def copyResources(destination)
puts "Copying initial resources"
FileList["src/main/resources/**/*"].exclude(/templates/).each do |fname|
if not File.directory? fname
dest = File.dirname(fname).sub("src/main/resources", destination)
FileUtils.mkdir_p dest
FileUtils.cp fname, dest
end
end
end
task :copyresources, :watch do |t, args|
copyResources "web/scripts"
end
task :devbuild, [:watch] => [:coffee, :templates, :copyjs, :copyresources] do |t, args|
end
task :clean do
FileUtils.rm_r "web/scripts"
FileUtils.rm_r "web-dist"
end
task :minify => [:buildVendor] do
puts "Minifying"
Dir.chdir("web/scripts") do
system "node ../../r.js -o name=main out=main-built.js baseUrl=. paths.css=vendor/amd_plugins/css paths.text=vendor/amd_plugins/text"
end
end
task :productionbuild => [:coffee, :templates, :copyjs, :copyresources, :minify] do
distDir = "web-dist"
FileUtils.mkdir_p "#{distDir}/scripts/vendor"
FileUtils.cp_r "web/preview_export", distDir
FileUtils.cp_r "web/zip", distDir
FileUtils.cp_r "web/res", distDir
FileUtils.cp "web/scripts/main-built.js", "#{distDir}/scripts"
FileUtils.cp "web/scripts/vendor/vendor-built.js", "#{distDir}/scripts/vendor"
FileUtils.cp "web/scripts/vendor/require.js", "#{distDir}/scripts/vendor"
copyResources "#{distDir}/scripts"
ignore = false
newIndex = File.open("#{distDir}/index.html", "w")
File.readlines("web/index.html").each do |line|
if not ignore
newIndex.write line
end
# just keep a map of "tagname" -> "replacement"
if line["/VENDOR"]
ignore = false
elsif line["VENDOR"]
newIndex.write '<script type="text/javascript" src="scripts/vendor/vendor-built.js"></script>'
ignore = true
elsif line["/MAIN"]
ignore = false
elsif line["MAIN"]
newIndex.write '<script type="text/javascript" data-main="scripts/main-built" src="scripts/vendor/require.js"></script>'
ignore = true
end
end
end
task :compileStylus do
end
task :buildVendor do
puts "Minifying vendor"
FileUtils.mkdir_p "web/scripts/vendor/temp"
system "#{cmdPrefix}rm web/scripts/vendor/vendor-built.js"
system "#{cmdPrefix}rm web/scripts/vendor/temp/*"
FileList["web/scripts/vendor/*.js"].exclude(/require/).each do |fname|
system %{uglifyjs #{fname} > web/scripts/vendor/temp/#{File.basename(fname, ".js")}.min.js}
end
if cmdPrefix != ""
system %{type web\\scripts\\vendor\\temp\\* >> web\\scripts\\vendor\\vendor-built.js}
else
system %{cat web/scripts/vendor/temp/* >> web/scripts/vendor/vendor-built.js}
end
FileUtils.rm_r "web/scripts/vendor/temp"
end
task :zipForLocal => [:coffee, :templates] do
system "tar -c web > Strut.tar"
system "gzip Strut.tar"
end
# yuidoc only parses javascript code (afaik) so we have to compile it first.
task :docs => [:coffee, :copyjs] do
system %{yuidoc web/scripts -o docs}
end
task :refactor, :source, :destination do |t, args|
source = args[:source]
destination = args[:destination]
if !destination
puts "usage: rake refactor[source,destination]"
exit
end
FileList["./**/*.js"].each do |fname|
#puts fname
pipe = IO.popen("amdRefactor #{source} < #{fname}")
result = pipe.readlines
pipe.close
if result.length > 0
# line col_start col_end
importLoc = result[0].split(",")
line = Integer(importLoc[0]) - 1
colStart = Integer(importLoc[1]) - 1
colEnd = Integer(importLoc[2])
lines = File.readlines(fname);
theLine = lines[line]
theLine =
"#{theLine[0..colStart]}#{destination}#{theLine[colEnd..theLine.length]}"
lines[line] = theLine
puts "Refactoring: #{fname}"
File.open(fname, 'w') { |f| f << lines }
#puts lines
end
end
end
task :showDeps, :package do |t, args|
myPkg = args[:package]
if !myPkg
puts "usage: rake showDeps[package]"
exit
end
FileList["web/scripts/#{myPkg}/**/*.js"].each do |fname|
pipe = IO.popen("amdDeps #{myPkg} < #{fname}")
result = pipe.readlines
pipe.close
if result.length > 0
puts result
end
end
end