Skip to content

Commit

Permalink
Fix hashing for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 6, 2022
1 parent e9452cd commit be82b9f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace smithy4s.example

structure Added {}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import smithy4s.dynamic.DynamicSchemaIndex

object Main extends App {
try {
println(smithy4s.example.Foo.IntCase(42))
println(smithy.api.NonEmptyString("nope").value)
} catch {
case _: java.lang.ExceptionInInitializerError =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ $ exists target/scala-2.13/resource_managed/main/smithy4s.example.ObjectService.

# check if code can run, this can reveal runtime issues
# such as initialization errors
> run
> run
$ copy-file example-added.smithy src/main/smithy/example-added.smithy
> compile
$ exists target/scala-2.13/src_managed/main/smithy4s/example/Added.scala

# ensuring that removing existing files removes their outputs
$ delete src/main/smithy/example.smithy
-> compile
17 changes: 16 additions & 1 deletion modules/codegen-plugin/src/smithy4s/codegen/JsonConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import sbt.HashFileInfo
import sjsonnew._
import cats.data.Validated.Invalid
import cats.data.Validated.Valid
import sbt.io.Hash

// Json codecs used by SBT's caching constructs
private[smithy4s] object JsonConverters {
Expand All @@ -32,7 +33,21 @@ private[smithy4s] object JsonConverters {
// changes and invalidate its relevant caches, leading to a call to Smithy4s' code generator.
implicit val pathFormat: JsonFormat[os.Path] =
BasicJsonProtocol.projectFormat[os.Path, HashFileInfo](
p => FileInfo.hash(p.toIO),
p => {
if (os.isFile(p)) FileInfo.hash(p.toIO)
else
// If the path is a directory, we get the hashes of all files
// then hash the concatenation of the hash's bytes.
FileInfo.hash(
p.toIO,
Hash(
os.walk(p)
.map(_.toIO)
.map(Hash(_))
.foldLeft(Array.emptyByteArray)(_ ++ _)
)
)
},
hash => os.Path(hash.file)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Smithy4sModuleSpec extends munit.FunSuite {
private val coreDep =
ivy"com.disneystreaming.smithy4s::smithy4s-core:${smithy4s.codegen.BuildInfo.version}"

test("basic codegen runs") {
test("basic codegen runs".only) {
object foo extends testKit.BaseModule with Smithy4sModule {
override def scalaVersion = "2.13.8"
override def ivyDeps = Agg(coreDep)
Expand All @@ -45,6 +45,18 @@ class Smithy4sModuleSpec extends munit.FunSuite {
ev.outPath / "smithy4sOutputDir.dest" / "scala" / "basic" / "MyNewString.scala",
shouldExist = true
)

withFile(
foo.millSourcePath / "smithy" / "added.smithy",
"""namespace basic
|
|structure Added {}""".stripMargin
)(compileWorks(foo, ev))

checkFileExist(
ev.outPath / "smithy4sOutputDir.dest" / "scala" / "basic" / "Added.scala",
shouldExist = true
)
}

test("codegen with dependencies") {
Expand Down Expand Up @@ -109,22 +121,20 @@ class Smithy4sModuleSpec extends munit.FunSuite {
shouldExist = true
)

val aScalaPath = foo.millSourcePath / "src" / "a.scala"

os.write(
aScalaPath,
withFile(
foo.millSourcePath / "src" / "a.scala",
"""package foo
|object a""".stripMargin,
createFolders = true
)
|object a""".stripMargin
)(compileWorks(bar, barEv))
}

try compileWorks(bar, barEv)
finally {
val _ =
// cleaning up, because the target path doesn't get cleared automatically on test re-runs
// (it's part of this test module's target path)
os.remove.all(aScalaPath)
}
private def withFile[A](path: os.Path, content: String)(f: => A): A = {
os.write(path, content, createFolders = true)
try f
finally
// we need to clean up, because we copy files to the target path
// (which doesn't get cleared automatically on test re-runs)
os.remove.all(path)
}

test(
Expand Down

0 comments on commit be82b9f

Please sign in to comment.