Last active
August 29, 2015 14:10
-
-
Save gregawoods/52767c570f3f660cb0ef to your computer and use it in GitHub Desktop.
Just some experimental rake tasks. The goal here is to simplify the process of pushing or pulling assets stored in the public/system directory of a Rails project.
This file contains 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 :git_sync do | |
namespace :push do | |
desc 'Push public/system files to staging' | |
task :staging => :environment do | |
do_rsync_push('staging') | |
end | |
desc 'Push public/system files to production' | |
task :production => :environment do | |
do_rsync_push('production') | |
end | |
end | |
namespace :pull do | |
desc 'Pull public/system files from staging' | |
task :staging => :environment do | |
do_rsync_pull('staging') | |
end | |
desc 'Pull public/system files from production' | |
task :production => :environment do | |
do_rsync_pull('production') | |
end | |
end | |
private | |
def do_rsync_push(remote) | |
remote_url = extract_git_remote(remote) | |
if remote_url | |
command = "rsync -ruzv public/system #{remote_url}/public/" | |
logger.info "Running command: #{command}" | |
logger.info `#{command}` | |
end | |
end | |
def do_rsync_pull(remote) | |
remote_url = extract_git_remote(remote) | |
if remote_url | |
command = "rsync -ruzv #{remote_url}/public/system public/" | |
logger.info "Running command: #{command}" | |
logger.info `#{command}` | |
end | |
end | |
def extract_git_remote(remote) | |
git_result = `git remote show #{remote}` | |
if git_result | |
match_data = git_result.match(/Fetch URL: (.+)\n/) | |
if match_data.nil? || match_data.length < 1 | |
logger.fatal "Failed to reason out the git remote url. Git returned:" | |
logger.fatal git_result | |
return false | |
else | |
remote_url = match_data[1] | |
return remote_url | |
end | |
end | |
end | |
def logger | |
@_logger = Logger.new(STDOUT) if @_logger.nil? | |
return @_logger | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment