如何配置 Bundler/Gemfile 在开发过程中使用不同的 gem 源?
我有一个 Sinatra 应用程序,需要我在本地开发的另一个 gem。我在配置 Bundler 时遇到问题,以在开发期间使用我的本地 gem 代码,但在生产中使用我供应的 gem 代码。
理想情况下,我可以做这样的事情,但 Bundler 不允许您两次指定相同的 gem:
# Doesn't work:
group :development do
gem 'awesome', :path => "~/code/awesome"
end
group :production do
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end
与此同时,我不得不手动供应 gem & 。每次部署的时候都要更新Gemfile中的gem源,比较麻烦。我的工作流程是这样的:
- 在开发过程中指向我的本地 gem (
gem 'awesome', :path => "~/code/awesome"
) - 准备部署时,将 gem 解压到
vendor /gems
- 更新 Gemfile 以指向供应商的 gem (
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
) - 运行
bundle install
(更新 Gemfile.lock) - 部署代码
- 返回步骤 1。
好麻烦!我想做一些比简单地编写 Rake 任务更干净的事情来自动化我当前的设置。
此场景的最佳工作流程是什么?
I have a Sinatra application that requires another gem I'm developing locally. I'm having trouble configuring Bundler to use my local gem code during development but my vendored gem code in production.
Ideally I could do something like this, but Bundler doesn't allow you to specify the same gem twice:
# Doesn't work:
group :development do
gem 'awesome', :path => "~/code/awesome"
end
group :production do
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end
In the meantime I've resorted to manually vendoring the gem & updating the gem source in the Gemfile every single time I deploy, which is quite a hassle. My workflow is this:
- Point to my local gem during development (
gem 'awesome', :path => "~/code/awesome"
) - When ready to deploy, unpack gem into
vendor/gems
- Update Gemfile to point to vendored gem (
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
) - Run
bundle install
(to update Gemfile.lock) - Deploy code
- Return to step 1.
What a hassle! I'd like to do something cleaner than simply writing Rake tasks to automate my current setup.
What's the best workflow for this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个新功能可以通过简单地指定
local.gem_name
配置选项来实现这一点,例如:仅当 gem 具有在 Gemfile 中指定的 git 存储库和分支时才有效。
有关更多详细信息,请参阅 Bundler 文档: http://bundler.io/v1.3/bundle_config.html
There is a new feature that allows to do that, by simply specyfing
local.gem_name
config option, like:This only works if the gem has a git repo and branch specified in the Gemfile.
See thr Bundler docs for more details: http://bundler.io/v1.3/bundle_config.html
显然,您可以在 Gemfile 中使用常规 Ruby。根据 这篇文章 你可以设置一个环境变量(或者我发现的任何其他变量),让你选择您想要使用的 gem 版本。
也许这会起作用。如果您需要供应正在进行的 gem,也许您可以制作一个小脚本来设置 ENV、供应它并重置 ENV。呃?
Apparently, you can use regular Ruby in your Gemfile. According to this article you can set an environment variable (or any other variable, I've found), to let you pick which version of a gem you want to use.
Maybe that'll work. If you need to vendor your in-progress gem maybe you could make a tiny little script that'll set the ENV, vendor it, and reset the ENV. Eh?
如果您使用 docker 构建容器,则始终可以在 dockerfile 中将路径设置为环境变量,并在 Gemfile 中使用此环境变量。请在下面找到 Dockerfile 和 Gemfile 的示例。
Dockerfile
Gemfile
If you are using doccker to build your containers, you can always set the path as an environment variable in the dockerfile, and use this environment variable in Gemfile. Please find an example of Dockerfile and Gemfile below.
Dockerfile
Gemfile
这是一个我没有完全发挥作用的建议(用于狂欢主题,我在主题中的一些样式表中遇到了问题):
复制您的 gemname.gemspec 文件并将其命名为 gemnamedev.gemspec,然后更改里面的 s.name它是“gemnamedev”。
Here is a suggestion which I didn't get to fully work (used for a spree theme and I got problems with some stylesheets from the theme):
Duplicate your gemname.gemspec file and call it gemnamedev.gemspec, and change s.name inside it to "gemnamedev".