Jex’s Note

Ruby 建立自己的 Gem 套件

建立 gem 的基本結構

jex@virtualBox:/tmp$ bundle gem exgem
      create  exgem/Gemfile                    <= 描述 dependency
      create  exgem/Rakefile                   <= 發佈和打包的 rake tasks
      create  exgem/LICENSE.txt                <= 註明 License
      create  exgem/README.md                  <= 簡單說明如何使用它
      create  exgem/.gitignore                 <= 不要進 Git 的檔案在這定義
      create  exgem/exgem.gemspec             <= gem 的 spec
      create  exgem/lib/exgem.rb              <= gem 裡的 library
      create  exgem/lib/exgem/version.rb      <= 版本紀錄
Initializating git repo in /tmp/exgem

首先看一下 Gemfile

exgem/Gemfile :

source 'https://rubygems.org'

# Specify your gem's dependencies in exgem.gemspec
gemspec

最後一行要你定義相依套件在 exgem.gemspec

開啟 GemSpec

exgem/exgem.gemspec :

裡面的內容大部份都已填好了, 所以我們改一點使它有變化

spec.description   = %q{Description : This is my gem for example.}
spec.summary       = %q{Summary : This is my gem for example.}

撰寫你的 lib

exgem/lib/exgem.rb :

加上 hi 這個 method :

require "exgem/version"

module Exgem
  # Your code goes here...
  def self.hi
    puts "Hello, world!"
  end
end

寫完之後 build

gem build exgem.gemspec
WARNING:  no homepage specified
  Successfully built RubyGem
  Name: exgem
  Version: 0.0.1
  File: exgem-0.0.1.gem
  • 出現提示 WARNING: no homepage specified 可以忽略它,
  • 因為在 exgem.gemspec 裡我們沒有定義 spec.homepage, 它是一個 URI
  • 目錄底下會多出此檔 exgem-0.0.1.gem

install 這個 package

在安裝之前你都還不能 require 它, 會噴錯誤

$ irb
2.0.0-p247 :001 > require 'exgem'
LoadError: cannot load such file -- exgem
        from /Users/apple/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /Users/apple/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from (irb):1
        from /Users/apple/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'

所以要先安裝(需要 sudo 權限)

$ sudo gem install exgem-0.0.1.gem
Password:
Successfully installed exgem-0.0.1
1 gem installed

就可以 require 了

$ irb
2.0.0p247 :001 > require 'exgem'
 => true
2.0.0p247 :002 > Exgem.hi
Hello, world!
 => nil
  • 像我習慣用 tmux 分頁切來切去, 如果一 TAB 執行 gem install exgem-0.0.1.gem, 另一 TAB 執行 irb 可能會造成 require exgem 失敗
  • 最好是 irb 離開再重進一次, 才抓得到新的 gem package

發佈在 RubyGems.org

[1] 更新

gem update --system

[2] 申請一組帳號

handle 是 username 的意思

[3] 發佈到 RubyGems.org

執行

$ gem push exgem-0.0.1.gem
Pushing gem to https://rubygems.org...
Successfully registered gem: exgem (0.0.1)

官網就可以看到剛剛發佈的 gem 了 gem.JPG

gem 的相關指令

刪除

$ sudo gem uninstall exgem
Successfully uninstalled exgem-0.0.1

Q & A


如果發生錯誤

ERROR:  While executing gem ... (RuntimeError)
    Can't find 'rubygems-update' in any repo. Check `gem source list`.

解決方法

sudo gem sources -a http://s3.amazonaws.com/production.s3.rubygems.org/
http://s3.amazonaws.com/production.s3.rubygems.org/ added to sources

如果發生

gem push exgem-0.0.1.gem
Pushing gem to https://rubygems.org...
Repushing of gem versions is not allowed.
Please use `gem yank` to remove bad gem releases.

修改你的 lib/exgem/version.rb, 將版本從 0.0.1 改成 0.0.2

$ gem build exgem.gemspec
WARNING:  no homepage specified
  Successfully built RubyGem
  Name: exgem
  Version: 0.0.2
  File: exgem-0.0.2.gem

會生成 exgem-0.0.2.gem, 而舊的 exgem-0.0.1.gem 不會消失


如果發佈發生此錯誤的話表示 gem 的名稱跟別人重覆了

$ sudo gem push exgem-0.0.2.gem
Password:
Pushing gem to https://rubygems.org...
You do not have permission to push to this gem.

ref : http://blog.xdite.net/posts/2012/01/04/how-to-pack-a-gem http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices/ http://guides.rubygems.org/make-your-own-gem/#first-gem http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/ http://i3zhe.github.io/blog/2012/10/29/ru-he-zhi-zuo-zi-ji-de-gem/

Comments