Sha256: 989b46cd4f46aabb437f598774472b293f9b70bb9619f6bda992644cdd16bdb5

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Sinatra
  module AssetPack
    module Builder
      def build!(&blk)
        build_packages!
        build_files!
      end

      def build_packages!(&blk)
        packages.each { |_, pack| build_package!(pack, &blk) }
      end

      def build_files!(&blk)
        files.each { |path, local| build_file!(path, local, &blk) }
      end

      private

      def build_get(path, &blk)
        @session ||= Rack::Test::Session.new app
        @session.get(path)
      end

      def build_package!(pack, &blk)
        response = build_get(pack.path)
        build_write(pack.path, response, &blk)
        build_write(pack.production_path, response, &blk)
      end

      def build_file!(path, local, &blk)
        response = build_get(path)
        build_write(path, response, &blk)
        build_write(BusterHelpers.add_cache_buster(path, local), response, &blk)
      end

      def build_write(path, response, &blk)
        require 'fileutils'

        mtime = Time.parse(response.headers['Last-Modified']) if response.headers['Last-Modified']
        path = File.join(@output_path, path)

        yield path if block_given?

        FileUtils.mkdir_p(File.dirname(path))
        File.open(path, 'wb') { |f| f.write(response.body) }
        File.utime(mtime, mtime, path) if mtime
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-assetpack-0.3.5 lib/sinatra/assetpack/builder.rb