Sha256: e671b504736c73b733324fbe6f52ed64e84e0b4f7720f722ce57bcc6099af983

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Rail
  class Precompiler
    class Storage
      attr_reader :root

      def initialize(root = 'public')
        @root = root
      end

      def write(file, stream)
        file = File.join(root, file)

        unless File.exist?(File.dirname(file))
          FileUtils.mkdir_p(File.dirname(file))
        end

        File.open(file, 'w') do |file|
          stream.each { |chunk| file.write(chunk) }
        end
      end
    end

    def initialize(pipeline, storage = nil)
      @pipeline = pipeline
      @storage = storage || Storage.new
    end

    def process(paths)
      if paths.empty?
        report('Nothing to precompile.')
        return
      end

      report('Precompiling assets...')

      paths.each_with_index do |path, i|
        report('%4d. %s' % [i + 1, path])
        storage.write(path, read(path))
      end

      report('Done.')
    end

    private

    attr_reader :pipeline, :storage

    def read(path)
      request = Request.new('REQUEST_METHOD' => 'GET', 'PATH_INFO' => path)
      _, _, source = pipeline.process(request)
      source
    end

    def report(message)
      puts message
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rail-0.1.1 lib/rail/precompiler.rb
rail-0.1.0 lib/rail/precompiler.rb
rail-0.0.8 lib/rail/precompiler.rb