Skip to content

Commit

Permalink
use blake3 to simplify build
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Jan 18, 2025
1 parent 6f805f7 commit 640cc78
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 68 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ lazy val core =
assembly / test := {},
libraryDependencies ++=
Seq(
blake3.value,
cats.value,
catsParse.value,
decline.value,
Expand All @@ -206,7 +207,6 @@ lazy val core =
.dependsOn(base, proto)
.jsSettings(
commonJsSettings,
Compile / npmDependencies += "js-sha256" -> "0.11.0"
)

lazy val coreJVM = core.jvm
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/scala/org/bykn/bosatsu/IOPlatformIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ object IOPlatformIO extends PlatformIO[IO, JPath] {
def readPackages(paths: List[Path]): IO[List[Package.Typed[Unit]]] =
readInterfacesAndPackages(Nil, paths).map(_._2)

def readLibrary(path: Path): IO[Hashed[Algo.Sha256, proto.Library]] =
readHashed[proto.Library, Algo.Sha256](path)
def readLibrary(path: Path): IO[Hashed[Algo.Blake3, proto.Library]] =
readHashed[proto.Library, Algo.Blake3](path)

def writeInterfaces(
interfaces: List[Package.Interface],
Expand Down
4 changes: 2 additions & 2 deletions cliJS/src/main/scala/org/bykn/bosatsu/Fs2PlatformIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ object Fs2PlatformIO extends PlatformIO[IO, Path] {
}
.map(_.flatten)

def readLibrary(path: Path): IO[Hashed[Algo.Sha256, proto.Library]] =
readHashed[proto.Library, Algo.Sha256](path)
def readLibrary(path: Path): IO[Hashed[Algo.Blake3, proto.Library]] =
readHashed[proto.Library, Algo.Blake3](path)

/** given an ordered list of prefered roots, if a packFile starts with one of
* these roots, return a PackageName based on the rest
Expand Down
24 changes: 0 additions & 24 deletions core/.js/src/main/scala/org/bykn/bosatsu/hashing/Sha256Hash.scala

This file was deleted.

6 changes: 3 additions & 3 deletions core/src/main/scala/org/bykn/bosatsu/MemoryMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object MemoryMain {
case class Str(str: String) extends FileContent
case class Packages(ps: List[Package.Typed[Unit]]) extends FileContent
case class Interfaces(ifs: List[Package.Interface]) extends FileContent
case class Lib(lib: Hashed[Algo.Sha256, proto.Library]) extends FileContent
case class Lib(lib: Hashed[Algo.Blake3, proto.Library]) extends FileContent
}

case class State(children: SortedMap[String, Either[State, FileContent]], stdOut: Doc, stdErr: Doc) {
Expand Down Expand Up @@ -260,15 +260,15 @@ object MemoryMain {
.map(_.flatten)
}

def readLibrary(path: Path): F[Hashed[Algo.Sha256, proto.Library]] =
def readLibrary(path: Path): F[Hashed[Algo.Blake3, proto.Library]] =
StateT
.get[G, MemoryMain.State]
.flatMap { files =>
files.get(path) match {
case Some(Right(MemoryMain.FileContent.Lib(lib))) =>
moduleIOMonad.pure(lib)
case other =>
moduleIOMonad.raiseError[Hashed[Algo.Sha256, proto.Library]](
moduleIOMonad.raiseError[Hashed[Algo.Blake3, proto.Library]](
new Exception(s"expect Library content, found: $other")
)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/bykn/bosatsu/PlatformIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ trait PlatformIO[F[_], Path] {

def readPackages(paths: List[Path]): F[List[Package.Typed[Unit]]]
def readInterfaces(paths: List[Path]): F[List[Package.Interface]]
def readLibrary(path: Path): F[Hashed[Algo.Sha256, proto.Library]]
def readLibrary(path: Path): F[Hashed[Algo.Blake3, proto.Library]]

/** given an ordered list of prefered roots, if a packFile starts with one of
* these roots, return a PackageName based on the rest
Expand Down
19 changes: 11 additions & 8 deletions core/src/main/scala/org/bykn/bosatsu/hashing/Algo.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bykn.bosatsu.hashing

import cats.parse.Parser
import pt.kcry.blake3.{Blake3 => B3}

sealed abstract class Algo[A] {
def name: String
Expand All @@ -15,15 +16,17 @@ case class HashValue[A](hex: String) {
}

object Algo {
trait Sha256
object Sha256 extends Sha256
trait Blake3
object Blake3 extends Blake3

implicit val sha256Algo: Algo[Sha256] =
new Algo[Sha256] {
def name: String = "sha256"
implicit val blake3Algo: Algo[Blake3] =
new Algo[Blake3] {
def name: String = "blake3"
def hexLen: Int = 64
def hashBytes(bytes: Array[Byte]): HashValue[Sha256] =
HashValue(Sha256Hash.sha256HashHex(bytes))
def hashBytes(bytes: Array[Byte]): HashValue[Blake3] =
HashValue(
B3.newHasher().update(bytes).doneHex(64)
)
}

sealed abstract class WithAlgo[F[_]] {
Expand Down Expand Up @@ -65,5 +68,5 @@ object Algo {
}

val parseIdent: Parser[WithAlgo[HashValue]] =
parseHashValue(sha256Algo)
parseHashValue(blake3Algo)
}
12 changes: 6 additions & 6 deletions core/src/main/scala/org/bykn/bosatsu/library/LibConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ case class LibConfig(
*/
def assemble(
vcsIdent: String,
previous: Option[Hashed[Algo.Sha256, proto.Library]],
previous: Option[Hashed[Algo.Blake3, proto.Library]],
packs: List[Package.Typed[Unit]],
deps: List[Hashed[Algo.Sha256, proto.Library]]): ValidatedNec[Error, proto.Library] = {
deps: List[Hashed[Algo.Blake3, proto.Library]]): ValidatedNec[Error, proto.Library] = {
val validated = validate(previous, packs, deps)

val valProto = unvalidatedAssemble(previous, vcsIdent, packs) match {
Expand All @@ -54,9 +54,9 @@ case class LibConfig(
* 8. there are no duplicate named dependencies
*/
def validate(
previous: Option[Hashed[Algo.Sha256, proto.Library]],
previous: Option[Hashed[Algo.Blake3, proto.Library]],
packs: List[Package.Typed[Unit]],
deps: List[Hashed[Algo.Sha256, proto.Library]]): ValidatedNec[Error, Unit] = {
deps: List[Hashed[Algo.Blake3, proto.Library]]): ValidatedNec[Error, Unit] = {

def inv(e: Error): ValidatedNec[Error, Nothing] = Validated.invalidNec(e)

Expand Down Expand Up @@ -156,7 +156,7 @@ case class LibConfig(
}

// just build the library without any validations
def unvalidatedAssemble(previous: Option[Hashed[Algo.Sha256, proto.Library]], vcsIdent: String, packs: List[Package.Typed[Unit]]): Either[Throwable, proto.Library] = {
def unvalidatedAssemble(previous: Option[Hashed[Algo.Blake3, proto.Library]], vcsIdent: String, packs: List[Package.Typed[Unit]]): Either[Throwable, proto.Library] = {
val depth = previous match {
case None => 0
case Some(Hashed(_, prevLib)) => prevLib.depth + 1
Expand Down Expand Up @@ -223,7 +223,7 @@ object LibConfig {
case class InvalidPreviousLib(note: String, previous: proto.Library) extends Error
case class DuplicateDep(note: String, name: String, desc: proto.LibDescriptor) extends Error
case class MissingDep(note: String, dep: proto.LibDependency) extends Error
case class DepHashMismatch(note: String, dep: proto.LibDependency, foundHash: HashValue[Algo.Sha256], found: proto.Library) extends Error
case class DepHashMismatch(note: String, dep: proto.LibDependency, foundHash: HashValue[Algo.Blake3], found: proto.Library) extends Error

def errorsToDoc(nec: NonEmptyChain[Error]): Doc =
Doc.intercalate(Doc.hardLine + Doc.hardLine,
Expand Down
21 changes: 0 additions & 21 deletions core/src/test/scala/org/bykn/bosatsu/hashing/Sha256HashTest.scala

This file was deleted.

1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sbt._
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._

object Dependencies {
lazy val blake3 = Def.setting("pt.kcry" %%% "blake3" % "3.1.2")
lazy val cats = Def.setting("org.typelevel" %%% "cats-core" % "2.12.0")
lazy val catsEffect =
Def.setting("org.typelevel" %%% "cats-effect" % "3.5.7")
Expand Down

0 comments on commit 640cc78

Please sign in to comment.