简体   繁体   English

耙运行两次

[英]Rake runs function twice

I started creating a set of Rake tasks (my first time, with limited Ruby experience) and when I run the first task, using either rake test or rake db , from the command line I get two outputs even though there is only one file in the directory in question. 我开始创建一组Rake任务(我第一次,在Ruby方面经验有限),当我使用rake testrake db运行第一个任务时,即使在命令行中只有一个文件,我也会得到两个输出。有问题的目录。

rakefile.rb rakefile.rb

require 'fileutils'

FILES    = ["html", "css", "js", "svg", "otf", "eot", "ttf", "woff", "jpeg", "map", "ico", "map", "png", "db"]

desc 'test script'
task :test => [:db]

task :db do
    copy_to 'data/', 'c:/xampp/htdocs/home/shared/data'
end

def copy_to(dest, src)

    files = FileList.new()
    files.include Dir.glob("#{src}/*.*")
    FILES.each {|ext| files.include "#{src}/*.#{ext}"}
    files.each do |src|
        puts "copying: #{src} to #{dest}"
        FileUtils.cp src, dest
    end

end

Output 产量

(in C:/xampp/htdocs/home/gateway)                        
copying: c:/xampp/htdocs/home/shared/data/foo.db to data/   
copying: c:/xampp/htdocs/home/shared/data/foo.db to data/   

When I do rake -T I get the following (which is what I would expect): 当我rake -T我得到以下信息(这是我期望的):

(in C:/xampp/htdocs/home/gateway)
rake test  # test script

Of course foo.db only gets copied once, or the second copy overwrites the first one. 当然, foo.db仅被复制一次,否则第二个副本将覆盖第一个副本。

EDIT ran rake test --trace from the command line 编辑从命令行运行rake test --trace

** Invoke test (first_time)                                                   
** Invoke db (first_time)                                                     
** Execute db                                                                 
copying: c:/xampp/htdocs/home/shared/data/foo.db to data/ 
copying: c:/xampp/htdocs/home/shared/data/foo.db to data/ 
** Execute test                                                               

Is it executing :db and then :test ? 它先执行:db 然后执行:test吗? Or is :db running twice as it appears to be doing here? 还是:db在这里似乎运行了两次? What am I missing? 我想念什么? Have I done something wrong? 我做错什么了吗?

The problem is that you're adding foo.db to the files FileList twice: 问题是您将foo.db两次添加到files FileList中:

files.include Dir.glob("#{src}/*.*")
FILES.each {|ext| files.include "#{src}/*.#{ext}"}

Because FILES (which is a misleading variable name) includes "db" , on the first line you're adding *.* to files and on the second line you're adding *.db to files . 由于FILES (这是一个令人误解的变量名)包含"db" ,因此在第一行中将*.*添加到files ,在第二行中将*.db添加到files

It's not clear why you have both lines, since the first line will add every file in the directory, so the second line is only going to add files that you've already added. 目前尚不清楚为什么要同时使用这两行,因为第一行将添加目录中的每个文件,因此第二行将仅添加已添加的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM