2013-07-26 55 views
0

下面的文件任务不执行。它是一个简单的Rakefile的内容,如果它不存在,它将创建一个名为hello.txt的文件。耙文件任务不起作用

task :default do 
    puts "before file task" 
    file "hello.txt" do 
     puts "in file task" 
     sh "touch hello.txt" 
    end 
    puts "after file task" 
end 

在其中Rake文件所在的目录shell提示运行rake后,输出为:

before file task 
after file task 

和没有创建hello.txt文件。

我不确定为什么文件任务不起作用,至于我的眼睛,Rakefile的文件任务部分的语法看起来很合理。我在这里做错了什么?

回答

1

文件任务必须作为常规任务方法直接调用或引用。例如。

task :default => "hello.txt" do 
    puts "after file task" 
end 

file "hello.txt" do 
    puts "in file task" 
    sh "touch hello.txt" 
end 
+0

耙包括文件实用程序模块。所以你可以使用'mkdir','rmdir','cp','mv','chmod'和'touch'方法:)所以'sh“touch hello.txt”'给出和'触摸“hello.txt”,但后者不会出现 – mbigras

3

当你调用default -task其文件任务之前,做了三件事

  1. 定义文件任务后的文件任务 'hello.txt的'

要重复最重要的事情:文件任务hello.txt的是定义,不执行

也许你想要做的事,如:

task :default do 
    puts "before file creation" 
    File.open("hello.txt","w") do |f| 
     puts "in file creation" 
     f << "content for hello.txt" 
    end 
    puts "after file creation" 
end 

这将创建文件始终。

您也可以使用该方法在pyotr6 answer

task :default => 'hello.txt' do 
    puts "default task, executed after prerequistes" 
end 

file 'hello.txt' do |tsk| 
    File.open(tsk.name, "w") do |f| 
    puts "in file creation of #{f.path}" 
    f << "content for hello.txt" 
    end 
end 

这一次创建hello.txt的。如果hello.txt已经存在,文件任务将不会启动。

要重新生成hello.txt,您需要一个prerequsite(通常这是另一个文件,它是hello.txt的源文件)。

可以强制再生用伪任务:

task :force #Dummy task to force file generation 
file 'hello.txt' => :force