lib/flay.rb in flay-2.0.1 vs lib/flay.rb in flay-2.1.0

- old
+ new

@@ -13,22 +13,23 @@ alias :binread :read unless RUBY19 end end class Flay - VERSION = '2.0.1' + VERSION = '2.1.0' def self.default_options { :diff => false, :mass => 16, :summary => false, :verbose => false, + :timeout => 10, } end - def self.parse_options + def self.parse_options args = ARGV options = self.default_options OptionParser.new do |opts| opts.banner = 'flay [options] files_or_dirs' opts.version = Flay::VERSION @@ -44,11 +45,12 @@ opts.on('-f', '--fuzzy', "DEAD: fuzzy similarities.") do abort "--fuzzy is no longer supported. Sorry. It sucked." end - opts.on('-m', '--mass MASS', Integer, "Sets mass threshold") do |m| + opts.on('-m', '--mass MASS', Integer, + "Sets mass threshold (default = #{options[:mass]})") do |m| options[:mass] = m.to_i end opts.on('-v', '--verbose', "Verbose. Show progress processing files.") do options[:verbose] = true @@ -60,17 +62,27 @@ opts.on('-s', '--summary', "Summarize. Show flay score per file only.") do options[:summary] = true end + opts.on('-t', '--timeout TIME', Integer, + "Set the timeout. (default = #{options[:timeout]})") do |t| + options[:timeout] = t.to_i + end + extensions = ['rb'] + Flay.load_plugins opts.separator "" opts.separator "Known extensions: #{extensions.join(', ')}" + extensions.each do |meth| + msg = "options_#{meth}" + send msg, opts, options if self.respond_to?(msg) + end + begin - opts.parse! + opts.parse! args rescue => e abort "#{e}\n\n#{opts}" end end @@ -153,12 +165,10 @@ process_sexp sexp rescue SyntaxError => e warn " skipping #{file}: #{e.message}" end end - - analyze end def analyze self.prune @@ -170,11 +180,11 @@ end end def process_rb file begin - RubyParser.new.process(File.binread(file), file) + RubyParser.new.process(File.binread(file), file, option[:timeout]) rescue Timeout::Error warn "TIMEOUT parsing #{file}. Skipping." end end @@ -249,10 +259,12 @@ score end def report prune = nil + analyze + puts "Total score (lower is better) = #{self.total}" puts if option[:summary] then @@ -280,10 +292,10 @@ count += 1 puts "%d) %s code found in %p (mass%s = %d)" % [count, match, node.first, bonus, mass] - nodes.each_with_index do |x, i| + nodes.sort_by { |x| [x.file, x.line] }.each_with_index do |x, i| if option[:diff] then c = (?A.ord + i).chr puts " #{c}: #{x.file}:#{x.line}" else puts " #{x.file}:#{x.line}"