如何让 fileutils 调用在 Rakefile 中输出其操作?
我有一个 Rakefile,在我自己构建的 Ruby 1.9.3 安装中,当我使用 FileUtils 方法(例如 cp、mkdir 等)时,它可以正确输出等效的 Unix shell。
然而,在 Mac OS X(特别是 10.5)附带的 Ruby 版本 1.8.6 上,他们不这样做。
我希望他们在执行命令时输出命令。有没有办法在 OS X 的 1.8.6 Ruby 中启用此功能,无需添加 :verbose =>每次调用都为 true
? (这甚至可能不起作用。)
有问题的 Rakefile 是: https://github .com/dpkendal/tools-osx/blob/master/Rakefile
I have a Rakefile which, on my own-built Ruby 1.9.3 installation, correctly outputs the Unix shell equivalent when I use a FileUtils method such as cp
, mkdir
etc.
However, on the stock Ruby that ships with Mac OS X (specifically 10.5), which is version 1.8.6, they don't do this.
I'd like them to output the commands as they're performed. Is there a way to enable this in OS X's 1.8.6 Ruby, short of adding :verbose => true
to every call? (Which may not even work.)
The Rakefile in question is: https://github.com/dpkendal/tools-osx/blob/master/Rakefile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这没有道理。 1.9.3 不应执行
:verbose
除非明确告知这样做。例如,您可以查看 1.9.3 库中mkdir
的实现:您可以看到,除非显式提供
:verbose
选项,否则不会生成消息。但是,要在所有
FileUtils
方法中启用:verbose
,您只需将 FileUtils::Verbose 包含到您的命名空间中即可。这在 1.8 和 1.9 ruby 中都有效:顺便说一句,Rake 可能已经在 1.9.3 中做到了这一点,这可以解释为什么它会执行 1.9.3 中的操作,而不是 1.8.6 中的操作。我没有检查这一点,但这是我能想到的唯一解释。
Rake 提供了自己的 FileUtils 扩展,称为 Rake::FileUtilsExt。该模块有一个
verbose
标志。要激活它,只需将其添加到 Rakefile 的顶部:That doesn't make sense. 1.9.3 should not do
:verbose
unless explicitly told to do so. You can look at the implementation ofmkdir
in the 1.9.3 lib for example:There you can see that the message is not generated unless the
:verbose
option is explicitly supplied.However to enable
:verbose
across allFileUtils
methods you can simplyinclude FileUtils::Verbose
into your namespace. This works in both 1.8 and 1.9 ruby:BTW, it might be that Rake already does this in 1.9.3, which would explain why it does what it does in 1.9.3 and not in 1.8.6. I did not check this, but that's the only explanation I can think of.
Rake provides its own FileUtils extension called
Rake::FileUtilsExt
. This module has averbose
flag. To activate it simply add this to the top of the Rakefile: