Rake error with a clean error in the absence of a file

I have a rakefile like this

task :clean do sh 'rm ./foo' end 

I want it to not report an error when the file 'foo' does not exist. How to do it?

I think I want: is there a way to check the file first and then decide what to do next.

For instance:

 file 'aaa' => 'bbb' do sh 'cp bbb aaa' end 

This task depends on the existence of the file 'bbb', so I want to know can I tell Rake that my task depends on the nonexistence of file 'foo' ?

+4
source share
3 answers

You can do this by stretching a bit:

Rakefile:

 require File.join(File.dirname(__FILE__), 'unfile_rake_ext') unfile 'target.txt' do File.delete('target.txt') end 

unfile_rake_ext.rb:

 class UnFileTask < Rake::FileTask def needed? File.exist?(name) end end def unfile(*args, &block) UnFileTask.define_task(*args, &block) end 

And my console output:

 D:\Projects\ZPersonal\tmp>ls Rakefile unfile_rake_ext.rb D:\Projects\ZPersonal\tmp>touch target.txt && ls Rakefile target.txt unfile_rake_ext.rb D:\Projects\ZPersonal\tmp>rake target.txt --trace ** Invoke target.txt (first_time) ** Execute target.txt D:\Projects\ZPersonal\tmp>ls Rakefile unfile_rake_ext.rb D:\Projects\ZPersonal\tmp>rake target.txt --trace ** Invoke target.txt (first_time, not_needed) D:\Projects\ZPersonal\tmp>ls Rakefile unfile_rake_ext.rb 

Hope this helps.

+4
source

In your rakefile:

 task :clean do rm 'foo' if File.exists? 'foo' end file 'aaa' => ['bbb', :clean] do |t| cp t.prerequisites[0], t.name end 

Now at the command prompt:

 echo 'test' > bbb rake aaa => cp bbb aaa touch foo rake aaa => rm foo => cp bbb aaa 
+2
source

How about this?

 if File.exists? './foo/' sh 'rm -f ./foo' end 
+1
source

Source: https://habr.com/ru/post/1387509/


All Articles