无法使用 guard 重新运行我的应用程序或重新运行

Cannot rerun my app with guard or rerun

所以我有一个应用程序,树是这样的:

- 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 }

这不会重新启动我的应用程序。

re运行 的问题是我在他们的回购协议中提出的问题:请参阅 https://github.com/alexch/rerun/issues/107

你的 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 说明要使用哪个 directories/files,而不是列出要忽略的目录。