【问题标题】:rake task to perform bundle install in another projectrake 任务以在另一个项目中执行捆绑安装
【发布时间】:2015-06-30 20:01:22
【问题描述】:

如何编写 rake 任务以在不同的项目中运行捆绑安装?我已经创建了一个只有 rake 的项目,并编写了这样的任务

task :bundle do
  projects.each do |name, repo|
    if Dir.exists?("../#{name}")
      exec("cd ../#{name} && bin/bundle install")
    end
  end
end

但是当我运行它时,我得到:

Using rake 10.3.2
Using bundler 1.9.6
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

起初看起来不错,但实际上是当前仅 rake 项目而不是目标 rails 项目的bundle install

我也尝试了反勾号

puts `cd ../#{name} && bin/bundle install`

但它也做了同样的事情。我也尝试了 bundle install 而不是 bin/bundle install,但没有成功。

当我直接在命令上运行它时,它会达到我的预期:

Using rake 10.4.2
Using CFPropertyList 2.3.1
...
...
Using turbolinks 2.5.3
Using uglifier 2.7.1
Bundle complete! 34 Gemfile dependencies, 120 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

如何让它正确处理bundle install

【问题讨论】:

    标签: ruby-on-rails ruby bash rake bundle


    【解决方案1】:

    有几点需要注意

    bin/bundle 仅在bundle 文件存在于bin 目录中时才有效

    • 在 shell 中执行命令的方法有很多种
      • exec('echo "Hello World"') - 运行命令并用它替换当前进程。
      • system('echo "Hello World"') - 在子 shell 中运行命令并捕获退出状态代码
      • `echo "hello world"` - 在子 shell 中运行命令并将所有输出捕获到 STDOUT 和 STDERR,但不捕获退出状态代码。

    为什么它不起作用

    您需要为捆绑程序提供一个干净的环境,以便它知道您正在捆绑不同的项目。用Bundler.with_clean_env 块包围你的rake 任务中的命令,如下所示

    task :bundle do
      Bundler.with_clean_env do
        projects.each do |name, repo|
          if Dir.exists?("../#{name}")
            exec("cd ../#{name} && bundle install")
          end
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2013-06-15
      • 2012-04-03
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2012-10-03
      • 2014-04-19
      • 1970-01-01
      相关资源
      最近更新 更多