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.
6 7 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 |
# File 'lib/condenser/manifest.rb', line 6 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.
4 5 6 |
# File 'lib/condenser/manifest.rb', line 4 def dir @dir end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
4 5 6 |
# File 'lib/condenser/manifest.rb', line 4 def environment @environment end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/condenser/manifest.rb', line 4 def filename @filename end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
4 5 6 |
# File 'lib/condenser/manifest.rb', line 4 def logger @logger end |
Instance Method Details
#[](key) ⇒ Object
80 81 82 83 |
# File 'lib/condenser/manifest.rb', line 80 def [](key) add(key) if @environment @data[key] end |
#add(*args) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/condenser/manifest.rb', line 56 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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/condenser/manifest.rb', line 69 def add_asset(asset) export = asset.export @data[asset.filename] = export.to_json outputs = export.write(@dir) asset.linked_assets.each do |la| @environment.resolve(la).each { |a| outputs += add_asset(a) } 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.
106 107 108 |
# File 'lib/condenser/manifest.rb', line 106 def clean(age = 2419200) clean_dir(@dir, @data.values.map{ |v| v['path'] }, Time.now - age) end |
#clean_dir(dir, assets, age) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/condenser/manifest.rb', line 110 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
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/condenser/manifest.rb', line 124 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
85 86 87 88 89 90 |
# File 'lib/condenser/manifest.rb', line 85 def compile(*args) reset outputs = add(*args.flatten) save outputs end |
#export(*args) ⇒ Object
99 100 101 102 |
# File 'lib/condenser/manifest.rb', line 99 def export(*args) add(*args) save end |
#reset ⇒ Object
52 53 54 |
# File 'lib/condenser/manifest.rb', line 52 def reset @data = {} end |
#save ⇒ Object
Persist manfiest back to FS
93 94 95 96 97 |
# File 'lib/condenser/manifest.rb', line 93 def save return if @filename.nil? FileUtils.mkdir_p File.dirname(@filename) Utils.atomic_write(@filename) { |f| f.write(JSON.generate(@data)) } end |