require 'git'
require 'fileutils'

namespace :ghpages do
  desc "Push latest simplecov to gh-pages"
  task :coverage => :environment do
    if test ?d, "coverage/"
      g = Git.open(Dir.pwd)
      FileUtils.mv("coverage/", "coverage_new/", :force => true)
      g.checkout(g.branch('gh-pages'))
      g.pull("origin", "gh-pages")
      FileUtils.rm_r("coverage", :force => true)
      FileUtils.mv("coverage_new/", "coverage/", :force => true)
      g.add("coverage/")
      g.commit_all("Update coverage.")
      g.push("origin", "gh-pages")
      if Rails.env == "production"
        g.checkout(g.branch('master'))
      else
        g.checkout(g.branch('development'))
      end
    end
  end