如何让 fileutils 调用在 Rakefile 中输出其操作?

发布于 12-26 13:13 字数 446 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区2025-01-02 13:13:26

这没有道理。 1.9.3 不应执行 :verbose 除非明确告知这样做。例如,您可以查看 1.9.3 库中 mkdir 的实现:

  def mkdir(list, options = {})
    fu_check_options options, OPT_TABLE['mkdir']
    list = fu_list(list)
    fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
    return if options[:noop]

    list.each do |dir|
      fu_mkdir dir, options[:mode]
    end
  end

您可以看到,除非显式提供 :verbose 选项,否则不会生成消息。

但是,要在所有 FileUtils 方法中启用 :verbose,您只需将 FileUtils::Verbose 包含到您的命名空间中即可。这在 1.8 和 1.9 ruby​​ 中都有效:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> include FileUtils::Verbose
=> Object
irb(main):003:0> mkdir 'fooof'
mkdir fooof
=> ["fooof"]

顺便说一句,Rake 可能已经在 1.9.3 中做到了这一点,这可以解释为什么它会执行 1.9.3 中的操作,而不是 1.8.6 中的操作。我没有检查这一点,但这是我能想到的唯一解释。


Rake 提供了自己的 FileUtils 扩展,称为 Rake::FileUtilsExt。该模块有一个verbose标志。要激活它,只需将其添加到 Rakefile 的顶部:

Rake::FileUtilsExt.verbose(true)

That doesn't make sense. 1.9.3 should not do :verbose unless explicitly told to do so. You can look at the implementation of mkdir in the 1.9.3 lib for example:

  def mkdir(list, options = {})
    fu_check_options options, OPT_TABLE['mkdir']
    list = fu_list(list)
    fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
    return if options[:noop]

    list.each do |dir|
      fu_mkdir dir, options[:mode]
    end
  end

There you can see that the message is not generated unless the :verbose option is explicitly supplied.

However to enable :verbose across all FileUtils methods you can simply include FileUtils::Verbose into your namespace. This works in both 1.8 and 1.9 ruby:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> include FileUtils::Verbose
=> Object
irb(main):003:0> mkdir 'fooof'
mkdir fooof
=> ["fooof"]

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 a verbose flag. To activate it simply add this to the top of the Rakefile:

Rake::FileUtilsExt.verbose(true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文