Skip to content
Browse files

Use a separate manifest file for source digests, and renamed digest_f…

…iles back to digests
  • Loading branch information...
1 parent 902171c commit 029faf9e16a5fb105d14a65800b5c040c89656e2 @ndbroadbent committed
View
10 README.md
@@ -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.
@@ -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)
View
8 lib/sprockets/static_non_digest_generator.rb
@@ -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
@@ -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}"
View
3 lib/turbo-sprockets-rails3.rb
@@ -1,5 +1,4 @@
require 'sprockets/railtie'
-require 'sprockets/helpers'
module Sprockets
# Assets
@@ -7,7 +6,7 @@ module Sprockets
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
View
19 lib/turbo-sprockets/railtie.rb
@@ -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
View
42 lib/turbo-sprockets/sprockets_overrides/helpers/rails_helper.rb
@@ -1,42 +0,0 @@
-require 'sprockets/helpers/rails_helper'
-
-module Sprockets
- module Helpers
- module RailsHelper
- def asset_paths
- @asset_paths ||= begin
- paths = RailsHelper::AssetPaths.new(config, controller)
- paths.asset_environment = asset_environment
- paths.digest_files = digest_files
- paths.compile_assets = compile_assets?
- paths.digest_assets = digest_assets?
- paths
- end
- end
-
- AssetPaths.class_eval do
- attr_accessor :digest_files
-
- def digest_for(logical_path)
- if digest_assets && digest_files && (digest = digest_files[logical_path])
- return digest
- end
-
- if compile_assets
- if digest_assets && asset = asset_environment[logical_path]
- return asset.digest_path
- end
- return logical_path
- else
- raise AssetPaths::AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
- end
- end
- end
-
- private
- def digest_files
- Rails.application.config.assets.digest_files
- end
- end
- end
-end
View
28 lib/turbo-sprockets/sprockets_overrides/static_compiler.rb
@@ -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
@@ -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]})"
@@ -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)
View
10 lib/turbo-sprockets/tasks/assets.rake
@@ -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
@@ -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
View
2 lib/turbo-sprockets/version.rb
@@ -1,3 +1,3 @@
module TurboSprockets
- VERSION = "0.1.17"
+ VERSION = "0.2.0"
end
View
32 test/assets_test.rb
@@ -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
@@ -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
@@ -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
@@ -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"
@@ -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
@@ -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
View
3 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
@@ -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',
View
1 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

0 comments on commit 029faf9

Please sign in to comment.
Something went wrong with that request. Please try again.