通過 Bundler 發(fā)布自己的 gem

創(chuàng)鍵 gem

  1. 生成工程目錄
bundle gem mytest
Creating gem 'mytest'...
MIT License enabled in config
Code of conduct enabled in config
      create  mytest/Gemfile
      create  mytest/lib/mytest.rb
      create  mytest/lib/mytest/version.rb
      create  mytest/mytest.gemspec
      create  mytest/Rakefile
      create  mytest/README.md
      create  mytest/bin/console
      create  mytest/bin/setup
      create  mytest/.gitignore
      create  mytest/LICENSE.txt
      create  mytest/CODE_OF_CONDUCT.md
Initializing git repo in /Users/steve/Desktop/mytest
  1. 編輯 mytest.gemspec

(1)origin:


lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "mytest/version"

Gem::Specification.new do |spec|
  spec.name          = "mytest"
  spec.version       = Mytest::VERSION
  spec.authors       = ["steve"]
  spec.email         = ["chengquan.wang@ele.me"]

  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. To allow pushes either set the 'allowed_push_host'
  # to allow pushing to a single host or delete this section to allow pushing 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 do |f|
    f.match(%r{^(test|spec|features)/})
  end
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.16"
  spec.add_development_dependency "rake", "~> 10.0"
end

其中包含 TODO || FIXME 標(biāo)記的必須改為你個人的工程信息
(2)modified:


lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "mytest/version"

Gem::Specification.new do |spec|
  spec.name          = "mytest"
  spec.version       = Mytest::VERSION
  spec.authors       = ["CNKCQ"]
  spec.email         = ["chengquan.personal@gmail.com"]

  spec.summary       = %q{happy setting}
  spec.description   = %q{effective command for boot setting}
  spec.homepage      = "https://github.com/CNKCQ/mytest.git"
  spec.license       = "MIT"

  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
  # to allow pushing to a single host or delete this section to allow pushing to any host.
  if spec.respond_to?(:metadata)
    spec.metadata["allowed_push_host"] = "https://rubygems.org"
  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 do |f|
    f.match(%r{^(test|spec|features)/})
  end
  # Mac os  bindir = "bin"  Windows  bindir = "exe"
  spec.bindir        = "bin"
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_runtime_dependency "colorize"

  spec.add_development_dependency "bundler", "~> 1.16"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "colorize"
  spec.add_development_dependency "rspec", "~> 3.2"
end

其中在不同的系統(tǒng)中執(zhí)行文件的目錄(bindir)是不同的当窗,我用的 macOS, 所以使用 bin 目錄

  1. 版本控制 (VERSION)
    編輯 mytest/lib/mytest/version.rb 可以更改版本號
module MyGem
 VERSION = '0.0.1'
end
  1. 執(zhí)行代碼
    編輯 mytest/lib/mytest/mytest.rb , 可以在里面添加執(zhí)行代碼
require "mytest/version"

module Mytest
  # Your code goes here...
  put 'hello my first gem'
end
  1. 依賴管理 Gemfile
    編輯 mytest/Gemfile , 可以在這里進行依賴管理
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in mytest.gemspec
gemspec
  1. Testing our gem
$ bundle install

獲取 RubyGems 賬戶

  1. 注冊并登陸 RubyGems 賬戶
  2. 生成 RubyGems 賬戶的訪問令牌
$ curl -u your-rubygems-username https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials

如果 “獲取 RubyGems 賬戶” 沒有做恩袱,下面發(fā)布的過程中會報錯:

$ rake release
mytest 0.0.1 built to pkg/mytest-0.0.1.gem.
Tag v0.0.1 has been created.
rake aborted!
Your rubygems.org credentials aren't set. Run `gem push` to set them.

Tasks: TOP => release
(See full trace by running task with --trace)

發(fā)布 gem

rake build

build 結(jié)束后提交到 git 倉庫后執(zhí)行:

rake release

發(fā)布成功, 恭喜你生成了一個自己的 gem

Reference

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末哨苛,一起剝皮案震驚了整個濱河市罐盔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌示绊,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暂论,死亡現(xiàn)場離奇詭異面褐,居然都是意外死亡,警方通過查閱死者的電腦和手機取胎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門展哭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人闻蛀,你說我怎么就攤上這事匪傍。” “怎么了觉痛?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵役衡,是天一觀的道長。 經(jīng)常有香客問我薪棒,道長手蝎,這世上最難降的妖魔是什么榕莺? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮柑船,結(jié)果婚禮上帽撑,老公的妹妹穿的比我還像新娘。我一直安慰自己鞍时,他們只是感情好亏拉,可當(dāng)我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著逆巍,像睡著了一般及塘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锐极,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天笙僚,我揣著相機與錄音,去河邊找鬼灵再。 笑死肋层,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的翎迁。 我是一名探鬼主播栋猖,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼汪榔!你這毒婦竟也來了蒲拉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤痴腌,失蹤者是張志新(化名)和其女友劉穎雌团,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體士聪,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡锦援,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了剥悟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片灵寺。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖懦胞,靈堂內(nèi)的尸體忽然破棺而出替久,到底是詐尸還是另有隱情凉泄,我是刑警寧澤躏尉,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站后众,受9級特大地震影響胀糜,放射性物質(zhì)發(fā)生泄漏颅拦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一教藻、第九天 我趴在偏房一處隱蔽的房頂上張望距帅。 院中可真熱鬧,春花似錦括堤、人聲如沸碌秸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽讥电。三九已至,卻和暖如春轧抗,著一層夾襖步出監(jiān)牢的瞬間恩敌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工横媚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留纠炮,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓灯蝴,卻偏偏與公主長得像恢口,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子绽乔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容