bundle exec rake是什么意思?

时间:2022-02-26 00:21:03

What does bundle exec rake db:migrate mean? Or just bundle exec rake <command> in general?

bundle exec rake是什么意思?或者只是打包exec rake ?

I understand that bundle takes care of maintaining things in the Gemfile. I know what the word "exec" means. I understand that rake maintains all the different scripty things you can do, and I know that db:migrate is one of those. I just don't know what all these words are doing together. Why should bundle be used to execute rake to execute a database migrate?

我知道bundle负责维护Gemfile中的东西。我知道exec这个词是什么意思。我理解rake维护了您可以做的所有不同脚本的事情,我也知道db:migrate就是其中之一。我只是不知道这些词在一起做什么。为什么应该使用bundle来执行rake来执行数据库迁移?

7 个解决方案

#1


418  

bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined.

bundle exec是Bundler命令,用于在当前bundle(目录Gemfile中的Gemfile)上下文中执行脚本。rake db: migration is the script where db is the namespace and migration is the task name defined。

So bundle exec rake db:migrate executes the rake script with the command db:migrate in the context of the current bundle.

因此,bundle exec rake db: migration使用命令db:migrate在当前bundle的上下文中执行rake脚本。

As to the "why?" I'll quote from the bundler page:

的“为什么?”我将引用bundler页面的话:

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

在某些情况下,如果可执行文件恰好安装在您的系统中,并且不引入任何与您的包冲突的gem,那么在没有bundle exec的情况下运行可执行文件可能是有效的。

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

然而,这是不可靠的,而且是相当痛苦的根源。即使它看起来是可行的,它也可能不会在将来或在另一台机器上工作。

#2


131  

You're running bundle exec on a program. The program's creators wrote it when certain versions of gems were available. The program Gemfile specifies the versions of the gems the creators decided to use. That is, the script was made to run correctly against these gem versions.

你在一个程序上运行bundle exec。当某些版本的gem可用时,程序的创建者就会编写它。程序Gemfile指定创建者决定使用的gem的版本。也就是说,该脚本是针对这些gem版本正确运行的。

Your system-wide Gemfile may differ from this Gemfile. You may have newer or older gems with which this script doesn't play nice. This difference in versions can give you weird errors.

系统范围的Gemfile可能与此Gemfile不同。您可能有更新的或旧的宝石,这个脚本不能很好地发挥作用。版本之间的差异会给您带来奇怪的错误。

Bundle exec helps you avoid these errors. It executes the script using the gems specified in the script's Gemfile rather than the systemwide Gemfile. It executes the certain gem versions with the magic of shell aliases.

Bundle exec帮助您避免这些错误。它使用脚本的Gemfile中指定的gems来执行脚本,而不是系统范围的Gemfile。它以外壳别名的魔力执行某些宝石版本。

See more on the man page.

请参阅手册页。

Here's an example Gemfile:

这里有一个例子Gemfile:

source 'http://rubygems.org'

gem 'rails', '2.8.3'

Here, bundle exec would execute the script using rails version 2.8.3 and not some other version you may have installed system-wide.

这里,bundle exec将使用rails版本2.8.3来执行脚本,而不是您可能在系统范围内安装的其他版本。

#3


7  

This comes up a lot when your gemfile.lock has different versions of the gems installed on your machine. You may get a warning after running rake (or rspec or others) such as:

这在你的gemfile中出现了很多。锁定有不同版本的宝石安装在您的机器上。在使用rake(或rspec或其他)之后,您可能会得到一个警告,例如:

You have already activated rake 10.3.1, but your Gemfile requires rake 10.1.0. Prepending "bundle exec" to your command may solve this.

您已经激活了rake 10.3.1,但是您的Gemfile需要rake 10.1.0。在命令前面加上“bundle exec”可以解决这个问题。

Prepending bundle exec tells the bundler to execute this command regardless of the version differential. There isn't always an issue, however, you might run into problems.

预挂包exec告诉bundler执行此命令,而不考虑版本差异。然而,问题并不总是存在,你可能会遇到问题。

Fortunately, there is a gem that solves this: rubygems-bundler.

幸运的是,有一颗宝石可以解决这个问题:rubygems-bundler。

$ gem install rubygems-bundler

$ $ gem regenerate_binstubs

$ gem安装rubygems-bundler $ gem regenerate_binstubs

Then try your rake, rspec, or whatever again.

然后再试试你的耙子,rspec,或者别的什么。

#4


6  

It should probably be mentioned, that there are ways to omit bundle exec (they are all stated in chapter 3.6.1 of Michael Hartls Ruby on Rails Tutorial book).

应该提到的是,有一些方法可以省略bundle exec(它们都在Michael Hartls Ruby on Rails教程的第3.6.1章中说明)。

The simplest is to just use a sufficiently up-to-date version of RVM (>= 1.11.x).

最简单的方法是使用一个最新版本的RVM (>= 1.11.x)。

If you're restricted to an earlier version of RVM, you can always use this method also mentioned by calasyr:

如果您被限制在RVM的早期版本中,您可以使用calasyr也提到的这个方法:

$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ bundle install --binstubs=./bundler_stubs

The bundler_stubs directory should then also be added to the .gitignore file.

然后,应该将bundler_stubs目录添加到.gitignore文件中。

A third option is to use the rubygems-bundler gem if you're not using RVM:

第三种选择是使用rubygems-bundler gem:

$ gem install rubygems-bundler
$ gem regenerate_binstubs

#5


1  

I have not used bundle exec much, but am setting it up now.

我没有使用太多的bundle exec,但是现在正在设置它。

I have had instances where the wrong rake was used and much time wasted tracking down the problem. This helps you avoid that.

我曾经遇到过使用错误耙子的情况,并且浪费了大量时间来跟踪问题。这可以帮助你避免这种情况。

Here's how to set up rvm so you can use bundle exec by default within a specific project directory:

以下是如何设置rvm,以便在特定的项目目录中默认使用bundle exec:

http://robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs

http://robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs

#6


0  

It means use rake that bundler is aware of and is part of your Gemfile over any rake that bundler is not aware of and run the db:migrate task.

它意味着使用bundler知道的rake,并且是Gemfile的一部分,而不是bundler不知道的rake,并运行db:migrate任务。

#7


0  

When you directly run the rake task or execute any binary file of a gem, there is no guarantee that the command will behave as expected. Because it might happen that you already have the same gem installed on your system which have a version say 1.0 but in your project you have higher version say 2.0. In this case you can not predict which one will be used.

当您直接运行rake任务或执行gem的任何二进制文件时,不能保证该命令的行为符合预期。因为你可能已经在你的系统上安装了相同的gem比如1.0但是在你的项目中你有更高的版本比如2.0。在这种情况下,您无法预测将使用哪一个。

To enforce the desired gem version you take the help of bundle exec command which would execute the binary in context of current bundle. That means when you use bundle exec, bundler checks the gem version configured for the current project and use that to perform the task.

为了实现所需的gem版本,您需要借助bundle exec命令,该命令将在当前bundle的上下文中执行二进制。这意味着当您使用bundle exec时,bundler将检查为当前项目配置的gem版本,并使用该版本执行任务。

I have also written a post about it which also shows how we can avoid using it using bin stubs.

我也写了一篇关于它的文章,也展示了如何避免使用bin stub。

#1


418  

bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined.

bundle exec是Bundler命令,用于在当前bundle(目录Gemfile中的Gemfile)上下文中执行脚本。rake db: migration is the script where db is the namespace and migration is the task name defined。

So bundle exec rake db:migrate executes the rake script with the command db:migrate in the context of the current bundle.

因此,bundle exec rake db: migration使用命令db:migrate在当前bundle的上下文中执行rake脚本。

As to the "why?" I'll quote from the bundler page:

的“为什么?”我将引用bundler页面的话:

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

在某些情况下,如果可执行文件恰好安装在您的系统中,并且不引入任何与您的包冲突的gem,那么在没有bundle exec的情况下运行可执行文件可能是有效的。

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

然而,这是不可靠的,而且是相当痛苦的根源。即使它看起来是可行的,它也可能不会在将来或在另一台机器上工作。

#2


131  

You're running bundle exec on a program. The program's creators wrote it when certain versions of gems were available. The program Gemfile specifies the versions of the gems the creators decided to use. That is, the script was made to run correctly against these gem versions.

你在一个程序上运行bundle exec。当某些版本的gem可用时,程序的创建者就会编写它。程序Gemfile指定创建者决定使用的gem的版本。也就是说,该脚本是针对这些gem版本正确运行的。

Your system-wide Gemfile may differ from this Gemfile. You may have newer or older gems with which this script doesn't play nice. This difference in versions can give you weird errors.

系统范围的Gemfile可能与此Gemfile不同。您可能有更新的或旧的宝石,这个脚本不能很好地发挥作用。版本之间的差异会给您带来奇怪的错误。

Bundle exec helps you avoid these errors. It executes the script using the gems specified in the script's Gemfile rather than the systemwide Gemfile. It executes the certain gem versions with the magic of shell aliases.

Bundle exec帮助您避免这些错误。它使用脚本的Gemfile中指定的gems来执行脚本,而不是系统范围的Gemfile。它以外壳别名的魔力执行某些宝石版本。

See more on the man page.

请参阅手册页。

Here's an example Gemfile:

这里有一个例子Gemfile:

source 'http://rubygems.org'

gem 'rails', '2.8.3'

Here, bundle exec would execute the script using rails version 2.8.3 and not some other version you may have installed system-wide.

这里,bundle exec将使用rails版本2.8.3来执行脚本,而不是您可能在系统范围内安装的其他版本。

#3


7  

This comes up a lot when your gemfile.lock has different versions of the gems installed on your machine. You may get a warning after running rake (or rspec or others) such as:

这在你的gemfile中出现了很多。锁定有不同版本的宝石安装在您的机器上。在使用rake(或rspec或其他)之后,您可能会得到一个警告,例如:

You have already activated rake 10.3.1, but your Gemfile requires rake 10.1.0. Prepending "bundle exec" to your command may solve this.

您已经激活了rake 10.3.1,但是您的Gemfile需要rake 10.1.0。在命令前面加上“bundle exec”可以解决这个问题。

Prepending bundle exec tells the bundler to execute this command regardless of the version differential. There isn't always an issue, however, you might run into problems.

预挂包exec告诉bundler执行此命令,而不考虑版本差异。然而,问题并不总是存在,你可能会遇到问题。

Fortunately, there is a gem that solves this: rubygems-bundler.

幸运的是,有一颗宝石可以解决这个问题:rubygems-bundler。

$ gem install rubygems-bundler

$ $ gem regenerate_binstubs

$ gem安装rubygems-bundler $ gem regenerate_binstubs

Then try your rake, rspec, or whatever again.

然后再试试你的耙子,rspec,或者别的什么。

#4


6  

It should probably be mentioned, that there are ways to omit bundle exec (they are all stated in chapter 3.6.1 of Michael Hartls Ruby on Rails Tutorial book).

应该提到的是,有一些方法可以省略bundle exec(它们都在Michael Hartls Ruby on Rails教程的第3.6.1章中说明)。

The simplest is to just use a sufficiently up-to-date version of RVM (>= 1.11.x).

最简单的方法是使用一个最新版本的RVM (>= 1.11.x)。

If you're restricted to an earlier version of RVM, you can always use this method also mentioned by calasyr:

如果您被限制在RVM的早期版本中,您可以使用calasyr也提到的这个方法:

$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ bundle install --binstubs=./bundler_stubs

The bundler_stubs directory should then also be added to the .gitignore file.

然后,应该将bundler_stubs目录添加到.gitignore文件中。

A third option is to use the rubygems-bundler gem if you're not using RVM:

第三种选择是使用rubygems-bundler gem:

$ gem install rubygems-bundler
$ gem regenerate_binstubs

#5


1  

I have not used bundle exec much, but am setting it up now.

我没有使用太多的bundle exec,但是现在正在设置它。

I have had instances where the wrong rake was used and much time wasted tracking down the problem. This helps you avoid that.

我曾经遇到过使用错误耙子的情况,并且浪费了大量时间来跟踪问题。这可以帮助你避免这种情况。

Here's how to set up rvm so you can use bundle exec by default within a specific project directory:

以下是如何设置rvm,以便在特定的项目目录中默认使用bundle exec:

http://robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs

http://robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs

#6


0  

It means use rake that bundler is aware of and is part of your Gemfile over any rake that bundler is not aware of and run the db:migrate task.

它意味着使用bundler知道的rake,并且是Gemfile的一部分,而不是bundler不知道的rake,并运行db:migrate任务。

#7


0  

When you directly run the rake task or execute any binary file of a gem, there is no guarantee that the command will behave as expected. Because it might happen that you already have the same gem installed on your system which have a version say 1.0 but in your project you have higher version say 2.0. In this case you can not predict which one will be used.

当您直接运行rake任务或执行gem的任何二进制文件时,不能保证该命令的行为符合预期。因为你可能已经在你的系统上安装了相同的gem比如1.0但是在你的项目中你有更高的版本比如2.0。在这种情况下,您无法预测将使用哪一个。

To enforce the desired gem version you take the help of bundle exec command which would execute the binary in context of current bundle. That means when you use bundle exec, bundler checks the gem version configured for the current project and use that to perform the task.

为了实现所需的gem版本,您需要借助bundle exec命令,该命令将在当前bundle的上下文中执行二进制。这意味着当您使用bundle exec时,bundler将检查为当前项目配置的gem版本,并使用该版本执行任务。

I have also written a post about it which also shows how we can avoid using it using bin stubs.

我也写了一篇关于它的文章,也展示了如何避免使用bin stub。