Rake删除文件任务

3
在msbuild中,我可以像这样删除特定目录中的部分文件。
<ItemGroup>
     <FilesToDelete Include="$(DeploymentDir)\**\*" exclude="$(DeploymentDir)\**\*.log"/>
</ItemGroup>
<Delete Files="@(FilesToDelete)" />

它将删除除*.txt之外的所有文件。

有没有一些rake任务可以执行类似的操作?

2个回答

11

Ruby内置了用于实现此目的的类:

Dir['deployment_dir/**/*'].delete_if { |f| f.end_with?('.txt') }

然而,对于某些内置任务,rake有相应的帮助程序。从API文档中改编,您可以这样选择文件:

files_to_delete = FileList.new('deployment_dir/**/*') do |fl|
  fl.exclude('*.txt')
end

然后您可以将此输入删除任务。

更好的方法是使用内置的CLEAN/CLOBBER任务:

# Your rake file:
require 'rake/clean'

# [] is alias for .new(), and we can chain .exclude
CLEAN = FileList['deployment_dir/**/*'].exclude('*.txt')

然后您可以在命令行上说:

rake clean

请阅读教程


1

@adzdavies的答案很好,但是将值赋给CLEAN会产生以下警告,因为CLEAN是一个常量:

warning: already initialized constant CLEAN

您应该使用CLEAN的实例方法。它是一个Rake::FileList,因此您可以将以下内容添加到您的Rakefile中:

require 'rake/clean'

# this is untested, but you get the idea
CLEAN.include('deployment_dir/**/*').exclude('*.txt')

然后运行:
rake clean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接