无法通过警卫重新运行我的应用程序或重新运行

时间:2016-08-07 09:55:56

标签: ruby guard

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

- 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

1 个答案:

答案 0 :(得分: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表明要使用哪些目录/文件。