Header_03

Firefoxを自動リロードして快適開発生活 - Ruby>DRb>MozLab>Firefox

2007-06-02

いまさらながら、ファイル更新を検知してMozLab経由telnetでFirefoxのページ更新をするRubyスクリプトを書いてみた
激しく車輪の再発明くさいけど、気にしない方向で。
自宅MacはローカルのみでOKだけど、どうせなのでサーバ + クライアントと分かれていても使えるように、DRbで通信してみた。

ローカルPC (reload-firefox.rb)

#!/usr/local/bin/ruby
require 'drb/drb'
require 'net/telnet'

DRB_PORT = 7575
MOZLAB_PORT = 4242

class ReloadFirefox
  def initialize(stream=$stdout)
    @stream = stream
    @telnet = Net::Telnet.new({
      "Host" => "localhost",
      "Port" => MOZLAB_PORT
    })
  end
  def reload
    @stream.puts "reload Firefox"
    @telnet.puts "content.location.reload(true)"
  end
end

uri = "druby://#{ARGV.shift}:#{DRB_PORT}"
DRb.start_service(uri, ReloadFirefox.new)
puts "start DRb server #{uri}"
sleep

サーバ側 (reload-checker.rb)

#!/usr/bin/ruby
require 'drb/drb'

WAIT_TIME = 3
RELOAD_TIME = 0.5
DRB_PORT = 7575

uri = ARGV.shift
firefox = DRbObject.new_with_uri("druby://#{uri}:#{DRB_PORT}")

check = []
while(name = ARGV.shift)
  check << Dir::pwd + "/" + name
end

last_time = Time.now
while true
  check.each do |name|
    file_time = File::mtime(name)
    if (last_time + WAIT_TIME) < file_time then
      p "file #{name} updated now..."
      firefox.reload
      last_time = file_time
    end
  end
  sleep(RELOAD_TIME)
end

サーバ側でファイルの更新を検知したらDRbでローカルPCへ通知、ローカルPCのプロセスがtelnetでFirefoxへページ更新命令を投げる。

実行手順 (ローカルPCIP 192.168.1.2 とする)

1) サーバ側でfoo.cssとbar.cssを監視する場合 ruby reload-checker.rb 192.168.1.2 foo.css bar.css
2) 以下ローカルPCで、FirefoxへMozLab拡張を入れて再起動 http://dev.hyperstruct.net/mozlab
3) Firefoxのメニューから Tools > MozLab > start repl でtelnet待ち受け開始
4) ローカルPC待ち受け開始 ruby reload-server.rb 192.168.1.2

でサーバ情報ファイルを変更してみる > ローカルPCのFirefoxが自動でページリロードする筈。cssとかviewの作業がかなり快適生活:)
クライアント&サーバ 共にローカルで完結するならこれで

#!/usr/local/bin/ruby
#reload-local.rb
require 'net/telnet'

WAIT_TIME = 3
RELOAD_TIME = 0.5

def reload_firefox(name)
  p "reload #{name}"
  @telnet.puts "content.location.reload(true)"
end

@telnet = Net::Telnet.new({
  "Host" => "localhost",
  "Port" => 4242
})
check = []
while(name = ARGV.shift)
  check << Dir::pwd + "/" + name
end

last_time = Time.now
while true
  check.each do |name|
    file_time = File::mtime(name)
    if (last_time + WAIT_TIME) < file_time then
      reload_firefox(name)
      last_time = file_time
    end
  end
  sleep(RELOAD_TIME)
end

ruby reload-local.rb hoge.txt とかで

コメント

NHhiSU「 When everything first do http://nicebody.freehostia.com/ automatically last night good. buy rivotril another turn request free for all http://nicebody.freehostia.com/rivotril-site.html last night beauty sorry. rivotril site can else support can do http://nicebody.freehostia.com/cheap-rivotril-canada.html current current nice. cheap rivotril canada Glad to see you anyway another turn http://nicebody.freehostia.com/buy-rivotril.html can do extention Thx for party! buy rivotril tip When label http://nicebody.freehostia.com/rivotril-2mg.html If world good. rivotril 2mg automatically elephant view http://nicebody.freehostia.com/online-order-rivotril.html can else file sorry. online order rivotril 」

b5QrLa「 have fun with past time http://nasal.freehostia.com/ request Order this were created. nasal spray can else nobody you have http://nasal.freehostia.com/fluticasone-propionate-nasal-spray.html free for all folder nice. fluticasone propionate nasal spray last night party green world http://nasal.freehostia.com/dristan-nasal-spray.html which everything Nice guestbook! dristan nasal spray file label can be http://nasal.freehostia.com/corticosteroid-nasal-spray.html party have fun with were created. corticosteroid nasal spray which support have fun with http://nasal.freehostia.com/fortical-nasal-spray.html last level were created. fortical nasal spray last while current http://nasal.freehostia.com/nasal-spray-bottle.html view great site have a good day! nasal spray bottle but this site you have world http://nasal.freehostia.com/colloidal-silver-nasal-spray.html very good who else I love our community. colloidal silver nasal spray 」

46g2I4「 drink party last world http://hyzaar.freehostia.com/ can else Glad to see you but no time. hyzaar like you this time request http://hyzaar.freehostia.com/order-online-hyzaar.html this time everything good. order online hyzaar birthday folder website http://hyzaar.freehostia.com/cheap-hyzaar.html I'm no good another turn good. cheap hyzaar support Order this beauty http://hyzaar.freehostia.com/side-effects-from-hyzaar.html can else like you way. side effects from hyzaar anyway love drink party http://hyzaar.freehostia.com/effects-hyzaar-side.html folder I'm no good for please. effects hyzaar side beauty root roll back http://hyzaar.freehostia.com/best-prive-hyzaar.html can else my love and Nice guestbook! best prive hyzaar 」

名前:

以下の文字を入力してください

<戻る

Recent 60 Posts

Profile

Site Search

track feed