Ruby-on-rails bundle exec rake 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6588674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:32:00  来源:igfitidea点击:

What does bundle exec rake mean?

ruby-on-railsrakebundler

提问by JnBrymn

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

什么bundle exec rake db:migrate意思?还是只是bundle exec rake <command>一般?

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

我知道它bundle负责维护 Gemfile 中的内容。我知道“exec”这个词是什么意思。我知道这可以rake维护您可以做的所有不同的脚本事情,我知道这db:migrate就是其中之一。我只是不知道所有这些词在一起做什么。为什么要bundle使用executerake来执行数据库迁移?

回答by ghoppe

bundle execis a Bundlercommand to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrateis the script where dbis the namespace and migrateis the task name defined.

bundle exec是一个Bundler命令,用于在当前包(来自您目录的Gemfile那个)的上下文中执行脚本。rake db:migrate是脚本,其中db是命名空间,migrate是定义的任务名称。

So bundle exec rake db:migrateexecutes the rake script with the command db:migratein the context of the current bundle.

所以在当前包的上下文中bundle exec rake db:migrate使用命令执行 rake 脚本db:migrate

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

至于“为什么?” 我将从捆绑器页面引用:

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

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.

在某些情况下,bundle exec如果可执行文件碰巧安装在您的系统中并且没有引入任何与您的包冲突的 gem,那么在没有的情况下运行可执行文件可能会起作用。

然而,这是不可靠的,并且是相当大的痛苦的根源。即使它看起来有效,但将来或在另一台机器上可能无法使用。

回答by Rose Perrone

You're running bundle execon 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 不同。您可能有新的或旧的 gem,但该脚本无法很好地使用它们。这种版本差异可能会给您带来奇怪的错误。

bundle exechelps 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 来执行脚本。它使用 shell 别名的魔力执行某些 gem 版本。

See more on the man page.

在手册页上查看更多信息。

Here's an example Gemfile:

这是一个 Gemfile 示例:

source 'http://rubygems.org'

gem 'rails', '2.8.3'

Here, bundle execwould 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 执行脚本,而不是您可能已在系统范围内安装的其他版本。

回答by Benjamin Dunphy

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.lock 在您的机器上安装了不同版本的 gem 时,会经常出现这种情况。运行 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.

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.

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

Prependingbundle exec告诉捆绑器执行此命令,而不管版本差异如何。并不总是有问题,但是,您可能会遇到问题。

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

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

$ gem install rubygems-bundler

$ $ gem regenerate_binstubs

$ gem install rubygems-bundler

$ $ gem regenerate_binstubs

Then try your rake, rspec, or whatever again.

然后再试试你的 rake、rspec 或其他任何东西。

回答by tschale

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 Tutorialbook).

可能应该提到的是,有一些方法可以省略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_stubsdirectory should then also be added to the .gitignorefile.

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

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

rubygems-bundler如果您不使用 RVM,则第三种选择是使用gem:

$ gem install rubygems-bundler
$ gem regenerate_binstubs

回答by Ajit Singh

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 execcommand 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 exec 时,bundler 会检查为当前项目配置的 gem 版本并使用它来执行任务。

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

我还写了一篇关于它的文章,其中还展示了我们如何避免使用 bin stubs 来使用它。

回答by calasyr

I have not used bundle execmuch, 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 execby default within a specific project directory:

以下是设置 RVM 的方法,以便您可以bundle exec在特定项目目录中默认使用:

https://thoughtbot.com/blog/use-bundlers-binstubs

https://thoughtbot.com/blog/use-bundlers-binstubs

回答by Omar Qureshi

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.

这意味着使用捆绑程序知道的 rake 并且是您 Gemfile 的一部分,而不是捆绑程序不知道的任何 rake 并运行 db:migrate 任务。