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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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|
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the previous files params for the task so we can accept multiple params comma separated like:

rake 'assets:generate_nondigest[file1.js, file2.js, folder with space/app.js]'

The args.extras will split it by ,.

Should I go back to task :generate_nondigest, [:files] => :environment and do the split in the task?
Will have a little more code in the task but might be a little easier to understand that the task need params. thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember how the command looks like with the files parameter, sorry. Could you post here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command would be similar but AFAIK we can't pass arrays to rake tasks, so with the files params:

task :generate_nondigest, [:files] => :environment do |t, args|
  split_files = args[:files].split ', '
  ...
end

And the command would be:

rake "assets:generate_nondigest['file1.js, file2.js, folder with space/app.js']"

But as you can see, we'll need to pass it as a string and split it. Code would start to get a little hairy as we'd need to make sure variations in the passed string (with different spaces counts) would still need to be correctly parsed if we decide to support folder names with spaces.

If we don't support folder with spaces, we could drop the , in the string and only do a files.split(' '):

task :generate_nondigest, [:files] => :environment do |t, args|
  split_files = args[:files].split ' '
  ...
end

And the command would be:

rake "assets:generate_nondigest['file1.js file2.js folder_no_space/app.js']"

Whatchathink?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the explicit argument would be better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done =)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the README as well to not include , but only spaces in the params

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