File: processor.rb

package info (click to toggle)
ruby-propshaft 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 782; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 1,800 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
require "propshaft/output_path"

class Propshaft::Processor
  attr_reader :load_path, :output_path, :compilers, :manifest_path

  def initialize(load_path:, output_path:, compilers:, manifest_path:)
    @load_path, @output_path = load_path, output_path
    @manifest_path = manifest_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(count)
    Propshaft::OutputPath.new(output_path, load_path.manifest).clean(count, 1.hour)
  end

  private
    def ensure_output_path_exists
      FileUtils.mkdir_p output_path
    end


    def write_manifest
      FileUtils.mkdir_p(File.dirname(manifest_path))
      File.open(manifest_path, "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 asset.compiled_content
        rescue Encoding::UndefinedConversionError
          # FIXME: Not sure if there's a better way here?
          file.write asset.compiled_content.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