Skip to content

Commit

Permalink
Implement new CompileTimeCalibanClientPlugin `ctCalibanClientsVersion…
Browse files Browse the repository at this point in the history
…edCode` option (#1087)

* Implement new `CompileTimeCalibanClientPlugin` `ctCalibanClientsVersionedCode` option

* Fix tests

* Fix tests

* Don't regenerate on outputChanged

* Add doc

* Add doc

* Add doc

Co-authored-by: Pierre Ricadat <[email protected]>
  • Loading branch information
guizmaii and ghostdogpr authored Oct 14, 2021
1 parent a1caddf commit 5d3497a
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ object CompileTimeCalibanClientPlugin extends AutoPlugin {
"(Required) List of projects being configured with the `CompileTimeCalibanServerPlugin` plugin for which we want to generate client(s)"
)

// ## Optional Plugin configurations
lazy val ctCalibanClientsVersionedCode: SettingKey[Boolean] =
settingKey[Boolean](
"(Optional) If true, the generated client will be in the `src/main/scala` of your sbt module. If false, the code will be generated in `target/scala_x.xx/src_managed`. Default: `true`"
)

// ## Plugin task
lazy val ctCalibanClientGenerate: TaskKey[Seq[File]] = taskKey[Seq[File]](
"Generate Caliban Client(s) code at compile time. Automatically configured to be triggered when compilation is triggered"
Expand All @@ -230,6 +236,7 @@ object CompileTimeCalibanClientPlugin extends AutoPlugin {
inTask(ctCalibanClient)(
Seq(
ctCalibanClientsSettings := Seq.empty,
ctCalibanClientsVersionedCode := true,
ctCalibanClientGenerate := {
// That helped: https://stackoverflow.com/q/26244115/2431728
Def.taskDyn {
Expand All @@ -240,7 +247,12 @@ object CompileTimeCalibanClientPlugin extends AutoPlugin {
val clientsSettings: Seq[Project] = (ctCalibanClient / ctCalibanClientsSettings).value
if (clientsSettings.isEmpty) Def.task { log.error(helpMsg); Seq.empty[File] }
else {
val baseDirValue: String = (thisProject / baseDirectory).value.absolutePath
val (baseDirValue: String, isVersioned: Boolean) =
Def.settingDyn {
val isVersioned: Boolean = (ctCalibanClient / ctCalibanClientsVersionedCode).value
(if (isVersioned) (thisProject / baseDirectory) else (thisProject / sourceManaged))
.map(_.absolutePath -> isVersioned)
}.value

def generateSources: Def.Initialize[Task[Seq[File]]] =
Def.taskDyn {
Expand Down Expand Up @@ -326,7 +338,8 @@ object CompileTimeCalibanClientPlugin extends AutoPlugin {
caliban.codegen.BuildInfo.version,
zio.BuildInfo.version,
clientsSettings.map(_.id).mkString,
serverProjectSettings.mkString
serverProjectSettings.mkString,
isVersioned.toString
)
)
}
Expand Down Expand Up @@ -389,9 +402,9 @@ private[caliban] object Functions {

Tracked.inputChanged(cacheDirectory / s"$cacheName-inputs") { (inChanged: Boolean, _: TrackedSettings) =>
Tracked.outputChanged(cacheDirectory / s"$cacheName-output") {
(outChanged: Boolean, outputs: FilesInfo[PlainFileInfo]) =>
(_: Boolean, outputs: FilesInfo[PlainFileInfo]) =>
Def.taskIf {
if (inChanged || outChanged) generateSources.value
if (inChanged) generateSources.value
else outputs.files.toList.map(_.file)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ThisBuild / homepage := Some(url("https://www.conduktor.io/"))
ThisBuild / licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0"))
ThisBuild / version := "0.0.1"
ThisBuild / scalaVersion := "2.12.14" // Must stay 2.12 in these tests because the plugin is compiled with 2.12
ThisBuild / resolvers += Resolver.mavenLocal
ThisBuild / resolvers += Resolver.sonatypeRepo("snapshots")

// ### Dependencies ###

Expand Down Expand Up @@ -37,7 +39,8 @@ lazy val root =
posts,
potatoes,
clients,
calibanClients
postsClients,
potatoesClients
)
.settings(
// Additional scripted tests commands
Expand Down Expand Up @@ -109,13 +112,23 @@ lazy val clients =
project
.in(file("modules/clients"))
.settings(libraryDependencies ++= sttp)
.dependsOn(calibanClients)
.dependsOn(postsClients, potatoesClients)

lazy val calibanClients =
lazy val postsClients =
project
.withId("caliban-clients")
.in(file("modules/caliban-clients"))
.withId("posts-clients")
.in(file("modules/posts-clients"))
.enablePlugins(CompileTimeCalibanClientPlugin)
.settings(
Compile / ctCalibanClient / ctCalibanClientsSettings := Seq(posts, potatoes)
Compile / ctCalibanClient / ctCalibanClientsSettings := Seq(posts),
Compile / ctCalibanClient / ctCalibanClientsVersionedCode := false
)

lazy val potatoesClients =
project
.withId("potatoes-clients")
.in(file("modules/potatoes-clients"))
.enablePlugins(CompileTimeCalibanClientPlugin)
.settings(
Compile / ctCalibanClient / ctCalibanClientsSettings := Seq(potatoes)
)
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sbt.librarymanagement.Resolver

resolvers += Resolver.mavenLocal
resolvers += Resolver.sonatypeRepo("snapshots")

sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("com.github.ghostdogpr" % "caliban-codegen-sbt" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
Expand Down
Loading

0 comments on commit 5d3497a

Please sign in to comment.