Sha256: a711aa8f879b1279b46da7089d642ba73b860f4bfc8b5bbc2886bcfea7b21a3a

Contents?: true

Size: 1.69 KB

Versions: 14

Compression:

Stored size: 1.69 KB

Contents

require "propshaft/output_path"

class Propshaft::Processor
  MANIFEST_FILENAME = ".manifest.json"

  attr_reader :load_path, :output_path, :compilers

  def initialize(load_path:, output_path:, compilers:)
    @load_path, @output_path = load_path, output_path
    @compilers = compilers
  end

  def process
    ensure_output_path_exists
    write_manifest
    output_assets
  end

  def clobber
    FileUtils.rm_r(output_path) if File.exist?(output_path)
  end

  def clean
    Propshaft::OutputPath.new(output_path, load_path.manifest).clean(2, 1.hour)
  end

  private
    def ensure_output_path_exists
      FileUtils.mkdir_p output_path
    end


    def write_manifest
      File.open(output_path.join(MANIFEST_FILENAME), "wb+") do |manifest|
        manifest.write load_path.manifest.to_json
      end
    end


    def output_assets
      load_path.assets.each do |asset|
        unless output_path.join(asset.digested_path).exist?
          Propshaft.logger.info "Writing #{asset.digested_path}"
          FileUtils.mkdir_p output_path.join(asset.digested_path.parent)
          output_asset(asset)
        end
      end
    end

    def output_asset(asset)
      compile_asset(asset) || copy_asset(asset)
    end

    def compile_asset(asset)
      File.open(output_path.join(asset.digested_path), "w+") do |file|
        begin
          file.write compilers.compile(asset)
        rescue Encoding::UndefinedConversionError
          # FIXME: Not sure if there's a better way here?
          file.write compilers.compile(asset).force_encoding("UTF-8")
        end
      end if compilers.compilable?(asset)
    end

    def copy_asset(asset)
      FileUtils.copy asset.path, output_path.join(asset.digested_path)
    end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/propshaft-0.6.4/lib/propshaft/processor.rb
propshaft-0.8.0 lib/propshaft/processor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/propshaft-0.6.4/lib/propshaft/processor.rb
propshaft-0.7.0 lib/propshaft/processor.rb
propshaft-0.6.4 lib/propshaft/processor.rb
propshaft-0.6.3 lib/propshaft/processor.rb
propshaft-0.6.2 lib/propshaft/processor.rb
propshaft-0.6.1 lib/propshaft/processor.rb
propshaft-0.6.0 lib/propshaft/processor.rb
propshaft-0.5.0 lib/propshaft/processor.rb
propshaft-0.4.4 lib/propshaft/processor.rb
propshaft-0.4.3 lib/propshaft/processor.rb
propshaft-0.4.2 lib/propshaft/processor.rb
propshaft-0.4.1 lib/propshaft/processor.rb