Rake构建在错误的上下文中运行 [英] Rake build is operating in an incorrect context

查看:94
本文介绍了Rake构建在错误的上下文中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的收集多个引擎的简单实现的宝石,以及为所有包含的引擎提供一些实用方法。一种这样的实用方法是发布任务以发布所有收集的引擎的新版本。



我的问题是,当我运行应该在其中一个收集的引擎的上下文中执行的代码时,它(git命令,文件系统操作等) ,除 rake build 命令外。出于某种原因,该命令以某种方式在伞宝石的上下文中运行,并从其Gemfile.lock中获取版本号。



将此代码用于例如:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $以同样的方式失败...

#p sh('bundle','exec','rake','build')

#p sh('bundle exec rake build')

#p`bundle exec rake build`

#thr = Thread.new {
#p`bundle exec rake build`
#}
#thr.join

#load File.join(Dir.pwd,'Rakefile')
#Rake :: Task ['build']。invoke

#p`gem build#{PATH_TO_COLLECTED_ENGINE_GEMSPEC_FILE}`
end



<当从一个标准的Ruby文件运行时,我得到了 collected_engine_a 3.12.9所需的输出,它构建到pkg / collected_engine_a-3.12.9.gem ,但是从RA我得到了令人费解的 collect_engine_a 3.12.9的输出,它构建到pkg / collected_engine_a-3.12.2.gem 。它似乎是从伞gem的Gemfile.lock派生的版本(如果我调整版本,它会影响输出)。



我试过了无论是否有 bundle exec 前言,基本上都有相同的结果。

任何人都可以想出一种方法来获取正确的上下文,或者我坚持将这些从rakefile中移出,并转换成标准脚本(重命名输出文件是一个不好的选项,不会被考虑)?

解决方案

bundle exec 具有特定的行为。您可以在 bundle help exec 中看到以下注释:



确保如果bundler在子shell中调用它,它使用相同的Gemfile(通过设置BUNDLE_GEMFILE)



所以在你的情况下,你会做:
$ b

bundle exec rake build BUNDLE_GEMFILE =#{PATH_TO_COLLECTED_ENGINE} / Gemfile


I have a gem that exists simply to collect several engines together for ease of implementation, as well as to provide a few utility methods to all of the included engines. One such utility method is a rake task to release new versions of all of the collected engines.

My problem is that, when I run code that should execute in the context of one of the collected engines it all (git commands, file system manipulation, etc.) works, except for the rake build command. For some reason, that command is somehow running in the context of the umbrella gem, and is picking up the version number from its Gemfile.lock.

Using this code for an example:

Dir.chdir( PATH_TO_COLLECTED_ENGINE ) do
  # Below lie all of my failed build attempts, all of which failed in the same way...

  #p sh( 'bundle' ,  'exec' , 'rake' , 'build' )

  #p sh( 'bundle exec rake build' )

  #p `bundle exec rake build`

  #thr = Thread.new {
  #  p `bundle exec rake build`
  #}
  #thr.join

  #load File.join(Dir.pwd, 'Rakefile')
  #Rake::Task['build'].invoke

  #p `gem build #{ PATH_TO_COLLECTED_ENGINE_GEMSPEC_FILE }`
end

When run from a standard Ruby file, I get the desired output of collected_engine_a 3.12.9 built to pkg/collected_engine_a-3.12.9.gem, but when running from a rake task in the umbrella gem, I get the puzzling output of collected_engine_a 3.12.9 built to pkg/collected_engine_a-3.12.2.gem. It seems that the version is being derived from the Gemfile.lock of the umbrella gem (If I adjust the version there, it affects the output).

I've tried both with and without a bundle exec preface, with basically the same result.

Can anyone think of a way to get this to pick up the correct context, or am I stuck moving these out of a rakefile, and into a standard script (renaming the output file is a poor option that will not be considered)?

解决方案

bundle exec has a specific behavior when launched in a subshell. You can see the following note in bundle help exec:

make sure that if bundler is invoked in the subshell, it uses the same Gemfile (by setting BUNDLE_GEMFILE)

So in your case, you would do:

bundle exec rake build BUNDLE_GEMFILE=#{PATH_TO_COLLECTED_ENGINE}/Gemfile

这篇关于Rake构建在错误的上下文中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆