Skip to content

Commit

Permalink
adding "nodigest" task to add non digest assets
Browse files Browse the repository at this point in the history
The new task `assets:nodigest` copies compiled files removing the md5 from the file.
  • Loading branch information
celsodantas committed Apr 24, 2015
1 parent 1548551 commit e29f107
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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.
Expand Down
23 changes: 23 additions & 0 deletions lib/sprockets/rails/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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|
Expand All @@ -69,6 +82,16 @@ def define
end
end

desc "Compile non-digest files"
task :generate_nondigest => :environment do |t, args|
files = args.extras
raise MissingParamsError.new("You must pass the files you want to generate nondigests (e.g. rake 'assets:generate_nondigests[file1.js, file2.js]')") if files.empty?

with_logger do
generate_nondigests(files)
end
end

desc "Remove old compiled assets"
task :clean, [:keep] => :environment do |t, args|
with_logger do
Expand Down
36 changes: 35 additions & 1 deletion test/test_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit e29f107

Please sign in to comment.