Class: RaaP::CLI

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

Overview

$ raap Integer#pow

Defined Under Namespace

Classes: Option

Constant Summary collapse

DEFAULT_SKIP =

Should skip methods has side effects

Set.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/raap/cli.rb', line 38

def initialize(argv)
  # defaults
  @option = Option.new(
    timeout: 3,
    size_from: 0,
    size_to: 99,
    size_by: 1,
    coverage: true,
    allow_private: false,
  )
  @argv = argv
  @skip = DEFAULT_SKIP.dup
  @results = []
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



36
37
38
# File 'lib/raap/cli.rb', line 36

def argv
  @argv
end

#optionObject

Returns the value of attribute option.



36
37
38
# File 'lib/raap/cli.rb', line 36

def option
  @option
end

#resultsObject

Returns the value of attribute results.



36
37
38
# File 'lib/raap/cli.rb', line 36

def results
  @results
end

#skipObject

Returns the value of attribute skip.



36
37
38
# File 'lib/raap/cli.rb', line 36

def skip
  @skip
end

Instance Method Details

#loadObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/raap/cli.rb', line 53

def load
  OptionParser.new do |o|
    o.version = RaaP::VERSION

    o.on('-I', '--include PATH') do |path|
      RaaP::RBS.loader.add(path: Pathname(path))
    end
    o.on('--library lib', 'load rbs library') do |lib|
      RaaP::RBS.loader.add(library: lib, version: nil)
    end
    o.on('--require lib', 'require ruby library') do |lib|
      require lib
    end
    o.on('--log-level level', "default: info") do |arg|
      RaaP.logger.level = arg
    end
    o.on('--timeout sec', Float, "default: #{@option.timeout}") do |arg|
      @option.timeout = arg
    end
    o.on('--size-from int', Integer, "default: #{@option.size_from}") do |arg|
      @option.size_from = arg
    end
    o.on('--size-to int', Integer, "default: #{@option.size_to}") do |arg|
      @option.size_to = arg
    end
    o.on('--size-by int', Integer, "default: #{@option.size_by}") do |arg|
      @option.size_by = arg
    end
    o.on('--allow-private', "default: #{@option.allow_private}") do
      @option.allow_private = true
    end
    o.on('--preload path', 'Kernel.load path') do |path|
      Kernel.load path
    end
    o.on('--[no-]coverage', "Show coverage for RBS (default: #{@option.coverage})") do |arg|
      @option.coverage = arg
    end
  end.parse!(@argv)

  self
end

#runObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/raap/cli.rb', line 95

def run
  Signal.trap(:INT) do
    puts "Interrupted by SIGINT"
    report
    exit 1
  end

  # Search skip tag
  @argv.each do |tag|
    if tag.start_with?('!')
      t = tag[1..] or raise
      t = "::#{t}" unless t.start_with?('::')
      t or raise
      @skip << t
    end
  end
  @skip.freeze

  @argv.each do |tag|
    next if tag.start_with?('!')

    case
    when tag.include?('#')
      run_by(kind: :instance, tag: tag)
    when tag.include?('.')
      run_by(kind: :singleton, tag: tag)
    when tag.end_with?('*')
      run_by_type_name_with_search(tag: tag)
    else
      run_by_type_name(tag: tag)
    end
  end

  report
end