Implement InternalActionResult

This commit is contained in:
Eugene Yokota 2024-08-11 16:54:38 -04:00
parent aaa68883aa
commit c8ddbaed0e
3 changed files with 30 additions and 12 deletions

View File

@ -356,16 +356,18 @@ trait Cont:
// wrap body in between output var declarations and var references // wrap body in between output var declarations and var references
def letOutput[A1: Type]( def letOutput[A1: Type](
outputs: List[Output] outputs: List[Output]
)(body: Expr[A1]): Expr[(A1, Seq[VirtualFile])] = )(body: Expr[A1]): Expr[ActionCache.InternalActionResult[A1]] =
Block( Block(
outputs.map(_.toVarDef), outputs.map(_.toVarDef),
'{ '{
( ActionCache.InternalActionResult(
$body, value = $body,
List(${ Varargs[VirtualFile](outputs.map(_.toRef.asExprOf[VirtualFile])) }: _*) outputs = List(${
Varargs[VirtualFile](outputs.map(_.toRef.asExprOf[VirtualFile]))
}: _*),
) )
}.asTerm }.asTerm
).asExprOf[(A1, Seq[VirtualFile])] ).asExprOf[ActionCache.InternalActionResult[A1]]
val WrapOutputName = "wrapOutput_\u2603\u2603" val WrapOutputName = "wrapOutput_\u2603\u2603"
// Called when transforming the tree to add an input. // Called when transforming the tree to add an input.

View File

@ -33,7 +33,7 @@ object ActionCache:
extraHash: Digest, extraHash: Digest,
tags: List[CacheLevelTag], tags: List[CacheLevelTag],
)( )(
action: I => (O, Seq[VirtualFile]) action: I => InternalActionResult[O],
)( )(
config: BuildWideCacheConfiguration config: BuildWideCacheConfiguration
): O = ): O =
@ -44,8 +44,8 @@ object ActionCache:
def organicTask: O = def organicTask: O =
// run action(...) and combine the newResult with outputs // run action(...) and combine the newResult with outputs
val (result, outputs) = val InternalActionResult(result, outputs) =
try action(key) try action(key): @unchecked
catch catch
case e: Exception => case e: Exception =>
cacheEventLog.append(ActionCacheEvent.Error) cacheEventLog.append(ActionCacheEvent.Error)
@ -90,6 +90,21 @@ object ActionCache:
if paths.isEmpty then organicTask if paths.isEmpty then organicTask
else valueFromStr(IO.read(paths.head.toFile()), result.origin) else valueFromStr(IO.read(paths.head.toFile()), result.origin)
case Left(_) => organicTask case Left(_) => organicTask
/**
* Represents a value and output files, used internally by the macro.
*/
class InternalActionResult[A1] private (
val value: A1,
val outputs: Seq[VirtualFile],
)
end InternalActionResult
object InternalActionResult:
def apply[A1](value: A1, outputs: Seq[VirtualFile]): InternalActionResult[A1] =
new InternalActionResult(value, outputs)
private[sbt] def unapply[A1](r: InternalActionResult[A1]): Option[(A1, Seq[VirtualFile])] =
Some(r.value, r.outputs)
end InternalActionResult
end ActionCache end ActionCache
class BuildWideCacheConfiguration( class BuildWideCacheConfiguration(

View File

@ -11,6 +11,7 @@ import xsbti.VirtualFileRef
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths import java.nio.file.Paths
import java.nio.file.Files import java.nio.file.Files
import ActionCache.InternalActionResult
object ActionCacheTest extends BasicTestSuite: object ActionCacheTest extends BasicTestSuite:
val tags = CacheLevelTag.all.toList val tags = CacheLevelTag.all.toList
@ -35,9 +36,9 @@ object ActionCacheTest extends BasicTestSuite:
def testActionCacheBasic(cache: ActionCacheStore): Unit = def testActionCacheBasic(cache: ActionCacheStore): Unit =
import sjsonnew.BasicJsonProtocol.* import sjsonnew.BasicJsonProtocol.*
var called = 0 var called = 0
val action: ((Int, Int)) => (Int, Seq[VirtualFile]) = { case (a, b) => val action: ((Int, Int)) => InternalActionResult[Int] = { case (a, b) =>
called += 1 called += 1
(a + b, Nil) InternalActionResult(a + b, Nil)
} }
IO.withTemporaryDirectory: (tempDir) => IO.withTemporaryDirectory: (tempDir) =>
val config = getCacheConfig(cache, tempDir) val config = getCacheConfig(cache, tempDir)
@ -57,10 +58,10 @@ object ActionCacheTest extends BasicTestSuite:
import sjsonnew.BasicJsonProtocol.* import sjsonnew.BasicJsonProtocol.*
IO.withTemporaryDirectory: (tempDir) => IO.withTemporaryDirectory: (tempDir) =>
var called = 0 var called = 0
val action: ((Int, Int)) => (Int, Seq[VirtualFile]) = { case (a, b) => val action: ((Int, Int)) => InternalActionResult[Int] = { case (a, b) =>
called += 1 called += 1
val out = StringVirtualFile1(s"$tempDir/a.txt", (a + b).toString) val out = StringVirtualFile1(s"$tempDir/a.txt", (a + b).toString)
(a + b, Seq(out)) InternalActionResult(a + b, Seq(out))
} }
val config = getCacheConfig(cache, tempDir) val config = getCacheConfig(cache, tempDir)
val v1 = val v1 =