为 Rails 项目分叉 gem
我发现自己遇到过两次这种情况:我在我的系统上安装了一个 gem,并开始从我的 Rails 项目中使用它。最终我需要对该宝石进行一些更改。我应该如何进行?
理想情况下,我想在某个地方查看该 gem 的源代码,例如 ~/third_party/gems,对其进行处理并让我的 Rails 项目使用它。这可能吗?
在所有情况下,gem 都在 github 上,所以我可能会在 github 上获取它,克隆它,抓住机会并维护我自己的分支。我想然后我会直接使用 gem install 在我的服务器上安装该分支。这有道理吗?
I've found myself twice in this situation: I install a gem on my system and start using it from my Rails project. Eventually I need to make some changes to that gem. How should I proceed?
Ideally I'd like to check out the source code of that gem somewhere, like ~/third_party/gems, work on it and have my Rails project use that instead. Is that possible?
In all the cases the gems were at github so I would probably for it at github, clone it, make my chances and maintain my own branch. I suppose then I would install that branch directly with gem install on my server. Does that make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如今,使用 Bundler 可以很轻松地做到这一点。您制作 gem 的本地副本,然后不再
在 Gemfile 中执行操作,而是执行以下操作:
运行捆绑安装后,将从该目录中选取 gem。即使您修改了其中的某些内容,您只需重新启动 Rails 即可重新加载它。
如果您需要使用自己对 Gem 的更改来部署应用程序,您可以在 Github 或类似的地方以及您所做的 Gemfile 上创建一个分支:
就是这样。它简单、直接、美观。
Today this is pretty easy to do with Bundler. You make a local copy of the gem and then instead of doing
in your Gemfile, you do:
After running bundle install, the gem is picked from that directory. Even if you modify something in there, all you need to do to re-load it is restart Rails.
If you need to deploy an application using your own changes of a Gem, you make a fork, on Github or similar and on the Gemfile you do:
and that's it. It's simple, straightforward and beautiful.
如果您确实需要破解实际的宝石源,那么是的,这就是实现它的方法。然而,这应该是最后的手段。如果没有必要,您不想维护实际的 gem。为什么不从 gem 源扩展类(您需要更改其功能)并使用您的类而不是 Rails 代码中的 gem 类?
我发现您实际上很少需要直接破解第 3 方代码来完成您需要做的事情。好的软件可以扩展/轻松增强。
If you really need to hack the actual gem source then yes, that would be the way to do it. However, it should be a last resort. You don't want to maintain the actual gem if you don't have to. Why not extend classes from the gem source whose functionality you need to change and use your classes instead of the gem classes in your Rails code?
I find it rare that you actually need to hack 3rd party code directly to do what you need to do. Good software can be extended/easily augmented.