Class: BrewCaskTools::Tasks::Cleanup

Inherits:
Caskroom
  • Object
show all
Defined in:
lib/bct/tasks/cleanup.rb

Overview

Cleanup task

Instance Method Summary collapse

Methods inherited from Caskroom

#casks, #enumerate, #get

Constructor Details

#initialize(casks) ⇒ Cleanup

Returns a new instance of Cleanup.

Parameters:

  • optional

    casks [Array] array of cask names to cleanup



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bct/tasks/cleanup.rb', line 10

def initialize(casks)
  super()

  @cleaned = []

  if casks.empty?
    progressbar.total = caskroom.casks.length
    progressbar.log "\nLooking for casks to cleanup..."

    @cleaned = compile
  else
    @cleaned = casks.map { |c| caskroom.get(c) }
    return say 'Invalid cask specified', :red if @cleaned.compact.empty?
  end
  clean
end

Instance Method Details

#cleanObject



37
38
39
40
41
42
43
# File 'lib/bct/tasks/cleanup.rb', line 37

def clean
  return say "\nNo cleanup operations are necessary", :green if @cleaned.empty?

  @cleaned.each do |cask|
    cask.cleanup(&clean_block)
  end
end

#clean_blockObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bct/tasks/cleanup.rb', line 45

def clean_block
  proc do |cask, versions|
    name = cask.name
    say "Cleaning up #{name}", :yellow
    if cask.deprecated? &&
       no?("#{name} has been deprecated. Ok to remove all versions?", :red)

      say 'No action taken.', :red
      next
    end
    say "Removed #{versions.length} old version(s)", :yellow
  end
end

#compileObject



27
28
29
30
31
32
33
34
35
# File 'lib/bct/tasks/cleanup.rb', line 27

def compile
  cleaned = []
  caskroom.enumerate do |cask|
    increment(cask)

    cleaned << cask if cask.can_cleanup?
  end
  cleaned
end