Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding "nodigest" task to add non digest assets #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (`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.
Expand Down
24 changes: 24 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,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
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