ymdp 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +17 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/lib/application_view/application_view.rb +210 -0
  8. data/lib/application_view/asset_tag_helper.rb +106 -0
  9. data/lib/application_view/commands/generate.rb +3 -0
  10. data/lib/application_view/compiler/template_compiler.rb +417 -0
  11. data/lib/application_view/config.rb +39 -0
  12. data/lib/application_view/environment.rb +29 -0
  13. data/lib/application_view/generator/base.rb +15 -0
  14. data/lib/application_view/generator/templates/javascript.js +51 -0
  15. data/lib/application_view/generator/templates/stylesheet.css +5 -0
  16. data/lib/application_view/generator/templates/translation.pres +8 -0
  17. data/lib/application_view/generator/templates/view.html.haml +8 -0
  18. data/lib/application_view/generator/view.rb +170 -0
  19. data/lib/application_view/helpers.rb +4 -0
  20. data/lib/application_view/processor/compressor.rb +81 -0
  21. data/lib/application_view/processor/processor.rb +132 -0
  22. data/lib/application_view/processor/validator.rb +125 -0
  23. data/lib/application_view/support/file.rb +54 -0
  24. data/lib/application_view/support/form_post.rb +58 -0
  25. data/lib/application_view/support/g.rb +11 -0
  26. data/lib/application_view/support/growl.rb +36 -0
  27. data/lib/application_view/support/timer.rb +40 -0
  28. data/lib/application_view/support/w3c.rb +22 -0
  29. data/lib/application_view/tag_helper.rb +147 -0
  30. data/lib/application_view/translator/base.rb +387 -0
  31. data/lib/application_view/translator/blank.rb +58 -0
  32. data/lib/application_view/translator/ymdp_translate.rb +92 -0
  33. data/lib/ymdp.rb +1 -0
  34. data/test/helper.rb +10 -0
  35. data/test/test_ymdp.rb +4 -0
  36. data/ymdp.gemspec +77 -0
  37. metadata +92 -0
@@ -0,0 +1,417 @@
1
+ require 'support/timer'
2
+
3
+ include Grit
4
+
5
+ include ApplicationView::Config
6
+ include ApplicationView::Support::Timer
7
+
8
+ class GitHelper
9
+ def get_hash(branch)
10
+ branch = get_current_branch || "master"
11
+ repo = Repo.new("#{YMDP_ROOT}/.")
12
+ repo.commits(branch).first.id
13
+ end
14
+
15
+ def get_current_branch
16
+ result = `git status`
17
+ if result =~ /# On branch (.*)/
18
+ return $1
19
+ end
20
+ end
21
+
22
+ def do_commit(message)
23
+ repo = Repo.new(".")
24
+ repo.add(".")
25
+ puts `git commit -am "#{message}"`
26
+ end
27
+ end
28
+
29
+ module TemplateBuilder
30
+ def initialize(file, domain="staging", git_hash="", message="", verbose=false, subdir=false)
31
+ @verbose = verbose
32
+ @domain = domain
33
+ @server = SERVERS[@domain]["server"]
34
+ @file = file
35
+ @assets_directory = "/om/assets/#{SERVERS[@domain]['assets_id']}"
36
+ @hash = git_hash
37
+ @message = message
38
+ @subdir = subdir
39
+ @version = CONFIG["version"]
40
+ @sprint_name = CONFIG["sprint_name"]
41
+ @view = base_filename(@file.split("/").last)
42
+ Application.current_view = @view
43
+ end
44
+
45
+ def build
46
+ unless @file =~ /#{YMDP_ROOT}\/app\/views\/_/
47
+ write_template(processed_template)
48
+ end
49
+ end
50
+
51
+ def processed_template
52
+ result = ""
53
+ File.open(@file) do |f|
54
+ template = f.read
55
+ if @file =~ /\.haml$/
56
+ result = process_haml(template, @file)
57
+ else
58
+ result = process_template(template)
59
+ end
60
+ end
61
+ result
62
+ end
63
+
64
+ private
65
+
66
+ def process_template(template)
67
+ raise "Define in child"
68
+ end
69
+
70
+ def output_filename
71
+ filename = convert_filename(@file.split("/").last)
72
+
73
+ dir = "#{YMDP_ROOT}/servers/#{@domain}"
74
+ directory = @file.gsub(/^\.\/app/, dir).gsub(/\/([^\/]+)$/, '')
75
+
76
+ FileUtils.mkdir_p(directory)
77
+ "#{directory}/#{filename}"
78
+ end
79
+
80
+ def write_template_without_layout(result)
81
+ path = output_filename
82
+
83
+ File.open(path, "w") do |f|
84
+ f.write(result)
85
+ end
86
+ $stdout.puts "Finished writing #{path}.\n" if @verbose
87
+ end
88
+
89
+ def write_template_with_layout(result)
90
+ @content = result
91
+ application_layout = "#{YMDP_ROOT}\/app\/views\/layouts\/application.html.haml"
92
+ if File.exists?("#{YMDP_ROOT}\/app\/views\/layouts\/application.html.haml")
93
+ layout = File.open(application_layout) do |f|
94
+ template = f.read
95
+ process_haml(template, application_layout)
96
+ end
97
+ elsif File.exists?("#{YMDP_ROOT}\/app\/views\/layouts\/application.html.erb")
98
+ layout = File.open(application_layout) do |f|
99
+ template = f.read
100
+ process_template(application_layout)
101
+ end
102
+ end
103
+ write_template_without_layout(layout)
104
+ end
105
+
106
+ def write_template(result)
107
+ write_template_with_layout(result)
108
+ end
109
+ end
110
+
111
+ class YMDPTemplate
112
+ include ActionView::Helpers::TagHelper
113
+ include TemplateBuilder
114
+ include ApplicationHelper
115
+ include ApplicationView::Base
116
+ include ApplicationView::AssetTagHelper
117
+ include ApplicationView::FormTagHelper
118
+ include ApplicationView::LinkTagHelper
119
+
120
+ attr_accessor :output_buffer
121
+
122
+ private
123
+
124
+ def base_filename(filename)
125
+ filename.gsub(/(\.html|\.erb|\.haml)/, "")
126
+ end
127
+
128
+ def convert_filename(filename)
129
+ base_filename(filename)
130
+ end
131
+
132
+ def process_template(template)
133
+ ERB.new(template, 0, "%<>").result(binding)
134
+ end
135
+
136
+ def process_haml(template, filename=nil)
137
+ options = {}
138
+ if filename
139
+ options[:filename] = filename
140
+ end
141
+ Haml::Engine.new(template, options).render(self)
142
+ end
143
+
144
+ def write_template(result)
145
+ write_template_with_layout(result)
146
+ ApplicationView::Validator::HTML.validate(output_filename) if validate_html?
147
+ end
148
+ end
149
+
150
+ class JSTemplate < YMDPTemplate
151
+ def compress_js(filename)
152
+ if compress_js_assets?
153
+ validate_filename = "#{filename}.min"
154
+ ApplicationView::Compressor::JavaScript.compress(filename)
155
+ end
156
+ end
157
+
158
+ def write_template(result)
159
+ filename = @file.split("/").last
160
+ tmp_filename = "./tmp/#{filename}"
161
+ save_to_file(result, tmp_filename)
162
+ result = ApplicationView::Compressor::JavaScript.compress(tmp_filename) || result
163
+ write_template_without_layout(result)
164
+ end
165
+ end
166
+
167
+
168
+ class YRBTemplate
169
+ include TemplateBuilder
170
+
171
+ def directory
172
+ directory = "#{BASE_PATH}/servers/#{@domain}/assets/yrb"
173
+ FileUtils.mkdir_p(directory)
174
+ directory
175
+ end
176
+
177
+ def output_filename
178
+ filename = convert_filename(@file.split("/").last)
179
+ "#{directory}/#{filename}"
180
+ end
181
+
182
+ def to_json
183
+ processed_template
184
+ end
185
+
186
+ def to_hash
187
+ JSON.parse(to_json)
188
+ end
189
+
190
+ def to_yaml
191
+ h = {}
192
+ to_hash.each do |k,v|
193
+ k = k.downcase
194
+ h[k] = "#{v}"
195
+ end
196
+ h.to_yaml
197
+ end
198
+
199
+ def processed_template
200
+ super.to_json
201
+ end
202
+
203
+ def validate
204
+ ApplicationView::Validator::JSON.validate(output_filename)
205
+ end
206
+
207
+ private
208
+
209
+ def base_filename(filename)
210
+ filename.gsub(/\.pres/, "")
211
+ end
212
+
213
+ def convert_filename(filename)
214
+ "#{base_filename(filename)}.json"
215
+ end
216
+
217
+ def process_template(template)
218
+ @hash = {}
219
+ lines = template.split("\n")
220
+ lines.each do |line|
221
+ unless line =~ /^[\s]*#/
222
+ line =~ /^([^\=]+)=(.+)/
223
+ key = $1
224
+ value = $2
225
+ unless key.blank?
226
+ if @hash.has_key?(key)
227
+ puts
228
+ puts "Duplicate value in #{output_filename}"
229
+ puts " #{key}=#{@hash[key]}"
230
+ puts " #{key}=#{value}"
231
+ puts
232
+ if @hash[key] == value
233
+ puts " Values are the same but duplicate values still should not exist!"
234
+ puts
235
+ end
236
+ raise "Duplicate key error"
237
+ end
238
+ @hash[key] = value
239
+ end
240
+ end
241
+ end
242
+ @hash
243
+ end
244
+
245
+ def write_template(result)
246
+ puts output_filename if verbose?
247
+ write_template_without_layout(result)
248
+ end
249
+ end
250
+
251
+ class TemplateCompiler
252
+ attr_accessor :domain, :git_hash, :options
253
+
254
+ def self.compile
255
+ time do
256
+ system "rm ./tmp/*"
257
+
258
+ options = parse_options
259
+
260
+ domain = options[:domain]
261
+ domains = SERVERS.keys
262
+
263
+ git = GitHelper.new
264
+
265
+ if options[:commit]
266
+ git.do_commit(options[:message])
267
+ end
268
+
269
+ git_hash = git.get_hash(options[:branch])
270
+
271
+ if domain
272
+ domains = [domain]
273
+ end
274
+
275
+ process_domains(domains, git_hash, options)
276
+
277
+ # system "rm ./tmp/*"
278
+ end
279
+ rescue StandardError => e
280
+ puts e.message
281
+ puts e.backtrace
282
+ end
283
+
284
+ def self.parse_options
285
+ options = {
286
+ :commit => false,
287
+ :branch => "master"
288
+ }
289
+ OptionParser.new do |opts|
290
+ options[:commit] = false
291
+ options[:verbose] = verbose?
292
+ opts.banner = "Usage: build.rb [options]"
293
+
294
+ opts.on("-d", "--domain [domain]", "Force Domain") do |v|
295
+ options[:domain] = v
296
+ end
297
+ opts.on("-b", "--branch [branch]", "Current Branch") do |v|
298
+ options[:branch] = v
299
+ end
300
+ opts.on("-m", "--message [message]", "Commit Message") do |v|
301
+ options[:commit] = true
302
+ options[:message] = v
303
+ end
304
+ opts.on("-n", "--no-commit", "Don't Commit") do |v|
305
+ options[:commit] = false
306
+ end
307
+ opts.on("-v", "--verbose", "Verbose (show all file writes)") do |v|
308
+ options[:verbose] = true
309
+ end
310
+ opts.on("-r", "--rake [task]", "Execute Rake task") do |v|
311
+ options[:rake] = v
312
+ end
313
+ opts.on("-c", "--compress", "Compress JavaScript and CSS") do |v|
314
+ options[:compress] = v
315
+ end
316
+ end.parse!
317
+
318
+ options
319
+ end
320
+
321
+ def self.process_domains(domains, git_hash, options)
322
+ domains.each do |d|
323
+ clean_domain(d)
324
+
325
+ compiler = self.new(d, git_hash, options)
326
+
327
+ ["views", "assets"].each do |dir|
328
+ compiler.process("#{YMDP_ROOT}/app/#{dir}/")
329
+ end
330
+ process_yrb(d, git_hash, options)
331
+ end
332
+
333
+ if options[:rake]
334
+ system "rake #{options[:rake]}"
335
+ end
336
+ end
337
+
338
+ def self.process_yrb(domain, hash, options)
339
+ puts "Processing ./app/assets/yrb/ for #{domain}"
340
+ ApplicationView::Base.supported_languages.each do |lang|
341
+ process_each_yrb(lang, domain, hash, options)
342
+ end
343
+ end
344
+
345
+ def self.process_each_yrb(lang, domain, hash, options)
346
+ tmp_file = "#{TMP_DIR}/keys_#{lang}.pres"
347
+ Dir["#{BASE_PATH}/app/assets/yrb/#{lang}/*"].each do |path|
348
+ system "cat #{path} >> #{tmp_file}"
349
+ end
350
+ yrb = YRBTemplate.new(tmp_file, domain, hash, options[:message], options[:verbose])
351
+ yrb.build
352
+ yrb.validate
353
+ end
354
+
355
+ def self.clean_domain(d)
356
+ dir = "#{YMDP_ROOT}/servers/#{d}"
357
+ system "rm -rf #{dir}/views"
358
+ system "rm -rf #{dir}/assets/javascripts"
359
+ system "rm -rf #{dir}/assets/stylesheets"
360
+ system "rm -rf #{dir}/assets/yrb"
361
+ end
362
+
363
+ def log(text)
364
+ "#{Time.now.to_s} #{text}"
365
+ end
366
+
367
+ def initialize(domain, git_hash, options)
368
+ @domain = domain
369
+ @git_hash = git_hash
370
+ @options = options
371
+ end
372
+
373
+ def create_directory(path)
374
+ dest = destination(path)
375
+
376
+ if File.exists?("#{YMDP_ROOT}/#{path}")
377
+ # puts " exists #{path}"
378
+ else
379
+ puts " create #{path}"
380
+ FileUtils.mkdir_p "#{YMDP_ROOT}/#{path}"
381
+ end
382
+ end
383
+
384
+ def destination(path)
385
+ destination = path.dup
386
+ destination.gsub!("#{YMDP_ROOT}/app", "#{YMDP_ROOT}/servers/#{domain}")
387
+ end
388
+
389
+ def process(path)
390
+ puts "Processing #{path} for #{domain}"
391
+ dest = destination("#{path}")
392
+ create_directory("servers/#{domain}")
393
+ Dir["#{path}**/*"].each do |f|
394
+ build_file(f)
395
+ end
396
+ copy_images
397
+ end
398
+
399
+ def copy_images
400
+ if options[:verbose]
401
+ puts log("Moving images into #{YMDP_ROOT}/servers/#{domain}/assets/images...")
402
+ end
403
+ system "rm -rf #{YMDP_ROOT}/servers/#{domain}/assets/images"
404
+ system "cp -r #{YMDP_ROOT}/app/assets/images #{YMDP_ROOT}/servers/#{domain}/assets"
405
+ end
406
+
407
+ def build_file(file)
408
+ if file.split("/").last !~ /^_/ && file !~ /\/app\/views\/layouts\//
409
+ if file =~ /(\.haml|\.erb)$/
410
+ YMDPTemplate.new(file, domain, git_hash, options[:message], options[:verbose]).build
411
+ elsif file =~ /\.js$/
412
+ JSTemplate.new(file, domain, hash, options[:message], options[:verbose]).build
413
+ end
414
+ end
415
+ end
416
+ end
417
+
@@ -0,0 +1,39 @@
1
+ module ApplicationView
2
+ module Config
3
+ def compress_embedded_js?
4
+ CONFIG["compress"]["embedded_js"]
5
+ end
6
+
7
+ def compress_js_assets?
8
+ CONFIG["compress"]["js_assets"]
9
+ end
10
+
11
+ def compress_css?
12
+ CONFIG["compress"]["css"]
13
+ end
14
+
15
+ def validate_embedded_js?
16
+ CONFIG["validate"]["embedded_js"][YMDP_ENV]
17
+ end
18
+
19
+ def validate_js_assets?
20
+ CONFIG["validate"]["js_assets"][YMDP_ENV]
21
+ end
22
+
23
+ def validate_json_assets?
24
+ CONFIG["validate"]["json_assets"][YMDP_ENV]
25
+ end
26
+
27
+ def validate_html?
28
+ CONFIG["validate"]["html"][YMDP_ENV]
29
+ end
30
+
31
+ def obfuscate?
32
+ CONFIG["compress"]["obfuscate"]
33
+ end
34
+
35
+ def verbose?
36
+ CONFIG["verbose"]
37
+ end
38
+ end
39
+ end