Class: Deadweight::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/deadweight/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, stdin, stderr, arguments = [], options = {}) ⇒ CLI



73
74
75
76
77
78
79
80
# File 'lib/deadweight/cli.rb', line 73

def initialize(stdout, stdin, stderr, arguments = [], options = {})
  @stdout = stdout
  @stdin  = stdin
  @stderr = stderr
  @arguments = arguments
  @options = options
  @output = options[:output]
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments



7
8
9
# File 'lib/deadweight/cli.rb', line 7

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options



7
8
9
# File 'lib/deadweight/cli.rb', line 7

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output



8
9
10
# File 'lib/deadweight/cli.rb', line 8

def output
  @output
end

#stderrObject (readonly)

Returns the value of attribute stderr



6
7
8
# File 'lib/deadweight/cli.rb', line 6

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin



6
7
8
# File 'lib/deadweight/cli.rb', line 6

def stdin
  @stdin
end

#stdoutObject (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
20
# File 'lib/deadweight/cli.rb', line 10

def self.execute(stdout, stdin, stderr, arguments = [])
  @options = {
    :root       => "",
    :log_file   => stderr,
    :output     => stdout,
    :proxy_port => 8002
  }

  self.parse_options(arguments)
  self.new(stdout, stdin, stderr, arguments, @options).execute!
end

.option_parserObject



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
# File 'lib/deadweight/cli.rb', line 22

def self.option_parser
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "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("-r", "--root URL-OR-PATH",
            "Specify a root for all urls/paths") do |r|
      @options[:root] = r
    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



68
69
70
71
# File 'lib/deadweight/cli.rb', line 68

def self.parse_options(arguments = [])
  self.option_parser.parse!(arguments)
  @options
end

Instance Method Details

#execute!Object



82
83
84
85
86
87
88
89
90
# File 'lib/deadweight/cli.rb', line 82

def execute!
  if options[:proxy]
    proxy
  elsif arguments.empty?
    stdout.puts self.class.option_parser.help
  else
    process
  end
end

#processObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/deadweight/cli.rb', line 92

def process
  # TODO pass stylesheets + pages as args
  dw = Deadweight.new

  # TODO this should be the default
  dw.root = options[:root]

  dw.log_file = options[:log_file]

  dw.stylesheets = options[:stylesheets]

  dw.rules = stdin.read if stdin.stat.size > 0

  if options[:lyndon]
    arguments.each do |file|
      dw.pages << IO.popen("cat #{file} | lyndon 2> /dev/null")
    end
  else
    dw.pages = arguments
  end

  dw.run
  dw.dump(output)
end

#proxyObject



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
185
186
187
188
# File 'lib/deadweight/cli.rb', line 117

def proxy
  dw = Deadweight.new

  # TODO note the boilerplate shared with #process
  dw.root = options[:root]
  dw.log_file = options[:log_file]
  dw.stylesheets = options[: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           => [
      [options[:log_file], WEBrick::AccessLog::COMMON_LOG_FORMAT],
      [options[:log_file], WEBrick::AccessLog::REFERER_LOG_FORMAT]
    ],
    :Logger              => WEBrick::Log.new(options[:log_file]),
    :Port                => options[:proxy_port],
    :ProxyContentHandler => lambda { |request, response|

      parse_this = false

      if options[:whitelist]
        options[: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 options[:output] == STDOUT
      dw.unused_selectors.each do |k,v|
        output.puts "#{k} { #{v} }"
      end
    end
  end

  @proxy.start
end