Rake 删除文件任务

发布于 2024-12-26 04:54:32 字数 298 浏览 0 评论 0原文

在 msbuild 中,我可以像这样删除某些目录中的部分文件

<ItemGroup>
     <FilesToDelete Include="$(DeploymentDir)\**\*" exclude="$(DeploymentDir)\**\*.log"/>
</ItemGroup>
<Delete Files="@(FilesToDelete)" />

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

是否有一些 rake 任务我可以做类似的事情?

In msbuild I can delete part of files in certain directory like this

<ItemGroup>
     <FilesToDelete Include="$(DeploymentDir)\**\*" exclude="$(DeploymentDir)\**\*.log"/>
</ItemGroup>
<Delete Files="@(FilesToDelete)" />

It will delete all files except *.txt

Is there some rake task I can similar thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

哽咽笑 2025-01-02 04:54:32

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')

然后您可以在 cmd 行上说:

rake clean

阅读 教程

Ruby has built in classes to make this easy:

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

However, for some built in tasks, rake has helpers for this. Adapted from the API docs you can select files like so:

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

Then you can feed this into your delete task.

Better yet, you can use the built in CLEAN/CLOBBER tasks:

# Your rake file:
require 'rake/clean'

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

Then you can say on the cmd line:

rake clean

Read up the tutorial.

流年已逝 2025-01-02 04:54:32

@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

@adzdavies's answer is good, but assigning to CLEAN will produce the following warning since CLEAN is a constant:

warning: already initialized constant CLEAN

You should instead use CLEAN's instance methods. It is a Rake::FileList, so you can add something like this to your Rakefile:

require 'rake/clean'

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

Then run:

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