Skip to content

Commit

Permalink
Use a separate manifest file for source digests, and renamed digest_f…
Browse files Browse the repository at this point in the history
…iles back to digests
  • Loading branch information
ndbroadbent committed Oct 25, 2012
1 parent 902171c commit 029faf9
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 103 deletions.
10 changes: 2 additions & 8 deletions README.md
Expand Up @@ -32,7 +32,7 @@ Warning: Don't add this gem to the `:assets` group. It must be available in the

Run `bundle` to install the gem, and you're done!

Test it out by running `rake assets:precompile`. When it's finished, your `public/assets/manifest.yml` file should include a `:source_digests` hash for your assets.
Test it out by running `rake assets:precompile`. When it's finished, you should see a new file at `public/assets/sources_manifest.yml`, which includes the source fingerprints for your assets.

Go on, run `rake assets:precompile` again, and it should be a whole lot faster than before.

Expand All @@ -42,13 +42,7 @@ Enjoy your lightning fast deploys!

### [asset_sync](https://github.com/rumblelabs/asset_sync)

Fully compatible. Just don't use the experimental `AssetSync.config.manifest = true` configuration option until
[my asset_sync patch](https://github.com/rumblelabs/asset_sync/pull/110) has been merged. Alternatively, you can use my patched `asset_sync` with the following line in your `Gemfile`:

```ruby
gem "asset_sync", :github => "ndbroadbent/asset_sync", :branch => "new_manifest_support"
```

Fully compatible.

### [wicked_pdf](https://github.com/mileszs/wicked_pdf)

Expand Down
8 changes: 4 additions & 4 deletions lib/sprockets/static_non_digest_generator.rb
Expand Up @@ -11,10 +11,10 @@ def initialize(env, target, paths, options = {})
@env = env
@target = target
@paths = paths
@digest_files = options.fetch(:digest_files, {})
@digests = options.fetch(:digests, {})

# Parse digests from digest_files hash
@asset_digests = Hash[*@digest_files.map {|file, digest_file|
# Parse digests from digests hash
@asset_digests = Hash[*@digests.map {|file, digest_file|
[file, digest_file[DIGEST_REGEX, 1]]
}.flatten]
end
Expand All @@ -32,7 +32,7 @@ def generate
end
next unless compile_path?(logical_path)

if digest_path = @digest_files[logical_path]
if digest_path = @digests[logical_path]
abs_digest_path = "#{@target}/#{digest_path}"
abs_logical_path = "#{@target}/#{logical_path}"

Expand Down
3 changes: 1 addition & 2 deletions lib/turbo-sprockets-rails3.rb
@@ -1,13 +1,12 @@
require 'sprockets/railtie'
require 'sprockets/helpers'

module Sprockets
# Assets
autoload :UnprocessedAsset, "sprockets/unprocessed_asset"
autoload :AssetWithDependencies, "sprockets/asset_with_dependencies"
end

Dir[File.expand_path('../turbo-sprockets/sprockets_overrides/**/*.rb', __FILE__)].each do |f|
Dir[File.expand_path('../turbo-sprockets/sprockets_overrides/*.rb', __FILE__)].each do |f|
require f
end

Expand Down
19 changes: 6 additions & 13 deletions lib/turbo-sprockets/railtie.rb
Expand Up @@ -13,18 +13,11 @@ class Railtie < ::Rails::Railtie
initializer "turbo-sprockets.environment", :after => "sprockets.environment", :group => :all do |app|
config = app.config

if config.assets.manifest
manifest_path = File.join(config.assets.manifest, "manifest.yml")
else
manifest_path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml")
end

if File.exist?(manifest_path)
manifest = YAML.load_file(manifest_path)
# Set both digest keys for backwards compatibility
config.assets.digests = config.assets.digest_files = manifest[:digest_files] || {}
config.assets.source_digests = manifest[:source_digests] || {}
end
manifest_dir = config.assets.manifest || File.join(Rails.public_path, config.assets.prefix)
digests_manifest = File.join(manifest_dir, "manifest.yml")
sources_manifest = File.join(manifest_dir, "sources_manifest.yml")
config.assets.digests = (File.exist?(digests_manifest) && YAML.load_file(digests_manifest)) || {}
config.assets.source_digests = (File.exist?(sources_manifest) && YAML.load_file(sources_manifest)) || {}
end
end
end
end
42 changes: 0 additions & 42 deletions lib/turbo-sprockets/sprockets_overrides/helpers/rails_helper.rb

This file was deleted.

28 changes: 19 additions & 9 deletions lib/turbo-sprockets/sprockets_overrides/static_compiler.rb
Expand Up @@ -17,9 +17,9 @@ def initialize(env, target, paths, options = {})
@zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/

@current_source_digests = options.fetch(:source_digests, {})
@current_digest_files = options.fetch(:digest_files, {})
@current_digests = options.fetch(:digests, {})

@digest_files = {}
@digests = {}
@source_digests = {}
end

Expand All @@ -38,19 +38,19 @@ def compile
@source_digests[logical_path] = asset.digest

# Recompile if digest has changed or compiled digest file is missing
current_digest_file = @current_digest_files[logical_path]
current_digest_file = @current_digests[logical_path]

if @source_digests[logical_path] != @current_source_digests[logical_path] ||
!(current_digest_file && File.exists?("#{@target}/#{current_digest_file}"))

if asset = env.find_asset(logical_path)
@digest_files[logical_path] = write_asset(asset)
@digests[logical_path] = write_asset(asset)
end

else
# Set asset file from manifest.yml
digest_file = @current_digest_files[logical_path]
@digest_files[logical_path] = digest_file
digest_file = @current_digests[logical_path]
@digests[logical_path] = digest_file

env.logger.debug "Not compiling #{logical_path}, sources digest has not changed " <<
"(#{@source_digests[logical_path][0...7]})"
Expand All @@ -62,22 +62,32 @@ def compile
# otherwise YAML dumps other string encodings as !binary
if RUBY_VERSION.to_f >= 1.9
@source_digests = encode_hash_as_utf8 @source_digests
@digest_files = encode_hash_as_utf8 @digest_files
@digests = encode_hash_as_utf8 @digests
end

if @manifest
write_manifest(:source_digests => @source_digests, :digest_files => @digest_files)
write_manifest(@digests, @source_digests)
end

# Store digests in Rails config. (Important if non-digest is run after primary)
config = ::Rails.application.config
config.assets.digest_files = @digest_files
config.assets.digests = @digests
config.assets.source_digests = @source_digests

elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
env.logger.debug "Processed #{'non-' unless @digest}digest assets in #{elapsed_time}ms"
end

def write_manifest(digests, source_digests)
FileUtils.mkdir_p(@manifest_path)
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
YAML.dump(digests, f)
end
File.open("#{@manifest_path}/sources_manifest.yml", 'wb') do |f|
YAML.dump(source_digests, f)
end
end

private

def encode_hash_as_utf8(hash)
Expand Down
10 changes: 5 additions & 5 deletions lib/turbo-sprockets/tasks/assets.rake
Expand Up @@ -54,8 +54,8 @@ namespace :assets do
config = Rails.application.config
config.assets.compile = true
config.assets.clean_after_precompile = false if config.assets.clean_after_precompile.nil?
config.assets.digest = digest unless digest.nil?
config.assets.digest_files ||= {}
config.assets.digest = digest unless digest.nil?
config.assets.digests ||= {}
config.assets.source_digests ||= {}

env = Rails.application.assets
Expand All @@ -65,16 +65,16 @@ namespace :assets do
# present, then generate non-digest assets from existing assets.
# It is assumed that `assets:precompile:nondigest` won't be run manually
# if assets have been previously compiled with digests.
if !config.assets.digest && config.assets.digest_files.any?
if !config.assets.digest && config.assets.digests.any?
generator = Sprockets::StaticNonDigestGenerator.new(env, target, config.assets.precompile,
:digest_files => config.assets.digest_files)
:digests => config.assets.digests)
generator.generate
else
compiler = Sprockets::StaticCompiler.new(env, target, config.assets.precompile,
:digest => config.assets.digest,
:manifest => digest.nil?,
:manifest_path => config.assets.manifest,
:digest_files => config.assets.digest_files,
:digests => config.assets.digests,
:source_digests => config.assets.source_digests
)
compiler.compile
Expand Down
2 changes: 1 addition & 1 deletion lib/turbo-sprockets/version.rb
@@ -1,3 +1,3 @@
module TurboSprockets
VERSION = "0.1.17"
VERSION = "0.2.0"
end
32 changes: 16 additions & 16 deletions test/assets_test.rb
Expand Up @@ -153,14 +153,14 @@ def assert_no_file_exists(filename)
precompile!
manifest = "#{app_path}/public/assets/manifest.yml"

digests = YAML.load_file(manifest)
digest_files, source_digests = digests[:digest_files], digests[:source_digests]
digests = YAML.load_file("#{app_path}/public/assets/manifest.yml")
sources = YAML.load_file("#{app_path}/public/assets/sources_manifest.yml")

assert_match(/application-([0-z]+)\.js/, digest_files["application.js"])
assert_match(/application-([0-z]+)\.css/, digest_files["application.css"])
assert_match(/application-([0-z]+)\.js/, digests["application.js"])
assert_match(/application-([0-z]+)\.css/, digests["application.css"])

assert_match(/[0-z]+/, source_digests["application.js"])
assert_match(/[0-z]+/, source_digests["application.css"])
assert_match(/[0-z]+/, sources["application.js"])
assert_match(/[0-z]+/, sources["application.css"])
end

test "the manifest file should be saved by default in the same assets folder" do
Expand All @@ -172,8 +172,8 @@ def assert_no_file_exists(filename)
precompile!

manifest = "#{app_path}/public/x/manifest.yml"
digest_files = YAML.load_file(manifest)[:digest_files]
assert_match(/application-([0-z]+)\.js/, digest_files["application.js"])
digests = YAML.load_file(manifest)
assert_match(/application-([0-z]+)\.js/, digests["application.js"])
end

test "precompile does not append asset digests when config.assets.digest is false" do
Expand All @@ -188,9 +188,9 @@ def assert_no_file_exists(filename)

manifest = "#{app_path}/public/assets/manifest.yml"

digest_files = YAML.load_file(manifest)[:digest_files]
assert_equal "application.js", digest_files["application.js"]
assert_equal "application.css", digest_files["application.css"]
digests = YAML.load_file(manifest)
assert_equal "application.js", digests["application.js"]
assert_equal "application.css", digests["application.css"]
end

test "assets do not require any assets group gem when manifest file is present" do
Expand All @@ -201,8 +201,8 @@ def assert_no_file_exists(filename)
precompile!

manifest = "#{app_path}/public/assets/manifest.yml"
digest_files = YAML.load_file(manifest)[:digest_files]
asset_path = digest_files["application.js"]
digests = YAML.load_file(manifest)
asset_path = digests["application.js"]

require "#{app_path}/config/environment"

Expand Down Expand Up @@ -240,7 +240,7 @@ def show_detailed_exceptions?() true end
test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled if digest is disabled" do
app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
add_to_config "config.assets.compile = false"
add_to_config "config.assets.digest_files = false"
add_to_config "config.assets.digests = false"

app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
Expand Down Expand Up @@ -289,9 +289,9 @@ def show_detailed_exceptions?() true end
app_file "app/assets/images/rails.png", "image changed"

precompile!
digest_files = YAML.load_file(manifest)[:digest_files]
digests = YAML.load_file(manifest)

assert_not_equal asset_path, digest_files["application.css"]
assert_not_equal asset_path, digests["application.css"]
end

test "precompile appends the md5 hash to files referenced with asset_path and run in production as default even using RAILS_GROUPS=assets" do
Expand Down
3 changes: 1 addition & 2 deletions test/sprockets_helper_test.rb
@@ -1,7 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/sprockets_helpers_abstract_unit")
require 'sprockets'
require 'sprockets/helpers/rails_helper'
require 'turbo-sprockets/sprockets_overrides/helpers/rails_helper'
require 'mocha'

class SprocketsHelperTest < ActionView::TestCase
Expand Down Expand Up @@ -355,7 +354,7 @@ def compute_host(source, request, options = {})
end

test "precedence of `config.digest = false` over manifest.yml asset digests" do
Rails.application.config.assets.digest_files = {'logo.png' => 'logo-d1g3st.png'}
Rails.application.config.assets.digests = {'logo.png' => 'logo-d1g3st.png'}
@config.assets.digest = false

assert_equal '/assets/logo.png',
Expand Down
1 change: 0 additions & 1 deletion test/sprockets_helper_with_routes_test.rb
@@ -1,7 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/sprockets_helpers_abstract_unit")
require 'sprockets'
require 'sprockets/helpers/rails_helper'
require 'turbo-sprockets/sprockets_overrides/helpers/rails_helper'
require 'mocha'

class SprocketsHelperWithRoutesTest < ActionView::TestCase
Expand Down

0 comments on commit 029faf9

Please sign in to comment.