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

Allow age to be altered in assets:clean #462

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Deployment task that compiles any assets listed in `config.assets.precompile` to

**`rake assets:clean`**

Only removes old assets (keeps the most recent 3 copies) from `public/assets`. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.
Only removes old assets (by default keeps the most recent 3 copies and files created within 3600 ) from `public/assets`. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled. Use `rake assets:clean[3,3600]` to keep the last 3 assets or created within the last hour or `rake assets:clean[1,0]` to keep only the most recent old assets. This never cleans up the currently used assets.

**`rake assets:clobber`**

Expand Down
14 changes: 11 additions & 3 deletions lib/sprockets/rails/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,18 @@ def define
end
end

desc "Remove old compiled assets"
task :clean, [:keep] => :environment do |t, args|
desc "Remove old compiled assets. Arguments\nkeep: minimum previous copies to keep\nage: all assets with age less than this will be kept"
task :clean, [:keep, :age] => :environment do |_t, args|
with_logger do
manifest.clean(Integer(args.keep || self.keep))
age =
if args.age
args.age
elsif respond_to?(:age)
age
else
3600
end
manifest.clean(Integer(args.keep || keep), Integer(age))
end
end

Expand Down
45 changes: 44 additions & 1 deletion test/test_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def test_clean_with_keep_specified
FileUtils.cp(path, new_path)

assert File.exist?(new_path)
mtime = Time.now - 3601
File.utime(mtime, mtime, new_path.to_s)
digest_path = @assets['foo-modified.js'].digest_path

@rake['assets:precompile'].invoke
Expand All @@ -135,7 +137,48 @@ def test_clean_with_keep_specified

@rake['assets:clean'].invoke(0)
assert File.exist?("#{@dir}/#{digest_path}")
# refute File.exist?("#{@dir}/#{old_digest_path}")
refute File.exist?("#{@dir}/#{old_digest_path}")
ensure
FileUtils.rm(new_path) if new_path
end

def test_clean_with_age_and_keep_specified
assert !@environment_ran
path = Pathname.new(@assets['foo.js'].filename)
new_path = path.join("../foo-modified.js")

FileUtils.cp(path, new_path)

assert File.exist?(new_path)
age = 3000
mtime = Time.now - age
File.utime(mtime, mtime, new_path.to_s)
digest_path = @assets['foo-modified.js'].digest_path
@rake['assets:precompile'].invoke
assert File.exist?("#{@dir}/#{digest_path}")
assert @environment_ran

# clean environment
setup

# modify file
File.open(new_path, "a") {|f| f.write("var Bar;") }
@rake['assets:precompile'].invoke
old_digest_path = digest_path
digest_path = @assets['foo-modified.js'].digest_path

refute_equal old_digest_path, digest_path
assert File.exist?("#{@dir}/#{old_digest_path}")
assert File.exist?("#{@dir}/#{digest_path}")

@rake['assets:clean'].invoke(0, age + 500)
assert File.exist?("#{@dir}/#{digest_path}")
assert File.exist?("#{@dir}/#{old_digest_path}")

@rake['assets:clean'].reenable
@rake['assets:clean'].invoke(0, age - 500)
assert File.exist?("#{@dir}/#{digest_path}")
refute File.exist?("#{@dir}/#{old_digest_path}")
ensure
FileUtils.rm(new_path) if new_path
end
Expand Down