# Gemfile 和插件改进

**URL:** <https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119>\
**Category:** Development\
**Created:** [2021年九月21日 16:39 UTC](https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119 "2021-09-21T16:39:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwardila](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/edwardila/32/217748_2.png) [@edwardila](https://meta.discourse.org/u/edwardila)\
**Post date:** [2021年九月21日 16:39 UTC](https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119/1 "2021-09-21T16:39:20Z")

</div>

你好，Discourse 团队

我想为插件目录中的 Gemfile 以及在需要时添加外部 gems 提供一个有趣的解决方案：

```plaintext
# 加载插件的 Gemfile
Dir.glob File.expand_path("../plugins/*/{Gemfile}", __FILE__ ) do |file|
  eval_gemfile file
end

```

如果我们将此添加到主 Gemfile 中，就可以加载来自插件的 Gemfile。  
让我们讨论一下这个解决方案。

附：eval\_gemfile 的 DSL 文档 = [https://apidock.com/ruby/v2\_6\_3/Bundler/Dsl/eval\_gemfile](https://apidock.com/ruby/v2_6_3/Bundler/Dsl/eval_gemfile)

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2021年九月21日 16:48 UTC](https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119/2 "2021-09-21T16:48:32Z")

</div>

这很有趣！目前，插件可以通过在 plugin.rb 文件中列出完整的递归依赖树来要求使用 gem，例如：

> <https://github.com/discourse/discourse-algolia/blob/main/plugin.rb#L9-L10>

---

<div class="post-metadata">

**Author:** ![edwardila](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/edwardila/32/217748_2.png) [@edwardila](https://meta.discourse.org/u/edwardila)\
**Post date:** [2021年九月21日 16:53 UTC](https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119/3 "2021-09-21T16:53:17Z")

</div>

谢谢，这正是我想要的。  
但我觉得我的决定对我来说更清晰、更易理解。  
原因（个人看法）：

1. 我们让 plugin.rb 保持更简洁
2. Gemfile 对 Rails 开发者来说更易理解。

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2021年九月28日 06:39 UTC](https://meta.discourse.org/t/gemfile-and-plugins-improvements/204119/4 "2021-09-28T06:39:23Z")

</div>

不幸的是，这意味着锁定文件将变得不断变化，而我们将它包含在源代码控制中。

Gem 文件虽然不错，但在我们能够发布增量锁定文件之前，此类解决方案将无法奏效。
