Skip to content

Commit

Permalink
Merge pull request #20 from daddykotex/dfrancoeur/build-improvements
Browse files Browse the repository at this point in the history
Improvements around smithy classpath configuration
  • Loading branch information
daddykotex authored Dec 30, 2023
2 parents 88c06d9 + 8430efc commit c8a8ef7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 54 deletions.
101 changes: 54 additions & 47 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,62 @@ lazy val frontend = (project in file("modules/frontend"))
buildInfoPackage := "smithy4s_codegen"
)

lazy val smithyClasspathSettings = Def.settings(
Universal / mappings ++= {
val depRes = dependencyResolution.value
val artifacts = smithyClasspath.value
val smithyClasspathOutput = target.value / "smithy-classpath"
val logger = sLog.value
val resolved = artifacts.flatMap { module =>
depRes.retrieve(module, None, target.value, logger) match {
case Left(value) =>
sys.error(s"Unable to resolve smithy classpath module $module")
case Right(value) => value.headOption.map(f => module -> f)
}
}
val smithyClasspathValue = smithyClasspathDir.value

val entries: Seq[SmithyClasspathEntry] =
resolved.map { case (module, file) =>
SmithyClasspathEntry(
module,
file,
s"$smithyClasspathValue/${file.name}"
)
}
val entriesMapping =
entries.map { case SmithyClasspathEntry(_, file, pathInImage) =>
file -> pathInImage
}

val smithyClasspathFile = target.value / "smithy-classpath.json"
SmithyClasspath.toFile(
smithyClasspathFile,
entries,
(Docker / defaultLinuxInstallLocation).value
)
val configMapping = Seq(
smithyClasspathFile -> s"$smithyClasspathValue/smithy-classpath.json"
)
entriesMapping ++ configMapping
},
dockerEnvVars ++= {
val inDockerPath = (Docker / defaultLinuxInstallLocation).value
val smithyClasspathValue = smithyClasspathDir.value
Map(
"SMITHY_CLASSPATH_CONFIG" -> s"$inDockerPath/$smithyClasspathValue/smithy-classpath.json"
)
}
)

lazy val backend = (project in file("modules/backend"))
.dependsOn(api)
.enablePlugins(
Smithy4sCodegenPlugin,
JavaAppPackaging,
DockerPlugin
)
.settings(smithyClasspathSettings)
.settings(
name := "smithy4s-code-generation-backend",
fork := true,
Expand All @@ -93,6 +142,8 @@ lazy val backend = (project in file("modules/backend"))
"software.amazon.smithy" % "smithy-model" % smithyVersion,
"org.http4s" %% "http4s-ember-server" % http4sVersion
),
smithyClasspath := Seq.empty,
smithyClasspathDir := "smithy-classpath",
Compile / resourceGenerators += Def.task {
val dir = frontend.base
val distDir = dir / "dist"
Expand Down Expand Up @@ -144,48 +195,12 @@ lazy val backend = (project in file("modules/backend"))
*/
lazy val backendDependencies = project
.enablePlugins(DockerPlugin)
.settings(smithyClasspathSettings)
.settings(
smithyClasspath := Seq(
"com.disneystreaming.alloy" % "alloy-core" % "0.2.8"
),
Universal / mappings := {
val depRes = dependencyResolution.value
val artifacts = smithyClasspath.value
val smithyClasspathOutput = target.value / "smithy-classpath"
val logger = sLog.value
val resolved = artifacts.flatMap { module =>
depRes.retrieve(module, None, target.value, logger) match {
case Left(value) =>
sys.error(s"Unable to resolve smithy classpath module $module")
case Right(value) => value.headOption.map(f => module -> f)
}
}
val smithyClasspathValue = smithyClasspathDir.value

val entries: Seq[SmithyClasspathEntry] =
resolved.map { case (module, file) =>
SmithyClasspathEntry(
module,
file,
s"$smithyClasspathValue/${file.name}"
)
}
val entriesMapping =
entries.map { case SmithyClasspathEntry(_, file, pathInImage) =>
file -> pathInImage
}

val smithyClasspathFile = target.value / "smithy-classpath.json"
SmithyClasspath.toFile(
smithyClasspathFile,
entries,
(Docker / defaultLinuxInstallLocation).value
)
val configMapping = Seq(
smithyClasspathFile -> s"$smithyClasspathValue/smithy-classpath.json"
)
entriesMapping ++ configMapping
},
smithyClasspathDir := "smithy-classpath",
Docker / packageName := "smithy4s-code-generation",
Docker / dockerRepository := Some("daddykotex"),
dockerAliases := {
Expand All @@ -204,13 +219,5 @@ lazy val backendDependencies = project
}
},
dockerEntrypoint := (backend / dockerEntrypoint).value,
dockerBaseImage := (backend / dockerAlias).value.toString,
smithyClasspathDir := "smithy-classpath",
dockerEnvVars ++= {
val inDockerPath = (Docker / defaultLinuxInstallLocation).value
val smithyClasspathValue = smithyClasspathDir.value
Map(
"SMITHY_CLASSPATH_CONFIG" -> s"$inDockerPath/$smithyClasspathValue/smithy-classpath.json"
)
}
dockerBaseImage := (backend / dockerAlias).value.toString
)
15 changes: 8 additions & 7 deletions modules/backend/src/main/scala/smithy4s_codegen/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ final class InvalidConfiguration(message: String)
final case class SmithyClasspathConfig(
entries: Map[String, FPath]
)
object SmithyClasspathConfig {
val Empty: SmithyClasspathConfig = SmithyClasspathConfig(Map.empty)
}

private final case class JsonSmithyClasspathConfig(
entries: Map[String, String]
Expand Down Expand Up @@ -56,17 +59,15 @@ object Config {
asFPath(jar).tupleLeft(dep)
}
.map(_.toMap)
.map(SmithyClasspathConfig)
.map(SmithyClasspathConfig.apply)
}
}

val smithyClasspathConfigFile =
val smithyClasspathConfig =
env("SMITHY_CLASSPATH_CONFIG")
.default("./smithy-classpath.json")
.evalMap { asFPath }

val smithyClasspathConfig = smithyClasspathConfigFile
.evalMap { loadSmithyClasspathConfig }
.evalMap { p => asFPath(p).flatMap(loadSmithyClasspathConfig) }
.option
.map(_.getOrElse(SmithyClasspathConfig.Empty))

val config = smithyClasspathConfig.map(Config.apply)

Expand Down

0 comments on commit c8a8ef7

Please sign in to comment.