Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created September 25, 2011 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nhocki/1241135 to your computer and use it in GitHub Desktop.
Save nhocki/1241135 to your computer and use it in GitHub Desktop.
Rake task to compile assets and push them automatically.
namespace :assets do
desc "Compile all the assets named in config.assets.precompile and push them"
task :compile do
AssetsCompiler.new.compile
end
class AssetsCompiler
def compile
ensure_clean_git
removed_previous_assets
compile_assets
commit_compiled_assets
push
end
def ensure_clean_git
raise "Can't deploy without a clean git status." if git_dirty?
end
def removed_previous_assets
run "bundle exec rake RAILS_ENV=production assets:clean"
end
def compile_assets
run "bundle exec rake RAILS_ENV=production assets:precompile"
end
def commit_compiled_assets
run "git add -u && git add . && git commit -am 'Compiled assets'"
end
def push
run "git push"
end
private
def run(command)
puts "+ Running: #{command}"
puts "-- #{system command}"
end
def git_dirty?
`[[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]`
dirty = $?.success?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment