Skip to content

Commit a1b1e5e

Browse files
committed
Allow setting keep explicitly
The number of backup copies of an asset that are kept after running `assets:clean` is currently hardcoded at 2. You currently cannot configure this number. Instead of pushing the ability to change this via an ENV var sstephenson/sprockets#461 it was suggested that this ability be done at the rake level which results in a clean API. So if you do not want to keep any copies of assets you can run ```sh $ rake assets:clean[0] ``` You can keep 1 copy of assets by running: ```sh $ rake assets:clean[1] ``` If no value is provided 2 copies will be stored. ATP
1 parent ec03b0a commit a1b1e5e

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* Allow keep value to be specified for `assets:clean` run with args
2+
for example `assets:clean[0]` will keep no old asset copies
3+
4+
*Richard Schneeman*

lib/sprockets/rails/task.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def define
6262
end
6363

6464
desc "Remove old compiled assets"
65-
task :clean => :environment do
65+
task :clean, [:keep] => :environment do |t, args|
66+
keep = Integer(args.keep || 2)
6667
with_logger do
6768
manifest.clean(keep)
6869
end

test/fixtures/foo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var Foo;
1+
var Foo;

test/test_task.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def setup
2727
Sprockets::Rails::Task.new do |t|
2828
t.environment = @assets
2929
t.manifest = @manifest
30-
t.assets = ['foo.js']
30+
t.assets = ['foo.js', 'foo-modified.js']
3131
t.log_level = :fatal
3232
end
3333
end
@@ -77,4 +77,38 @@ def test_clean
7777
@rake['assets:clean'].invoke
7878
assert File.exist?("#{@dir}/#{digest_path}")
7979
end
80+
81+
def test_clean_with_keep_specified
82+
assert !@environment_ran
83+
path = @assets['foo.js'].pathname
84+
new_path = path.join("../foo-modified.js")
85+
86+
FileUtils.cp(path, new_path)
87+
88+
assert File.exist?(new_path)
89+
digest_path = @assets['foo-modified.js'].digest_path
90+
91+
@rake['assets:precompile'].invoke
92+
assert File.exist?("#{@dir}/#{digest_path}")
93+
assert @environment_ran
94+
95+
# clean environment
96+
setup
97+
98+
# modify file
99+
File.open(new_path, "a") {|f| f.write("var Bar;") }
100+
@rake['assets:precompile'].invoke
101+
old_digest_path = digest_path
102+
digest_path = @assets['foo-modified.js'].digest_path
103+
104+
refute_equal old_digest_path, digest_path
105+
assert File.exist?("#{@dir}/#{old_digest_path}")
106+
assert File.exist?("#{@dir}/#{digest_path}")
107+
108+
@rake['assets:clean'].invoke(0)
109+
assert File.exist?("#{@dir}/#{digest_path}")
110+
refute File.exist?("#{@dir}/#{old_digest_path}")
111+
ensure
112+
FileUtils.rm(new_path) if new_path
113+
end
80114
end

0 commit comments

Comments
 (0)