Module: CleanupVendor

Defined in:
lib/cleanup_vendor.rb,
lib/cleanup_vendor/path.rb,
lib/cleanup_vendor/version.rb

Defined Under Namespace

Classes: Error, Path

Constant Summary collapse

CONFIG_FILE =
File.expand_path('defaults.yml', __dir__)
DEFAULTS =
YAML.safe_load(File.binread(CONFIG_FILE)).transform_keys(&:to_sym).freeze
VERSION =
'0.6.1'

Class Method Summary collapse

Class Method Details

.filter(dir, opts = {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cleanup_vendor.rb', line 33

def filter(dir, opts = {})
  raise Error, 'Not a directory' unless File.directory?(dir.to_s)
  return to_enum(:filter, dir, opts) unless block_given?

  files, directories, filtered, exclude = get_options(opts)

  Path.new(dir).recursive_entries do |path|
    next if path.match?(exclude)
    next if path.include?(filtered)
    next unless path.file? && path.match?(files) || path.directory? && path.match?(directories)

    filtered << path
    yield(path)
  end
end

.format_summary(prefix, number) ⇒ Object



57
58
59
# File 'lib/cleanup_vendor.rb', line 57

def format_summary(prefix, number)
  "\t#{prefix}\t#{number.to_s.rjust(20)}"
end

.get_options(options) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



50
51
52
53
54
# File 'lib/cleanup_vendor.rb', line 50

def get_options(options)
  options.values_at(:files, :directories, :filtered, :exclude).map do |v|
    (v || []).to_set
  end
end


67
68
69
70
# File 'lib/cleanup_vendor.rb', line 67

def print_path(path)
  $stdout.write path
  $stdout.putc 0
end


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cleanup_vendor.rb', line 73

def print_summary(summary)
  all_files = summary.flatten
  count = all_files.count
  blocks = all_files.map(&:blocks).sum
  bytes = all_files.map(&:size).sum

  $stderr.puts 'Summary:'
  $stderr.puts format_summary('Removed files:', count)
  $stderr.puts format_summary('Total blocks:', blocks)
  $stderr.puts format_summary('Total bytes:', bytes)
end


62
63
64
# File 'lib/cleanup_vendor.rb', line 62

def print_verbose(path)
  $stderr.puts "Removing #{path}..."
end

.run(dir, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cleanup_vendor.rb', line 17

def run(dir, opts = {})
  summary = []

  filter(dir, DEFAULTS.merge(opts)) do |p|
    summary << p.summary if opts[:summary]

    print_verbose(p) if opts[:verbose]
    print_path(p) if opts[:print0]

    FileUtils.remove_entry(p) unless opts[:dry_run]
  end

  print_summary(summary) if opts[:summary]
end