Rails:获取环境中调用的 rake 任务的名称。rb?

发布于 2024-10-02 01:35:44 字数 1555 浏览 0 评论 0原文

我尝试在我的 environment.rb 中为 rake 任务“gems:install”禁用插件自动加载,因为它可能会导致无法解决的依赖关系和错误(请阅读 http://blog.joopp.com/2009/01 /26/plugin-gem-dependencies-in-your-environmentrb/ 了解更多信息)。

在实现这个“hack”时,我很快注意到变量 $rails_gem_installer(如果 gems:install 运行的话应该设置为 true),未设置。 (==nil)

现在我正在寻找一种方法来获取有关被调用的 rake 任务的信息,或者是否有其他工作解决方案?

我正在运行 Rails 2.3.10 / Ruby 1.8.7

以下是我的environment.rb中的代码,以便更好地理解:

Rails::Initializer.run do |config|

# fix for plugins dependent on gems
# see http://blog.joopp.com/2009/01/26/plugin-gem-dependencies-in-your-environmentrb/ 

if $rails_gem_installer
 # We stop the initializer to load the files from the /config/initializers dir. This is to disable the usage of plugins or gems in that code.
 puts 'Disabling the application initializers (rails_gem_installer == true)'
 class Rails::Initializer
   def load_application_initializers; end
 end

 # Next, do _only_ load the needed plugins that are not dependent on gems. For example exception_notification since that one is used in application.rb.
 puts 'Not loading all plugins (rails_gem_installer == true)'
 config.plugins = [:exception_notification]
else
 # Otherwise, when we're just loading the environment.. load everything in the right order. So this is YOUR config.plugins = [something]!
 config.plugins = [:all]
end

[... stuff like config.gem and so on]

i tried to disable autoloading of plugins in my environment.rb for the rake task "gems:install", since it may lead to unresolved dependencies and errors (read http://blog.joopp.com/2009/01/26/plugin-gem-dependencies-in-your-environmentrb/ for further information).

While implementing this "hack", i quickly noticed that the variable $rails_gem_installer, which should be set to true if gems:install is run, is not set. (==nil)

Now i'm looking for a way to get information about the called rake task, or is there any other working solution?

Im running Rails 2.3.10 / Ruby 1.8.7

Here's the code from my environment.rb for better understanding:

Rails::Initializer.run do |config|

# fix for plugins dependent on gems
# see http://blog.joopp.com/2009/01/26/plugin-gem-dependencies-in-your-environmentrb/ 

if $rails_gem_installer
 # We stop the initializer to load the files from the /config/initializers dir. This is to disable the usage of plugins or gems in that code.
 puts 'Disabling the application initializers (rails_gem_installer == true)'
 class Rails::Initializer
   def load_application_initializers; end
 end

 # Next, do _only_ load the needed plugins that are not dependent on gems. For example exception_notification since that one is used in application.rb.
 puts 'Not loading all plugins (rails_gem_installer == true)'
 config.plugins = [:exception_notification]
else
 # Otherwise, when we're just loading the environment.. load everything in the right order. So this is YOUR config.plugins = [something]!
 config.plugins = [:all]
end

[... stuff like config.gem and so on]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2024-10-09 01:35:44

与其沿着让 rake gems:install 正常工作的道路(这似乎在每种情况下都无法正常工作),最好将依赖项移植到 bundler 代替。尽管这是 Rails 3 默认配置的方式,但它可以在任何 ruby​​ 项目上使用,并且做得更好。

Gemfile 还可以用作人类可读的依赖项清单,这并不总是可以轻松地从 Rails 配置文件中提取出来。

他们的网站上有一个使用 Rails 2.3 捆绑程序的示例: http://gembundler.com/rails23.html

Instead of going down the road of getting rake gems:install to work properly, which was something that never seemed to work properly in every situation, it might be better to port your dependencies to bundler instead. Although this is how Rails 3 is configured by default, it can be used on any ruby project and does a much better job.

A Gemfile also serves as a human-readable manifest of dependencies, something that's not always easily extracted from a Rails configuration file.

There's an example on using bundler with Rails 2.3 on their site: http://gembundler.com/rails23.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文