Jewelerで作ったGemをGithubとRubygemsに登録

Pokebell gem を作った際の記録。
Ruby - 読みをポケベル入力数字に変換するPokebell gem - Qiita
アカウントは予め github には登録済みで、既に pokebell フォルダに、符号化できる状態の pokebell.rb を作成してある状況から開始しました。

Jewelerをインストール

$ gem install jeweler
参考

Opinionated tool for creating and managing Rubygem projects

Jeweler について

Jeweler とは、RubyGems 作成支援のツールであり RubyGems の作成を簡単にしてくれます。 下記のような機能があります。

  • パッケージのひな形の作成
  • github へのソースの登録
  • RubyGems への公開
  • 公開したパッケージの更新
パッケージのひな形作成
$ jeweler --rspec --create-repo hoge

hoge の部分がパッケージ名です。先に rubygems.org で自分が作ろうとしているパッケージ名が先に登録されていないか確認しておきましょう。

オプション

テストフレームワークrspec を使用します。

  • --create-repo

githubリポジトリを作成します。 ~/.gitconfig に github の設定が行われている必要があります。

github 用の プロジェクトのスケルトンコードの作成

まだ github のプロジェクトは作っていないので、jeweler に作ってもらいましょう。
jeweler のオプションに --create-repo オプションを追加すると、スケルトンコードの他に github 上のリポジトリも一緒に作成してくれます。

Jeweler のオプション
$ jeweler --help
Usage: jeweler [options] reponame
e.g. jeweler the-perfect-gem
        --directory [DIRECTORY]      specify the directory to generate into (deprecated)

        --rspec                      generate rspec code examples
        --shoulda                    generate shoulda tests
        --testunit                   generate test/unit tests
        --bacon                      generate bacon specifications
        --testspec                   generate test/spec tests
        --minitest                   generate minitest tests
        --micronaut                  generate micronaut examples
        --riot                       generate riot tests
        --shindo                     generate shindo tests

        --[no-]bundler               use bundler for managing dependencies
        --cucumber                   generate cucumber stories in addition to the other tests

        --reek                       generate rake task for reek
        --roodi                      generate rake task for roodi

        --summary [SUMMARY]          specify the summary of the project
        --description [DESCRIPTION]  specify a description of the project

        --user-name [USER_NAME]      the user's name, ie that is credited in the LICENSE
        --user-email [USER_EMAIL]    the user's email, ie that is credited in the Gem specification

        --github-username [GITHUB_USERNAME]
                                     name of the user on GitHub to set the project up under
        --git-remote [GIT_REMOTE]    URI to set the git origin remote to
        --homepage [HOMEPAGE]        the homepage for your project (defaults to the GitHub repo)
        --create-repo                create the repository on GitHub

        --yard                       use yard for documentation
        --rdoc                       use rdoc for documentation
    -v, --version                    show version
    -h, --help                       display this help and exit

Jeweler で gem 作成開始

作業1(失敗:github情報を ~/.gitconfig に未登録)
$ jeweler --rspec --create-repo pokebell
Please specify --github-username or set github.user in ~/.gitconfig (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt

あら。
そういえば ~/.gitconfig に登録してないですね。それと token も作ってない。
まず
New personal access token
Github の token を作ります。 Token description は記入必須です。テキトーに入れます。

So let’s settle on a standard, pioneered by GitNub:

$ git config --global github.user defunkt
$ git config --global github.token 6ef8395fecf207165f1a82178ae1b984

(Remember to replace defunkt and 6ef8395fecf207165f1a82178ae1b984 with your own username and token.)

これと同様に設定します。

作業2(失敗:同名フォルダが存在)
$ jeweler --rspec --create-repo pokebell
The directory pokebell already exists. Maybe move it out of the way before continuing?

先に pokebell フォルダを作っていたのがマズかったようだ。
仕方ないのでバックアップを取った上でフォルダ削除。

作業3(成功\o/)

三度目の正直。

$ jeweler --rspec --create-repo pokebell
	create	.gitignore
	create	Rakefile
	create	Gemfile
	create	LICENSE.txt
	create	README.rdoc
	create	.document
	create	lib
	create	lib/pokebell.rb
	create	spec
	create	spec/spec_helper.rb
	create	spec/pokebell_spec.rb
	create	.rspec
Jeweler has prepared your gem in ./pokebell
Please provide your Github password to create the Github repository
Password: 

ここで Github のパスワードを入力します。

Faraday::Builder is now Faraday::RackBuilder.
Jeweler has pushed your repo to git@github.com:riocampos/pokebell.git

当然ですが lib/pokebell.rb の中身は空っぽです。ので、バックアップを取っていた内容を流し込みます。そして git commit

Rakeタスク一覧
$ rake -T # 定義されているタスク一覧を表示

では pokebell フォルダ内でrake -Tしてみます。

$ rake -T
rake build               # Build gem into pkg/
rake clean               # Remove any temporary products
rake clobber             # Remove any generated file
rake clobber_rdoc        # Remove RDoc HTML files
rake console[script]     # Start IRB with all runtime dependencies loaded
rake gemcutter:release   # Release gem to Gemcutter
rake gemspec             # Generate and validate gemspec
rake gemspec:debug       # Display the gemspec for debugging purposes, as j...
rake gemspec:generate    # Regenerate the gemspec on the filesystem
rake gemspec:release     # Regenerate and validate gemspec, and then commit...
rake gemspec:validate    # Validates the gemspec on the filesystem
rake git:release         # Tag and push release to git
rake install             # Build and install gem using `gem install`
rake rdoc                # Build RDoc HTML files
rake release             # Release gem
rake rerdoc              # Rebuild RDoc HTML files
rake simplecov           # Code coverage detail
rake spec                # Run RSpec code examples
rake version             # Displays the current version
rake version:bump:major  # Bump the major version by 1
rake version:bump:minor  # Bump the a minor version by 1
rake version:bump:patch  # Bump the patch version by 1
rake version:write       # Writes out an explicit version
バージョン設定

ひとまずバージョンを0.0.1にしておきたい。

$ rake version:write
Updated version: 0.0.0
$ rake version:bump:patch
Current version: 0.0.0
Updated version: 0.0.1

として VERSION ファイルの作成及び記載バージョンを0.0.1に変更しました。

  • VERSION
0.0.1

git commit も為されていました。

gemspec ファイル作成
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}

の部分を変更しておく必要があるようです。

gem.summary = %Q{Japanese "Pokebell" pager message encoder}
gem.description = %Q{Japanese charactor displayed by hiragana, alphabet, or number encode to 2-digit in used "Pokebell" pager.}

に変更します。
続いて rake gemspec を実行します。

$ rake gemspec
Generated: pokebell.gemspec
pokebell.gemspec is valid.

pokebell.gemspec ファイルには

# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'

とありますので、 gemspec ファイルを訂正する場合には Rakefile を直しましょう。ちなみに Rakefile の gem.summary などが **TODO** のままであってもそのまま gemspec ファイルが生成されます。先に直しておきましょう。

rubygems を生成

なお、gemspec ファイルを作成しなくてもrake buildは実行可能です。

$ rake build
  Successfully built RubyGem
  Name: pokebell
  Version: 0.0.1
  File: pokebell-0.0.1.gem
rubygems.org への登録

Edit Profile | RubyGems.org | your community gem host
の最下段の curl コマンドを実行し rubygems.org のパスワードを入力すると、 ~/.gem/credentialsAPI key を設定できます。

$ curl -u riocampos https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
Enter host password for user 'riocampos':
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56    0    56    0     0     23      0 --:--:--  0:00:02 --:--:--    23
リリース☆

rake release 一発で Github へのアップロード(git push origin master)、 gemspec ファイル作成(rake gemspec)、 rubygems 生成(rake build)、 rubygems.org への登録(gem push ./pkg/pokebell-0.0.1.gem)を行ってくれます。

$ rake release
Committing pokebell.gemspec
Pushing master to origin
Generated: pokebell.gemspec
pokebell.gemspec is valid.
  Successfully built RubyGem
  Name: pokebell
  Version: 0.0.1
  File: pokebell-0.0.1.gem
Executing "gem push ./pkg/pokebell-0.0.1.gem":
gem push ./pkg/pokebell-0.0.1.gem
Pushing gem to https://rubygems.org...
Successfully registered gem: pokebell (0.0.1)

YARDの確認

$ yard server --reload

でローカルに YARD サーバが起動します。localhost:8808 で確認できます。

更新作業

$ rake version:bump:patch
$ git commit -a
$ rake release

でOKです。
あとはテストですね…。