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