Ruby bundle

来源:互联网 发布:sqlserver怎么下载 编辑:程序博客网 时间:2024/06/18 05:53

Bundle介绍:

Rails 3中引入Bundle来管理项目中所有gem依赖,该命令只能在一个含有Gemfile的目录下执行,如rails 3项目的根目录。


关于Gemfile和Gemfile.lock

所有Ruby项目的信赖包都在Gemfile中进行配置,不再像以往那样,通过require来查找。Rails 3中如果需要require某个gem包,必须通过修改Gemfile文件来管理。

Gemfile.lock则用来记录本机目前所有依赖的Ruby Gems及其版本。所以强烈建议将该文件放入版本控制器,从而保证大家基于同一环境下工作。


Bundle命令详解:


# 显示所有的依赖包
$ bundle show


# 显示指定gem包的安装位置
$ bundle show [gemname]


# 检查系统中缺少那些项目以来的gem包
# 注:如果系统中存在所有项目以来的包,则会输出:The Gemfile's dependencies are satisfied
$ bundle check


# 安装项目依赖的所有gem包
# 注:此命令会尝试更新系统中已存在的gem包
$ bundle install


# 安装指定的gem包
$ bundle install [gemname]


# 更新系统中存在的项目依赖包,并同时更新项目Gemfile.lock文件
$ bundle update


# 更新系统中指定的gem包信息,并同时更新项目Gemfile.lock中指定的包信息
$ bundle update [gemname]

 

# 向项目中添加新的gem包引用
$ gem [gemname], [ver]


# 你还可以指定包依赖关系
$ gem [gemname], :require => [dependence_gemname]


# 你甚至还可以指定gem包的git源
$ gem [gemname], :git => [git_source_url]


# 锁定当前环境
# 可以使用bundle lock来锁定当前环境,这样便不能通过bundle update来更新依赖包的版本,保证了统一的环境
$ bundle lock


# 解除锁定
$ bundle unlock


# 打包当装环境
# bundle package会把当前所有信赖的包都放到 ./vendor/cache/ 目录下,发布时可用来保证包版本的一致性。
$ bundle package



Automating bundle exec

Bundler is a program for installing the libraries (i.e. gems)that a Ruby application depends on, in a sandbox. By specifying anapplication's dependencies with Bundler developers can guarantee thatonly specific versions of the dependencies are used when the applicationis running on a user's computer, or when deployed to a web server.

Quick instructions

Before I get into the nitty gritty of how it works, here are the quickinstallation instructions:

$ curl -L https://github.com/gma/bundler-exec/raw/master/bundler-exec.sh > ~/.bundler-exec.sh$ echo "[ -f ~/.bundler-exec.sh ] && source ~/.bundler-exec.sh" >> ~/.bashrc

Read more on the GitHub page.

How bundler-exec works

Once bundler-exec is configured, it replaces a bunch of common Rubycommands (e.g.rake, ruby, rails, rspec) with a shell functionthat will:

  1. Check whether your current directory is within a bundled project (itchecks for aGemfile in your current directory, or one of itsparents), and
  2. Run the command you entered, prefixing it with bundle exec ifappropriate.

It does this with the magic of shell aliases (and therefore only workswith Bourne style shells, or shells that support thealias command).Browse the code on GitHub; it's very simple stuff. The full listof commands that it looks after for you is currently:

  • cap
  • capify
  • cucumber
  • heroku
  • rackup
  • rails
  • rake
  • rspec
  • ruby
  • shotgun
  • spec
  • spork
  • thin
  • unicorn
  • unicorn_rails

You can easily override that list by setting BUNDLED_COMMANDS in your~/.bashrc file before~/.bundler-exec.sh gets loaded, like this:

BUNDLED_COMMANDS="ruby rails"[ -f ~/.bundler-exec.sh ] && source ~/.bundler-exec.sh

If you need to add commands that are in common use, please fork theproject, add them to the script, and send me a pull request. Cheers!

To check whether bundler-exec is configured, check what your shellwill do if you run Ruby. It should tell you that Ruby has been aliased,like this:

$ type rubyruby is aliased to `run-with-bundler ruby'

What can go wrong if you forget bundle exec?

Remembering to run bundle exec is a pain, can be easy to forget, andno end of subtle bugs can occur if you forget it.

Imagine that your project requires version 1.0.0 of a gem, but you'vegot both 1.0.0 and 1.0.1 installed. It all goes south like this:

  • Your shell will find the command that shipped with 1.0.1, and run it.
  • The command will load your application code, which callsBundler.require orBundler.setup.
  • Bundler (quite rightly) complains when it tries to load 1.0.0 andfinds that 1.0.1 is "already activated").

原创粉丝点击