Class: Homeostasis::Asset
- Inherits:
-
Stasis::Plugin
- Object
- Stasis::Plugin
- Homeostasis::Asset
- Includes:
- Helpers
- Defined in:
- lib/homeostasis.rb
Class Method Summary collapse
- .concat(dest, files) ⇒ Object
- .config(options) ⇒ Object
- .stamped(path, version) ⇒ Object
- .version(paths) ⇒ Object
Instance Method Summary collapse
- #after_all ⇒ Object
- #before_all ⇒ Object
-
#initialize(stasis) ⇒ Asset
constructor
A new instance of Asset.
Constructor Details
#initialize(stasis) ⇒ Asset
Returns a new instance of Asset.
65 66 67 68 69 70 71 72 |
# File 'lib/homeostasis.rb', line 65 def initialize(stasis) @stasis = stasis @@matcher = /\.(jpg|png|gif|css|js)/i @@replace_matcher = /\.(html|css|js)/i @@mapping = {} @@concats = {} @@orig_concats = {} end |
Class Method Details
.concat(dest, files) ⇒ Object
170 171 172 |
# File 'lib/homeostasis.rb', line 170 def self.concat(dest, files) @@concats[dest] = files end |
.config(options) ⇒ Object
165 166 167 168 |
# File 'lib/homeostasis.rb', line 165 def self.config() @@matcher = [:matcher] if [:matcher] @@replace_matcher = [:replace_matcher] if [:replace_matcher] end |
.stamped(path, version) ⇒ Object
148 149 150 151 152 |
# File 'lib/homeostasis.rb', line 148 def self.stamped(path, version) path = path.split('.') path.insert(path.length > 1 ? -2 : -1, version) path.join('.') end |
.version(paths) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/homeostasis.rb', line 154 def self.version(paths) paths = [paths] if !paths.is_a?(Array) versions = paths.map do |path| log = `git log -n1 #{path} 2> /dev/null`.split("\n") log.length > 1 ? log[0].split(' ').last : '0' end versions.size == 1 ? versions[0] : Digest::SHA1.hexdigest(versions.join('.')) end |
Instance Method Details
#after_all ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/homeostasis.rb', line 96 def after_all assets = {} strip_dest = (@stasis.destination.length + 1)..-1 # concatenate files with stamps @@orig_concats.each do |concatted, files| full_concatted = self.class.stamped( File.join(@stasis.destination, concatted), self.class.version(files)) content = files.map do |full_orig| orig = full_orig[(@stasis.root.length+1)..-1] full_dest = File.join(@stasis.destination, @@mapping[orig]) raise "File not found #{full_dest}" if !File.exists?(full_dest) contents = Helpers.read(full_dest) @@mapping.delete(orig) File.delete(full_dest) contents end.join("\n") File.open(full_concatted, 'w') { |f| f.print(content) } assets[concatted] = full_concatted[strip_dest] end # stamp files that don't require concatentation @@mapping.each do |orig, dest| next if dest !~ @@matcher full_orig = File.join(@stasis.root, orig) full_dest = File.join(@stasis.destination, dest) versioned = self.class.stamped(full_dest, self.class.version(full_orig)) File.rename(full_dest, versioned) assets[full_dest[strip_dest]] = versioned[strip_dest] end # read contents of each file, search/replace assets with stamps front_site = Homeostasis::Front._front_site inverted = @@mapping.invert Dir.glob("#{@stasis.destination}/**/*").each do |file| next if file !~ @@replace_matcher || File.directory?(file) contents = Helpers.read(file) front = front_site[inverted[file.sub("#{@stasis.destination}/", "")]] assets.each do |old, new| old = Regexp.escape(old) contents.gsub!(/([^a-zA-Z0-9\.\-_])#{old}/, "\\1#{new}") contents.gsub!(/^#{old}/, new) if front && front[:body] # for RSS feed front[:body].gsub!(/([^a-zA-Z0-9\.\-_])#{old}/, "\\1#{new}") front[:body].gsub!(/^#{old}/, new) end end File.open(file, 'w') { |f| f.print(contents) } end end |
#before_all ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/homeostasis.rb', line 74 def before_all # build mapping of relative filenames => destination @stasis.paths.each do |path| next if ignore?(path) relative = path[(@stasis.root.length+1)..-1] ext = Tilt.mappings.keys.find { |ext| File.extname(path)[1..-1] == ext } dest = (ext && File.extname(relative) == ".#{ext}") ? relative[0..-1*ext.length-2] : relative @@mapping[relative] = dest end # build orig_concats of destination => [original filenames,] inverted = @@mapping.invert @@concats.each do |dest, files| @@orig_concats[dest] = files.map do |file| raise "Asset not found #{file}" if !inverted.has_key?(file) File.join(@stasis.root, inverted[file]) end end end |