lib/csscss/cli.rb in csscss-0.1.0 vs lib/csscss/cli.rb in csscss-0.2.0
- old
+ new
@@ -1,21 +1,47 @@
module Csscss
class CLI
def initialize(argv)
- @argv = argv
+ @argv = argv
@verbose = false
+ @color = true
@minimum = 3
+ @compass = false
end
def run
parse(@argv)
execute
end
def execute
+
all_redundancies = @argv.map do |filename|
- contents = File.read(filename)
+ contents = if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp)
+ begin
+ require "sass"
+ rescue LoadError
+ puts "Must install sass gem before parsing sass/scss files"
+ exit 1
+ end
+
+ sass_options = {cache:false}
+ sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass
+ begin
+ Sass::Engine.for_file(filename, sass_options).render
+ rescue Sass::SyntaxError => e
+ if e.message =~ /compass/ && !@compass
+ puts "Enable --compass option to use compass's extensions"
+ exit 1
+ else
+ raise e
+ end
+ end
+ else
+ open(filename) {|f| f.read }
+ end
+
RedundancyAnalyzer.new(contents).redundancies(@minimum)
end
combined_redundancies = all_redundancies.inject({}) do |combined, redundancies|
if combined.empty?
@@ -28,11 +54,12 @@
end
if @json
puts JSONReporter.new(combined_redundancies).report
else
- puts Reporter.new(combined_redundancies).report(@verbose)
+ report = Reporter.new(combined_redundancies).report(verbose:@verbose, color:true)
+ puts report unless report.empty?
end
rescue Parslet::ParseFailed => e
puts "Had a problem parsing the css"
puts e.cause.ascii_tree
@@ -46,16 +73,31 @@
opts.on("-v", "--[no-]verbose", "Display each rule") do |v|
@verbose = v
end
+ opts.on("--[no-]color", "Colorizes output") do |c|
+ @color = c
+ end
+
opts.on("-n", "--num N", Integer, "Print matches with at least this many rules. Defaults to 3") do |n|
@minimum = n
end
opts.on("-V", "--version", "Show version") do |v|
puts opts.ver
exit
+ end
+
+ opts.on("--[no-]compass", "Enables compass extensions when parsing sass/scss") do |compass|
+ if @compass = compass
+ begin
+ require "compass"
+ rescue LoadError
+ puts "Must install compass gem before enabling its extensions"
+ exit 1
+ end
+ end
end
opts.on("-j", "--[no-]json", "Output results in JSON") do |j|
@json = j
end