Class: Deadweight::CLI
- Inherits:
-
Object
- Object
- Deadweight::CLI
- Defined in:
- lib/deadweight/cli.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
- .execute(stdout, stdin, stderr, arguments = []) ⇒ Object
- .option_parser ⇒ Object
- .parse_options(arguments = []) ⇒ Object
Instance Method Summary collapse
- #execute! ⇒ Object
-
#initialize(stdout, stdin, stderr, arguments = [], options = {}) ⇒ CLI
constructor
A new instance of CLI.
- #process ⇒ Object
- #proxy ⇒ Object
Constructor Details
#initialize(stdout, stdin, stderr, arguments = [], options = {}) ⇒ CLI
Returns a new instance of CLI.
67 68 69 70 71 72 73 74 |
# File 'lib/deadweight/cli.rb', line 67 def initialize(stdout, stdin, stderr, arguments = [], = {}) @stdout = stdout @stdin = stdin @stderr = stderr @arguments = arguments @options = @output = [:output] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
7 8 9 |
# File 'lib/deadweight/cli.rb', line 7 def arguments @arguments end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/deadweight/cli.rb', line 7 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
8 9 10 |
# File 'lib/deadweight/cli.rb', line 8 def output @output end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
6 7 8 |
# File 'lib/deadweight/cli.rb', line 6 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
6 7 8 |
# File 'lib/deadweight/cli.rb', line 6 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
6 7 8 |
# File 'lib/deadweight/cli.rb', line 6 def stdout @stdout end |
Class Method Details
.execute(stdout, stdin, stderr, arguments = []) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/deadweight/cli.rb', line 10 def self.execute(stdout, stdin, stderr, arguments = []) @options = { :log_file => stderr, :output => stdout, :proxy_port => 8002 } self.(arguments) self.new(stdout, stdin, stderr, arguments, @options).execute! end |
.option_parser ⇒ Object
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 |
# File 'lib/deadweight/cli.rb', line 21 def self.option_parser @option_parser ||= OptionParser.new do |opts| opts. = "Usage: #{$0} [options] <url> [<url> ...]" @options[:stylesheets] = [] opts.on("-L", "--lyndon", "Pre-process HTML with Lyndon") do @options[:lyndon] = true end opts.on("-l", "--log FILE", "Where to write log messages") do |v| @options[:log_file] = v end opts.on("-P", "--proxy", "Run in proxy mode") do @options[:proxy] = true end opts.on("-p", "--proxy-port", "Port to run the proxy on") do |v| @options[:proxy] = true @options[:proxy_port] = v.to_i end opts.on("-O", "--output FILE", "Where to output orphaned CSS rules") do |v| @options[:output] = File.new(v, "w") end opts.on("-s", "--stylesheet FILE", "Apply the specified stylesheet to the target") do |v| @options[:stylesheets] << v end opts.on("-w", "--whitelist URL-PREFIX", "Specifies a prefix for URLs to process") do |v| @options[:whitelist] ||= [] @options[:whitelist] << v end end end |
.parse_options(arguments = []) ⇒ Object
62 63 64 65 |
# File 'lib/deadweight/cli.rb', line 62 def self.(arguments = []) self.option_parser.parse!(arguments) @options end |
Instance Method Details
#execute! ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/deadweight/cli.rb', line 76 def execute! if [:proxy] proxy elsif arguments.empty? stdout.puts self.class.option_parser.help else process end end |
#process ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/deadweight/cli.rb', line 86 def process # TODO pass stylesheets + pages as args dw = Deadweight.new # TODO this should be the default dw.root = "" dw.log_file = [:log_file] dw.stylesheets = [:stylesheets] dw.rules = stdin.read if stdin.stat.size > 0 if [:lyndon] arguments.each do |file| dw.pages << IO.popen("cat #{file} | lyndon 2> /dev/null") end else dw.pages = arguments end unused_rules = dw.run unused_rules.each do |k,v| output.puts "#{k} { #{v} }" end end |
#proxy ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/deadweight/cli.rb', line 113 def proxy dw = Deadweight.new # TODO note the boilerplate shared with #process dw.root = "" dw.log_file = [:log_file] dw.stylesheets = [:stylesheets] dw.rules = stdin.read if stdin.stat.size > 0 # initialize selectors dw.run stdout.puts "#{dw.unused_selectors.length} rules loaded." require 'webrick/httpproxy' @proxy = WEBrick::HTTPProxyServer.new \ :AccessLog => [ [[:log_file], WEBrick::AccessLog::COMMON_LOG_FORMAT], [[:log_file], WEBrick::AccessLog::REFERER_LOG_FORMAT] ], :Logger => WEBrick::Log.new([:log_file]), :Port => [:proxy_port], :ProxyContentHandler => lambda { |request, response| parse_this = false if [:whitelist] [:whitelist].each do |x| sliced_request_uri = response.request_uri.to_s[0..x.length - 1] if sliced_request_uri.downcase == x.downcase parse_this = true break end end else parse_this = true end if parse_this && response.header["content-type"] =~ /text\/html/ # TODO this slows things down significantly; better would be to # remove the Accept-Encoding header during the request phase body = if response.header["content-encoding"] == "gzip" Zlib::GzipReader.new(StringIO.new(response.body)).read elsif response.header["content-encoding"] == "deflate" Zlib::Inflate.inflate(response.body) else response.body end dw.process!(body) stdout.puts "After reviewing <#{response.request_uri}>, there were #{dw.unused_selectors.length} rules left" # stdout.puts "After reviewing <#{response.request_uri}>, these were left:" # dw.unused_selectors.each do |k,v| # stdout.puts "#{k} { #{v} }" # end end } trap('INT') do @proxy.shutdown # dump the remaining CSS rules if output is set unless [:output] == STDOUT dw.unused_selectors.each do |k,v| output.puts "#{k} { #{v} }" end end end @proxy.start end |