Sha256: 76691f42806b499a4121fee632e2f1e4d0b847891b9ff1513ae77499b4f8d5b5

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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 }
        opts.on("--version") do
          puts "cycromatic version #{VERSION}"
          exit
        end
      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
          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

3 entries across 3 versions & 1 rubygems

Version Path
cycromatic-0.1.3 lib/cycromatic/cli.rb
cycromatic-0.1.2 lib/cycromatic/cli.rb
cycromatic-0.1.1 lib/cycromatic/cli.rb