📜 ⬆️ ⬇️

Bundler - manager to manage gem'ami

What is a Bundler?


This is a manager for managing gem dependencies in ruby ​​applications. This utility allows you to easily install the necessary gems for your application, while not depending on the installed on the system. If you used Rails for your development, then you will remember how you defined gem dependencies using config.gem in enviroment.rb, the Bundler solves this problem much more conveniently and easily. It was included in Rails 3.0 by default and now it is used to manage gem dependencies in this version of the framework. This utility can be used for any ruby ​​framework.

Installation


Install this utility like any other gem:

gem install bundler

For Rails 3.0, nothing else needs to be done, because it is used by default. And for Rails.2.3.x you need to perform the following steps. C start adding the below code in boot.rb before Rails.boot !:

class Rails::Boot
def run
load_initializer
extend_environment
Rails::Initializer.run(:set_load_path)
end

def extend_environment
Rails::Initializer.class_eval do
old_load = instance_method(:load_environment)
define_method(:load_environment) do
Bundler.require :default, Rails.env
old_load.bind(self).call
end
end
end
end


Next, create the preinitializer.rb file in the config / initializers directory with the contents:
')
begin
# Require the preresolved locked set of gems.
require File.expand_path('../../.bundle/environment', __FILE__)
rescue LoadError
# Fallback on doing the resolve at runtime.
require "rubygems"
require "bundler"
Bundler.setup
end


Configuration


After creating a new Rails application in version 3.0, a Gemfile file already exists in the project root, which is a config for Bundler. For versions of Rails 2.3.x, you need to create it yourself. To do this, go to the project directory and execute the command:

bundle init

In this file all necessary gem dependencies are specified. Let's consider which features this particular file provides.

First, the resource is set from which gems will be installed by default:

source 'http://gemcutter.org'

As many already know, the gemcutter.org resource becomes a kind of standard for storing gems, so when creating a config, this resource will be installed by default. But you can easily replace it with a valid gems.github.com or add as many resources as you need:

source 'http://gemcutter.org'
source 'http://gems.github.com'
source 'http://gems.rubyforge.org'


Next comes the list of gems that are needed for the application to work:

gem 'will_paginate'
gem 'oauth'
gem 'money'


It should be noted here that gems can be combined into groups and then only certain groups can be set:

group :development do
gem 'rspec'
gem 'populator'
gem 'faker'
end

group :production do
gem 'memcache-client'
end


Another form of gem grouping is available:

gem 'rspec', :group => 'development'
gem 'populator', :group => 'development'
gem 'memcache-client', :group => 'production'


By default, all gems are included in the default group.
If you need a specific version of gem, you can set its number:

gem "rack", "1.0.1"
gem "rails", ">=2.3.2"


It is possible to set the name of the file that will be connected when the library is connected by the Bundler. By default, this is the name of the gem, so in most cases you do not need to specify anything. The option that allows you to specify a file name for the connection is called require, it is used in the following way:

gem 'gchartrb', :require => 'google_chart'

If you need to specify the git repository for downloading the gem, then you need to use the git option:

gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git'

Using


As the current set all the necessary gem'y, you need to run the command:

bundle install

This command will solve all dependencies and install the missing gems. Moreover, if you run:

gem list

Then you will not see gems there that were installed using the bundler. All installed gems are in the ~ / .bundler directory. Where he will connect them.
You can view the list of installed gems using the command:

bundle show

If the desired gem is already installed in the system, then a link will be created for it. After each change of the Gemfile file, you need to run the install command. The ~ / .bundler folder is optional, if you want to install gems into another folder, this is easy to do:

bundle install ./vendor/bundler_gems

If you remember, then in the config file it is possible to include all gems in groups, so during installation you can specify which groups of gems not to install:

bundle install —without test

Suppose why install gems on a production server that is only needed for testing.
If gem has executable files, then they can be run as follows:

bundle exec cassandra_helper cassandra

Once you have finished developing the application, you need to block the change to the gemfile:

bundle lock

After executing this command, a Gemfile.lock file will be created, which will contain all dependencies based on the installed gems on your computer. This is done in order to fix versions of gems for which the application works correctly. This file will look like this:

---
dependencies:
faker:
group:
- :development
version: ">= 0"
memcache-client:
group:
- :test
version: ">= 0"
sqlite3-ruby:
group:
- :default
version: ">= 0"
oauth:
group:
- :default
version: ">= 0"
specs:
- stomp:
version: 1.1.4
- populator:
version: 0.2.5
- json:
version: 1.2.0
- thrift:
version: 0.2.0
- thrift_client:
version: 0.3.3
- rspec:
version: 1.3.0
- ruby-hmac:
version: 0.4.0
- oauth:
version: 0.3.6
hash: 0ac3c8666943a1e2294be2851316d83791479451
sources:
- Rubygems:
uri: gemcutter.org


At the same time, if you change the Gemfile and try to execute the install command, the installation will not be performed, since gems are blocked. In order to install new gems in a locked state, the install command is executed with the relockinstall stall --relock parameter, and the command is executed; you need to block the change:

bundle install --relock

The bundler provides the possibility of packing gems:

bundle pack

After executing this command, all necessary gems will be saved in the vendor / cache directory, after which the gems will be installed from this directory. This option will be useful only for those who do not have the ability to install gems from public repositories on the production server.

That's all that I wanted to tell you about this handy utility. Try to work with her for at least an hour and I think you will like her.

Source: https://habr.com/ru/post/85201/


All Articles