class Upstream::Wheneverizer

Attributes

path[R]
pull[R]

Public Class Methods

new(path, pull=false) click to toggle source
# File lib/upstream.rb, line 62
def initialize(path, pull=false)
  @path = path
  @already_existed = schedule_exists?
  @pull = pull
end

Public Instance Methods

already_existed?() click to toggle source
# File lib/upstream.rb, line 89
def already_existed?
  @already_existed
end
clean_up() click to toggle source
# File lib/upstream.rb, line 115
def clean_up
  file_count = `ls -a config | wc -l`.strip.to_i

  if !already_existed?
    if file_count > 3
      `rm config/schedule.rb`
    else
      `rm -rf config`
    end
  end
end
run() click to toggle source
# File lib/upstream.rb, line 68
def run
  setup
  write_schedule
  wheneverize
  clean_up
end
schedule_exists?() click to toggle source
# File lib/upstream.rb, line 85
def schedule_exists?
  File.exist?("#{path}/config/schedule.rb")
end
setup() click to toggle source
# File lib/upstream.rb, line 75
def setup
  if !already_existed?
    if !File.exist?("#{path}/config")
      `cd #{path} && mkdir config && wheneverize .`
    else
      `cd #{path} && wheneverize .`
    end
  end
end
wheneverize() click to toggle source
# File lib/upstream.rb, line 111
def wheneverize
  `whenever --update-crontab`
end
write_schedule() click to toggle source
# File lib/upstream.rb, line 93
    def write_schedule
      if !schedule_exists?
        write_args = ['config/schedule.rb', 'w+']
      else
        write_args = ['config/schedule.rb', 'a']
      end

      fetch_or_pull = pull ? "pull" : "fetch"

      File.open(*write_args) do |f|
        f.puts <<-COMMAND.gsub(/^ {10}/, '')
          every 1.hour do
            command "eval $(ssh-agent) && ssh-add ~/.ssh/github_id_rsa && cd #{path} && git #{fetch_or_pull} upstream master"
          end
        COMMAND
      end
    end