生成基础文件结构

bundler gem your_gem创建一个基础文件结构
第一次使用时,需要配置一些默认值:

测试

bash
Creating gem 'demo_gem'...
Do you want to generate tests with your gem?
Future `bundle gem` calls will use your choice. This setting can be changed anytime with `bundle config gem.test`.
Enter a test framework. rspec/minitest/test-unit/(none)

CI

bash
Do you want to set up continuous integration for your gem? Supported services:
* CircleCI:       https://circleci.com/
* GitHub Actions: https://github.com/features/actions
* GitLab CI:      https://docs.gitlab.com/ee/ci/

Future `bundle gem` calls will use your choice. This setting can be changed anytime with `bundle config gem.ci`.
Enter a CI service. github/gitlab/circle/(none)

MIT license

bash
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at https://choosealicense.com/licenses/mit. y/(n)

代码准则

ruby
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see https://bit.ly/coc-enforcement. y/(n):

更新日志

bash
Do you want to include a changelog?
A changelog is a file which contains a curated, chronologically ordered list of notable changes for each version of a project. To make it easier for users and contributors to see precisely what notable changes have been made between each release (or version) of the project. Whether consumers or developers, the end users of software are human beings who care about what's in the software. When the software changes, people want to know why and how. see https://keepachangelog.com y/(n)

代码优化

bash
Do you want to add a code linter and formatter to your gem? Supported Linters:
* RuboCop:       https://rubocop.org
* Standard:      https://github.com/testdouble/standard

Future `bundle gem` calls will use your choice. This setting can be changed anytime with `bundle config gem.linter`.
Enter a linter. rubocop/standard/(none)

以上的配置都不是必备的,如果后面想修改配置,也可以通过bundle config gem.对应的配置名(上面的每个代码块儿都有写)从新配置。
比如通过:bundle config gem.test就能找到所有的配置信息:

bash
RubymineProjects: bundle config gem.test
Settings for `gem.test` in order of priority. The top value will be used
Set for the current user (/Users/qinsicheng/.bundle/config): "minitest"
RubymineProjects: cat /Users/qinsicheng/.bundle/config
---
BUNDLE_GEM__TEST: "minitest"
BUNDLE_GEM__CI: "false"
BUNDLE_GEM__MIT: "true"
BUNDLE_GEM__COC: "false"
BUNDLE_GEM__CHANGELOG: "true"
BUNDLE_GEM__LINTER: "false"

目录结构

bash
demo_gem/
├── CHANGELOG.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin 
│   ├── console
│   └── setup
├── demo_gem.gemspec                 这里是重点:设置gem的配置和描述信息
├── lib                              这里是重点:存放gem的核心功能
│   ├── demo_gem
│   │   └── version.rb
│   └── demo_gem.rb
├── sig
│   └── demo_gem.rbs
└── test
    ├── test_demo_gem.rb
    └── test_helper.rb

本地测试

  1. rake build
  2. rake install

当执行完成后,就可以通过irb进行测试使用

发布到rubygems

这一步需要先到rubygems注册账号,并在gem发布前推送的github

rake release

简单配置发布

上面的步骤一大堆,对现已经写好的项目转gem并不方面,我们只需要提取核心部分进行改造即可

  1. 创建 your_gem_name.gemspec 文件

    bash
    # your_gem_name.gemspec
    
    Gem::Specification.new do |spec|
      spec.name          = "your_gem_name"
      spec.version       = "0.1.0"
      spec.authors       = ["Your Name"]
      spec.email         = ["your_email@example.com"]
      spec.summary       = "A short description of your gem"
      spec.description   = "A longer description of your gem"
    
      spec.files         = Dir["lib/**/*.rb"]
      spec.bindir        = "bin"
      spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
      spec.require_paths = ["lib"]
    
      spec.homepage      = "https://github.com/your_username/your_gem_name"
      
      # 添加你的依赖项
      spec.add_dependency "gem_dependency", "~> 1.0"
    end

    在文件中,你需要设置 gem 的基本信息,比如名称、版本号、作者、描述等。也可以添加 gem 依赖项,以确保用户在安装你的 gem 时会同时安装所需的其他 gem。请根据你的项目进行相应的修改。

  2. 构建 gem 包:使用以下命令来构建 gem 包:

    bash
    gem build your_gem_name.gemspec

    这将在当前目录下生成一个 .gem 文件,其文件名与你在 gemspec 文件中定义的 gem 名称和版本号相匹配。

  3. 发布 gem 包:如果你希望将 gem 发布到 RubyGems.org 或其他 gem 托管服务上,可以使用以下命令发布你的 gem 包:

    bash
    gem push your_gem_name-0.1.0.gem

    发布 gem 包需要一个 RubyGems.org 的账号,如果你没有账号,可以使用 gem register 命令来注册一个。

  4. 安装和使用 gem:其他人可以使用以下命令安装并使用你的 gem:

    bash
    gem install your_gem_name

这里我发布了一个正则包装器(玩具)的gem:search | RubyGems.org | your community gem host,如果你发布成功,应该也可以搜索到自己的gem。