Skip to content

Instantly share code, notes, and snippets.

@jhsu
Created July 8, 2011 20:18
Show Gist options
  • Save jhsu/1072723 to your computer and use it in GitHub Desktop.
Save jhsu/1072723 to your computer and use it in GitHub Desktop.
load the environment once and run tasks
#!/usr/bin/env ruby
# Move this script to RAILS_ROOT/scripts/taskrunner, `chmod +x` it and run with ./script/taskrunner
# run one or more rake tasks `Task:> db:migrate app:do_something env_variables=like_this`
require 'readline'
require 'irb'
require './config/environment'
load './Rakefile'
while task = Readline.readline('Tasks:> ', true)
begin
exit if task =~ /^exit/
puts 'Reloading...'
version = Rails.version
if version =~ /^2/
ActiveSupport::Dependencies.clear
elsif version =~ /^3/
ActionDispatch::Callbacks.new(Proc.new {}, false).call({})
end
task.gsub!(/\n+$/,'')
# set env variables
env = task.scan(/\w+=\w+/)
env.each do |env_var|
varname, value = env_var.split('=')
ENV[varname] = value
end
# remove env variables from task list
tasks = task.gsub(/\w+=\w+/, '').split(/\s+/)
begin
tasks.each do |task|
if raketask = Rake::Task[task]
puts "running '#{task}'"
raketask.invoke
raketask.reenable
Rake::Task.tasks.each {|t| t.reenable unless t.to_s == 'environment'}
end
end
# clear env variables
env.each do |env_var|
varname, value = env_var.split('=')
ENV[varname] = nil
end
rescue Interrupt => e
nil
rescue Exception => e
puts e.message
puts e.inspect
end
rescue Interrupt => e
exit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment