临时更改Rake中的当前目录。

[英]Temporarily change current directory in Rake


I would like to run multiple commands need to be (or is easier to be) in another directory, then once they're done, return back to the previous working directory.

我希望在另一个目录中运行需要(或更容易)的多个命令,然后一旦它们完成,返回到以前的工作目录。

I'm envisioning something similar to Fabric's with cd(path):, e.g.:

我在设想类似于布的cd(path):,例如:

cd('.git') do
   File.unlink('config')
end

Is there an inbuilt way of doing this in Rake, or should I be writing a custom method that accepts a block, etc.?

在Rake中是否有一种内置的方法来实现这个目的,或者我应该编写一个接受块的自定义方法,等等?

1 个解决方案

#1


33  

It is simply the inbuilt Dir#chdir call:

它只是内置的Dir#chdir调用:

Dir.chdir('.git') do
  File.unlink('config')
end

Excerpt from the docs:

文档的摘录:

If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits.

如果给定了一个块,它将被传递新当前目录的名称,并且该块将以当前目录的形式执行。当块退出时,恢复原始工作目录。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/05/14/3a681779ed0b7c3cee6029b89bdf26c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告