如何使用 gemspec add_runtime_dependency 和 `bundle install`

How to work with gemspec add_runtime_dependency and `bundle install`

我有一个代码库,其中有一个 gemspec 文件,例如:

require "rubygems"
::Gem::Specification.new do |specification|
  specification.add_development_dependency "yard", "~> 0.9"
  specification.add_runtime_dependency "dry-validation", "~> 0.13"
end

bundle install 将安装这两种依赖类型。所以我只想为我的 CI 脚本安装运行时依赖项。我看到 bundle install --with <group> 存在,但我没有群组。 运行 以交互方式,返回的规范具有从 .groups 返回的空结果。我很想合理化这两个世界。我必须为每个 gem 依赖项显式添加一个组吗? add_runtime_dependency 和 add_development_dependency 有区别吗?

来自 bundler's documentation

Because we have the gemspec method call in our Gemfile, Bundler will automatically add this gem to a group called “development” which then we can reference any time we want to load these gems with the following line:

Bundler.require(:default, :development)

在您的情况下,如果您希望安装所有不用于开发的 ruby​​gems,请尝试

bundle install --without development

对于未来的捆绑器版本,您可以在本地(或全局)配置它

bundle config set --local without 'development'

要使其全部正常工作,请确认您的项目中有一个 Gemfile,它看起来像

# frozen_string_literal: true

source 'https://rubygems.org'

gemspec