LoginSignup
1
0

More than 1 year has passed since last update.

gem を作って公開してみた 〜準備編〜

Last updated at Posted at 2021-11-30

0. はじめに

Ruby や Railsの開発を行なっているとアプリケーションの機能拡充をしたりする上でgemを使う方はいるがご自身でgemの開発、公開をしたことがある方もいらっしゃらないかと思います。
その方向けにgemの開発〜公開までのフローについてまとめました。
記事としては以下の3つに分けて説明していこうと思います。 今回は準備編です。

  1. 準備編
  2. 開発編
  3. 公開編

今回は私が公開した以下のgemをベースにお話しします。

開発環境

私が上記の開発、公開を行った環境は以下の通りとなります。

  • OS: Mac
  • ruby: 2.5.8
  • bundler: 2.2.27

1. 準備編

今回の記事では、gemの開発に必要なものについての紹介とgemの雛形を作成するところまでを解説していきます。

2. gemの開発、公開に必要なもの

開発〜公開に必要なものは以下の通りとなります。

  • メールアドレス
    • 後述するRubyGems.org(gemの公開しているサイト)のアカウント作成、開発する際のgemにemailを記載する箇所等で使います。
  • RubyGems.org
    • gemの公開時にはこちらのサイト(https://rubygems.org/) のアカウントが必要となります。新規登録からメールアドレス、ユーザー名、パスワードの3項目を入力すれば問題ありません。
    • 詳しい説明は開発編の際に行います
  • bundler
    • 今回では主にgemの新規作成時に必要になります

3. gemの新規作成

3-1. bundlerのインストール、アップデート

すでに最新版のbundlerのインストールをされている方はこちらスキップで構いません。

$ gem install bundler # インストール
$ gem update bundler # アップデート

3-2. gemの作成コマンド実行

gemの雛形の作成は以下のコマンドでを実行できます。

$ bundle gem [gem名] -t # ex) bundle gem ruby_calendar -t

※【注意】  [gem名] ですが既に同名の名前のものが公開されている場合開発を終えても公開することができません。
ですので既に同名のものがないかをあらかじめ確認することをオススメします。

確認方法ですが、以下の方法があります(個人的にはCLIでの確認の方が分かりやすいかなと思います)

3-2-1. CLIコマンドでgemの検索を行う

$ gem search ^rails

*** REMOTE GEMS ***

rails (6.1.4.1) # <-- 例えば`rails`という同名のgemでは公開できないということです
rails-3-settings (0.1.1)
rails-acm (0.1.0)
rails-action-args (0.1.1)
...

3-2-2. GUIからgemの検索を行う

RubyGemsからでも検索が行えます
https://rubygems.org/search?query=rails

3-3. コマンド実行時の質問

初めてコマンドを実行した際にはいくつか質問されます。対話式なので該当するもので進んでいけばOKです。

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): 
  • gemのテストは何を使用するか(rspec/minitest/test-unit/(なし))
    • gemで使用するテストツールをここで導入できます
    • そのままEnterを押すと何も導入されないで進みます
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/
* Travis CI:      https://travis-ci.org/

Future `bundle gem` calls will use your choice. This setting can be changed anytime with `bundle config gem.ci`.
Enter a CI service. github/travis/gitlab/circle/(none):
  •  CIの導入はどうするか尋ねられます(github/travis/gitlab/circle/(なし))
    • gemで使用するCIの設定を尋ねられます
    • こちらもそのままエンターを押すと何も導入されないで進みます
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): 
  • MIT_Licenseを使うかどうかを尋ねられます(yes/(no))
    • 特段問題なければyesでもよいかと思います
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): 
  • Code of Conductを含めるか尋ねられます(yes/(no))
    • 特段問題なければyesでもよいかと思います
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): 
  • changelogを含めてもよいか尋ねられています(yes/(no))
    • 詳しくは こちら
    • 特段問題なければyesでもよいかと思います(変更履歴を書いておくとgemを利用する人も複数人で開発をする人にも分かりやすくなるはずです)
Do you want to add rubocop as a dependency for gems you generate?
RuboCop is a static code analyzer that has out-of-the-box rules for many of the guidelines in the community style guide. For more information, see the RuboCop docs (https://docs.rubocop.org/en/stable/) and the Ruby Style Guides (https://github.com/rubocop-hq/ruby-style-guide). y/(n): 
  • Rubocopを適用しますかと尋ねられています(yes/(no))
    • 静的解析ツールで一般的なコーディング規約で書かれているかみてくれるツールです
    • 他のせて機械的ツールやlintツールを利用する予定がなければ入れてもよいかと思います

こちらのやりとりを終えれば完成です! ディレクトリにコマンド実行時に指定したgem名のディレクトリ名ができているかと思います。

4. 最後に

 こちらで準備についてと雛形の作成までの流れを説明しました。
 次回は開発編です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0