Skip to content

Commit

Permalink
Improve cache in code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
majk-p committed Apr 22, 2024
1 parent b6c7897 commit e732967
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
14 changes: 9 additions & 5 deletions modules/codegen-plugin/src/smithy4s/codegen/JsonConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ private[smithy4s] object JsonConverters {
)

// format: off
type GenTarget = List[os.Path] :*: os.Path :*: os.Path :*: Set[FileType] :*: Boolean:*: Option[Set[String]] :*: Option[Set[String]] :*: List[String] :*: List[String] :*: List[String] :*: List[os.Path] :*: LNil
type GenTarget = List[os.Path] :*: String :*: String :*: Set[FileType] :*: Boolean:*: Option[Set[String]] :*: Option[Set[String]] :*: List[String] :*: List[String] :*: List[String] :*: List[os.Path] :*: LNil
// format: on

// `output` and `resourceOutput` are intentionally serialized as strings
// instead paths. This is to avoid hashing the directories that are generated by smithy4s anyway
// See https://github.com/disneystreaming/smithy4s/issues/1495 for reference on this decision
implicit val codegenArgsIso = LList.iso[CodegenArgs, GenTarget](
{ ca: CodegenArgs =>
("specs", ca.specs) :*:
("output", ca.output) :*:
("resourceOutput", ca.resourceOutput) :*:
("output", ca.output.toString) :*:
("resourceOutput", ca.resourceOutput.toString) :*:
("skip", ca.skip) :*:
("discoverModels", ca.discoverModels) :*:
("allowedNS", ca.allowedNS) :*:
Expand All @@ -92,8 +96,8 @@ private[smithy4s] object JsonConverters {
(_, localJars) :*: LNil =>
CodegenArgs(
specs,
output,
resourceOutput,
os.Path(output),
os.Path(resourceOutput),
skip,
discoverModels,
allowedNS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
(inputDirs ++ generatedFiles)
.filter(_.exists())
.toList
val outputPath = (conf / smithy4sOutputDir).value
val resourceOutputPath = (conf / smithy4sResourceDir).value
val outputPath = (conf / smithy4sOutputDir).value / "smithy4s"
val resourceOutputPath = (conf / smithy4sResourceDir).value / "smithy4s"
val allowedNamespaces =
(conf / smithy4sAllowedNamespaces).?.value.map(_.toSet)
val excludedNamespaces =
Expand All @@ -424,7 +424,7 @@ object Smithy4sCodegenPlugin extends AutoPlugin {

val filePaths = inputFiles.map(_.getAbsolutePath())
val codegenArgs = CodegenArgs(
filePaths.map(os.Path(_)).toList,
filePaths.sorted.map(os.Path(_)).toList,
output = os.Path(outputPath),
resourceOutput = os.Path(resourceOutputPath),
skip = skipSet,
Expand All @@ -433,8 +433,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
excludedNS = excludedNamespaces,
repositories = res,
dependencies = List.empty,
transformers = transforms,
localJars = localJars
transformers = transforms.sorted,
localJars = localJars.sorted
)

val cached =
Expand All @@ -443,14 +443,16 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
) {
Function.untupled {
Tracked.lastOutput[(Boolean, CodegenArgs), Seq[File]](
s.cacheStoreFactory.make("output")
s.cacheDirectory / "smithy4s-output"
) { case ((inputChanged, args), outputs) =>
if (inputChanged || outputs.isEmpty) {
s.log.debug("Regenerating sources since")
val resPaths = smithy4s.codegen.Codegen
.generateToDisk(args)
.toList
resPaths.map(path => new File(path.toString))
} else {
s.log.debug("Using cached version of outputs")
outputs.getOrElse(Seq.empty)
}
}
Expand Down

0 comments on commit e732967

Please sign in to comment.