¿Cómo hacer que Sinatra recargue automáticamente el archivo después de cada cambio?


Estoy usando

# my_app.rb
load 'index.rb'

Y comenzar el sever así

ruby my_app.rb

Pero nunca recarga ningún cambio que hice en la página de índice.
¿Me perdí algo aquí?

 110
Author: Nakilon, 2009-08-08

9 answers

Ver el Sinatra FAQ ,

"¿Cómo puedo recargar mi aplicación Sinatra en los cambios?"

En primer lugar, la recarga de código en proceso en Ruby es difícil y tiene un solución que funciona para cada escenario es técnicamente imposible.

Es por eso que le recomendamos hacer recarga fuera del proceso.

Primero necesitas instalar volver a ejecutar si aún no lo has hecho:

 $ gem install rerun

Ahora si inicia su aplicación Sinatra como esto:

$ ruby app.rb

Todo lo que tienes que hacer para recargar es hacer esto:

$ rerun 'ruby app.rb'

Si, por ejemplo, está usando rackup, en su lugar haga lo siguiente:

Rer volver a ejecutar 'rackup'

Entiendes la idea.

Si todavía quieres recargar en proceso, echa un vistazo Sinatra::Reloader.

 188
Author: dbr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-12 12:19:41

Gem install sinatra-reloader

require 'sinatra'
require 'sinatra/reloader'

Nota : recargará solo los controladores sinatra (y, tal vez, algunos comandos de configuración del servidor sinatra), pero no los archivos personalizados, que debe recargar manualmente.

 42
Author: Nakilon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-20 17:16:39

Puedes usar la gema rerun.

gem install rerun
rerun 'ruby app.rb' 

O si está usando rackup

rerun 'rackup'
 11
Author: zeronone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-24 04:52:00

Gem install sinatra-reloader

require 'sinatra/base'
require "sinatra/reloader"

class MyApp < Sinatra::Base
  register Sinatra::Reloader

  get '/' do
    "Hello Testing1!"
  end
end

Es posible que desee establecer la variable de entorno en desarrollo y cargar condicionalmente la gema.

 6
Author: rafidude,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-09 19:54:48

Cuando ejecute la aplicación con Passenger Standalone , simplemente cree un archivo tmp/always_restart:

$ touch tmp/always_restart.txt

Consulte la documentación del Pasajero para obtener más información.

 6
Author: karmi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-18 18:13:18

Me gusta la gema de la escopeta. Si está utilizando una aplicación modular Sinatra y tiene una config.ru archivo es fácil de ejecutar.

shotgun config.ru

Echa un vistazo a la gema aquí. Es bastante sencillo y no necesita configuración.

 5
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-30 22:25:00

En Windows, estoy usando mi gema de reinicio para esto:

restart ruby my_app.rb

O, con rackup:

restart rackup

Ver aquí para más información, espero que le resulte útil.

 4
Author: Vais Salikhov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-23 14:14:23

Podrías usar guardabarros. Levantado de un artículo en dblock.org :

Añade esto a tu Gemfile:

group :development do
  gem "guard"
  gem "guard-bundler"
  gem "guard-rack"
end

Luego, crea un Guardfile en la raíz de tu proyecto con este contenido:

guard 'bundler' do
  watch('Gemfile')
end

guard 'rack' do
  watch('Gemfile.lock')
  watch(%r{^(config|app|api)/.*})
end

Por último, corre Guardia, así: bundle exec guard, y rackup se recargará cada vez.

 1
Author: jeffbyrnes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-24 17:25:06

Si solo cambia sus plantillas, sinatra siempre las volverá a enviar si establece su entorno en desarrollo:

ruby app.rb -e development
 0
Author: three,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-14 11:21:00