Created
February 8, 2011 10:44
-
-
Save miaout17/816257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
參考xdite's blog http://blog.xdite.net/?p=1839 | |
建立一個名為railsc的執行檔,以monkey patch的方式啟用wirble及hirb | |
將railsc存到你PATH下,並設定可執行(chmod 755) | |
執行railsc取代rails c,即可載入wirble及hirb,不用修改project directory下的東西 | |
P.S. | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
這個我還是寫在.irbrc裡(事實上我是用show_log和hide_log開關) | |
Pros: | |
* 不用修改rails app下的檔案,也不用硬改rails gem中的程式 | |
(Survey其他人的project好用,clone下來不用改code) | |
Cons: | |
* 如果Railites內部API被修改,monkey patch可能會失效 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
version = ">= 0" | |
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then | |
version = $1 | |
ARGV.shift | |
end | |
gem 'rails', version | |
require 'rails' | |
require 'rails/script_rails_loader' | |
# Monkey Patch | |
module Rails | |
module ScriptRailsLoader | |
def self.exec_script_rails! | |
cwd = Dir.pwd | |
return unless in_rails_application? || in_rails_application_subdirectory? | |
exec_script_rails_with_exts! if in_rails_application? | |
Dir.chdir("..") do | |
# Recurse in a chdir block: if the search fails we want to be sure | |
# the application is generated in the original working directory. | |
exec_script_rails! unless cwd == Dir.pwd | |
end | |
rescue SystemCallError | |
# could not chdir, no problem just return | |
end | |
def self.exec_script_rails_with_exts! | |
script = File.read(SCRIPT_RAILS) | |
script.gsub!('__FILE__', "'#{File.join(Dir.pwd, SCRIPT_RAILS)}'") | |
script = <<-EOS | |
require 'rubygems' | |
require "irb" | |
begin | |
gem 'wirble' | |
require 'wirble' | |
Wirble.init | |
Wirble.colorize | |
rescue LoadError | |
puts "Wirble is not loaded!!(Missing gem?)" | |
end | |
begin | |
gem 'wirble' | |
require 'hirb' | |
Hirb.enable | |
rescue LoadError | |
puts "Hirb is not loaded!!(Missing gem?)" | |
end | |
#{script} | |
EOS | |
# puts script | |
exec RUBY, "-e", script, "c" if in_rails_application? | |
end | |
end | |
end | |
load Gem.bin_path('rails', 'rails', version) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment