class Marv::Project::Assets

Public Class Methods

new(builder) click to toggle source

Initialize assets builder

# File lib/marv/project/builder/assets.rb, line 9
def initialize(builder)
  @builder = builder
  @task = builder.task
  @project = builder.project
  @config = builder.project.config

  init_sprockets
end

Public Instance Methods

build_asset_file(asset) click to toggle source

Build asset file

# File lib/marv/project/builder/assets.rb, line 59
def build_asset_file(asset)
  destination = ::File.join(@project.build_path, asset)

  @task.shell.mute do
    sprocket = @sprockets.find_asset(asset.last)
    # Create asset destination
    @task.empty_directory ::File.dirname(destination) unless ::File.directory?(::File.dirname(destination))
    # Write file to destination
    sprocket.write_to(destination) unless sprocket.nil?
  end
end
build_assets() click to toggle source

Build assets

# File lib/marv/project/builder/assets.rb, line 47
def build_assets
  @project.assets.each do |asset|
    # Catch any sprockets errors and continue the process
    begin
      build_asset_file asset
    rescue Exception => e
      print_asset_error asset, e.message
    end
  end
end
clean_images() click to toggle source

Clean images

# File lib/marv/project/builder/assets.rb, line 19
def clean_images
  @task.shell.mute do
    # Remove screenshot image
    ::Dir.glob(::File.join(@project.build_path, 'screenshot.*')).each do |file|
      @task.remove_file file
    end

    # Remove images folder
    @task.remove_dir ::File.join(@project.build_path, 'images')
  end
end
copy_images() click to toggle source

Copy images

# File lib/marv/project/builder/assets.rb, line 32
def copy_images
  @task.shell.mute do
    ::Dir.glob(::File.join(@project.assets_path, 'images', '*')).each do |filename|
      # Check for screenshot and move it into main build directory
      if filename.index(/screenshot/)
        @task.copy_file filename, ::File.join(@project.build_path, ::File.basename(filename)), :force => true
      else
        # Copy the other files in images directory
        @task.copy_file filename, ::File.join(@project.build_path, 'images', ::File.basename(filename)), :force => true
      end
    end
  end
end
init_sprockets() click to toggle source

Init sprockets

# File lib/marv/project/builder/assets.rb, line 84
def init_sprockets
  @sprockets   = ::Sprockets::Environment.new
  autoprefixer = @config[:autoprefixer]

  ['javascripts', 'stylesheets'].each do |dir|
    @sprockets.append_path ::File.join(@project.assets_path, dir)
  end

  # Check for js compression
  if @config[:compress_js]
    @sprockets.js_compressor = :uglify
  end

  # Check for css compression
  if @config[:compress_css]
    @sprockets.css_compressor = :scss
  end

  # Passing the @project instance variable to the Sprockets::Context instance
  # used for processing the asset ERB files
  @sprockets.context_class.instance_exec(@project) do |project|
    define_method :config do
      project.config
    end
  end

  unless autoprefixer == false
    AutoprefixerRails.install(@sprockets, Hash(autoprefixer))
  end
end
print_asset_error(asset, message) click to toggle source

Print error to screen and file