-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrycompressor.rb
More file actions
executable file
·74 lines (63 loc) · 1.75 KB
/
Copy pathrycompressor.rb
File metadata and controls
executable file
·74 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
class RYCompressor
DEFAULT_OPTIONS = {
core_jar_path: File.expand_path( "yuicompressor-2.4.7.jar", File.dirname(__FILE__) ),
charset: "GBK"
}.freeze
def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options.reject {|k, v| v.nil?})
end
def compress(file)
if File.file?(file)
compress_file(file) if to_compress?(file)
elsif File.directory?(file)
# support recursion!
Dir.glob(File.join(file, "*")) do |f|
compress(f)
end
else
warn "Legal file or directory must be supplied!"
end
end
def to_compress?(file)
is_normal_js_or_css? file and not is_merge_file? file
end
protected
def is_normal_js_or_css? file
file =~ /(?<!-min)\.(js|css)$/
end
def is_merge_file? file
file =~ /merge\./
end
# file: absolute file path,the file should be css or js file.
def compress_file(file)
type = file[/\.(css|js)$/, 1]
minfile = file.sub /(?=\.(css|js)$)/, "-min"
# SECURITY NOTICE:
# some fields come from user specificated source
# e.g. charset can be ' gbk && sudo rm / '
# This is dangerous especially when this script
# acts as a web service.
result = %x[
java -jar #{@options[:core_jar_path]} \
--type #{type} \
--charset #{@options[:charset]} \
-o #{minfile} #{file}
]
puts "#{file} => #{minfile}"
result
end
# Simple help infomation
def self.usage
puts "You need supply at least one file or directory as parameter. e.g.:",
"./rycompressor.rb some-file.js some/dir/"
end
end
if __FILE__ == $0
if ARGV.empty?
RYCompressor.usage
else
cpsr = RYCompressor.new
ARGV.each { |src| cpsr.compress(src) }
end
end