From ac2e2c0e7800ebbd6ca8087e546fb9d3480cb57e Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Fri, 17 Mar 2017 03:39:54 -0400 Subject: add helper for caching based on a status file --- stage1/Stage1Lib.scala | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'stage1') diff --git a/stage1/Stage1Lib.scala b/stage1/Stage1Lib.scala index 7f7fa37..f66eff4 100644 --- a/stage1/Stage1Lib.scala +++ b/stage1/Stage1Lib.scala @@ -482,6 +482,21 @@ ${sourceFiles.sorted.mkString(" \\\n")} StandardOpenOption.APPEND ) } + def cached[T]( targetDirectory: File, inputLastModified: Long )( action: () => T ): (Option[T],Long) = { + val t = targetDirectory + val start = System.currentTimeMillis + def lastSucceeded = t.lastModified + def outputLastModified = t.listRecursive.diff(t :: Nil).map(_.lastModified).maxOption.getOrElse(0l) + def updateSucceeded(time: Long) = Files.setLastModifiedTime( t.toPath, FileTime.fromMillis(time) ) + ( + ( inputLastModified >= lastSucceeded ).option{ + val result: T = action() + updateSucceeded( start ) + result + }, + outputLastModified + ) + } } import scala.reflect._ -- cgit v1.2.3