Gemspec 是否应该与 Ruby Gem 一起打包?

Should the Gemspec be packaged with a Ruby Gem?

我读到我们 shouldn't package test files with a Ruby Gem

现在我在想,我们是否应该打包 Gemspec.

例如,给定以下 Ruby Gem:

tree .
.
├── LICENSE.txt
├── README.md
├── lib
│   └── simplegem.rb
├── simplegem.gemspec
└── test
    ├── simplegem_test.rb
    └── thing_test.rb

2 directories, 6 files

我的 Gem::Specification 应该是这样的:

$LOAD_PATH.unshift 'lib'
require 'simplegem'

Gem::Specification.new do |s|
  s.name        = 'simplegem'
  s.version     = Simplegem::VERSION
  s.licenses    = ['MIT']
  s.summary     = 'This is a simple gem!'
  s.description = 'Much longer explanation of the simple gem!'
  s.authors     = ['...']
  s.email       = '...'
  s.files       = %w(simplegem.gemspec
                     LICENSE.txt
                     README.md
                     lib/simplegem.rb)
  s.homepage    = 'https://github.com/mbigras'
end

或:

$LOAD_PATH.unshift 'lib'
require 'simplegem'

Gem::Specification.new do |s|
  s.name        = 'simplegem'
  s.version     = Simplegem::VERSION
  s.licenses    = ['MIT']
  s.summary     = 'This is a simple gem!'
  s.description = 'Much longer explanation of the simple gem!'
  s.authors     = ['...']
  s.email       = '...'
  s.files       = %w(LICENSE.txt
                     README.md
                     lib/simplegem.rb)
  s.homepage    = 'https://github.com/mbigras'
end

注意第一个如何在 s.files 数组中包含 simplegem.gemspec 而第二个不包含。

在对我 gem 家中安装的 gem 进行的一项调查中,我注意到 Ruby gem 不需要打包 Gemspec实际上大多数 gem 不包含 gemspec 文件:

 ~/.rvm/gems/ruby-2.3.7/gems $ find . -name \*.gemspec | wc -l
      95
 ~/.rvm/gems/ruby-2.3.7@happy-backend/gems $ ls | wc -l
     318

为什么不打包?

  • 它在您的 GEM
  • 中节省了几个字节

为什么要打包?

  • 它允许轻松拆包和重新打包 gem
  • 为什么不呢? gem 规范无论如何都会上传到 gem 服务器,所以那里没有秘密,如果 'user' 真的想要它,它会在 $GEM_HOME/specifications 文件夹中