Class: Jets::Builders::Tidy

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/builders/tidy.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_root, noop: false) ⇒ Tidy

Returns a new instance of Tidy.



3
4
5
6
# File 'lib/jets/builders/tidy.rb', line 3

def initialize(project_root, noop: false)
  @project_root = project_root
  @noop = noop
end

Instance Method Details

#always_removalsObject

These directories will be removed regardless of dir level



71
72
73
# File 'lib/jets/builders/tidy.rb', line 71

def always_removals
  %w[.git spec tmp]
end

#cleanup!Object



8
9
10
11
12
13
14
15
16
# File 'lib/jets/builders/tidy.rb', line 8

def cleanup!
  removals.each do |removal|
    removal = removal.sub(%r{^/},'') # remove leading slash
    path = "#{@project_root}/#{removal}"
    rm_rf(path)
  end

  tidy_bundled
end

#get_removals(file) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/jets/builders/tidy.rb', line 30

def get_removals(file)
  path = file
  return [] unless File.exist?(path)

  removal = File.read(path).split("\n")
  removal.map {|i| i.strip}.reject {|i| i =~ /^#/ || i.empty?}
  # IE: ["/handlers", "/bundled*", "/vendor/jets]
end

#jetskeepObject

We clean out ignored files pretty aggressively. So provide a way for users to keep files from being cleaned out.



41
42
43
44
45
46
47
48
49
# File 'lib/jets/builders/tidy.rb', line 41

def jetskeep
  defaults = %w[.bundle bundled pack handlers public/assets]
  path = "#{@project_root}/.jetskeep"
  return defaults unless File.exist?(path)

  keep = IO.readlines(path)
  keep = keep.map {|i| i.strip}.reject { |i| i =~ /^#/ || i.empty? }
  (defaults + keep).uniq
end

#removalsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/builders/tidy.rb', line 18

def removals
  removals = always_removals
  removals += get_removals("#{@project_root}/.gitignore")
  removals += get_removals("#{@project_root}/.dockerignore")
  removals = removals.reject do |p|
    jetskeep.find do |keep|
      p.include?(keep)
    end
  end
  removals.uniq
end

#rm_rf(path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/jets/builders/tidy.rb', line 62

def rm_rf(path)
  exists = File.exist?("#{path}/.gitkeep") || File.exist?("#{path}/.keep")
  return if exists

  # say "  rm -rf #{path}".colorize(:yellow) # uncomment to debug
  system("rm -rf #{path}") unless @noop
end

#say(message) ⇒ Object



75
76
77
78
# File 'lib/jets/builders/tidy.rb', line 75

def say(message)
  message = "NOOP #{message}" if @noop
  puts message
end

#tidy_bundledObject

folders to remove in the bundled folder regardless of the level of the folder



52
53
54
55
56
57
58
59
60
# File 'lib/jets/builders/tidy.rb', line 52

def tidy_bundled
  Dir.glob("#{@project_root}/bundled/**/*").each do |path|
    next unless File.directory?(path)
    dir = File.basename(path)
    next unless always_removals.include?(dir)

    rm_rf(path)
  end
end