Skip to content

Commit

Permalink
Remove dependency on zio-config from caliban-tools (#2122)
Browse files Browse the repository at this point in the history
* Remove dependency from on zio-config from `tools`

* Add mima exclusions
kyri-petrou authored Feb 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 6393a7f commit 1e67c31
Showing 9 changed files with 134 additions and 140 deletions.
51 changes: 25 additions & 26 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.typesafe.tools.mima.core.{ DirectMissingMethodProblem, MissingClassProblem, ProblemFilters }
import org.scalajs.linker.interface.ModuleSplitStyle
import sbtcrossproject.CrossPlugin.autoImport.{ crossProject, CrossType }

@@ -170,29 +171,24 @@ lazy val core = project
.settings(enableMimaSettingsJVM)
.settings(
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= {
if (scalaVersion.value == scala212) {
Seq("org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0")
} else {
Seq()
}
} ++
libraryDependencies ++=
Seq(
"com.lihaoyi" %% "fastparse" % "3.0.2",
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-query" % zqueryVersion,
"dev.zio" %% "zio-prelude" % zioPreludeVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"dev.zio" %% "zio-json" % zioJsonVersion % Optional,
"com.softwaremill.sttp.tapir" %% "tapir-core" % tapirVersion % Optional,
"io.circe" %% "circe-core" % circeVersion % Optional,
"io.circe" %% "circe-parser" % circeVersion % Test,
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % jsoniterVersion % Optional,
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % jsoniterVersion % Provided,
"org.playframework" %% "play-json" % playJsonVersion % Optional,
"org.apache.commons" % "commons-lang3" % "3.14.0" % Test
"com.lihaoyi" %% "fastparse" % "3.0.2",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0",
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-query" % zqueryVersion,
"dev.zio" %% "zio-prelude" % zioPreludeVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"dev.zio" %% "zio-json" % zioJsonVersion % Optional,
"com.softwaremill.sttp.tapir" %% "tapir-core" % tapirVersion % Optional,
"io.circe" %% "circe-core" % circeVersion % Optional,
"io.circe" %% "circe-parser" % circeVersion % Test,
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % jsoniterVersion % Optional,
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % jsoniterVersion % Provided,
"org.playframework" %% "play-json" % playJsonVersion % Optional,
"org.apache.commons" % "commons-lang3" % "3.14.0" % Test
)
)
.dependsOn(macros)
@@ -221,8 +217,6 @@ lazy val tools = project
"org.scalameta" % "scalafmt-interfaces" % scalafmtVersion,
"io.get-coursier" % "interface" % "1.0.19",
"com.softwaremill.sttp.client3" %% "zio" % sttpVersion,
"dev.zio" %% "zio-config" % zioConfigVersion,
"dev.zio" %% "zio-config-magnolia" % zioConfigVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"dev.zio" %% "zio-json" % zioJsonVersion % Test
@@ -268,7 +262,9 @@ lazy val codegenSbt = project
crossScalaVersions := Seq(scala212),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")),
libraryDependencies ++= Seq(
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
"dev.zio" %% "zio-config" % zioConfigVersion,
"dev.zio" %% "zio-config-magnolia" % zioConfigVersion,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)
)
.enablePlugins(SbtPlugin)
@@ -717,7 +713,10 @@ lazy val enableMimaSettingsJVM =
Def.settings(
mimaFailOnProblem := enforceMimaCompatibility,
mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %% moduleName.value % _).toSet,
mimaBinaryIssueFilters ++= Seq()
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[MissingClassProblem]("caliban.tools.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("caliban.tools.*")
)
)

lazy val enableMimaSettingsJS =
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ object CalibanCli {
args: List[String],
genType: GenType
): Task[Unit] =
Options.fromArgs(args).flatMap {
OptionsParser.fromArgs(args).flatMap {
case Some(arguments) =>
for {
_ <- printLine(s"Generating code for ${arguments.schemaPath}")
83 changes: 83 additions & 0 deletions codegen-sbt/src/main/scala/caliban/codegen/OptionsParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package caliban.codegen

import caliban.tools.Options
import zio.config.magnolia.Descriptor
import zio.config.{ read, ConfigDescriptor, ConfigSource }
import zio.{ UIO, ZIO }

object OptionsParser {
final private case class RawOptions(
scalafmtPath: Option[String],
headers: Option[List[String]],
packageName: Option[String],
clientName: Option[String],
genView: Option[Boolean],
effect: Option[String],
scalarMappings: Option[List[String]],
imports: Option[List[String]],
abstractEffectType: Option[Boolean],
splitFiles: Option[Boolean],
enableFmt: Option[Boolean],
extensibleEnums: Option[Boolean],
preserveInputNames: Option[Boolean],
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String]
)

private object DescriptorUtils {
def from[A](configSource: ConfigSource)(implicit config: Descriptor[A]): ConfigDescriptor[A] =
Descriptor.descriptor[A] from configSource
}

def fromArgs(args: List[String]): UIO[Option[Options]] =
args match {
case schemaPath :: toPath :: other =>
val configSource: ConfigSource =
ConfigSource.fromCommandLineArgs(
args = other,
keyDelimiter = Some('.'),
valueDelimiter = Some(',')
)
val configDescriptor: ConfigDescriptor[RawOptions] = DescriptorUtils.from[RawOptions](configSource)

read[RawOptions](configDescriptor).map { rawOpts =>
Options(
schemaPath,
toPath,
rawOpts.scalafmtPath,
rawOpts.headers.map {
_.flatMap { rawHeader =>
rawHeader.split(":").toList match {
case name :: values if values.nonEmpty => Some(Options.Header(name, values.mkString(":")))
case _ => None
}
}
},
rawOpts.packageName,
rawOpts.clientName,
rawOpts.genView,
rawOpts.effect,
rawOpts.scalarMappings.map {
_.flatMap { rawMapping =>
rawMapping.split(":").toList match {
case name :: value :: Nil => Some(name -> value)
case _ => None
}
}.toMap
},
rawOpts.imports,
rawOpts.abstractEffectType,
rawOpts.splitFiles,
rawOpts.enableFmt,
rawOpts.extensibleEnums,
rawOpts.preserveInputNames,
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives
)
}.option
case _ => ZIO.none
}

}
Original file line number Diff line number Diff line change
@@ -3,5 +3,6 @@ object Version {
sys.props.get("plugin.version") match {
case Some(x) => x
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}}
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package caliban.tools
package caliban.codegen

import caliban.tools.Options
import caliban.tools.Options.Header
import zio.test._
import zio.test.*

object OptionsSpec extends ZIOSpecDefault {
object OptionsParserSpec extends ZIOSpecDefault {
override def spec =
suite("OptionsSpec")(
suite("OptionsParserSpec")(
test("full arguments") {
val input = List("schema", "output", "--scalafmtPath", "fmtPath", "--headers", "header1:value1,header2:value2")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result == Some(
Options(
@@ -37,7 +38,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("full arguments (--headers option first)") {
val input = List("schema", "output", "--headers", "header1:value1,header2:value2", "--scalafmtPath", "fmtPath")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result == Some(
Options(
@@ -66,7 +67,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("minimum arguments") {
val input = List("schema", "output")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -96,25 +97,25 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("not enough arguments") {
val input = List("schema")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(result == None)
}
},
test("--scalafmtPath value missing") {
val input = List("schema", "output", "--scalafmtPath", "--headers", "header1:value1,header2:value2")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(result == None)
}
},
test("empty list") {
val input = Nil
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(result == None)
}
},
test("provide package name") {
val input = List("schema", "output", "--packageName", "com.github.ghostdogpr")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -144,7 +145,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide client name") {
val input = List("schema", "output", "--clientName", "GraphqlClient.scala")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -174,7 +175,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide effect") {
val input = List("schema", "output", "--effect", "cats.effect.IO")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -204,7 +205,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide genView") {
val input = List("schema", "output", "--genView", "true")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -234,7 +235,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide extensibleEnums") {
val input = List("schema", "output", "--extensibleEnums", "true")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -264,7 +265,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide scalarMappings") {
val input = List("schema", "output", "--scalarMappings", "Long:scala.Long")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -294,7 +295,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide imports") {
val input = List("schema", "output", "--imports", "a.b.Clazz,b.c._")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -324,7 +325,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide abstractEffectType") {
val input = List("schema", "output", "--effect", "F", "--abstractEffectType", "true")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -354,7 +355,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("provide preserveInputNames") {
val input = List("schema", "output", "--preserveInputNames", "true")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
@@ -384,7 +385,7 @@ object OptionsSpec extends ZIOSpecDefault {
},
test("header with a colon in the value") {
val input = List("schema", "output", "--scalafmtPath", "fmtPath", "--headers", "aaa:bbb:ccc")
Options.fromArgs(input).map { result =>
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
9 changes: 0 additions & 9 deletions tools/src/main/scala-2/caliban/tools/DescriptorUtils.scala

This file was deleted.

9 changes: 0 additions & 9 deletions tools/src/main/scala-3/caliban/tools/DescriptorUtils.scala

This file was deleted.

72 changes: 0 additions & 72 deletions tools/src/main/scala/caliban/tools/Options.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package caliban.tools

import zio.{ UIO, ZIO }
import zio.config.magnolia.Descriptor
import zio.config.{ read, ConfigDescriptor, ConfigSource }

final case class Options(
schemaPath: String,
toPath: String,
@@ -27,72 +23,4 @@ final case class Options(

object Options {
final case class Header(name: String, value: String)
final case class RawOptions(
scalafmtPath: Option[String],
headers: Option[List[String]],
packageName: Option[String],
clientName: Option[String],
genView: Option[Boolean],
effect: Option[String],
scalarMappings: Option[List[String]],
imports: Option[List[String]],
abstractEffectType: Option[Boolean],
splitFiles: Option[Boolean],
enableFmt: Option[Boolean],
extensibleEnums: Option[Boolean],
preserveInputNames: Option[Boolean],
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String]
)

def fromArgs(args: List[String]): UIO[Option[Options]] =
args match {
case schemaPath :: toPath :: other =>
val configSource: ConfigSource =
ConfigSource.fromCommandLineArgs(
args = other,
keyDelimiter = Some('.'),
valueDelimiter = Some(',')
)
val configDescriptor: ConfigDescriptor[RawOptions] = DescriptorUtils.from[RawOptions](configSource)

read(configDescriptor).map { rawOpts =>
Options(
schemaPath,
toPath,
rawOpts.scalafmtPath,
rawOpts.headers.map {
_.flatMap { rawHeader =>
rawHeader.split(":").toList match {
case name :: values if values.nonEmpty => Some(Header(name, values.mkString(":")))
case _ => None
}
}
},
rawOpts.packageName,
rawOpts.clientName,
rawOpts.genView,
rawOpts.effect,
rawOpts.scalarMappings.map {
_.flatMap { rawMapping =>
rawMapping.split(":").toList match {
case name :: value :: Nil => Some(name -> value)
case _ => None
}
}.toMap
},
rawOpts.imports,
rawOpts.abstractEffectType,
rawOpts.splitFiles,
rawOpts.enableFmt,
rawOpts.extensibleEnums,
rawOpts.preserveInputNames,
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives
)
}.option
case _ => ZIO.none
}
}

0 comments on commit 1e67c31

Please sign in to comment.