Class: Hbc::CLI::Cleanup

Inherits:
AbstractCommand show all
Defined in:
Library/Homebrew/cask/lib/hbc/cli/cleanup.rb

Constant Summary collapse

OUTDATED_DAYS =
10
OUTDATED_TIMESTAMP =
Time.now - (60 * 60 * 24 * OUTDATED_DAYS)

Instance Attribute Summary collapse

Attributes inherited from AbstractCommand

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCommand

abstract?, command_name, run, visible

Methods included from Options

included, #process_arguments

Constructor Details

#initialize(*args, cache_location: Hbc.cache) ⇒ Cleanup

Returns a new instance of Cleanup



17
18
19
20
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 17

def initialize(*args, cache_location: Hbc.cache)
  super(*args)
  @cache_location = Pathname.new(cache_location)
end

Instance Attribute Details

#cache_locationObject (readonly)

Returns the value of attribute cache_location



15
16
17
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 15

def cache_location
  @cache_location
end

Class Method Details

.helpObject



7
8
9
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 7

def self.help
  "cleans up cached downloads and tracker symlinks"
end

.needs_init?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 11

def self.needs_init?
  true
end

Instance Method Details

#cache_completesObject



45
46
47
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 45

def cache_completes
  cache_files.reject(&method(:incomplete?))
end

#cache_filesObject



26
27
28
29
30
31
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 26

def cache_files
  return [] unless cache_location.exist?
  cache_location.children
                .map(&method(:Pathname))
                .reject(&method(:outdated?))
end

#cache_incompletesObject



41
42
43
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 41

def cache_incompletes
  cache_files.select(&method(:incomplete?))
end

#delete_paths(paths) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 72

def delete_paths(paths)
  cleanup_size = 0
  processed_files = 0
  paths.each do |item|
    next unless item.exist?
    processed_files += 1

    begin
      LockFile.new(item.basename).with_lock do
        puts item
        cleanup_size += File.size(item)
        item.rmtree
      end
    rescue OperationInProgressError
      puts "skipping: #{item} is locked"
      next
    end
  end

  if processed_files.zero?
    puts "Nothing to do"
  else
    disk_space = disk_usage_readable(cleanup_size)
    ohai "This operation has freed approximately #{disk_space} of disk space."
  end
end

#disk_cleanup_sizeObject



49
50
51
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 49

def disk_cleanup_size
  cache_files.map(&:disk_usage).inject(:+)
end

#incomplete?(file) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 37

def incomplete?(file)
  file.extname == ".incomplete"
end

#outdated?(file) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 33

def outdated?(file)
  outdated_only? && file && file.stat.mtime > OUTDATED_TIMESTAMP
end

#remove_cache_files(*tokens) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 53

def remove_cache_files(*tokens)
  message = "Removing cached downloads"
  message.concat " for #{tokens.join(", ")}" unless tokens.empty?
  message.concat " older than #{OUTDATED_DAYS} days old" if outdated_only?
  ohai message

  deletable_cache_files = if tokens.empty?
    cache_files
  else
    start_withs = tokens.map { |token| "#{token}--" }

    cache_files.select do |path|
      path.basename.to_s.start_with?(*start_withs)
    end
  end

  delete_paths(deletable_cache_files)
end

#runObject



22
23
24
# File 'Library/Homebrew/cask/lib/hbc/cli/cleanup.rb', line 22

def run
  remove_cache_files(*args)
end