新建gem包
$ bundler gem my_plugin
-
Gemfile
描述dependency -
Rakefile
发布和打包的rake tasks -
my_plugin.gemspec
gem的spec
修改gemspec文件
#去除TODO
spec.summary = %q{Write a short summary, because Rubygems requires one.}
spec.description = %q{Write a longer description or delete this line.}
#添加依赖
spec.add_dependency 'activerecord', ['>=3.0', '<5.0']
#配置上传服务器
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
产生GEM包
$ rake build
生成的gem包在pkg文件夹下。
安装GEM包
$ rake install
//将gem包安装到系统gem包中,可以require使用
为GEM包添加方法
//lib/my_plugin.rb
require "my_plugin/version"
module MyPlugin
def self.hello
p "Hello from my_plugin"
end
end
测试使用
$ rake install
require "my_plugin"
MyPlugin.hello
$ bundle console
$ > MyPlugin.hello
发布GEM包
rake release
//输入你在rubygems.org的账号和密码
使用本地gem包
gem 'my_plugin', :path => '~/developer/my_plugin'
等开发完成后,可以将gem包上传到git上。
网友评论