Class: Machined::StaticCompiler
- Inherits:
-
Object
- Object
- Machined::StaticCompiler
- Defined in:
- lib/machined/static_compiler.rb
Instance Attribute Summary collapse
-
#machined ⇒ Object
readonly
A reference to the Machined environment which created this instance.
Instance Method Summary collapse
-
#compile ⇒ Object
Loop through and compile each available asset to the appropriate output path.
-
#compile?(url) ⇒ Boolean
Determines if we should precompile the asset with the given url.
-
#initialize(machined) ⇒ StaticCompiler
constructor
Creates a new instance, which will compile the assets to the given
output_path
. -
#write_asset(environment, asset) ⇒ Object
Writes the asset to its destination, also writing a gzipped version if necessary.
Constructor Details
#initialize(machined) ⇒ StaticCompiler
Creates a new instance, which will compile the assets to the given output_path
.
11 12 13 |
# File 'lib/machined/static_compiler.rb', line 11 def initialize(machined) @machined = machined end |
Instance Attribute Details
#machined ⇒ Object (readonly)
A reference to the Machined environment which created this instance.
7 8 9 |
# File 'lib/machined/static_compiler.rb', line 7 def machined @machined end |
Instance Method Details
#compile ⇒ Object
Loop through and compile each available asset to the appropriate output path.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/machined/static_compiler.rb', line 17 def compile compiled_assets = {} machined.sprockets.each do |sprocket| next unless sprocket.compile? sprocket.each_logical_path do |logical_path| url = File.join(sprocket.config.url, logical_path) next unless compiled_assets[url].nil? && compile?(url) if asset = sprocket.find_asset(logical_path) compiled_assets[url] = write_asset(sprocket, asset) end end end compiled_assets end |
#compile?(url) ⇒ Boolean
Determines if we should precompile the asset with the given url. By default, we skip over any files that begin with ‘_’, like partials.
36 37 38 |
# File 'lib/machined/static_compiler.rb', line 36 def compile?(url) File.basename(url) !~ /^_/ end |
#write_asset(environment, asset) ⇒ Object
Writes the asset to its destination, also writing a gzipped version if necessary.
42 43 44 45 46 47 48 |
# File 'lib/machined/static_compiler.rb', line 42 def write_asset(environment, asset) filename = path_for(environment, asset) FileUtils.mkdir_p File.dirname(filename) asset.write_to filename asset.write_to "#{filename}.gz" if gzip?(filename) asset.digest end |