Class: Sprockets::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/manifest.rb

Overview

The Manifest logs the contents of assets compiled to a single directory. It records basic attributes about the asset for fast lookup without having to compile. A pointer from each logical path indicates which fingerprinted asset is the current one.

The JSON is part of the public API and should be considered stable. This should make it easy to read from other programming languages and processes that don't have sprockets loaded. See `#assets` and `#files` for more infomation about the structure.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Manifest

Create new Manifest associated with an `environment`. `filename` is a full path to the manifest json file. The file may or may not already exist. The dirname of the `filename` will be used to write compiled assets to. Otherwise, if the path is a directory, the filename will default a random “manifest-123.json” file in that directory.

Manifest.new(environment, "./public/assets/manifest.json")


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sprockets/manifest.rb', line 26

def initialize(*args)
  if args.first.is_a?(Base) || args.first.nil?
    @environment = args.shift
  end

  @directory, @filename = args[0], args[1]

  # Expand paths
  @directory = File.expand_path(@directory) if @directory
  @filename  = File.expand_path(@filename) if @filename

  # If filename is given as the second arg
  if @directory && File.extname(@directory) != ""
    @directory, @filename = nil, @directory
  end

  # Default dir to the directory of the filename
  @directory ||= File.dirname(@filename) if @filename

  # If directory is given w/o filename, pick a random manifest.json location
  if @directory && @filename.nil?
    # Find the first manifest.json in the directory
    filenames = Dir[File.join(@directory, "manifest*.json")]
    if filenames.any?
      @filename = filenames.first
    else
      @filename = File.join(@directory, "manifest-#{SecureRandom.hex(16)}.json")
    end
  end

  unless @directory && @filename
    raise ArgumentError, "manifest requires output filename"
  end

  data = {}

  begin
    if File.exist?(@filename)
      data = json_decode(File.read(@filename))
    end
  rescue JSON::ParserError => e
    logger.error "#{@filename} is invalid: #{e.class} #{e.message}"
  end

  @data = data
end

Instance Attribute Details

#directoryObject (readonly) Also known as: dir

Returns the value of attribute directory



77
78
79
# File 'lib/sprockets/manifest.rb', line 77

def directory
  @directory
end

#environmentObject (readonly)

Returns the value of attribute environment



16
17
18
# File 'lib/sprockets/manifest.rb', line 16

def environment
  @environment
end

#filenameObject (readonly) Also known as: path

Returns String path to manifest.json file.



74
75
76
# File 'lib/sprockets/manifest.rb', line 74

def filename
  @filename
end

Class Method Details

.compile_match_filter(filter) ⇒ Object

Deprecated: Compile logical path matching filter into a proc that can be passed to logical_paths.select(&proc).

compile_match_filter(proc { |logical_path|
  File.extname(logical_path) == '.js'
})

compile_match_filter(/application.js/)

compile_match_filter("foo/*.js")

Returns a Proc or raise a TypeError.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sprockets/manifest.rb', line 121

def self.compile_match_filter(filter)
  # If the filter is already a proc, great nothing to do.
  if filter.respond_to?(:call)
    filter
  # If the filter is a regexp, wrap it in a proc that tests it against the
  # logical path.
  elsif filter.is_a?(Regexp)
    proc { |logical_path| filter.match(logical_path) }
  elsif filter.is_a?(String)
    # If its an absolute path, detect the matching full filename
    if PathUtils.absolute_path?(filter)
      proc { |logical_path, filename| filename == filter.to_s }
    else
      # Otherwise do an fnmatch against the logical path.
      proc { |logical_path| File.fnmatch(filter.to_s, logical_path) }
    end
  else
    raise TypeError, "unknown filter type: #{filter.inspect}"
  end
end

Instance Method Details

#assetsObject

Returns internal assets mapping. Keys are logical paths which map to the latest fingerprinted filename.

Logical path (String): Fingerprint path (String)

{ "application.js" => "application-2e8e9a7c6b0aafa0c9bdeec90ea30213.js",
  "jquery.js"      => "jquery-ae0908555a245f8266f77df5a8edca2e.js" }


88
89
90
# File 'lib/sprockets/manifest.rb', line 88

def assets
  @data['assets'] ||= {}
end

#clean(count = 2, age = 3600) ⇒ Object

Cleanup old assets in the compile directory. By default it will keep the latest version, 2 backups and any created within the past hour.

Examples

To force only 1 backup to be kept, set count=1 and age=0.

To only keep files created within the last 10 minutes, set count=0 and
age=600.


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/sprockets/manifest.rb', line 252

def clean(count = 2, age = 3600)
  asset_versions = files.group_by { |_, attrs| attrs['logical_path'] }

  asset_versions.each do |logical_path, versions|
    current = assets[logical_path]

    versions.reject { |path, _|
      path == current
    }.sort_by { |_, attrs|
      # Sort by timestamp
      Time.parse(attrs['mtime'])
    }.reverse.each_with_index.drop_while { |(_, attrs), index|
      age = [0, Time.now - Time.parse(attrs['mtime'])].max
      # Keep if under age or within the count limit
      age < age || index < count
    }.each { |(path, _), _|
       # Remove old assets
      remove(path)
    }
  end
end

#clobberObject

Wipe directive



275
276
277
278
279
# File 'lib/sprockets/manifest.rb', line 275

def clobber
  FileUtils.rm_r(directory) if File.exist?(directory)
  logger.info "Removed #{directory}"
  nil
end

#compile(*args) ⇒ Object

Compile and write asset to directory. The asset is written to a fingerprinted filename like `application-2e8e9a7c6b0aafa0c9bdeec90ea30213.js`. An entry is also inserted into the manifest file.

compile("application.js")


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sprockets/manifest.rb', line 186

def compile(*args)
  unless environment
    raise Error, "manifest requires environment for compilation"
  end

  filenames = []

  find(*args) do |asset|
    files[asset.digest_path] = {
      'logical_path' => asset.logical_path,
      'mtime'        => asset.mtime.iso8601,
      'size'         => asset.bytesize,
      'digest'       => asset.hexdigest,
      'integrity'    => asset.integrity
    }
    assets[asset.logical_path] = asset.digest_path

    target = File.join(dir, asset.digest_path)

    if File.exist?(target)
      logger.debug "Skipping #{target}, already exists"
    else
      logger.info "Writing #{target}"
      asset.write_to target
    end

    filenames << asset.filename
  end
  save

  filenames
end

#filesObject

Returns internal file directory listing. Keys are filenames which map to an attributes array.

 Fingerprint path (String):
   logical_path: Logical path (String)
   mtime: ISO8601 mtime (String)
   digest: Base64 hex digest (String)

{ "application-2e8e9a7c6b0aafa0c9bdeec90ea30213.js" =>
    { 'logical_path' => "application.js",
      'mtime' => "2011-12-13T21:47:08-06:00",
      'digest' => "2e8e9a7c6b0aafa0c9bdeec90ea30213" } }


105
106
107
# File 'lib/sprockets/manifest.rb', line 105

def files
  @data['files'] ||= {}
end

#filter_logical_paths(*args) ⇒ Object Also known as: find_logical_paths

Deprecated: Filter logical paths in environment. Useful for selecting what files you want to compile.

Returns an Enumerator.



146
147
148
149
150
151
# File 'lib/sprockets/manifest.rb', line 146

def filter_logical_paths(*args)
  filters = args.flatten.map { |arg| self.class.compile_match_filter(arg) }
  environment.logical_paths.select do |a, b|
    filters.any? { |f| f.call(a, b) }
  end
end

#find(*args) ⇒ Object

Public: Find all assets matching pattern set in environment.

Returns Enumerator of Assets.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/sprockets/manifest.rb', line 156

def find(*args)
  unless environment
    raise Error, "manifest requires environment for compilation"
  end

  return to_enum(__method__, *args) unless block_given?

  filters = args.flatten.map { |arg| self.class.compile_match_filter(arg) }

  environment.logical_paths do |logical_path, filename|
    if filters.any? { |f| f.call(logical_path, filename) }
      environment.find_all_linked_assets(filename) do |asset|
        yield asset
      end
    end
  end

  nil
end

#remove(filename) ⇒ Object

Removes file from directory and from manifest. `filename` must be the name with any directory path.

manifest.remove("application-2e8e9a7c6b0aafa0c9bdeec90ea30213.js")


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sprockets/manifest.rb', line 224

def remove(filename)
  path = File.join(dir, filename)
  logical_path = files[filename]['logical_path']

  if assets[logical_path] == filename
    assets.delete(logical_path)
  end

  files.delete(filename)
  FileUtils.rm(path) if File.exist?(path)

  save

  logger.info "Removed #{filename}"

  nil
end