Created
July 2, 2009 14:47
-
-
Save aiwilliams/139504 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :copy do | |
# Allows for the copy of asset directories from remote environments into the | |
# corresponding system directory. The download is maintained in your /tmp | |
# directory, so that subsequent executions will not fetch from the remote | |
# server unless forced to. | |
# | |
# This is extremely useful for developing/debugging migrations, where you'd | |
# like to focus on just a few assets, and need to re-run the migration many | |
# times. | |
# | |
# asset_path may be to any depth in order to reduce the amount of content | |
# which is retrieved. For instance, I regularly use this: | |
# | |
# cap production copy:assets -Sasset_path=/public/avatars/000/000/004/4 | |
# | |
desc 'Copy remote assets to local application' | |
task :assets, :roles => :app, :except => {:no_release => true} do | |
asset_path = fetch(:asset_path, nil) | |
raise "Please specify '-Sasset_path=public/avatars'" if asset_path.nil? | |
compressed_file_name = asset_path.gsub(/\//, '_') + '.tar.bz2' | |
compressed_file_path = "/tmp/#{compressed_file_name}" | |
if File.exists?(compressed_file_path) && fetch(:force, false) == false | |
logger.debug "Assets exist '#{compressed_file_path}'. Force download with '-Sforce=true'." | |
else | |
remote_asset_path = File.join(current_path, asset_path) | |
logger.debug "Copying remote assets '#{remote_asset_path}' to '#{File.join(FileUtils.pwd, asset_path)}'" | |
asset_path_exists = capture "if [ -d #{remote_asset_path} ]; then echo 'true'; else echo 'false'; fi" | |
raise "Assets do not exist on #{environment} at #{remote_asset_path}" unless asset_path_exists | |
run "tar -C #{current_path} -cjf #{compressed_file_path} #{asset_path}" | |
get compressed_file_path, compressed_file_path | |
run "rm #{compressed_file_path}" | |
end | |
system "tar -xjf #{compressed_file_path}" | |
logger.debug "Assets extracted in #{FileUtils.pwd}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment