所以我有一個應用程序,該樹是這樣的:不能重新運行我的應用程序後衛或重新運行
- Gemfile
- Guardfile
- source/
- dist/
- app.rb
啓動服務器是ruby app.rb
命令(或require_relative './app.rb'
,它做同樣的事情)
我想運行此命令並在任何文件更改時重新運行它。
唯一的例外是dist /文件夾 - 其中的任何文件更改應該被忽略。
這裏是我的嘗試,到目前爲止與guard
和guard-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
什麼版本的Ruby和哪個版本的Guard? – kcdragon
後衛2.14.0,ruby 2.3.0p0(2015-12-25修訂版53290)[x86_64-linux] –