Class: Machined::StaticCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/machined/static_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#machinedObject (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

#compileObject

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.

Returns:

  • (Boolean)


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