通过 Bundler 发布自己的 gem
2018-07-14 本文已影响97人
CNKCQ
创键 gem
- 生成工程目录
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
- 编辑
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
标记的必须改为你个人的工程信息
(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
其中在不同的系统中执行文件的目录(bindir
)是不同的,我用的 macOS
, 所以使用 bin
目录
- 版本控制 (
VERSION
)
编辑mytest/lib/mytest/version.rb
可以更改版本号
module MyGem
VERSION = '0.0.1'
end
- 执行代码
编辑mytest/lib/mytest/mytest.rb
, 可以在里面添加执行代码
require "mytest/version"
module Mytest
# Your code goes here...
put 'hello my first gem'
end
- 依赖管理
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
- Testing our gem
$ bundle install
获取 RubyGems
账户
- 注册并登陆
RubyGems
账户 - 生成
RubyGems
账户的访问令牌
$ curl -u your-rubygems-username https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
如果 “获取 RubyGems
账户” 没有做,下面发布的过程中会报错:
$ 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)
发布 gem
rake build
build
结束后提交到 git
仓库后执行:
rake release
发布成功, 恭喜你生成了一个自己的 gem