pentominoコマンドをgemに公開する最小の手順

前回のpentominoコマンドをgemに公開するまでの手順のメモ。次回公開する時に、悩まず・素早く・苦労最小限で公開できますように!

必要な環境

  • hubコマンドをインストールした環境であること。

gem名を決める

  • 候補の名前をremote検索してみた。
$ gem search -r pentomino

 *** REMOTE GEMS ***
  • pentominoは存在しないようだ。
  • よって、pentominoに決定する。

bundlerでgemの雛形を作る

  • 既存のプロジェクト名をpentominoに変更しておいた。
  • bundle gem pentominoコマンドで雛形を生成した。
    • ちなみに~/Desktop/pentomino_rbは、以前からGitでバージョン管理されている。
    • その状況でbundle gemを実行しても、既存のファイルが破壊されることはなかった。
$ mv ~/Desktop/pentomino_rb ~/Desktop/pentomino
$ cd ~/Desktop
$ bundle gem pentomino
      create  pentomino/Gemfile
      create  pentomino/Rakefile
      create  pentomino/LICENSE.txt
      create  pentomino/README.md
      create  pentomino/.gitignore
      create  pentomino/pentomino.gemspec
      create  pentomino/lib/pentomino.rb
      create  pentomino/lib/pentomino/version.rb
Initializing git repo in /Users/zari/Desktop/pentomino
$ cd pentomino
  • 生成された雛形ファイルはすでにステージング済みなので、そのままコミットしておいた。
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#	new file:   .gitignore
#	new file:   Gemfile
#	new file:   LICENSE.txt
#	new file:   README.md
#	new file:   Rakefile
#	new file:   lib/pentomino.rb
#	new file:   lib/pentomino/version.rb
#	new file:   pentomino.gemspec
#
$ git commit -m 'bundle gem pentominoを実行'

gemspecファイルを編集

  • pentomino.gemspecのTODO項目を編集しておく。


@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
spec.version = Pentomino::VERSION
spec.authors = ["zariganitosh"]
spec.email = ["XXXX@example.com"]
- spec.summary = %q{TODO: Write a short summary. Required.}
- spec.description = %q{TODO: Write a longer description. Optional.}
+ spec.summary = %q{Find answer of pentomino puzzle.}
+ spec.description = %q{Find answer of pentomino puzzle on the board(3x20, 4x15, 5x12, 6x10, 7x9-3, 8x8-4).}
spec.homepage = ""
spec.license = "MIT"

READMEを編集

  • README.mdのTODO項目も編集しておいた。


# Pentomino

-TODO: Write a gem description
+Find answer of pentomino puzzle on the board(3x20, 4x15, 5x12, 6x10, 7x9-3, 8x8-4).

## Installation

@@ -19,9 +19,29 @@ Or install it yourself as:
$ gem install pentomino

## Usage

-TODO: Write usage instructions here
+````
+Usage: pentomino [options] [board size(3-8)]
+ -q Hide progress putting a piece on board.(quiet mode)
+
+board size:
+ [3] x 20
+ [4] x 15
+ [5] x 12
+ [6] x 10 (Default)
+ [7] x 9 - 3
+ [8] x 8 - 4
+
+example:
+ 6 x 10 7 x 9 - 3 8 x 8 - 4
+11 12 13 14 15 16 17 18 19 1A 11 12 13 14 15 16 17 18 19 11 12 13 14 15 16 17 18
+21 22 23 24 25 26 27 28 29 2A 21 22 23 24 25 26 27 28 29 21 22 23 24 25 26 27 28
+31 32 33 34 35 36 37 38 39 3A 31 32 33 34 35 36 37 38 39 31 32 33 34 35 36 37 38
+41 42 43 44 45 46 47 48 49 4A 41 42 43 47 48 49 41 42 43 46 47 48
+51 52 53 54 55 56 57 58 59 5A 51 52 53 54 55 56 57 58 59 51 52 53 56 57 58
+61 62 63 64 65 66 67 68 69 6A 61 62 63 64 65 66 67 68 69 61 62 63 64 65 66 67 68
+ 71 72 73 74 75 76 77 78 79 71 72 73 74 75 76 77 78
+ 81 82 83 84 85 86 87 88
+````

## Contributing

1. Fork it ( https://github.com/[my-github-username]/pentomino/fork )

コマンドファイルを用意

  • binフォルダを作成して、その中にpentomino.rbを入れた。
  • ファイル名をコマンド名と同じpentominoに変更した。
$ mkdir bin
$ mv pentomino.rb bin/pentomino

バージョンファイルを設定

  • lib/pentomino/version.rbのバージョンを設定しておく。
    • デフォルトは"0.0.1"が設定されている。
module Pentomino
  VERSION = "0.0.1"
end

Githubに公開しておく

  • Githubにも公開しておくことにした。
$ hub create
Updating origin
created repository: zarigani/pentomino
$ hub browse
  • pentomino.gemspecのspec.homepageに上記GithubのURLを追記しておいた。


@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ["XXXX@example.com"]
spec.summary = %q{Find answer of pentomino puzzle.}
spec.description = %q{Find answer of pentomino puzzle on the board(3x20, 4x15, 5x12, 6x10, 7x9-3, 8x8-4).}
- spec.homepage = ""
+ spec.homepage = "https://github.com/zarigani/pentomino"
spec.license = "MIT"

  • ここまでのgemの設定を一旦コミット。
$ git add .
$ git commit -m 'gemの設定'
  • Githubに最初のpushをしておく。
$ git push -u origin master
Counting objects: 46, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (32/32), done.
Writing objects: 100% (46/46), 8.90 KiB | 0 bytes/s, done.
Total 46 (delta 11), reused 0 (delta 0)
To git@github.com:zarigani/pentomino.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
  • これ以降のpushはgit pushのみでOK。

ローカルのgem環境にインストールして動作確認

  • bundlerのrakeタスクを利用すると便利。
$ rake -T
rake build    # Build pentomino-0.0.1.gem into the pkg directory
rake install  # Build and install pentomino-0.0.1.gem into system gems
rake release  # Create tag v0.0.1 and build and push pentomino-0.0.1.gem to Rubygems
  • gemをパッケージングして、インストールしてみる。
$ sudo rake install
 ** Invoke install (first_time)
 ** Invoke build (first_time)
 ** Execute build
pentomino 0.0.1 built to pkg/pentomino-0.0.1.gem.
 ** Execute install
pentomino (0.0.1) installed.
  • gemがインストールされていることを確認。
  • pentominoコマンドが使えることを確認。
$ gem list pentomino

 *** LOCAL GEMS ***

pentomino (0.0.1)

$ pentomino -h
Usage: pentomino [options] [board size(3-8)]
    -q                               Hide progress putting a piece on board.(quiet mode)

board size:
    [3] x 20
    [4] x 15
    [5] x 12
    [6] x 10 (Default)
    [7] x  9 - 3
    [8] x  8 - 4

example:
            6 x 10                        7 x 9 - 3                   8 x 8 - 4
11 12 13 14 15 16 17 18 19 1A    11 12 13 14 15 16 17 18 19    11 12 13 14 15 16 17 18
21 22 23 24 25 26 27 28 29 2A    21 22 23 24 25 26 27 28 29    21 22 23 24 25 26 27 28
31 32 33 34 35 36 37 38 39 3A    31 32 33 34 35 36 37 38 39    31 32 33 34 35 36 37 38
41 42 43 44 45 46 47 48 49 4A    41 42 43          47 48 49    41 42 43       46 47 48
51 52 53 54 55 56 57 58 59 5A    51 52 53 54 55 56 57 58 59    51 52 53       56 57 58
61 62 63 64 65 66 67 68 69 6A    61 62 63 64 65 66 67 68 69    61 62 63 64 65 66 67 68
                                 71 72 73 74 75 76 77 78 79    71 72 73 74 75 76 77 78
                                                               81 82 83 84 85 86 87 88

$ pentomino
Pentomino 6x10

No. 1 (TRY: 22902)
...中略...

問題ないことを確認した。

gemに公開

  • rakeタスクのreleaseで素早く公開する。
$ rake release
pentomino 0.0.1 built to pkg/pentomino-0.0.1.gem.
Tagged v0.0.1.
Pushed git commits and tags.
Pushed pentomino 0.0.1 to rubygems.org.
  • タスクの内容は以下のとおり。
    • gemパッケージをbuildして、
    • 現在のバージョンを元に、タグ v0.0.1 を自動的に設定してくれた。
    • Githubへのpush、
    • rubygems.orgへのpush。(gemの公開)

確認

  • 公開完了した。
$ gem search pentomino

 *** REMOTE GEMS ***

pentomino (0.0.1)
  • インストール。
$ sudo gem install pentomino
Fetching: pentomino-0.0.1.gem (100%)
Successfully installed pentomino-0.0.1
Parsing documentation for pentomino-0.0.1
Installing ri documentation for pentomino-0.0.1
1 gem installed

$ gem list pentomino

 *** LOCAL GEMS ***

pentomino (0.0.1)

これで素早くpentominoコマンドが使えるようになった!