Class: Cycromatic::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cycromatic/cli.rb', line 13

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

Class Method Details

.start(args = ARGV) ⇒ Object



9
10
11
# File 'lib/cycromatic/cli.rb', line 9

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

Instance Method Details

#pathsObject



47
48
49
# File 'lib/cycromatic/cli.rb', line 47

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

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cycromatic/cli.rb', line 25

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