Monday 25 November 2013

Ruby : Make and Publish Gem with bundler

What is Ruby Gem ?

A gem is a packaged Ruby application or library. RubyGems is the standard way to distribute Ruby applications and libraries and is available to you after you have downloaded and installed Ruby.

We can install gem with
gem install gem_name
How to create Gem ?

Lets move one step ahead and start building our first new gem. As we are going to publish it later, lets check availability of gem name on rubygems.com

1) Suppose we want to create gem with name  "test_gem". Type  below command to your terminal and wait for result.
gem list -r test_gem
We will get below result.

 *** REMOTE GEMS ***
test_gem (0.0.3)
test_gem_AF (0.0.0)
test_gem_eer (0.0.1)
test_gem_mjn (0.0.1)
test_gem_sup (0.0.0)
test_gem_Thomas (0.0.0)
test_gemeroni (1.0.0)

That mean our gem name is already been taken So lets try some thing unique.

gem list -r test_gem_ketan
We will get below result.

*** REMOTE GEMS ***



If we did not get any gem in list then our name is unique and we can continue with this name to build our gem.

Until now we just check if our gem name is available or not. Now lets start to create gem "test_gem_ketan"

2) Type below command to create sample template of out gem with bundler.
Remove test_gem_ketan with test_gem_yourname if you want gem with your name. But then dont forget to update below command as per your name.
 bundle gem test_gem_ketan
We will get below result.

      create  test_gem_ketan/Gemfile
      create  test_gem_ketan/Rakefile
      create  test_gem_ketan/LICENSE.txt
      create  test_gem_ketan/README.md
      create  test_gem_ketan/.gitignore
      create  test_gem_ketan/test_gem_ketan.gemspec
      create  test_gem_ketan/lib/test_gem_ketan.rb
      create  test_gem_ketan/lib/test_gem_ketan/version.rb
Initializating git repo in /home/webonise/StudyProjects/gems/test_gem_ketan

This will generate the basic skeleton of our gem. Go inside the gem folder with
cd test_gem_ketan
3) Lets have look on important files of gem

test_gem_ketan.gemspec

Gemspec file sets the specification of gem. The gemspec defines what’s in the gem, who made it, and the version of the gem. It’s also your interface to RubyGems.org. All of the information you see on a gem page (like jekyll’s) comes from the gemspec.

Type below command and lets have look on gemspec file.
cat test_gem_ketan.gemspec
We will get below result

# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'test_gem_ketan/version'

Gem::Specification.new do |spec|
  spec.name          = "test_gem_ketan"
  spec.version       = TestGemKetan::VERSION
  spec.authors       = ["Ketan Ghumatkar"]
  spec.email         = ["ketanghumatkar@gmail.com"]
  spec.description   = %q{TODO: Write a gem description}
  spec.summary       = %q{TODO: Write a gem summary}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = `git ls-files`.split($/)
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

Replace  spec.description and spec.summary  with description and summary.
In our case, it will be


  spec.description   = %q{Test Gem Description}
  spec.summary       = %q{Test Gem Summary}


Version specifies the  current version of gem. We have create separate file to maintain version
cat lib/test_gem_ketan/version.rb
We will have result

module TestGemKetan
  VERSION = "0.0.1"
end

Now the last and most important file is test_gem_ketan.rb
Here you can write the gem code. lets simple print "something" to test it.

require "test_gem_ketan/version"
module TestGemKetan
  # Your code goes here...
  puts "Well Done. First Gem created !!!"
end

4) Now We will build gem with single command
gem build test_gem_ketan.gemspec
We will get result.

WARNING:  no homepage specified
  Successfully built RubyGem
  Name: test_gem_ketan
  Version: 0.0.1
  File: test_gem_ketan-0.0.1.gem

 5) Then we can install the generated gem locally to test it out.
gem install test_gem_ketan-0.0.1.gem


Successfully installed test_gem_ketan-0.0.1
1 gem installed
Installing ri documentation for test_gem_ketan-0.0.1...
Building YARD (yri) index for test_gem_ketan-0.0.1...
Installing RDoc documentation for test_gem_ketan-0.0.1...

6) Lets test it.


 irb                                                                  
irb(main):001:0> require 'test_gem_ketan'
Well Done. First Gem created !!!
=> true
irb(main):002:0> exit

yeee... it works

7) Now its time to publish our gem to rubygems.org

Create account at rubygems or use already existing account to push gem
 gem push test_gem_ketan-0.0.1.gem
Enter your RubyGems.org credentials.
Don't have an account yet? Create one at http://rubygems.org/sign_up
   Email:   WriteYourEmail
Password:   WriteYourPassword
Pushing gem to https://rubygems.org...
Signed in.
Pushing gem to https://rubygems.org...
Successfully registered gem: test_gem_ketan (0.0.1)

 Now check is it publish ??
gem list -r test_gem_ketan 
We will get result.

*** REMOTE GEMS ***
test_gem_ketan (0.0.1)


Cong rates. This means we have created and publish our first gem successfully.

Deleting  gem at Rubygems.org

Since our gem is for demonstration purposes only, I would like to delete my gem from RubyGems.org’s index.

Install gemcutter as below:
gem install gemcutter

Then to remove gem type
gem yank test_gem_ketan -v 0.0.1 

Yanking gem from RubyGems.org...
Pushing gem to https://rubygems.org...
Successfully yanked gem: test_gem_ketan (0.0.1)


However, the gem is still available for download for two main reasons:
  1. Other gems may have been pushed that depend on your gem.
  2. A mass deletion of important community based gems can be easily prevented.
That’s it. Thank you.

No comments:

Post a Comment