-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log the args diff when re-running the smithy4s codegen (#1559)
This PR introduces a change in Smithy4s sbt codegen plugin that would allow users to see what caused the codegen to re-execute. It is done by replacing `sbt.util.Tracked.inputChanged` with a similar implementation that keeps the entire codegen args serialized in cache, and when codegen needs to be rerun, plugin will print the diff of the values to debug log. The diff is calculated using [munit-diff](https://scalameta.org/munit/blog/2024/05/22/release-1.0.0.html#diff-module-extracted-to-a-separate-module). When running `sbt` with `set logLevel := Level.Debug`, user will notice a diff like this when codegen needs to rerun:
- Loading branch information
Showing
5 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
modules/codegen-plugin/src/sbt-test/codegen-plugin/defaults/test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
modules/codegen-plugin/src/smithy4s/codegen/CachedTask.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2021-2024 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.codegen | ||
|
||
import munit.diff.Diff | ||
import sbt._ | ||
import sbt.util.CacheImplicits._ | ||
import sbt.util.CacheStore | ||
import sbt.util.Logger | ||
import sjsonnew._ | ||
import sjsonnew.support.murmurhash.Hasher | ||
import sjsonnew.support.scalajson.unsafe.Converter | ||
import sjsonnew.support.scalajson.unsafe.{PrettyPrinter => prettify} | ||
|
||
import scala.util.Try | ||
|
||
private[codegen] object CachedTask { | ||
|
||
// This implementation is inspired by sbt.util.Tracked.inputChanged | ||
// The main difference is that when the values don't match, the difference is calculated | ||
// using munit-diff and recorded to debug log | ||
def inputChanged[I: JsonFormat, O](store: CacheStore, logger: Logger)( | ||
f: (Boolean, I) => O | ||
): I => O = { in => | ||
def debug(str: String): Unit = logger.debug(s"[smithy4s] $str") | ||
|
||
val previousValue = Try(store.read[ValueAndHash[I]]()).toOption | ||
val newValueHash = hash(in) | ||
store.write[ValueAndHash[I]]((in, newValueHash)) | ||
|
||
previousValue match { | ||
case None => | ||
debug("Could not read previous inputs value from cache.") | ||
f(true, in) | ||
|
||
case Some((oldValue, previousHash)) => | ||
(toJson(oldValue), toJson(in)) match { | ||
case (Some(oldArgs), Some(newArgs)) if !oldArgs.equals(newArgs) => | ||
val diff = new Diff(prettify(oldArgs), prettify(newArgs)) | ||
val report = diff.createReport( | ||
"Arguments changed between smithy4s codegen invocations, diff:", | ||
printObtainedAsStripMargin = false | ||
) | ||
debug(report) | ||
f(true, in) | ||
|
||
case (_, _) if (previousHash != newValueHash) => | ||
debug( | ||
"Codegen arguments didn't change, but their hashes didn't match. " + | ||
"This means file change on paths provided as codegen arguments." | ||
) | ||
f(true, in) | ||
|
||
case _ => | ||
debug("Input didn't change between codegen invocations") | ||
f(false, in) | ||
} | ||
|
||
} | ||
} | ||
|
||
private type ValueAndHash[I] = (I, Int) | ||
|
||
private def toJson[I: JsonFormat](args: I) = | ||
Converter.toJson(args).toOption | ||
|
||
private def hash[I: JsonFormat](in: I) = | ||
Hasher.hash(in).toOption.getOrElse(-1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters