2016-08-07 30 views
1

所以我有一個應用程序,該樹是這樣的:不能重新運行我的應用程序後衛或重新運行

- Gemfile 
- Guardfile 
- source/ 
- dist/ 
- app.rb 

啓動服務器是ruby app.rb命令(或require_relative './app.rb',它做同樣的事情)

我想運行此命令並在任何文件更改時重新運行它。

唯一的例外是dist /文件夾 - 其中的任何文件更改應該被忽略。

這裏是我的嘗試,到目前爲止與guardguard-shell(道歉代碼轉儲):

require 'childprocess' 

# Global constant tracking whether the app has been started 
RunningProcess = {gen_rb: false} 

# Method to stop the app if it's been started 
def ensure_exited_server 
    begin 
    RunningProcess[:gen_rb] && RunningProcess[:gen_rb].poll_for_exit(10) 
    rescue ChildProcess::TimeoutError 
    RunningProcess[:gen_rb].stop # tries increasingly harsher methods to kill the process. 
    end 
    nil 
end 

# Start the app using 'child-process' 
def start_app 
    # prevent 'port in use' errors 
    ensure_exited_server 
    # The child-process gem starts a process and exposes its stdout 
    RunningProcess[:gen_rb] = ChildProcess.build("ruby", "gen.rb") 
    RunningProcess[:gen_rb].io.inherit! 
    RunningProcess[:gen_rb].start 
    nil 
end 

# Always start the app, not just when a file changes. 
start_app 

# The guard-shell gem runs a block whenever some set of files has changed. 
guard :shell do 
    # This regex matches anything except the dist/ folder 
    watch /^[^dist\/].+/ do |m| 
    start_app 
    # Print a little message when a file changes. 
    m[0] + " has changed." 
    end 
    nil 
end 

# Make sure the app does not run after guard exits 
at_exit { ensure_exited_server } 

這不會永遠重新啓動我的應用程序。

與重播的問題是我對他們的瞭解回購提出的一個問題:看到https://github.com/alexch/rerun/issues/107

+0

什麼版本的Ruby和哪個版本的Guard? – kcdragon

+0

後衛2.14.0,ruby 2.3.0p0(2015-12-25修訂版53290)[x86_64-linux] –

回答

1

如何像這樣爲你Guardfile?

guard :shell do 
    watch(%r{^source/.+\.(rb)}) do |m| 
    `ruby app.rb` 
    end 

    watch('app.rb') do |m| 
    `ruby app.rb` 
    end 
end 

而不是列出該目錄忽略的,這watch州哪些目錄/文件來使用。

+0

我沒有提到這個問題,但我有苗條,rb,咖啡,sass和js文件該應用程序預編譯,我需要觀看。我希望能使用'--ignore',這樣我就可以將源文件放在source /以外的頂級文件夾中。 –

+0

真棒,我得到它的工作與您的建議,包括而不是排除在正則表達式。除非m.instance_variable_get(「@ original_value」)。to_s.include?(「dist /」)',否則我正在使用'start_app'進行'exclusion'檢查。 –

+0

我仍然有興趣知道'exclude'正則表達式模式是否從根本上被打破,或者如果我的原始方法有問題。 –

相關問題