Search code examples
rubymsbuildrake

Rake delete files task


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?


Solution

  • 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.