Class: Upstream::Wheneverizer

Inherits:
Object
  • Object
show all
Defined in:
lib/upstream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, pull = false) ⇒ Wheneverizer

Returns a new instance of Wheneverizer.



62
63
64
65
66
# File 'lib/upstream.rb', line 62

def initialize(path, pull=false)
  @path = path
  @already_existed = schedule_exists?
  @pull = pull
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



60
61
62
# File 'lib/upstream.rb', line 60

def path
  @path
end

#pullObject (readonly)

Returns the value of attribute pull.



60
61
62
# File 'lib/upstream.rb', line 60

def pull
  @pull
end

Instance Method Details

#already_existed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/upstream.rb', line 89

def already_existed?
  @already_existed
end

#clean_upObject



115
116
117
118
119
120
121
122
123
124
125
# 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

#runObject



68
69
70
71
72
73
# File 'lib/upstream.rb', line 68

def run
  setup
  write_schedule
  wheneverize
  clean_up
end

#schedule_exists?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/upstream.rb', line 85

def schedule_exists?
  File.exist?("#{path}/config/schedule.rb")
end

#setupObject



75
76
77
78
79
80
81
82
83
# 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

#wheneverizeObject



111
112
113
# File 'lib/upstream.rb', line 111

def wheneverize
  `whenever --update-crontab`
end

#write_scheduleObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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