Class: Sorbet::Eraser::CLI

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

Overview

A small CLI that takes filepaths and erases them by writing back to the original file.

Constant Summary collapse

POOL_SIZE =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verify, filepaths) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/sorbet/eraser/cli.rb', line 12

def initialize(verify, filepaths)
  @verify = verify
  @filepaths = filepaths
end

Instance Attribute Details

#filepathsObject (readonly)

Returns the value of attribute filepaths.



10
11
12
# File 'lib/sorbet/eraser/cli.rb', line 10

def filepaths
  @filepaths
end

#verifyObject (readonly)

Returns the value of attribute verify.



10
11
12
# File 'lib/sorbet/eraser/cli.rb', line 10

def verify
  @verify
end

Class Method Details

.start(argv) ⇒ Object



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

def self.start(argv)
  verify = false

  if argv.first == "--verify"
    verify = true
    argv.shift
  end

  filepaths = []
  argv.each { |pattern| filepaths.concat(Dir.glob(pattern)) }

  new(verify, filepaths).start
end

Instance Method Details

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sorbet/eraser/cli.rb', line 17

def start
  queue = Queue.new
  filepaths.each { |filepath| queue << filepath }

  workers =
    POOL_SIZE.times.map do
      # push a symbol onto the queue for each thread so that it knows when
      # the end of the queue is and will exit its infinite loop
      queue << :eoq

      Thread.new do
        while filepath = queue.shift
          break if filepath == :eoq
          process(filepath)
        end
      end.tap { |thread| thread.abort_on_exception = true }
    end

  workers.each(&:join)
end