| 1 |
require 'fileutils' |
|---|
| 2 |
require 'tempfile' |
|---|
| 3 |
|
|---|
| 4 |
class CompressUtil |
|---|
| 5 |
class << self |
|---|
| 6 |
def jar |
|---|
| 7 |
@@jar |
|---|
| 8 |
end |
|---|
| 9 |
|
|---|
| 10 |
def jar=(val) |
|---|
| 11 |
@@jar = val |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
def js_compress(base_dir, options = {}) |
|---|
| 15 |
options = { |
|---|
| 16 |
:join => nil, |
|---|
| 17 |
:pattern => /src_/, |
|---|
| 18 |
:prefix => 'src_', |
|---|
| 19 |
:replace => '', |
|---|
| 20 |
:tmp_file => false, |
|---|
| 21 |
}.merge(options) |
|---|
| 22 |
data = '' |
|---|
| 23 |
begin |
|---|
| 24 |
Dir.glob("#{base_dir}/#{options[:prefix]}*.js").collect do |src_path| |
|---|
| 25 |
if options[:join].nil? |
|---|
| 26 |
dest_path = src_path.sub(options[:pattern], options[:replace]) |
|---|
| 27 |
if options[:tmp_file] |
|---|
| 28 |
temp = Tempfile::new('tmp.js', base_dir) |
|---|
| 29 |
open(src_path) do |f| |
|---|
| 30 |
f.each do |line| |
|---|
| 31 |
temp.puts(line.sub(%r|//.*$|, '')) |
|---|
| 32 |
end |
|---|
| 33 |
end |
|---|
| 34 |
temp.close |
|---|
| 35 |
data = compress_js(temp.path) |
|---|
| 36 |
temp.close(true) |
|---|
| 37 |
else |
|---|
| 38 |
data = compress_js(src_path) |
|---|
| 39 |
end |
|---|
| 40 |
write_js(dest_path, data) |
|---|
| 41 |
else |
|---|
| 42 |
data += compress_js(src_path) |
|---|
| 43 |
end |
|---|
| 44 |
end |
|---|
| 45 |
write_js("#{base_dir}/#{options[:join]}.js", data) unless options[:join].nil? |
|---|
| 46 |
rescue Exception |
|---|
| 47 |
puts $!.to_s |
|---|
| 48 |
end |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def spinelz_img(src, dest) |
|---|
| 52 |
begin |
|---|
| 53 |
Dir.glob("#{src}/**/*.gif").collect do |src_path| |
|---|
| 54 |
dest_path = src_path.dup |
|---|
| 55 |
dest_path[src] = dest |
|---|
| 56 |
cp(src_path, dest_path) |
|---|
| 57 |
end |
|---|
| 58 |
rescue Exception |
|---|
| 59 |
puts $!.to_s |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
def spinelz_js(base_dir, spinelz_js, spinelz_utils) |
|---|
| 64 |
begin |
|---|
| 65 |
data = '' |
|---|
| 66 |
spinelz_js.each do |js| |
|---|
| 67 |
data += compress_js("#{base_dir}/compress/javascripts/spinelz/#{js}.js") |
|---|
| 68 |
end |
|---|
| 69 |
write_js("#{base_dir}/compress/javascripts/spinelz/spinelz_for_rubricks.js", data) |
|---|
| 70 |
data = '' |
|---|
| 71 |
spinelz_utils.each do |js| |
|---|
| 72 |
data += compress_js("#{base_dir}/compress/javascripts/spinelz_lib/#{js}.js") |
|---|
| 73 |
end |
|---|
| 74 |
write_js("#{base_dir}/compress/javascripts/spinelz_lib/spinelz_util_for_rubricks.js", data) |
|---|
| 75 |
rescue Exception |
|---|
| 76 |
puts $!.to_s |
|---|
| 77 |
end |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
def spinelz_css(src, dest, prefix, spinelz_css) |
|---|
| 81 |
begin |
|---|
| 82 |
Dir.glob("#{src}/**/*.css").collect do |src_path| |
|---|
| 83 |
dest_path = src_path.dup |
|---|
| 84 |
dest_path[src] = dest |
|---|
| 85 |
data = compress_css(src_path) |
|---|
| 86 |
File.open(dest_path, 'w') do |file| |
|---|
| 87 |
file.print data |
|---|
| 88 |
end |
|---|
| 89 |
end |
|---|
| 90 |
data = '' |
|---|
| 91 |
dest_path = "#{dest}/#{prefix}for_rubricks.css" |
|---|
| 92 |
spinelz_css.each do |css| |
|---|
| 93 |
src_path = "#{src}/#{css}.css" |
|---|
| 94 |
data += compress_css(src_path) |
|---|
| 95 |
end |
|---|
| 96 |
file = File.open(dest_path, 'w') do |file| |
|---|
| 97 |
file.print data |
|---|
| 98 |
end |
|---|
| 99 |
rescue Exception |
|---|
| 100 |
puts $!.to_s |
|---|
| 101 |
end |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
def system_css(src, prefix = 'src_', pattern = /src_/, replace = '') |
|---|
| 105 |
begin |
|---|
| 106 |
Dir.glob("#{src}/**/#{prefix}*.css").collect do |src_path| |
|---|
| 107 |
dest_path = src_path.sub(pattern, replace) |
|---|
| 108 |
data = compress_css(src_path) |
|---|
| 109 |
File.open(dest_path, 'w') do |file| |
|---|
| 110 |
file.print data |
|---|
| 111 |
end |
|---|
| 112 |
end |
|---|
| 113 |
rescue Exception |
|---|
| 114 |
puts $!.to_s |
|---|
| 115 |
end |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
private |
|---|
| 119 |
def compress_css(path) |
|---|
| 120 |
return File.read(path).gsub(/\t/, ' ').gsub(/[ ]+/, ' ').gsub(/[\f\n\r]/, '') |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
def compress_js(path) |
|---|
| 124 |
return `java -jar #{@@jar} -c #{path}` |
|---|
| 125 |
end |
|---|
| 126 |
|
|---|
| 127 |
def confirm_directory(path) |
|---|
| 128 |
dir = path.sub(%r(^(.*)/([^/]+)$), '\1') |
|---|
| 129 |
unless File.exist?(dir) |
|---|
| 130 |
FileUtils.mkdir_p(dir) |
|---|
| 131 |
end |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
def cp(src, dest) |
|---|
| 135 |
confirm_directory(dest) |
|---|
| 136 |
FileUtils.cp(src, dest) |
|---|
| 137 |
end |
|---|
| 138 |
|
|---|
| 139 |
def write_js(path, data) |
|---|
| 140 |
confirm_directory(path) |
|---|
| 141 |
file = File.open(path, 'w') |
|---|
| 142 |
file.binmode |
|---|
| 143 |
file.print data |
|---|
| 144 |
file.close |
|---|
| 145 |
end |
|---|
| 146 |
end |
|---|
| 147 |
end |
|---|