ymdp 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,19 +31,19 @@ module YMDP
31
31
  @verbose = params[:verbose]
32
32
  @domain = params[:domain]
33
33
 
34
- @file = params[:file]
35
- @assets_directory = "/om/assets/#{servers[@domain]['assets_id']}"
36
- @hash = params[:git_hash]
37
- @message = params[:message]
38
-
39
34
  server_settings = servers[@domain]
40
35
  if server_settings
41
36
  @server = server_settings["server"]
42
37
  else
43
- raise StandardError.new("server name is required")
38
+ raise StandardError.new("Server settings are required.")
44
39
  end
45
40
 
46
- raise StandardError.new("server name is required") unless @server
41
+ raise StandardError.new("Server name does not exist in server settings.") unless @server
42
+
43
+ @file = params[:file]
44
+ @assets_directory = "/om/assets/#{servers[@domain]['assets_id']}"
45
+ @hash = params[:git_hash]
46
+ @message = params[:message]
47
47
 
48
48
  set_content_variables
49
49
 
@@ -73,12 +73,13 @@ module YMDP
73
73
  # If the filename begins with a _ it's a partial.
74
74
  #
75
75
  def partial?
76
- @file =~ /#{paths[:base_path]}\/app\/views\/_/
76
+ File.basename(@file) =~ /^_/
77
77
  end
78
78
 
79
79
  # Compile this view unless it is a partial.
80
80
  #
81
81
  def build
82
+ # puts "Base build"
82
83
  unless partial?
83
84
  write_template(processed_template)
84
85
  end
@@ -87,17 +88,20 @@ module YMDP
87
88
  # Returns the compiled template code after its Haml or ERB has been processed.
88
89
  #
89
90
  def processed_template
91
+ # "Base processed_template"
90
92
  result = ""
91
- File.open(@file) do |f|
92
- template = f.read
93
- if @file =~ /\.haml$/
94
- result = process_haml(template, @file)
95
- else
96
- result = process_template(template)
97
- end
93
+ template = File.read(@file)
94
+ if @file =~ /\.haml$/
95
+ result = process_haml(template, @file)
96
+ else
97
+ result = process_template(template)
98
98
  end
99
99
  result
100
100
  end
101
+
102
+ def base_filename(filename)
103
+ raise "Define in child"
104
+ end
101
105
 
102
106
  # Implemented in child classes, this defines what must be done to process a template.
103
107
  #
@@ -153,11 +157,15 @@ module YMDP
153
157
  #
154
158
  def write_template_without_layout(result)
155
159
  path = destination_path
156
-
160
+
161
+ # puts "\n\n\nBase write_template_without_layout: #{result}, #{path}"
162
+
157
163
  File.open(path, "w") do |f|
158
164
  f.write(result)
159
165
  end
160
166
  verbose "Finished writing #{path}.\n"
167
+
168
+ result
161
169
  end
162
170
 
163
171
  # Writes the input string to the destination file, passing it through the
@@ -166,21 +174,16 @@ module YMDP
166
174
  # The application layout can be either Haml or ERB.
167
175
  #
168
176
  def write_template_with_layout(result)
177
+ # puts "Base write_template_with_layout"
169
178
  @content = result
179
+ layout = result
180
+
170
181
  application_layout = "#{paths[:base_path]}\/app\/views\/layouts\/application.html"
171
182
  haml_layout = application_layout + ".haml"
172
- erb_layout = application_layout + ".erb"
173
183
 
174
184
  if File.exists?(haml_layout)
175
- layout = File.open(haml_layout) do |f|
176
- template = f.read
177
- process_haml(template, haml_layout)
178
- end
179
- elsif File.exists?(erb_layout)
180
- layout = File.open(erb_layout) do |f|
181
- template = f.read
182
- process_template(erb_layout)
183
- end
185
+ template = File.read(haml_layout)
186
+ layout = process_haml(template, haml_layout)
184
187
  end
185
188
 
186
189
  write_template_without_layout(layout)
@@ -192,6 +195,7 @@ module YMDP
192
195
  # uses a template or not.
193
196
  #
194
197
  def write_template(result)
198
+ # puts "Base write_template"
195
199
  write_template_with_layout(result)
196
200
  end
197
201
 
@@ -211,10 +215,7 @@ module YMDP
211
215
  #
212
216
  include ActionView::Helpers::TagHelper
213
217
 
214
- begin
215
- include ApplicationHelper
216
- rescue NameError
217
- end
218
+ include ApplicationHelper if defined?(ApplicationHelper)
218
219
 
219
220
  include YMDP::ApplicationView
220
221
  include YMDP::AssetTagHelper
@@ -242,12 +243,14 @@ module YMDP
242
243
  # Process this template with ERB.
243
244
  #
244
245
  def process_template(template)
246
+ # puts "View process_template"
245
247
  ERB.new(template, 0, "%<>").result(binding)
246
248
  end
247
249
 
248
250
  # Process this template with Haml.
249
251
  #
250
252
  def process_haml(template, filename=nil)
253
+ # puts "View process_haml"
251
254
  options = {}
252
255
  if filename
253
256
  options[:filename] = filename
@@ -260,8 +263,11 @@ module YMDP
260
263
  # Validate the resulting HTML file if that option is turned on.
261
264
  #
262
265
  def write_template(result)
263
- write_template_with_layout(result)
266
+ # puts "View write_template"
267
+ result = super(result)
264
268
  YMDP::Validator::HTML.validate(destination_path) if CONFIG.validate_html?
269
+
270
+ result
265
271
  end
266
272
  end
267
273
 
@@ -277,7 +283,7 @@ module YMDP
277
283
  def write_template(result)
278
284
  filename = @file.split("/").last
279
285
  tmp_filename = "./tmp/#{filename}"
280
- save_to_file(result, tmp_filename)
286
+ F.save_to_file(result, tmp_filename)
281
287
  result = YMDP::Compressor::JavaScript.compress(tmp_filename) if CONFIG.compress_embedded_js?
282
288
  write_template_without_layout(result)
283
289
  end
@@ -382,14 +388,14 @@ module YMDP
382
388
  key, value = key_and_value_from_line(line)
383
389
  unless key.blank?
384
390
  if @hash.has_key?(key)
385
- puts
386
- puts "Duplicate value in #{destination_path}"
387
- puts " #{key}=#{@hash[key]}"
388
- puts " #{key}=#{value}"
389
- puts
391
+ $stdout.puts
392
+ $stdout.puts "Duplicate value in #{destination_path}"
393
+ $stdout.puts " #{key}=#{@hash[key]}"
394
+ $stdout.puts " #{key}=#{value}"
395
+ $stdout.puts
390
396
  if @hash[key] == value
391
- puts " Values are the same but duplicate values still should not exist!"
392
- puts
397
+ $stdout.puts " Values are the same but duplicate values still should not exist!"
398
+ $stdout.puts
393
399
  end
394
400
  raise "Duplicate key error"
395
401
  end
@@ -403,7 +409,7 @@ module YMDP
403
409
  # Write JSON file to its destination.
404
410
  #
405
411
  def write_template(result)
406
- puts destination_path if CONFIG.verbose?
412
+ $stdout.puts destination_path if CONFIG.verbose?
407
413
  write_template_without_layout(result)
408
414
  end
409
415
  end
@@ -110,9 +110,9 @@ module YMDP
110
110
  end
111
111
 
112
112
  def file_not_found(filename)
113
- puts
114
- puts "Create #{filename} with the following command:\n\n ./script/config"
115
- puts
113
+ $stdout.puts
114
+ $stdout.puts "Create #{filename} with the following command:\n\n ./script/config"
115
+ $stdout.puts
116
116
 
117
117
  raise "File not found: #{filename}"
118
118
  end
@@ -1,20 +1,54 @@
1
+ require 'rubygems'
2
+ require 'ymdp/base'
3
+ require 'active_support'
1
4
  require 'support/file'
2
5
 
3
6
  module YMDP
4
- module Compressor
5
- class Base
6
- extend YMDP::FileSupport
7
-
7
+ module Compressor #:nodoc:
8
+ # Compresses a file using the specified compressor/minifier (currently YUI Compressor 2.4.2).
9
+ #
10
+ # === Usage
11
+ #
12
+ # YMDP::Compressor::Base.compress("filename.js", "type" => "js")
13
+ #
14
+ # YMDP::Compressor::Base.compress("filename.css", "type" => "css")
15
+ #
16
+ # Outputs the compressed file to <tt>_path_.min.</tt>
17
+ #
18
+ # === Options
19
+ #
20
+ # "type":: "js" or "css". Identifies the file type.
21
+ #
22
+ # "obfuscate":: true or false. Change internal variable names within the code.
23
+ #
24
+ # "verbose":: true or false. Output warnings about code quality.
25
+ #
26
+ # "preserve_semi":: true or false. Preserve unnecessary semicolons.
27
+ #
28
+ # TODO: Make it support multiple compressors.
29
+ #
30
+ # TODO: Convert it to an object, so you instantiate the Compressor class and then call it
31
+ # on a file or a string.
32
+ #
33
+ # TODO: Get some stringify_keys! action going so you can send in symbols or strings.
34
+ #
35
+ class Base < YMDP::Base
8
36
  def self.compress(path, options={})
37
+ options.stringify_keys!
9
38
  compressed_display_path = display_path(path)
10
39
  compressed_path = "#{path}.min"
11
40
 
12
41
  options["type"] ||= "js"
13
-
42
+ options["type"] = options["type"].to_s
43
+
44
+ # if the compressed_file exists, don't create it again
45
+ #
14
46
  unless File.exists?(compressed_path)
15
47
  $stdout.print " #{compressed_display_path} compressing . . . "
16
48
  compressed = ''
17
49
 
50
+ # set options and defaults
51
+ #
18
52
  if options.delete("obfuscate")
19
53
  options["nomunge"] = ""
20
54
  end
@@ -27,21 +61,26 @@ module YMDP
27
61
  options["preserve-semi"] = ""
28
62
  end
29
63
 
64
+ # join the options together for appending to the command line
65
+ #
30
66
  options_string = options.map {|k,v| "--#{k} #{v}"}.join(" ")
31
67
 
32
- result = `java -jar ./script/yuicompressor-2.4.2.jar #{options_string} #{path} -o #{compressed_path} 2>&1`
68
+ # call the compressor
69
+ #
70
+ command = "java -jar ./script/yuicompressor-2.4.2.jar #{options_string} #{path} -o #{compressed_path} 2>&1"
71
+ result = F.execute(command, :return => true)
33
72
 
34
73
  result.split("\n").each do |line|
35
74
  if line =~ /\[ERROR\] (\d+):(\d+):(.*)/
36
75
  line_number = $1.to_i
37
76
  error = "Error at #{compressed_display_path} line #{line_number} character #{$2}: #{$3}"
38
- error += get_line_from_file(path, line_number)
77
+ error += F.get_line_from_file(path, line_number)
39
78
 
40
79
  $stdout.puts error
41
- growl(error)
80
+ g(error)
42
81
  end
43
82
  end
44
-
83
+
45
84
  if result =~ /ERROR/
46
85
  raise "JavaScript errors in #{compressed_display_path}"
47
86
  else
@@ -49,10 +88,10 @@ module YMDP
49
88
  end
50
89
  end
51
90
 
91
+ # The compressed file should exist now. If it does, use it. If not, raise an error.
92
+ #
52
93
  if File.exists?(compressed_path)
53
- File.open(compressed_path) do |c|
54
- compressed = c.read
55
- end
94
+ compressed = File.read(compressed_path)
56
95
  else
57
96
  raise "File does not exist: #{compressed_display_path}"
58
97
  end
@@ -61,13 +100,27 @@ module YMDP
61
100
  end
62
101
  end
63
102
 
103
+ # Compresses a CSS file using the specified compressor/minifier (currently YUI Compressor 2.4.2).
104
+ #
105
+ # === Usage
106
+ #
107
+ # YMDP::Compressor::Stylesheet.compress("filename.css")
108
+ #
64
109
  class Stylesheet < Base
110
+ # TODO: Add options hash
65
111
  def self.compress(path)
66
112
  super(path, "type" => "css")
67
113
  end
68
114
  end
69
115
 
70
- class JavaScript < Base
116
+ # Compresses a JavaScript file using the specified compressor/minifier (currently YUI Compressor 2.4.2).
117
+ #
118
+ # === Usage
119
+ #
120
+ # YMDP::Compressor::JavaScript.compress("filename.js")
121
+ #
122
+ class JavaScript < Base
123
+ # TODO: Add options hash
71
124
  def self.compress(filename)
72
125
  super(filename, "type" => "js")
73
126
  end
@@ -4,9 +4,9 @@ require 'rubygems'
4
4
  require 'mime/types'
5
5
  require 'net/http'
6
6
  begin
7
- require 'CGI'
7
+ require 'CGI'
8
8
  rescue StandardError => e
9
- puts e.message
9
+ # puts e.message
10
10
  end
11
11
 
12
12
  class FormField
@@ -1,37 +1,51 @@
1
+ require 'ymdp/base'
1
2
  require 'support/file'
2
3
  require 'processor/w3c'
3
4
  require 'processor/form_post'
4
5
 
5
6
  module YMDP
6
7
  module Validator
7
- class Base
8
- extend YMDP::FileSupport
8
+ class Base < YMDP::Base
9
9
  end
10
10
 
11
11
  class HTML < Base
12
12
  def self.validate(path)
13
13
  html_display_path = display_path(path)
14
+ log_path = "#{TMP_PATH}/#{File.basename(path)}_errors.html"
14
15
 
16
+ log_path = validation_errors(path)
17
+ if log_path
18
+ g("HTML validation errors found")
19
+ F.execute "open #{log_path}"
20
+ raise "Invalid HTML"
21
+ else
22
+ $stdout.puts " #{html_display_path} validating . . . OK"
23
+ end
24
+ end
25
+
26
+ def self.validation_errors(path)
27
+ html_display_path = display_path(path)
15
28
  doctype = CONFIG["doctype"] || "HTML 4.0 Transitional"
16
29
 
17
- resp = post_file_to_w3c_validator(path, doctype)
30
+ resp = W3CPoster.post_file_to_w3c_validator(path, doctype)
18
31
  html = resp.read_body
19
32
  if html.include? "[Valid]"
20
- $stdout.puts " #{html_display_path} validating . . . OK"
21
- else
33
+ false
34
+ else
22
35
  log_path = "#{TMP_PATH}/#{File.basename(path)}_errors.html"
23
36
  $stdout.puts " #{html_display_path} is not valid HTML, writing to #{display_path(log_path)}"
24
37
  $stdout.puts
25
38
  $stdout.puts " To view errors:"
26
39
  $stdout.puts " open #{display_path(log_path)}"
27
40
  $stdout.puts
28
- File.open(log_path,'w') { |f| f.puts html }
29
- $stdout.puts " Viewing errors..."
30
-
31
- g("HTML validation errors found")
32
- system "open #{log_path}"
33
- raise "Invalid HTML"
34
- end
41
+
42
+ File.open(log_path,'w') do |f|
43
+ f.puts html
44
+ end
45
+
46
+ $stdout.puts " Viewing errors..."
47
+ log_path
48
+ end
35
49
  end
36
50
  end
37
51
 
@@ -79,7 +93,7 @@ JSLINT
79
93
  f.puts output
80
94
  end
81
95
 
82
- results = `java org.mozilla.javascript.tools.shell.Main ./script/jslint.js #{js_fragment_path}`
96
+ results = F.execute("java org.mozilla.javascript.tools.shell.Main ./script/jslint.js #{js_fragment_path}", :return => true)
83
97
 
84
98
  if results =~ /jslint: No problems found/
85
99
  $stdout.puts "OK"
@@ -89,12 +103,12 @@ JSLINT
89
103
  if result =~ /line (\d+) character (\d+): (.*)/
90
104
  line_number = $1.to_i
91
105
  error = "Error at #{fragment_display_path} line #{line_number-jslint_settings_count} character #{$2}: #{$3}"
92
- error += get_line_from_file(js_fragment_path, line_number)
106
+ error += F.get_line_from_file(js_fragment_path, line_number)
93
107
 
94
108
  $stdout.puts error
95
109
  end
96
110
  end
97
- message = "Javascript Errors embedded in #{display}"
111
+ message = "JavaScript Errors embedded in #{display}"
98
112
  g(message)
99
113
  raise message
100
114
  end
@@ -103,12 +117,18 @@ JSLINT
103
117
  end
104
118
 
105
119
  class JSON < JavaScript
106
- def pre_process(output)
120
+ def self.pre_process(output)
107
121
  output
108
122
  end
109
123
 
110
124
  def self.jslint_settings
111
125
  end
112
126
  end
127
+
128
+ class Stylesheet < Base
129
+ def self.validate(filename)
130
+ true
131
+ end
132
+ end
113
133
  end
114
134
  end