LoremCasiano
This gem describes the way to build a gem using bundler
Tutorial
$ 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. = ["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'
# Specify your gem's dependencies in lorem_casiano.gemspec 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.
Usage
TODO: Write usage instructions here
Contributing
- 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"
http://no-fucking-idea.com/blog/2012/04/11/building-gem-with-bundler/ "Building Gem With Bundler"