Skip to content

Commit

Permalink
validate in check
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Feb 1, 2025
1 parent ff10c6e commit a3dc357
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 101 deletions.
4 changes: 4 additions & 0 deletions core/src/main/scala/org/bykn/bosatsu/hashing/Algo.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bykn.bosatsu.hashing

import cats.arrow.FunctionK
import cats.parse.Parser
import pt.kcry.blake3.{Blake3 => B3, Hasher => B3Hasher}

Expand Down Expand Up @@ -51,6 +52,9 @@ object Algo {
def algo: Algo[A]
def value: F[A]

def mapK[G[_]](fn: FunctionK[F, G]): WithAlgo[G] =
WithAlgo[G, A](algo, fn(value))

override def equals(obj: Any): Boolean =
obj match {
case e: WithAlgo[_] => (e.algo == algo) && (e.value == value)
Expand Down
76 changes: 52 additions & 24 deletions core/src/main/scala/org/bykn/bosatsu/library/Command.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bykn.bosatsu.library

import cats.{Monad, MonoidK}
import cats.arrow.FunctionK
import cats.data.NonEmptyList
import com.monovore.decline.Opts
import org.bykn.bosatsu.tool.{
Expand All @@ -10,7 +11,7 @@ import org.bykn.bosatsu.tool.{
PathGen,
PackageResolver
}
import org.bykn.bosatsu.hashing.{Algo, HashValue}
import org.bykn.bosatsu.hashing.{Algo, Hashed, HashValue}
import org.bykn.bosatsu.LocationMap.Colorize
import org.bykn.bosatsu.{Json, PlatformIO}
import org.typelevel.paiges.Doc
Expand Down Expand Up @@ -253,13 +254,15 @@ object Command {
)
}
prevLib <- prevLibPath.traverse(platformIO.readLibrary(_))
prevLibDec <- prevLib.traverse(DecodedLibrary.decode(_))
packages <- platformIO.readPackages(packs)
depLibs <- deps.traverse(platformIO.readLibrary(_))
decLibs <- depLibs.traverse(DecodedLibrary.decode(_))
maybeNewLib = conf.assemble(
vcsIdent = gitSha,
prevLib,
prevLibDec,
packages,
depLibs
decLibs
)
lib <- moduleIOMonad.fromTry(LibConfig.Error.toTry(maybeNewLib))
} yield (Output.Library(lib, outPath): Output[P])
Expand Down Expand Up @@ -313,22 +316,31 @@ object Command {
inputSrcs <- PathGen
.recursiveChildren(confDir, ".bosatsu")(platformIO)
.read
_ <- NonEmptyList.fromList(inputSrcs) match {
allPacks <- NonEmptyList.fromList(inputSrcs) match {
case Some(inputNel) =>
platformIO.withEC { ec =>
CompilerApi.typeCheck(
platformIO,
inputNel,
pubDecodes.flatMap(_.interfaces) ::: privDecodes.flatMap(
_.interfaces
),
colorize,
inputRes
)(ec)
}.void
platformIO
.withEC { ec =>
CompilerApi.typeCheck(
platformIO,
inputNel,
pubDecodes.flatMap(_.interfaces) ::: privDecodes.flatMap(
_.interfaces
),
colorize,
inputRes
)(ec)
}
.map(_._1.toMap.values.toList.map(_.void))
case None =>
moduleIOMonad.unit
moduleIOMonad.pure(Nil)
}
prevThis = None // TODO
validated = conf.validate(
prevThis,
allPacks,
pubDecodes ::: privDecodes
)
_ <- moduleIOMonad.fromTry(LibConfig.Error.toTry(validated))
} yield Doc.text("")
}

Expand Down Expand Up @@ -381,7 +393,9 @@ object Command {

def casPaths(
dep: proto.LibDependency
): SortedMap[Algo.WithAlgo[HashValue], P] =
): SortedMap[Algo.WithAlgo[HashValue], Algo.WithAlgo[Lambda[
A => Hashed[A, P]
]]] =
hashes(dep)
.map { withAlgo =>
val algoName = withAlgo.algo.name
Expand All @@ -391,22 +405,35 @@ object Command {
val path =
platformIO.resolve(casDir, algoName :: hex1 :: hex2 :: Nil)

(withAlgo, path)
(
withAlgo,
withAlgo.mapK(new FunctionK[HashValue, Lambda[A => Hashed[A, P]]] {
def apply[A](fn: HashValue[A]) = Hashed(fn, path)
})
)
}
.to(SortedMap)

def libFromCas(dep: proto.LibDependency): F[Option[proto.Library]] =
casPaths(dep).values.toList.collectFirstSomeM { path =>
def libFromCas(
dep: proto.LibDependency
): F[Option[Hashed[Algo.Blake3, proto.Library]]] =
casPaths(dep).values.toList.collectFirstSomeM { hashed =>
val path = hashed.value.arg
platformIO.fileExists(path).flatMap {
case true => platformIO.readLibrary(path).map(h => Some(h.arg))
case true => platformIO.readLibrary(path).map(h => Some(h))
case false => Monad[F].pure(None)
}
}

def depsFromCas(
pubDeps: List[proto.LibDependency],
privDeps: List[proto.LibDependency]
): F[(List[proto.Library], List[proto.Library])] =
): F[
(
List[Hashed[Algo.Blake3, proto.Library]],
List[Hashed[Algo.Blake3, proto.Library]]
)
] =
(
pubDeps.parTraverse(dep => libFromCas(dep).map(dep -> _)),
privDeps.parTraverse(dep => libFromCas(dep).map(dep -> _))
Expand Down Expand Up @@ -467,7 +494,8 @@ object Command {
val paths = casPaths(dep)
val uris = depUris(dep)

paths.transform { (hashValue, path) =>
paths.transform { (hashValue, hashed) =>
val path = hashed.value.arg
platformIO
.fileExists(path)
.flatMap {
Expand Down Expand Up @@ -547,7 +575,7 @@ object Command {
case None => Nil
case Some(dep) =>
// we will find the transitivies by walking them
dep.publicDependencies.toList ::: dep.privateDependencies.toList
dep.arg.publicDependencies.toList ::: dep.arg.privateDependencies.toList
}

fetchedDeps.filterNot { dep =>
Expand Down
Loading

0 comments on commit a3dc357

Please sign in to comment.