为了获得良好的开端,您可以使用bundle gem
命令和rspec --init
.
~/code $ bundle gem my_lib
create my_lib/Gemfile
create my_lib/Rakefile
create my_lib/LICENSE.txt
create my_lib/README.md
create my_lib/.gitignore
create my_lib/my_lib.gemspec
create my_lib/lib/my_lib.rb
create my_lib/lib/my_lib/version.rb
Initializating git repo in /Users/john/code/my_lib
~/code $ cd my_lib/
~/code/my_lib $ git commit -m "Empty project"
~/code/my_lib $ rspec --init
The --configure option no longer needs any arguments, so true was ignored.
create spec/spec_helper.rb
create .rspec
- 代码进入
lib
- 规格进入
spec
- 测试数据或文件进入
spec/fixtures/
- 要求所有 ruby 文件在
lib/my_lib.rb
. 您也可以在该文件中或在他们自己的文件中定义您的例外——根据您自己的偏好。
- C源文件进入
ext/my_lib
- shell脚本和可执行文件进入
bin
如有疑问,请查看其他宝石的布局方式。
更多信息:
您应该在 gemspec 中添加 rspec 作为开发依赖项,以使其他开发人员更容易
- 编辑 my_lib.gemspec,在底部附近添加
gem.add_development_dependency 'rspec'
和。gem.add_development_dependency 'rake'
- 添加
Bundler.setup
和require 'my_lib'
到 spec/spec_helper.rb 的顶部,以确保在运行规范时加载您的 gem 依赖项。
require "rspec/core/rake_task"
将和添加task :default => :spec
到您的 Rakefile,以便运行rake
将运行您的规范。
在您处理最新创建时,guard-rspec可以通过在文件更改时自动运行您的规范,提醒您规范失败,从而节省您的时间和麻烦。
~/code/my_lib $ git add spec/spec_helper.rb
~/code/my_lib $ git commit -am "Add RSpec"
~/code/my_lib $ vim my_lib.gemspec # add guard development dependency
~/code/my_lib $ bundle
~/code/my_lib $ bundle exec guard init
~/code/my_lib $ vim Guardfile # Remove the sections below the top one
~/code/my_lib $ git add Guardfile
~/code/my_lib $ git commit -am "Add Guard"
在你对你的创作感到满意之后,将它推送到 github
# create a github repository for your gem, then push it up
~/code/my_lib $ curl -u myusername https://api.github.com/user/repos -d '{"name":"my_lib"}'
~/code/my_lib $ git remote add origin git@github.com:myusername/my_lib.git
~/code/my_lib $ git push
然后,当您准备好在 Rubygems.org 上发布您的 gem 时,运行rake release
,它将引导您完成这些步骤。
~/code/my_lib $ rake release
更多参考资料