-
Notifications
You must be signed in to change notification settings - Fork 58
/
Rakefile
449 lines (360 loc) · 12.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# Copyright (c) 2002-2012 Rally Software Development Corp. All rights reserved.
require 'fileutils'
task :default => [:debug,:build]
desc "Create an app with the provided name (and optional SDK version)"
task :new, :app_name, :sdk_version do |t, args|
args.with_defaults(:sdk_version => "2.0p")
Dir.chdir(Rake.original_dir)
config = Rally::AppSdk::AppConfig.new(args.app_name, args.sdk_version)
Rally::AppSdk::AppTemplateBuilder.new(config).build
end
desc "Build a deployable app which includes all JavaScript and CSS resources inline"
task :build => [:jslint] do
Dir.chdir(Rake.original_dir)
Rally::AppSdk::AppTemplateBuilder.new(get_config_from_file).build_app_html
end
desc "Build a debug version of the app, useful for local development"
task :debug do
Dir.chdir(Rake.original_dir)
Rally::AppSdk::AppTemplateBuilder.new(get_config_from_file).build_app_html(true)
end
desc "Clean all generated output"
task :clean do
Dir.chdir(Rake.original_dir)
remove_files Rally::AppSdk::AppTemplateBuilder.get_auto_generated_files
end
desc "Run jslint on all JavaScript files used by this app"
task :jslint do |t|
Dir.chdir(Rake.original_dir)
config = get_config_from_file
files_to_run = config.javascript
options = { :browser => true, :predef => ['Rally', 'Ext'], :nomen => false, :onevar => false, :plusplus => false }
Rally::Jslint.run_jslint(files_to_run, options)
end
module Rally
module AppSdk
## Builds the JSON config file as well as the JavaScript, CSS, and HTML
## template files.
class AppTemplateBuilder
CONFIG_FILE = "config.json"
JAVASCRIPT_FILE = "App.js"
CSS_FILE = "app.css"
HTML = "App.html"
HTML_DEBUG = "App-debug.html"
CLASS_NAME = "CustomApp"
def self.get_auto_generated_files
[HTML, HTML_DEBUG]
end
def initialize(config)
@config = config
end
def build
fail_if_file_exists get_template_files
@config.javascript = JAVASCRIPT_FILE
@config.css = CSS_FILE
@config.class_name = CLASS_NAME
create_file_from_template CONFIG_FILE, CONFIG_TPL
create_file_from_template JAVASCRIPT_FILE, JAVASCRIPT_TPL, { :escape => true }
create_file_from_template CSS_FILE, CSS_TPL
end
def build_app_html(debug = false)
@config.validate
file = debug ? HTML_DEBUG : HTML
template = debug ? HTML_DEBUG_TPL : HTML_TPL
template = populate_template_with_resources(template,
"JAVASCRIPT_BLOCK",
@config.javascript,
debug,
"\"VALUE\"",
3)
template = populate_template_with_resources(template,
"STYLE_BLOCK",
@config.css,
debug,
"<link rel=\"stylesheet\" type=\"text/css\" href=\"VALUE\">",
2)
create_file_from_template file, template, { :debug => debug, :escape => true }
end
private
def get_template_files
[CONFIG_FILE, JAVASCRIPT_FILE, CSS_FILE]
end
def create_file_from_template(file, template, opts = {})
populated_template = replace_placeholder_variables template, opts
write_file file, populated_template
end
def write_file(path, content)
puts "Creating #{path}..."
File.open(path, "w") { |file| file.write(content) }
end
def populate_template_with_resources(template, placeholder, resources, debug, debug_tpl, indent_level)
block = ""
indent_level = 1 if debug
indent = " " * indent_level
separator = ""
resources.each do |file|
if debug
block << separator << debug_tpl.gsub("VALUE", file)
if is_javascript_file(file)
separator = ",\n" + indent * 4
else
separator = "\n"
end
else
IO.readlines(file).each do |line|
block << indent << line.to_s.gsub(/\\'/, "\\\\\\\\'")
end
end
end
template.gsub(placeholder, block)
end
def replace_placeholder_variables(str, opts = {})
# by default, we will esacpe single quotes
escape = opts.has_key?(:escape) ? opts[:escape] : false
debug = opts.has_key?(:debug) ? opts[:debug] : false
str.gsub("APP_READABLE_NAME", @config.name).
gsub("APP_NAME", escape ? escape_single_quotes(@config.name) : @config.name).
gsub("APP_TITLE", @config.name).
gsub("APP_SDK_VERSION", @config.sdk_version).
gsub("APP_SDK_PATH", debug ? @config.sdk_debug_path : @config.sdk_path).
gsub("DEFAULT_APP_JS_FILE", list_to_quoted_string(@config.javascript)).
gsub("DEFAULT_APP_CSS_FILE", list_to_quoted_string(@config.css)).
gsub("CLASS_NAME", @config.class_name)
end
def list_to_quoted_string(list)
"\"#{list.join("\",\"")}\""
end
def escape_single_quotes(string)
string.gsub("'", "\\\\\\\\'")
end
def is_javascript_file(file)
file.split('.').last.eql? "js"
end
end
## Simple object wrapping the configuration of an App
class AppConfig
SDK_RELATIVE_URL = "/apps"
SDK_ABSOLUTE_URL = "https://rally1.rallydev.com/apps"
SDK_FILE = "sdk.js"
SDK_DEBUG_FILE = "sdk-debug.js"
attr_reader :name, :sdk_version
attr_accessor :javascript, :css, :class_name
def self.from_config_file(config_file)
unless File.exist? config_file
raise Exception.new("Could not find #{config_file}. Did you run 'rake new[\"App Name\"]'?")
end
name = Rally::JSON.get(config_file, "name")
sdk_version = Rally::JSON.get(config_file, "sdk")
class_name = Rally::JSON.get(config_file, "className")
javascript = Rally::JSON.get_array(config_file, "javascript")
css = Rally::JSON.get_array(config_file, "css")
config = Rally::AppSdk::AppConfig.new(name, sdk_version)
config.javascript = javascript
config.css = css
config.class_name = class_name
config
end
def initialize(name, sdk_version)
@name = sanitize_string name
@sdk_version = sdk_version
@javascript = []
@css = []
end
def javascript=(file)
@javascript = (@javascript << file).flatten
end
def css=(file)
@css = (@css << file).flatten
end
def validate
@javascript.each do |file|
raise Exception.new("Could not find JavaScript file #{file}") unless File.exist? file
end
@css.each do |file|
raise Exception.new("Could not find CSS file #{file}") unless File.exist? file
end
class_name_valid = false
@javascript.each do |file|
file_contents = File.open(file, "rb").read
if file_contents =~ /Ext.define\(\s*['"]#{class_name}['"]\s*,/
class_name_valid = true
break
end
end
unless class_name_valid
msg = "The 'className' property '#{class_name}' in #{Rally::AppSdk::AppTemplateBuilder::CONFIG_FILE} was not used when defining your app.\n" +
"Please make sure that the 'className' property in #{Rally::AppSdk::AppTemplateBuilder::CONFIG_FILE} and the class name you use to define your app match!"
raise Exception.new(msg)
end
end
def sdk_debug_path
"#{SDK_ABSOLUTE_URL}/#{@sdk_version}/#{SDK_DEBUG_FILE}"
end
def sdk_path
"#{SDK_RELATIVE_URL}/#{@sdk_version}/#{SDK_FILE}"
end
end
end
class Jslint
def self.check_for_jslint_support
puts "Running jslint..."
begin
require 'jslint-v8'
rescue Exception
puts "In order to run jslint, you will need to install the 'jslint-v8' Ruby gem.\n" +
"You can do that by running:\n\tgem install jslint-v8\n"
false
end
end
def self.run_jslint(files_to_run, options)
return unless check_for_jslint_support
output_stream = STDOUT
formatter = JSLintV8::Formatter.new(output_stream)
runner = JSLintV8::Runner.new(files_to_run)
runner.jslint_options.merge!(options)
lint_result = runner.run do |file, errors|
formatter.tick(errors)
end
output_stream.print "\n"
formatter.summary(files_to_run, lint_result)
raise "Jslint failed" unless lint_result.empty?
end
end
## Pure (very simple) Ruby JSON implementation
module JSON
class << self
def get(file, key)
get_value(file, key)
end
def get_array(file, key)
get_array_values(file, key)
end
private
def get_value(file, key)
File.open(file, "r").each_line do |line|
if line =~ /^\s*"#{key}"\s*:\s*"(.*)".*$/ || line =~ /^\s*"#{key}"\s*:\s*(.*)\s*$/
return $1
end
end
nil
end
def get_array_values(file, key)
values = []
in_block = false
File.open(file).each_line do |line|
in_block = true if line =~ /^\s*"#{key}"\s*:\s*\[/
if in_block
if line =~ /^\s*"#{key}"\s*:\s*\[(.*)\]/
add_prop values, $1, key
elsif line =~ /^\s*(".*")[,\]]?/
add_prop values, $1, key
end
end
in_block = false if in_block && line =~ /.*\][,]?/
end
values
end
def add_prop(array, values, exclude)
values.split(',').each do |value|
value = value.chomp.strip
value = value.chomp[1..(value.length-2)]
array << value unless value == exclude || value.nil?
end
end
end
end
end
## Helpers
def get_config_from_file
config_file = Rally::AppSdk::AppTemplateBuilder::CONFIG_FILE
Rally::AppSdk::AppConfig.from_config_file(config_file)
end
def remove_files(files)
files.map { |f| File.delete(f) if File.exists?(f) }
end
def fail_if_file_exists(files)
files.each do |file|
raise Exception.new "I found an existing app file - #{file}. If you want to create a new app, please remove this file!" if File.exists? file
end
end
def sanitize_string(value)
value.gsub(/[^a-zA-Z0-9 \-_\.']/, "")
end
## Templates
JAVASCRIPT_TPL = <<-END
Ext.define('CLASS_NAME', {
extend: 'Rally.app.App',
componentCls: 'app',
launch: function() {
//Write app code here
}
});
END
HTML_DEBUG_TPL = <<-END
<!DOCTYPE html>
<html>
<head>
<title>APP_TITLE</title>
<script type="text/javascript" src="APP_SDK_PATH"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/2.1.6/highcharts.js"></script>
<script type="text/javascript" src="https://raw.github.com/JoeKuan/Highcharts_ExtJs_4/master/Chart/ux/HighChart.js"></script>
<script type="text/javascript" src="https://raw.github.com/lmaccherone/Lumenize/master/deploy/lumenize.js"></script>
<script type="text/javascript">
Rally.onReady(function() {
Rally.loadScripts([
JAVASCRIPT_BLOCK
], function() {
Rally.launchApp('CLASS_NAME', {
name: 'APP_NAME'
})
});
});
</script>
STYLE_BLOCK
</head>
<body></body>
</html>
END
HTML_TPL = <<-END
<!DOCTYPE html>
<html>
<head>
<title>APP_TITLE</title>
<script type="text/javascript" src="APP_SDK_PATH"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/2.1.6/highcharts.js"></script>
<script type="text/javascript" src="https://raw.github.com/JoeKuan/Highcharts_ExtJs_4/master/Chart/ux/HighChart.js"></script>
<script type="text/javascript" src="https://raw.github.com/lmaccherone/Lumenize/master/deploy/lumenize.js"></script>
<script type="text/javascript">
Rally.onReady(function() {
JAVASCRIPT_BLOCK
Rally.launchApp('CLASS_NAME', {
name: 'APP_NAME'
});
});
</script>
<style type="text/css">
STYLE_BLOCK </style>
</head>
<body></body>
</html>
END
CONFIG_TPL = <<-END
{
"name": "APP_READABLE_NAME",
"className": "CustomApp",
"sdk": "APP_SDK_VERSION",
"javascript": [
DEFAULT_APP_JS_FILE
],
"css": [
DEFAULT_APP_CSS_FILE
]
}
END
CSS_TPL = <<-END
.app {
/* Add app styles here */
}
END