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 23, 2015
1 parent 1548551 commit fa4e776
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
22 changes: 22 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,15 @@ def define
end
end

desc "Compile non-digest files"
task :generate_nondigest, [:files] => :environment do |t, args|
raise MissingParamsError.new("You must pass a --files param to assets:nondigest task") if args.files.nil?

with_logger do
generate_nondigests(Array(args.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 fa4e776

Please sign in to comment.