Class: Jets::Builders::Tidy

Inherits:
Object
  • Object
show all
Includes:
Util
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.



5
6
7
8
# File 'lib/jets/builders/tidy.rb', line 5

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



98
99
100
# File 'lib/jets/builders/tidy.rb', line 98

def always_removals
  %w[.git spec tmp]
end

#clean_vendor_gemsObject

folders to remove in the vendor/gems folder regardless of the level of the folder



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jets/builders/tidy.rb', line 69

def clean_vendor_gems
  # Thanks: https://stackoverflow.com/questions/11385795/ruby-list-directory-with-dir-including-dotfiles-but-not-and
  Dir.glob("#{@project_root}/vendor/gems/**/*", File::FNM_DOTMATCH).each do |path|
    next unless File.directory?(path)
    dir = File.basename(path)
    next unless always_removals.include?(dir)

    rm_rf(path)
  end

  remove_gem_cache
end

#clean_webpack_assetsObject

Clean out unnecessary src and compiled packs because Jets serves them out of s3. This keeps the code size down to help keep it in size limit so we can use the live Lambda console editor.



24
25
26
27
28
29
30
31
32
# File 'lib/jets/builders/tidy.rb', line 24

def clean_webpack_assets
  FileUtils.rm_rf("#{@project_root}/app/javascript/src")

  return unless File.exist?("#{@project_root}/public/packs") # this class works for rack subfolder too
  FileUtils.mv("#{@project_root}/public/packs/manifest.json", "#{stage_area}/manifest.json")
  FileUtils.rm_rf("#{@project_root}/public/packs")
  FileUtils.mkdir_p("#{@project_root}/public/packs")
  FileUtils.mv("#{stage_area}/manifest.json", "#{@project_root}/public/packs/manifest.json")
end

#cleanup!Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/jets/builders/tidy.rb', line 10

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

  clean_vendor_gems
  clean_webpack_assets
end

#get_removals(file) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/jets/builders/tidy.rb', line 47

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.



58
59
60
61
62
63
64
65
66
# File 'lib/jets/builders/tidy.rb', line 58

def jetskeep
  always = %w[.bundle /public/packs /public/packs-test vendor]
  path = "#{@project_root}/.jetskeep"
  return always unless File.exist?(path)

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

#removalsObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jets/builders/tidy.rb', line 34

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

#remove_gem_cacheObject

Reason do not remove the cache folder generally is because some gems have actual cache folders that they used.



84
85
86
87
# File 'lib/jets/builders/tidy.rb', line 84

def remove_gem_cache
  cache_path = "#{@project_root}/vendor/gems/ruby/#{Jets.ruby_folder}/cache"
  FileUtils.rm_rf(cache_path)
end

#rm_rf(path) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/jets/builders/tidy.rb', line 89

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

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

#say(message) ⇒ Object



102
103
104
105
# File 'lib/jets/builders/tidy.rb', line 102

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