diff --git a/README.md b/README.md index 96fc73d1..0abfd0c0 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ Only removes old assets (keeps the most recent 3 copies) from `public/assets`. U Nuke `public/assets`. +**`rake 'assets:generate_nondigests[app_sdk.js app_sdk.css]''`** + +Will generate non-digest files of the files passed in the parameters of the task (`app_sdk.js` and `app_sdk.css` in the example above). Useful if you need some of your assets to be available for developers outside of your application (Javascript SDK for example). + +Remember that it's your responsibility to deal with cache expiration for those assets. + #### Customize If the basic tasks don't do all that you need, it's straight forward to redefine them and replace them with something more specific to your app. diff --git a/lib/sprockets/rails/task.rb b/lib/sprockets/rails/task.rb index 9c0af33b..702e62d3 100644 --- a/lib/sprockets/rails/task.rb +++ b/lib/sprockets/rails/task.rb @@ -7,6 +7,9 @@ module Sprockets module Rails class Task < Rake::SprocketsTask + class MissingParamsError < StandardError; end + class MissingFileError < StandardError; end + attr_accessor :app def initialize(app = nil) @@ -49,6 +52,16 @@ def manifest end end + def generate_nondigests(files) + files.each do |file| + digest_file = manifest.assets[file] + raise MissingFileError.new("#{file} does not exists. Maybe you didn't run assets:precompile yet?") unless digest_file + + path = manifest.directory + FileUtils.cp("#{path}/#{digest_file}", "#{path}/#{file}") + end + end + def define namespace :assets do %w( environment precompile clean clobber ).each do |task| @@ -69,6 +82,17 @@ def define end end + desc "Compile non-digest files" + task :generate_nondigest, [:files_list] => :environment do |t, args| + raise MissingParamsError.new("You must pass the files you want to generate nondigests (e.g. rake assets:generate_nondigests['file1.js, file2.js'])") if args.files_list.nil? + + files = args.files_list.split(' ') + + with_logger do + generate_nondigests(files) + end + end + desc "Remove old compiled assets" task :clean, [:keep] => :environment do |t, args| with_logger do diff --git a/test/test_task.rb b/test/test_task.rb index e3b68af7..26c478b8 100644 --- a/test/test_task.rb +++ b/test/test_task.rb @@ -25,7 +25,7 @@ def setup Sprockets::Rails::Task.new do |t| t.environment = @assets t.manifest = @manifest - t.assets = ['foo.js', 'foo-modified.js'] + t.assets = ['foo.js', 'foo-modified.js', 'file1.js', 'file2.js'] t.log_level = :fatal end @@ -139,4 +139,38 @@ def test_clean_with_keep_specified ensure FileUtils.rm(new_path) if new_path end + + def test_generate_nondigests + assert !@environment_ran + asset1_name = "file1.js" + asset2_name = "file2.js" + + digest1_path = @assets[asset1_name].digest_path + digest2_path = @assets[asset2_name].digest_path + + @rake['assets:precompile'].invoke + assert @environment_ran + + setup + + assert !@environment_ran + assert File.exist?("#{@dir}/#{digest1_path}") + refute File.exist?("#{@dir}/#{asset1_name}") + assert File.exist?("#{@dir}/#{digest2_path}") + refute File.exist?("#{@dir}/#{asset2_name}") + + @rake['assets:generate_nondigest'].invoke("#{asset1_name} #{asset2_name}") + + assert @environment_ran + assert File.exist?("#{@dir}/#{digest1_path}"), "digest file 1 not found" + assert File.exist?("#{@dir}/#{asset1_name}"), "nondigest file 1 not found" + assert File.exist?("#{@dir}/#{digest2_path}"), "digest file 2 not found" + assert File.exist?("#{@dir}/#{asset2_name}"), "nondigest file 2 not found" + end + + def test_generate_nondigests_with_no_params + assert_raises Sprockets::Rails::Task::MissingParamsError do + @rake['assets:generate_nondigest'].invoke + end + end end