Bundler & Geminabox

fabio_pitino@symantec.com

Bundler

# installation
gem install bundler

# setup
bundle init

Gemfile

source 'https://rubygems.org'

# always looks for the newest version
gem 'nokogiri'

# specific version
gem 'rails', '3.0.0.beta3'

# specific range: >, >=, <, <=
gem 'rack',  '>=1.0'

# considers any 1.1.* versions
gem 'thin',  '~>1.1'

# when lib name differs from gem name
gem 'rack-cache', :require => 'rack/cache'

# install but don't require
gem 'rubocop', :require => false

# associate gem to group(s)
gem 'debugger', :group => [:development, :test]

group :test do
  gem 'minitest'
end

Bundler commands

# install all gems in Gemfile
bundle install

# update all gems in Gemfile
bundle update

# update specific gem
bundle update minitest

Using the gems

# file: ~/proj/app.rb
require 'rubygems'
require 'bundler/setup'

Bundler.require(:default, :development)

# Bundler.require(:default, ENV['RAILS_ENV'].to_sym)

require 'gem-outside-gemfile'

Let's create a gem!

bundle gem greeting --test=minitest

Creating gem 'greeting'...
      create  greeting/Gemfile
      create  greeting/.gitignore
      create  greeting/lib/greeting.rb
      create  greeting/lib/greeting/version.rb
      create  greeting/greeting.gemspec
      create  greeting/Rakefile
      create  greeting/README.md
      create  greeting/bin/console
      create  greeting/bin/setup
      create  greeting/.travis.yml
      create  greeting/test/test_helper.rb
      create  greeting/test/greeting_test.rb


inside the gemspec

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

Gem::Specification.new do |spec|
  spec.name          = "greeting"
  spec.version       = Greeting::VERSION
  spec.authors       = ["Fabio Pitino"]
  spec.email         = ["pitinofabio@gmail.com"]

  spec.summary       = %q{TODO: Write a short summary, because Rubygems requires one.}
  spec.description   = %q{TODO: Write a longer description or delete this line.}
  spec.homepage      = "TODO: Put your gem's website or public repo URL here."

  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
  # delete this section to allow pushing this gem to any host.
  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

  spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.11"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "minitest", "~> 5.0"
end

publishing

# package the gem
gem build greeting.gemspec

# after registering to rubygems.org
# we can publish it
gem push greeting-0.1.0.gem

Geminabox

gem install geminabox

setup Geminabox

# file: ~/gemserver/config.ru
require "rubygems"
require "geminabox"

# specify the directory where to store the gems
Geminabox.data = "./data"

# set to true if want to store gems from rubygems.org
# when not found locally
Geminabox.rubygems_proxy = true

run Geminabox::Server

start gemserver

# start gemserver on port 9292
rackup

#=> Thin web server (v1.6.4 codename Gob Bluth)
#=> Maximum connections set to 1024
#=> Listening on localhost:9292, CTRL+C to stop

back to greeting.gemspec

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

Gem::Specification.new do |spec|
  spec.name          = "greeting"
  spec.version       = Greeting::VERSION
  spec.authors       = ["Fabio Pitino"]
  spec.email         = ["pitinofabio@gmail.com"]

  spec.summary       = %q{Greeting fun gem.}
  spec.description   = %q{A test gem that says hello.}
  spec.homepage      = "http://greeting-gem.com"

  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
  # delete this section to allow pushing this gem to any host.
  if spec.respond_to?(:metadata)
    spec.metadata['allowed_push_host'] = "http://gems.company.com:9292"
  else
    raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
  end

  spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.11"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "minitest", "~> 5.0"
end

publishing to Geminabox

# package the gem
gem build greeting.gemspec

# push to internal gemserver
gem inabox greeting-0.1.0.gem --host http://gems.company.com:9292

# or the standard 'gem push' command
gem push greeting-0.1.0.gem --host http://gems.company.com:9292

# NOTE: using 'gem push' still requires 'geminabox' to be
# installed on the build environment

Eat your own dog food!

# file: ~/greeting_project/Gemfile

source 'https://rubygems.org'
gem 'rest-client'
gem 'json'

source 'http://gems.company.com:9292' do
  gem 'greeting', '0.1.0'
end
gem install greeting --source http://gems.company.com:9292

Bundler & Geminabox

By Fabio Pitino

Bundler & Geminabox

How to use Bundler and Gemfile for dependency management in a Ruby project. Create and deploy Gems to a private Gem server

  • 1,150