Class: Homeostasis::Asset

Inherits:
Stasis::Plugin
  • Object
show all
Includes:
Helpers
Defined in:
lib/homeostasis.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stasis) ⇒ Asset

Returns a new instance of Asset.



70
71
72
73
74
75
76
77
# File 'lib/homeostasis.rb', line 70

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



175
176
177
# File 'lib/homeostasis.rb', line 175

def self.concat(dest, files)
  @@concats[dest] = files
end

.config(options) ⇒ Object



170
171
172
173
# File 'lib/homeostasis.rb', line 170

def self.config(options)
  @@matcher = options[:matcher] if options[:matcher]
  @@replace_matcher = options[:replace_matcher] if options[:replace_matcher]
end

.stamped(path, version) ⇒ Object



153
154
155
156
157
# File 'lib/homeostasis.rb', line 153

def self.stamped(path, version)
  path = path.split('.')
  path.insert(path.length > 1 ? -2 : -1, version)
  path.join('.')
end

.version(paths) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/homeostasis.rb', line 159

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_allObject



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
147
148
149
150
151
# File 'lib/homeostasis.rb', line 101

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_allObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/homeostasis.rb', line 79

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