【问题标题】:Developing Multiple Rails Plugins with Dependencies开发多个具有依赖关系的 Rails 插件
【发布时间】:2015-09-19 07:53:01
【问题描述】:

我开始在我正在开发的项目中构建一系列插件和引擎,我遇到了必须列出主应用程序和插件的所有 Gemfile 中的依赖项路径的问题/engines 如果我想让 rake 工作。

Rake 适用于主应用程序,因为它的 Gemfile 列出了我想要的插件/引擎的相对路径,但是当一个插件/引擎依赖于另一个并且没有列出所有相对路径时,使用 rake rdoc 我'会得到一个类似下面的错误(大概我会在尝试运行测试/虚拟应用程序/等时得到同样的错误):

Bundler could not find compatible versions for gem "user":
  In Gemfile:
    auth (>= 0) ruby depends on
      user (>= 0) ruby
Could not find gem 'user (>= 0) ruby', which is required by gem 'auth (>= 0) ruby', in any of the sources.

我尝试在插件/引擎中指定 git 存储库,而不是必须使用路径:

# User engine
gem 'user', git: 'https://localhost/git/project/user.git', branch: 'master'

然后使用bundler config local.user /path/to/local/repo 命令使其指向本地存储库进行开发。这似乎工作得很好......直到我在本地仓库中更改插件的版本,然后它在任何依赖的插件/引擎/应用程序中吐出这个错误:

Could not find user-0.0.1 in any of the sources
Run `bundle install` to install missing gems.

虽然这并不是什么大问题——无论如何,版本号最终都会改变——如果你在本地仓库的一个分支上,它也会抛出以下错误主人:

Local override for user at /path/to/local/repo is using branch deleteme but Gemfile specifies master

从 Gemfile 中省略分支选项会导致我出现以下错误:

Cannot use local override for user at /path/to/local/repo because :branch is not specified in Gemfile. Specify a branch or use `bundle config --delete` to remove the local override

所以我只是坚持让, path: "../local-repo-directory" 散布在所有插件/引擎的 Gemfiles 中,在开发过程中相互依赖,或者有一种方法可以同时为 Rails 开发多个相互依赖的插件/引擎不使用真正草率/不雅的解决方案?

我正在用其他方法来做这件事,所以任何帮助都将不胜感激。我希望我已经解释得足够清楚了,但是如果还有什么我可以澄清的,请告诉我。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby dependencies bundler ruby-on-rails-plugins


    【解决方案1】:

    坚持在您的 Gemfile 中指定 git 存储库并使用 bundle local overrides 处理它们。

    解决方案可能存在的问题: 1. Local override for user at /path/to/local/repo is using branch deleteme but Gemfile specifies master 如果您的 Gemfile 指定分支“master”,则本地覆盖应该检查分支“master”。这是因为本地覆盖的目标是让您可以在运行和测试应用程序时处理本地文件夹中的 gem。在生产中,它会检查 Gemfile 和 Gemfile.lock 中指定的分支和修订版,它应该与您使用本地覆盖运行的完全一致。

    2. Could not find user-0.0.1 in any of the sources Run `bundle install` to install missing gems. 当您运行 bundle install 时,它会在 Gemfile.lock 中为每个 gem 放置一个版本号。该文件被添加到 git 中,以便其他开发人员和您的生产服务器运行与您相同的 gems 版本。您的 Gemfile.lock 需要指定与本地覆盖 gemspec 文件中返回的相同版本的 gem。如果您在本地覆盖中增加了版本号,您需要在使用它的应用程序中运行“bundle update gemname”,以便更新 Gemfile.lock。请注意,在您增加版本之前,bundler 会将所有内容缓存在 gemspec 文件中。因此,如果您不增加 gemspec 中的版本号,则无法在 gemspec 中添加新的依赖项或更改依赖项版本。

    git-bundle gem

    如果您在 Gemfile 中使用带有本地覆盖的 git 存储库,则捆绑器会将 git 修订哈希存储在您的 Gemfile.lock 中,如上所述。示例:

    宝石文件: gem 'example', git: 'https://github.com/your_name/example.git', branch: :master

    捆绑配置 shell 命令: bundle config local.example /path/to/local/git/repository

    Gemfile.lock(自动生成): GIT remote: https://github.com/your_name/example.git revision: b9270e61abb89e1ff77fb8cfacb463e4d04388ad branch: master

    在本地覆盖存储库中提交后,您需要在主应用程序上运行 bundle install ,以便它重建 Gemfile.lock 以包含新的修订哈希并提交它。我建议使用下面的 gem,因为它可以为您自动执行此过程,并且还有助于其他情况。有关详细信息,请参阅 gem 页面。

    https://github.com/EPI-USE-Labs/git-bundle

    activesupport-decorators gem

    作为旁注,当您需要在 gem 之间扩展类时,您可以使用由 activesupport-decorators gem 优雅地实现的装饰器模式:

    https://github.com/EPI-USE-Labs/activesupport-decorators

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-10
      • 2019-07-04
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2014-08-19
      相关资源
      最近更新 更多