This gem describes the way to build a gem using bundler
-
$ bundle gem lorem_casiano create lorem_casiano/Gemfile create lorem_casiano/Rakefile create lorem_casiano/LICENSE create lorem_casiano/README.md create lorem_casiano/.gitignore create lorem_casiano/lorem_casiano.gemspec create lorem_casiano/lib/lorem_casiano.rb create lorem_casiano/lib/lorem_casiano/version.rb Initializating git repo in /Users/casiano/Dropbox/src/ruby/makingagemwithbundler/lorem_casiano makingagemwithbundler$ cd lorem_casiano/
As it says, it initializes a git repo
-
The lorem_casiano.gemfile file looks like this:
$ cat lorem_casiano.gemspec # -- encoding: utf-8 -- require File.expand_path('../lib/lorem_casiano/version', FILE)
Gem::Specification.new do |gem|
gem.authors = ["Casiano Rodriguez"]
gem.email = ["[email protected]"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "lorem_casiano"
gem.require_paths = ["lib"]
gem.version = LoremCasiano::VERSION
end
-
The version for the project is taken from lib/lorem_casiano/version.rb
$ cat lib/lorem_casiano/version.rb module LoremCasiano VERSION = "0.0.1" end
modify it to "0.0.2"
-
vi lib/lorem_casiano.rb. Introduce method ipsum
-
Fill the TODOs and homepage fields in lorem_casiano.gemspec vi lorem_casiano.gemspec
-
Generate the gem:
$ gem build lorem_casiano.gemspec Successfully built RubyGem Name: lorem_casiano Version: 0.0.2 File: lorem_casiano-0.0.2.gem
which generates a file "lorem_casiano-0.0.2.gem"
-
Push the gem to rubygems.org
$ gem push lorem_casiano-0.0.2.gem Pushing gem to https://rubygems.org... Successfully registered gem: lorem_casiano (0.0.2)
Of course you have to have an account in rubygems.org
-
Lead your browser to "https://rubygems.org/gems/lorem_casiano". Your gem must be allocated there
-
The "Gemfile" file has this contents:
$ cat Gemfile source 'https://rubygems.org'
gemspec
The "gemspec" leads Bundler to use "lorem_casiano.gemspec" to solve the dependencies. This way, there is no need to specify dependencies here.
-
$ bundle Fetching gem metadata from https://rubygems.org/.... Installing diff-lcs (1.1.3) Using lorem_casiano (0.0.3) from source at /Users/casiano/Dropbox/src/ruby/makingagemwithbundler/lorem_casiano Installing rspec-core (2.10.1) Installing rspec-expectations (2.10.0) Installing rspec-mocks (2.10.1) Installing rspec (2.10.0) Using bundler (1.1.3) Your bundle is complete! Use
bundle show [gemname]
to see where a bundled gem is installed. -
A new call to bundle recomputes and install the dependencies:
$ bundle
Fetching gem metadata from https://rubygems.org/....
Installing diff-lcs (1.1.3)
Using lorem_casiano (0.0.3) from source at /Users/casiano/Dropbox/src/ruby/makingagemwithbundler/lorem_casiano
Installing rspec-core (2.10.1)
Installing rspec-expectations (2.10.0)
Installing rspec-mocks (2.10.1)
Installing rspec (2.10.0)
Using bundler (1.1.3)
Your bundle is complete! Use bundle show [gemname]
to see where a bundled gem is installed.
- The Rakefile contains:
$ cat Rakefile #!/usr/bin/env rake require "bundler/gem_tasks"
Thes are the tasks that provides:
$ rake -T rake build # Build lorem_casiano-0.0.3.gem into the pkg directory rake install # Build and install lorem_casiano-0.0.3.gem into system gems rake release # Create tag v0.0.3 and build and push lorem_casiano-0.0.3.gem to Rubygems
-
Let us see the "build" target:
$ rake build lorem_casiano 0.0.3 built to pkg/lorem_casiano-0.0.3.gem
Now we have this structure:
$ tree . ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── lib │ ├── lorem_casiano │ │ └── version.rb │ └── lorem_casiano.rb ├── lorem_casiano-0.0.2.gem ├── lorem_casiano.gemspec └── pkg └── lorem_casiano-0.0.3.gem
-
We can install the gem:
$ rake install lorem_casiano 0.0.3 built to pkg/lorem_casiano-0.0.3.gem lorem_casiano (0.0.3) installed
-
The rake release task creates a tag on GitHub with the version of your gem, push the locally committed files to the master on GitHub and publishes your gem on RubyGems.org. Remember, commit your changes, before run this task.
$git commit -m 'preparing release 0.0.3' -a
Set the remote:
$ git remote rm origin
$ git remote add origin [email protected]:crguezl/buildingagemwithbundler.git
push the changes:
$ git push origin master
Counting objects: 31, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (26/26), done.
Writing objects: 100% (31/31), 5.78 KiB, done.
Total 31 (delta 11), reused 0 (delta 0)
To [email protected]:crguezl/buildingagemwithbundler.git
* [new branch] master -> master
Release:
$ rake release
lorem_casiano 0.0.3 built to pkg/lorem_casiano-0.0.3.gem
Tagged v0.0.3
Pushed git commits and tags
Pushed lorem_casiano 0.0.3 to rubygems.org
TODO: Write usage instructions here
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
##See Also
-
http://railscasts.com/episodes/245-new-gem-with-bundler "New Gem with Bundler" Source code: https://github.com/ryanb/railscasts-episodes/tree/master/episode-245
-
http://no-fucking-idea.com/blog/2012/04/11/building-gem-with-bundler/ "Building Gem With Bundler"
-
Gem::Specification Reference: http://docs.rubygems.org/read/chapter/20