2008年8月24日星期日

Start Browser with Mongrel Server

Do you want let your browser auto start with mongrel server? Yep, it's simply, what you should be done is just add a parameter to command, such as script/server -x ff, this the firefox browser will be started with server.
Installation
Install firewatir: sudo gem install firewatir, if you use firewaitr, you should install Firefox Extensions(http://wiki.openqa.org/display/WTR/FireWatir+Installation)
Install watir: sudo gem install watir
Setting Mongrel Server
Find mongrel.rb in C:\ruby\lib\ruby\gems\1.8\gems\rails-2.1.0\lib\commands\servers\ and add the following blod lines:
......
opts.on("-e", "--environment=name", String,
"Specifies the environment to run this server under (test/development/production).",
"Default: development") { |v| OPTIONS[:environment] = v }
opts.on("-x", "--browser=browser", String, "Start browser with serve", "Default: firefox") { |v| OPTIONS[:browser] = v }

opts.separator ""
......


if OPTIONS[:detach]
`mongrel_rails #{parameters.join(" ")} -d`
else
ENV["RAILS_ENV"] = OPTIONS[:environment]
browser = OPTIONS[:browser]
if browser
browser_downcase = browser.downcase
if((browser_downcase <=> "ie")==0) || ((browser_downcase <=> "firefox")==0) || ((browser_downcase <=> "ff")==0)
ENV["BROWSER_ENV"] = browser_downcase
else
puts "#{browser} is not the support browser, please use IE or Firefox"; exit;
end
end
RAILS_ENV.replace(OPTIONS[:environment]) if defined?(RAILS_ENV)

Setting mongrel_rails under folder C:\ruby\lib\ruby\gems\1.8\gems\mongrel-1.1.5-x86-mswin32-60\bin\
1.
.......
require 'firewatir'
include FireWatir
require 'watir'
include Watir

Mongrel::Gems.require 'gem_plugin'
.......

2.
.......
['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil],
['-x', '--browser', "Enable debugging mode", :@browser, "firefox"]
]
end
.......
3.
.......
if config.defaults[:daemon]
config.write_pid_file
else
config.log "Use CTRL-C to stop."
end

browser = ENV["BROWSER_ENV"]
if !browser.nil?
if (browser <=> "ie") ==0
begin
IE.new.goto("http://localhost:#{@port}")
config.log "Start IE browser succefully."
rescue Exception
config.log "Start IE browser failed."
ensure
end
elsif ((browser <=> "firefox") ==0) || ((browser <=> "ff") ==0)
begin
Firefox.new.goto("http://localhost:#{@port}")
config.log "Start Firefox browser succefully."
rescue Exception
config.log "Start Firefox browser failed."
ensure
end
end
end

config.join

if config.needs_restart
.......

How to use:
Go to your rails project and run command script/server -x ff/firefox/ie.

That's all.

2008年8月5日星期二

Bloggin my footprint

Now, it's a beginning. Just blog and Enjoy.