Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 88 lines (76 sloc) 2.872 kb
4c99415 @KlausTrainer Initial commit
authored
1 #!/usr/bin/env ruby
2
a643580 @KlausTrainer Rewrite with lots of improvements
authored
3 JAVASCRIPT_DIR = "javascripts"
4c99415 @KlausTrainer Initial commit
authored
4 COMPRESSOR = "uglifyjs -nc"
5
6 def doit!
a643580 @KlausTrainer Rewrite with lots of improvements
authored
7 exit_and_print_usage! if ['-h', '--help'].include?(ARGV.first)
8 destination = if ['-d', '--destination'].include?(ARGV.first)
9 unless ARGV[1]
10 exit_and_print_usage!
11 else
12 ARGV[1]
13 end
4c99415 @KlausTrainer Initial commit
authored
14 end
15
a643580 @KlausTrainer Rewrite with lots of improvements
authored
16 filenames = if destination
17 ARGV.slice(2, ARGV.length - 2) if ARGV.length > 2
18 else
19 ARGV.slice(0, ARGV.length)
4c99415 @KlausTrainer Initial commit
authored
20 end
21
a643580 @KlausTrainer Rewrite with lots of improvements
authored
22 unless filenames
23 `couchapp push #{destination}`
24 exit(0)
25 end
4c99415 @KlausTrainer Initial commit
authored
26
a643580 @KlausTrainer Rewrite with lots of improvements
authored
27 htmls = filenames.map do |filename|
28 begin
29 File.read(filename)
30 rescue Exception
31 exit_from_file_read_error!(filename)
4c99415 @KlausTrainer Initial commit
authored
32 end
33 end
34
a643580 @KlausTrainer Rewrite with lots of improvements
authored
35 begin
36 # remove all script tags with source paths from the html files
37 # and insert the concatenated compressed scripts instead
38 compressed_scripts_files = []
39 htmls.each_with_index do |html, i|
bf0f177 @KlausTrainer Improve script tag regex
authored
40 script_regex = /<script (?:[^>]* )?src=['"]([^'"]+)['"](?: [^>]*)?>.*<\/script>/
a643580 @KlausTrainer Rewrite with lots of improvements
authored
41 script_paths = html.scan(script_regex).flatten
42 html_without_scripts = html.gsub(/#{script_regex}/, '')
43 compressed_scripts = compress_and_concat_scripts(script_paths)
44 compressed_scripts_filename = "#{Time.now.to_i}.min.js"
45 compressed_scripts_pathname = "_attachments/#{JAVASCRIPT_DIR}/#{compressed_scripts_filename}"
46 File.write(compressed_scripts_pathname, compressed_scripts)
47 compressed_scripts_files << compressed_scripts_pathname # cleanup later
48 script_tag = "<script type=\"text/javascript\" src=\"#{JAVASCRIPT_DIR}/#{compressed_scripts_filename}\"></script>"
49 exit_no_body_tag!(filenames[i]) unless html_without_scripts.match('</body>')
50 new_html = html_without_scripts.sub('</body>', "</body>#{script_tag}")
51 File.write(filenames[i], new_html)
52 end
4c99415 @KlausTrainer Initial commit
authored
53 `couchapp push #{destination}`
a643580 @KlausTrainer Rewrite with lots of improvements
authored
54 ensure
55 # restore
56 htmls.each_with_index {|html, i| File.write(filenames[i], html) }
57 # and cleanup
58 compressed_scripts_files.each {|filename| File.delete(filename) }
4c99415 @KlausTrainer Initial commit
authored
59 end
60 end
61
a643580 @KlausTrainer Rewrite with lots of improvements
authored
62 def compress_and_concat_scripts(script_paths)
63 script_paths.reduce('') do |compressed_scripts, script_path|
64 filename = `find -type f -name #{File.basename(script_path)}`.split("\n").first
65 exit_from_file_read_error!(script_path) unless filename
66 compressor_output = `#{COMPRESSOR} #{filename} 2> /dev/null`
67 compressed_scripts << if $?.exitstatus == 0
68 "#{compressor_output}\n\n"
4c99415 @KlausTrainer Initial commit
authored
69 else
a643580 @KlausTrainer Rewrite with lots of improvements
authored
70 exit_from_file_read_error!(script_paths[i])
4c99415 @KlausTrainer Initial commit
authored
71 end
72 end
73 end
74
a643580 @KlausTrainer Rewrite with lots of improvements
authored
75 def exit_and_print_usage!()
76 abort("#{$0}:\n\tusage: #{$0} [-d <destination>] [<filename(s)>]\n\texample: #{$0} -d production _attachments/index.html templates/post.html")
77 end
78
79 def exit_from_file_read_error!(filename)
80 abort("error: cannot read the file '#{filename}'")
81 end
82
83 def exit_no_body_tag!(filename)
84 abort("error: the file '#{filename}' does not contain a </body> tag!")
85 end
86
4c99415 @KlausTrainer Initial commit
authored
87 doit!
Something went wrong with that request. Please try again.