Sha256: a2497788e2550bd1a88fa3dc95f9ca78c24b1af107b5400d653fec6f02d0a0b4

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'optparse'
require 'rainbow'
require 'pathname'

module Cycromatic
  class CLI
    attr_reader :args

    def self.start(args = ARGV)
      self.new(args).run
    end

    def initialize(args)
      @args = args

      OptionParser.new do |opts|
        opts.on("--format FORMAT") {|fmt| @format = fmt }
      end.parse!(args)
    end

    def run
      formatter = @format == 'json' ? JSONFormatter.new(io: STDOUT) : TextFormatter.new(io: STDOUT)

      FileEnumerator.new(paths: paths).each do |path|
        formatter.started path: path
        begin
          node = Parser::CurrentRuby.parse(path.read, path.to_s)
          if node
            Calculator.new(node: node).each_complexity do |complexity|
              formatter.calculated(path: path, complexity: complexity)
            end
          end
        rescue => exn
          require 'pp'
          p exn
          pp exn.backtrace
          formatter.error(path: path, exception: exn)
        ensure
          formatter.finished path: path
        end
      end

      formatter.completed
    end

    def paths
      args.map {|arg| Pathname(arg) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cycromatic-0.1.0 lib/cycromatic/cli.rb