我用的是uptodate? Ruby 中的方法来确定文件是否必须重新编译/重新链接/重新生成等。有时我使用的构建框架在这些步骤中速度如此之快,以至于输出文件和先决条件文件具有完全相同的文件修改时间戳.这会导致构建框架不必要地重新编译/重新链接文件。
我这样比较构建的时间戳 -
compile_file(file) unless uptodate?(file, %W(#{dependencies}))
我查找了更新的来源?来自 here它看起来像这样 -
def uptodate?(new, old_list, options = nil)
raise ArgumentError, 'uptodate? does not accept any option' if options
return false unless File.exist?(new)
new_time = File.mtime(new)
old_list.each do |old|
if File.exist?(old)
return false unless new_time > File.mtime(old)
end
end
true
end
正如所怀疑的那样,如果时间戳相等,它会返回 false。解决这个问题最优雅的方法是什么?我曾尝试在 Linux 和 Windows 上运行该框架,但我遇到了同样的问题。从我读到的here它不太可能是文件系统特定的文件修改时间分辨率问题(因为 ext4 的分辨率为 1 微秒)。
最佳答案
似乎 Ruby 的 File
对象不支持文件修改时间戳的亚秒分辨率,即使底层文件系统支持也是如此。一种解决方案是使用 ls --full-time
并使用 Ruby 的 DateTime
解析结果,它支持小数秒:
module FileUtilsPlus
def self.uptodate?(new, old_list, options = nil)
return true if FileUtils.uptodate?(new, old_list, options)
return false unless File.exist?(new)
new_time = filemtime(new)
old_list.each do |old|
if File.exist?(old)
return false unless new_time > filemtime(old)
end
end
end
def self.filemtime(path)
DateTime.parse(`ls --full-time foo | awk '{ print $6 " " $7 }'`)
end
end
请注意,这将比 Ruby 的本地文件操作慢得多,所以我们只会在 FileUtils::uptodate?
返回 false 时才施展魔法。
关于ruby - FileUtils 更新了吗? Ruby 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999396/