lib/babot.rb in babot-0.2.1 vs lib/babot.rb in babot-0.3.0
- old
+ new
@@ -11,51 +11,48 @@
class Babot
class << self
def update(name)
- git = Git.open(root.join("bots", name), :log => Logger.new(STDOUT))
- git.fetch "origin"
- git.merge "origin", "master"
- system "cd #{root.join('bots', name)} && bundle install"
+ run "cd '#{root.join('bots', name)}' && git pull --rebase origin master"
+ run "cd '#{root.join('bots', name)}' && bundle install"
end
def schedule
- cron = File.open root.join("schedule.rb"), "w"
- list.map { |name| instanciate(name) }.each do |bot|
- cron.puts <<-eos
+ Tempfile.open('schedule') do |cron|
+ cron.puts(list.map { |name| instanciate(name) }.map do |bot|
+ <<-eos
every '#{bot.when}' do
command 'cd #{root.join('bots', bot.name)} && bundle exec babot call #{bot.name}'
end
eos
+ end)
+ cron.flush
+ run "whenever -w -f '#{cron.path}'"
end
- cron.close
-
- Whenever::CommandLine.execute(file: cron.path, write: true)
end
def add(name, repository)
if repository =~ /\//
- FileUtils.ln_s repository, root.join("bots", name)
+ run "ln -s '#{repository}' '#{root.join("bots", name)}'"
else
- Git.clone repository, root.join("bots", name)
+ run "git clone '#{repository}' '#{root.join("bots", name)}'"
end
File.open(root.join("config", name).to_s, 'w') do |config|
config.write({ 'consumer_key' => "",
'consumer_secret' => "",
'oauth_token' => "",
'oauth_token_secret' => "" }.to_yaml)
end
end
def delete(name)
- FileUtils.rm_rf root.join("bots", name)
- FileUtils.rm_f root.join("config", name)
+ run "rm -rf #{root.join('bots', name)} #{root.join('config', name)}"
end
def configure(name)
- system "#{ENV['EDITOR'] || 'nano'} #{root.join("config", name).to_s}"
+ run "#{ENV['EDITOR'] || 'nano'} #{root.join("config", name).to_s}"
end
def call(name)
Twitter.update dry(name)
end
@@ -66,10 +63,22 @@
def dry(name)
instanciate(name).call
end
+ def dump
+ run "cd ~ && tar --exclude=.git -cf '#{Dir.pwd}/babot-#{Time.now.to_i}.tar' .babot"
+ end
+
+ def install(dump)
+ run "cd ~ && rm -rf '.babot' && tar -xf '#{Dir.pwd}/#{dump}'"
+ end
+
+ def push(remote)
+ run "scp -qr '#{root}' '#{remote}:~/.' && ssh '#{remote}' 'babot schedule'"
+ end
+
def instanciate(name)
require Babot.root.join("bots", name, 'lib', name)
options = YAML.load_file(root.join("config", name))
Babot::const_get(name.camelize).new(name, options)
@@ -77,9 +86,14 @@
puts error, error.backtrace
end
def root
Pathname.new(ENV["HOME"]).join ".babot"
+ end
+
+ def run(command)
+ puts command
+ system command
end
end
attr_accessor :name