簡體   English   中英

耙任務以在另一個項目中執行捆綁安裝

[英]rake task to perform bundle install in another project

如何編寫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的。

我也試過back

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

需要注意的幾件事

僅當bin目錄中存在bundle文件時, bin/bundle才起作用

  • 在shell中執行命令的方法有很多
    • exec('echo "Hello World"') -運行命令並用它替換當前進程。
    • system('echo "Hello World"') -在子shell中運行命令並捕獲退出狀態代碼
    • `echo "hello world"` -在子shell中運行命令,並捕獲所有輸出到STDOUT和STDERR的輸出,但不捕獲退出狀態代碼。

對於為什么它不起作用

您需要為捆綁程序提供一個干凈的環境,以便它知道您要捆綁另一個項目。 如下所示,使用Bundler.with_clean_env塊將耙任務中的命令包圍Bundler.with_clean_env

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM