Class: Condenser::Manifest
- Inherits:
-
Object
- Object
- Condenser::Manifest
- Defined in:
- lib/condenser/manifest.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #add(*args) ⇒ Object
- #add_asset(asset) ⇒ Object
-
#clean(age = 2419200) ⇒ Object
Cleanup old assets in the compile directory.
- #clean_dir(dir, assets, age) ⇒ Object
-
#clobber ⇒ Object
Wipe directive.
- #compile(*args) ⇒ Object
- #export(*args) ⇒ Object
-
#initialize(*args) ⇒ Manifest
constructor
A new instance of Manifest.
- #reset ⇒ Object
-
#save ⇒ Object
Persist manfiest back to FS.
Constructor Details
#initialize(*args) ⇒ Manifest
Returns a new instance of Manifest.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/condenser/manifest.rb', line 8 def initialize(*args) args.compact! if args.first.is_a?(Condenser) @environment = args.shift @logger = @environment.logger else @environment = nil @logger = Logger.new($stdout, level: :info) end @dir, @filename = args[0], args[1] # Expand paths @dir = File.(@dir) if @dir @filename = File.(@filename) if @filename # If filename is given as the second arg if @dir && File.extname(@dir) != "" @dir, @filename = nil, @dir end # Default dir to the directory of the filename @dir ||= File.dirname(@filename) if @filename # If directory is given w/o filename, pick a random manifest location if @dir && @filename.nil? @filename = File.join(@dir, 'manifest.json') end unless @dir && @filename raise ArgumentError, "manifest requires output filename" end if File.exist?(@filename) begin @data = JSON.parse(File.read(@filename)) rescue JSON::ParserError => e @data = {} logger.error "#{@filename} is invalid: #{e.class} #{e.}" end else @data = {} end end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
6 7 8 |
# File 'lib/condenser/manifest.rb', line 6 def dir @dir end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
6 7 8 |
# File 'lib/condenser/manifest.rb', line 6 def environment @environment end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/condenser/manifest.rb', line 6 def filename @filename end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/condenser/manifest.rb', line 6 def logger @logger end |
Instance Method Details
#[](key) ⇒ Object
82 83 84 85 |
# File 'lib/condenser/manifest.rb', line 82 def [](key) add(key) if @environment @data[key] end |
#add(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/condenser/manifest.rb', line 58 def add(*args) if @environment.nil? raise Error, "manifest requires environment for compilation" end outputs = [] args.each do |arg| @environment.resolve(arg).each do |asset| outputs += add_asset(asset) end end end |
#add_asset(asset) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/condenser/manifest.rb', line 71 def add_asset(asset) export = asset.export @data[asset.filename] = export.to_json outputs = export.write(@dir) asset.linked_assets.each do |la| outputs += add_asset(la) end outputs end |
#clean(age = 2419200) ⇒ Object
Cleanup old assets in the compile directory. By default it will keep the latest version and remove any other files over 4 weeks old.
108 109 110 |
# File 'lib/condenser/manifest.rb', line 108 def clean(age = 2419200) clean_dir(@dir, @data.values.map{ |v| v['path'] }, Time.now - age) end |
#clean_dir(dir, assets, age) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/condenser/manifest.rb', line 112 def clean_dir(dir, assets, age) Dir.each_child(dir) do |child| child = File.join(dir, child) next if assets.find { |x| child.start_with?(x) } if File.directory?(child) clean_dir(dir, assets, age) elsif File.file?(child) && File.stat(child).mtime < age File.delete(child) end end end |
#clobber ⇒ Object
Wipe directive
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/condenser/manifest.rb', line 126 def clobber return if !Dir.exist?(dir) FileUtils.rm(filename) logger.info "Removed #{filename}" Dir.each_child(dir) do |child| FileUtils.rm_r(File.join(dir, child)) end logger.info "Removed contents of #{dir}" nil end |
#compile(*args) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/condenser/manifest.rb', line 87 def compile(*args) reset outputs = add(*args.flatten) save outputs end |
#export(*args) ⇒ Object
101 102 103 104 |
# File 'lib/condenser/manifest.rb', line 101 def export(*args) add(*args) save end |
#reset ⇒ Object
54 55 56 |
# File 'lib/condenser/manifest.rb', line 54 def reset @data = {} end |
#save ⇒ Object
Persist manfiest back to FS
95 96 97 98 99 |
# File 'lib/condenser/manifest.rb', line 95 def save return if @filename.nil? FileUtils.mkdir_p File.dirname(@filename) Utils.atomic_write(@filename) { |f| f.write(JSON.generate(@data)) } end |