Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rake-仅复制已更改的文件_Ruby_File_Copy_Rake - Fatal编程技术网

Ruby Rake-仅复制已更改的文件

Ruby Rake-仅复制已更改的文件,ruby,file,copy,rake,Ruby,File,Copy,Rake,因此,我有一个如下所示的rake文件: require 'fileutils' task :copy do FileUtils.cp_r 'src', 'target' end 我怎样才能: 仅复制已更改的文件 使:copy任务依赖于src目录,以便它仅在需要时启动:copy=>'src'和:copy=>文件列表['src/*']。to_a似乎不起作用 我可以回答第一个问题如下: task :copy do sh 'rsync -ru src/* target' end 如果可

因此,我有一个如下所示的rake文件:

require 'fileutils'

task :copy do
  FileUtils.cp_r 'src', 'target'
end
我怎样才能:

  • 仅复制已更改的文件
  • 使
    :copy
    任务依赖于
    src
    目录,以便它仅在需要时启动<代码>:copy=>'src'和
    :copy=>文件列表['src/*']。to_a
    似乎不起作用
  • 我可以回答第一个问题如下:

    task :copy do
        sh 'rsync -ru src/* target'
    end
    

    如果可能的话,我希望只使用ruby/rake来实现这一点。这也在一定程度上解决了第二个问题,因为如果没有文件发生更改,
    rsync
    不会做任何事情,但我希望尽可能不执行rake任务。

    为了避免执行任务,Rake需要知道目标和源以及确定是否执行任务的规则,以便从源创建目标

    通常情况下,规则是“时间修改的”,即,如果源比目标更新,则Rake将运行任务。您可以通过提供一个Proc作为源依赖项来修改它。请参阅“高级规则”下的内容(但确实需要一些实验才能了解正在发生的事情!)

    因此,您的任务必须取决于目标。如果您知道目标不存在,那么应该很容易:

    task :copy => 'target/' do
      sh 'rsync -ru src/ target'  # trailing slash is significant; target will be created
    done
    

    如果目标已经存在,它会变得更加复杂。您可以为每个文件定义一个规则,但坦率地说,我只需要运行rsync就可以了。Rsync在本地文件系统上速度非常快,每次运行它都没什么大不了的。

    为了避免执行任务,Rake需要知道目标和源以及确定是否执行任务的规则,以便从源创建目标

    通常情况下,规则是“时间修改的”,即,如果源比目标更新,则Rake将运行任务。您可以通过提供一个Proc作为源依赖项来修改它。请参阅“高级规则”下的内容(但确实需要一些实验才能了解正在发生的事情!)

    因此,您的任务必须取决于目标。如果您知道目标不存在,那么应该很容易:

    task :copy => 'target/' do
      sh 'rsync -ru src/ target'  # trailing slash is significant; target will be created
    done
    

    如果目标已经存在,它会变得更加复杂。您可以为每个文件定义一个规则,但坦率地说,我只需要运行rsync就可以了。Rsync在本地文件系统上速度非常快,每次运行它都没什么大不了的。

    这里有一个独立于操作系统的纯Ruby解决方案

    class File
      def self.update(src, dst)
        if File.exist? dst
          # skip if src file modification or creation time is same or earlier than target
          return if [File.ctime(src), File.mtime(src)].max <= [File.ctime(dst), File.mtime(dst)].max
        else
          # create target folder if not present
          FileUtils.mkdir_p(File.dirname(dst)) unless File.exist? File.dirname(dst)
        end
        FileUtils.cp(src, dst)
        puts src
      rescue => e
        puts "#{e}\n#{e.backtrace}"
      end
    end
    
    File.update source_file, target_file
    
    类文件
    def自我更新(src、dst)
    如果File.exist存在?dst
    #如果src文件修改或创建时间等于或早于目标时间,则跳过
    如果[File.ctime(src),File.mtime(src)].max e,则返回
    放置“#{e}\n#{e.backtrace}”
    终止
    终止
    File.update源文件、目标文件
    
    这里有一个独立于操作系统的纯Ruby解决方案

    class File
      def self.update(src, dst)
        if File.exist? dst
          # skip if src file modification or creation time is same or earlier than target
          return if [File.ctime(src), File.mtime(src)].max <= [File.ctime(dst), File.mtime(dst)].max
        else
          # create target folder if not present
          FileUtils.mkdir_p(File.dirname(dst)) unless File.exist? File.dirname(dst)
        end
        FileUtils.cp(src, dst)
        puts src
      rescue => e
        puts "#{e}\n#{e.backtrace}"
      end
    end
    
    File.update source_file, target_file
    
    类文件
    def自我更新(src、dst)
    如果File.exist存在?dst
    #如果src文件修改或创建时间等于或早于目标时间,则跳过
    如果[File.ctime(src),File.mtime(src)].max e,则返回
    放置“#{e}\n#{e.backtrace}”
    终止
    终止
    File.update源文件、目标文件
    
    rubyrake.org似乎不再有rake文档;我相信正确的链接是:(from)。rubyrake.org似乎不再有rake文档;我相信正确的链接是:(from)。