Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 102 lines (80 sloc) 2.792 kb
#!/usr/bin/env ruby
require 'benchmark'
require 'fileutils'
require 'readline'
module GoGo
def self.preload(&block)
@preload = block if block_given?
@preload || Proc.new
end
def self.command_filter(&block)
@command_filter = block if block_given?
@command_filter || Proc.new
end
def self.before_each_ruby_command(&block)
@before_each_ruby_command = block if block_given?
@before_each_ruby_command || Proc.new
end
end
unless File.exists?("Gofile")
puts "Creating default Gofile for Rails..."
default_template_path = File.expand_path('../../lib/gogo/gofile_templates/rails.rb', __FILE__)
FileUtils.cp(default_template_path, "Gofile")
end
load("Gofile")
GoGo.preload.call
Readline.completion_proc = Proc.new do |str|
Readline.completion_append_character = "" # space doesn't work on OS X :(
command_candidates = AUTOCOMPLETE_COMMANDS.grep(/^#{Regexp.escape(str)}/)
file_candidates = Dir[str+'*'].grep(/^#{Regexp.escape(str)}/)
file_candidates.map! do |path|
# add a space unless a directory
File.directory?(path) ? path : path + ' '
end
rake_task_candidates = RAKE_COMMANDS.grep(/^#{Regexp.escape(str)}/)
command_candidates + file_candidates + rake_task_candidates
end
ENV['GOGO_ENV'] ||= 'development'
history_file_path = ".gogo_history-#{ENV['GOGO_ENV']}"
# load old history
if File.exists?(history_file_path)
if Readline::HISTORY.respond_to?(:clear)
Readline::HISTORY.clear
else
Readline::HISTORY.size.times { Readline::HISTORY.pop }
end
File.read(history_file_path).lines.each { |l| Readline::HISTORY << l.chomp }
end
while line = Readline.readline("\033[1;32m#{Dir.pwd.sub(/.*\//, '')} \033[0;35m#{ENV['GOGO_ENV']} \033[0;34m$ \033[0m", true)
if line.blank?
Readline::HISTORY.pop # don't store blank commands in history
next
end
given_command, *arguments = line.split
command = GoGo.command_filter.call(given_command)
unless given_command == 'eval'
command = `which #{command}`.chomp
if command.size == 0
STDERR.puts "#{given_command}: command not found"
next
end
end
command_pid = fork do
File.open(history_file_path, "a") { |f| f.puts(line) }
if given_command == "eval"
p eval(line.sub(/^eval\s+/, ''))
elsif `file #{command}` =~ /: a .*ruby.* script text executable/
GoGo.before_each_ruby_command.call(given_command, arguments)
silence_warnings { ARGV = arguments }
load(command)
else
exec line
end
end
# send cntl-c to the child
trap("INT") { Process.kill("INT", command_pid) }
command_time = Benchmark.realtime { Process.wait(command_pid) }
puts "#{line.size < 15 ? line : given_command} took %.2f seconds." % command_time if command_time > 0.4
trap("INT", "DEFAULT")
end
puts
Jump to Line
Something went wrong with that request. Please try again.