Giter VIP home page Giter VIP logo

require_reloader's Introduction

RequireReloader

Gem Version

Auto-reload require files or local gems without restarting server during Rails development.

Currently, it supports Rails 3+ and above, including 3.1 and 3.2.

It uses ActionDispatch::Callbacks.to_prepare to reload the require files before each request. In Rails 3.2, it uses watchable_dirs to reload only when you modify a file. More details in this blog post.

Usage

Given a Gemfile

# Gemfile
gem 'my_gem',  :path => '~/work/my_gem'
gem 'my_gem2', :path => '~/fun/my_gem2', :module_name => 'MYGEM2'

To reload all local gems (the ones with :path attribute, or using local git repo):

# config/environments/development.rb
YourApp::Application.configure do
  ...
  RequireReloader.watch_local_gems!
end

To reload a specific local gem:

RequireReloader.watch :my_gem

You can also reload a .rb file in lib or any directory:

RequireReloader.watch :half_baked_gem  # in lib dir
RequireReloader.watch :foo, :path => 'app/models'

The :path option is optional. In Rails 3.2, :path value is added into watchable_dirs. Rails 3.1 and 3.0 ignore this value.

The :module_name option is optional. By default, it is assumed that the top-level module is a CamelCase version of the gem name. If this is not the case, you can pass this option, a String. This value will also be picked up if included in the metadata of the gemspec, as in spec.metadata = {'module_name' => 'MYMOD'}.

RequireReloader adds lib into watchable_dirs. So, specify :path only if it is not specified in Gemfile and the file is located in other directory.

RequireReloader.watch :foo, :callback => lambda { |gem| puts "#{gem} got reloaded" }

You can supply a Proc with the optional :callback option. This proc gets called everytime after your gem got reloaded.

Installation

Add this line to your application's Gemfile:

gem 'require_reloader'

And then execute:

$ bundle

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits

This gem is forked from Colin Young's gem_reloader, based on a solution by Timothy Cardenas, inspired by a post from Leitch.

Contributors

  • @sven-winkler: trigger callback after gem reloaded (issue #2)
  • @aceofspades: support gem metadata :module_name in Gemfile (issue #4)

Changelog

  • v0.2.1: Add Rails 5.1 support (pull request from @kibenimatik)
  • v0.2.0: Support module_name option/spec-metadata
  • v0.1.6: RequireReloader::watch accepts :callback, runs it after gem reloaded (pull request from @sven-winkler).
  • v0.1.5: properly guess top-level module name based on gem name pattern, only watch git repo if it's local.
  • v0.1.4: remove 'vendor/gems' from watchable_dirs & autoload_paths, as local gem path is already specified in Gemfile.
  • v0.1.3: Skip reload local gem if it's itself; added integration tests.
  • v0.1.2: Minor rephrase on gem's description and summary.
  • v0.1.1: Use Bundler to fetch local gems info, instead of parsing Gemfile by hand.
  • v0.1.0: Forked colinyoung/gem_reloader, renamed & major rewrite to support Rails 3.2 and new features.
  • v0.0.2: Added "vendor/gems" to the config.autoload_paths so the user doesn't have to.

require_reloader's People

Contributors

colinyoung avatar dougpuchalski avatar kibenimatik avatar teohm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

require_reloader's Issues

Rails 4 support

I am unable to get this to work in Rails 4? Is it supported?

Fallback behavior for rubygems < v1.9

RubyGems does not support #metadata in versions before 1.9, so this gem will not work in rubygems v1.8.x. Updating rubygems in not an option for me at the moment, so I'd like to see if we can find a good fallback behavior.

One option(or hack) that I've been using is to fallback to the gem name when the gem metadata is not available. This worked well for me; although there is probably a better/less hacky way to solve this issue.

undefined method `local?' for [email protected]:company/repo.git (at master):Bundler::Source::Git

When using your useful gem with github repos it breaks. Here is the stack trace:

rake aborted!
undefined method `local?' for [email protected]:company/repo.git (at master):Bundler::Source::Git
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/require_reloader-0.1.5/lib/require_reloader.rb:69:in `block in local_gems'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/require_reloader-0.1.5/lib/require_reloader.rb:67:in `delete_if'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/require_reloader-0.1.5/lib/require_reloader.rb:67:in `local_gems'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/require_reloader-0.1.5/lib/require_reloader.rb:15:in `watch_local_gems!'
/Users/user_name/Documents/github/topgun/config/application.rb:15:in `<class:Application>'
/Users/user_name/Documents/github/topgun/config/application.rb:13:in `<module:Topgun>'
/Users/user_name/Documents/github/topgun/config/application.rb:12:in `<top (required)>'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/user_name/Documents/github/topgun/Rakefile:5:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:583:in `raw_load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:89:in `block in load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:88:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:72:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p194/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p194/bin/rake:23:in `<main>'

Thank you!

Can't load local gems with dashes

If I create a new project with require_reloader

rails new lala

and I'm creating a gem with a dash in its name, let's say:

bundle gem foo-bar

then load the gem changing the Gemfile like:

gem 'foo-bar', path: '../foo-bar'

it throws:

/home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- foo-bar (LoadError)
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/require_reloader-0.2.1/lib/require_reloader.rb:58:in `block (2 levels) in watch'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:413:in `instance_exec'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:413:in `block in make_lambda'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:600:in `catch'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:600:in `block in default_terminator'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:198:in `block in halting'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `block in invoke_before'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `each'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `invoke_before'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:130:in `run_callbacks'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/activesupport-5.1.4/lib/active_support/reloader.rb:87:in `prepare!'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/application/finisher.rb:61:in `block in <module:Finisher>'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:228:in `block in tsort_each'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:347:in `each'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:347:in `call'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:347:in `each_strongly_connected_component'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:226:in `tsort_each'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/tsort.rb:205:in `tsort_each'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
        from /home/alter/temp/lala/config/environment.rb:5:in `<top (required)>'
        from config.ru:3:in `require_relative'
        from config.ru:3:in `block in <main>'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/builder.rb:55:in `instance_eval'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/builder.rb:55:in `initialize'
        from config.ru:in `new'
        from config.ru:in `<main>'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/builder.rb:49:in `eval'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/builder.rb:49:in `new_from_string'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/builder.rb:40:in `parse_file'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/server.rb:319:in `build_app_and_options_from_config'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/server.rb:219:in `app'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:24:in `app'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/rack-2.0.4/lib/rack/server.rb:354:in `wrapped_app'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:80:in `log_to_stdout'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:42:in `start'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
        from /home/alter/temp/lala/bin/rails:9:in `require'
        from /home/alter/temp/lala/bin/rails:9:in `<top (required)>'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
        from /home/alter/.rvm/gems/ruby-2.4.2@default/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
        from /home/alter/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:68:in `require'
        from /home/alter/temp/lala/bin/spring:15:in `<top (required)>'
        from bin/rails:3:in `load'
        from bin/rails:3:in `<main>'

with an absolute path is the same story.

Rails 6 support?

Even though I could make the Gem easily "compile" under Rails 6, it doesn't function as intended. I guess because the new loader Zeitwork is interfering.

Is there a way to get require_reloader to work under Rails 6. Also Stackoverflow: https://stackoverflow.com/q/66640532/278842

Uncaught exception: Invalid single-table inheritance type: KbtPrimary is not a subclass of KbTagged

Environment includes Rails 4.2.5.1 and require_reloader 0.2.1

When I try to create KbtPrimary which inherits from KbTagged which inherits from Keybox, I receive this error that says that KbtPrimary is not a subclass of KbTagged when I try to save an instance of KbtPrimary.

The KbtPrimary class definition includes:
class KbtPrimary < KbTagged

I load these classes in development.rb:

RequireReloader.watch :keybox, path: 'app/models/keyboxes', callback: lambda { |klass| puts "#{klass} got reloaded" }
RequireReloader.watch :kb_untagged, path: 'app/models/keyboxes', callback: lambda { |klass| puts "#{klass} got reloaded" }
RequireReloader.watch :kb_tagged, path: 'app/models/keyboxes', callback: lambda { |klass| puts "#{klass} got reloaded" }
RequireReloader.watch :kbt_primary, path: 'app/models/keyboxes', callback: lambda { |klass| puts "#{klass} got reloaded" }
RequireReloader.watch :kbt_general, path: 'app/models/keyboxes', callback: lambda { |klass| puts "#{klass} got reloaded" }

If I use eager loading instead, it works:
config.eager_load = true

At the time of the save, KbTagged.subclasses shows:
[0] = {Class} KbtPrimary

And KbtPrimary.superclass.name shows:
"KbTagged"

RequireReloader tries to reload gems without having path set

When i setup RequireReloader to ´´´watch:local_gems``` it tries to watch gems, which are provided by git.

My gemfile looks like this:

gem 'rails', '3.2.11'
gem 'redis-store', '1.0.0.1'

gem 'haml-rails', git: 'git://github.com/indirect/haml-rails.git',
                  ref: 'dad799d', require: 'haml'

When debugging

   Bundler.definition.specs.
        select{|s| s.source.is_a?(Bundler::Source::Path) }

expands to:

[#<Gem::Specification name=haml-rails version=0.3.4>, #<Gem::Specification name=haml_assets version=0.2.1>, ....]

When I run my app an exception gets raised:

>> Using rack adapter
/Users/svenwinkler/.rvm/gems/ruby-1.9.3-p194/gems/require_reloader-0.1.4/lib/require_reloader.rb:49:in `const_defined?': wrong constant name Haml-rail (NameError)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.