Leon's Blogging

Coding blogging for hackers.

Make You Own Gem

| Comments

bundle 本身就有提供可以產生 gem 相關檔案的功能

1
2
3
4
5
6
7
8
9
10
11
bundle gem xxx --mit -t=rspec
#後面可以設定參數,ex: 自動產生 LICENSE.txt, 測試用 rspec or mintest 等等

#xxx/Gemfile                  <= dependency 哪些套件,但基本上裡面是指定到 xxx.gemspec,將相依的套件定義在 xxx.gemspec 即可
#xxx/Rakefile                 <= 發佈和打包的 rake tasks
#xxx/LICENSE.txt              <= 註明 License
#xxx/README.md                <= 說明如何使用它(github上會顯示)
#xxx/.gitignore               <= 不要進 Git 的檔案在這定義
#xxx/xxx.gemspec             <= gem 的 spec
#xxx/lib/xxx.rb              <= gem 裡的 library,主要 code 都是在這裡
#xxx/lib/xxx/version.rb      <= 版本紀錄

xxx.gemspec gem 裡面的基本設定都會在這邊,需要寫的都會註明 TODO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'xxx/version'

Gem::Specification.new do |spec|
  spec.name          = "xxx"
  spec.version       = Xxx::VERSION
  spec.authors       = ["TODO: Write your name"]
  spec.email         = ["TODO: Write your email address"]

  spec.summary       = %q{TODO: Write a short summary, because Rubygems requires one.}
  spec.description   = %q{TODO: Write a longer description or delete this line.}
  spec.homepage      = "TODO: Put your gem's website or public repo URL here."
  spec.license       = "MIT"

  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
  # delete this section to allow pushing this gem to any host.
  if spec.respond_to?(:metadata)
    spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
  else
    raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
  end

  spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.11"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "rspec", "~> 3.0"
end

Code

xxx/lib/xxx.rb

1
2
3
4
5
6
7
8
require "xxx/version"

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

Build

1
2
3
4
5
6
7
gem build xxx.gemspec
#目錄底下會多出此檔 xxx-0.0.1.gem

Successfully built RubyGem
Name: xxx
Version: 0.1.0
File: xxx-0.1.0.gem

Install

1
2
3
4
5
6
7
8
gem install xxx

$ irb
require 'xxx'
#=> true

Xxx.hi
#=>Hello, world!

發佈到 RubyGems.org

  • gem update –system
  • 申請帳號 rubygems
  • 發佈
1
2
gem push xxx-0.0.1.gem
#push 後會要輸入你剛剛的申請的帳號密碼,就ok囉

command-reference

command-reference

參考文件:

Comments