Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dependency tracking mechanism #607

Merged
merged 10 commits into from
Nov 23, 2022
158 changes: 9 additions & 149 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ lazy val docs =
Dependencies.Http4s.emberServer.value,
Dependencies.Decline.effect.value
),
Compile / genSmithyDependencies ++= Seq(Dependencies.Smithy.testTraits),
Compile / smithy4sDependencies ++= Seq(Dependencies.Smithy.testTraits),
Compile / sourceGenerators := Seq(genSmithyScala(Compile).taskValue),
Compile / smithySpecs := Seq(
(Compile / sourceDirectory).value / "smithy",
Expand Down Expand Up @@ -160,7 +160,7 @@ lazy val core = projectMatrix
"smithy.waiters",
"alloy"
),
genSmithyDependencies ++= Seq(
smithy4sDependencies ++= Seq(
Dependencies.Smithy.waiters
),
Compile / sourceGenerators := Seq(genSmithyScala(Compile).taskValue),
Expand Down Expand Up @@ -261,7 +261,7 @@ lazy val `aws-kernel` = projectMatrix
Dependencies.Weaver.cats.value % Test,
Dependencies.Weaver.scalacheck.value % Test
),
genSmithyDependencies ++= Seq(Dependencies.Smithy.awsTraits),
smithy4sDependencies ++= Seq(Dependencies.Smithy.awsTraits),
Compile / allowedNamespaces := Seq(
"aws.api",
"aws.auth",
Expand Down Expand Up @@ -328,7 +328,7 @@ lazy val `aws-http4s` = projectMatrix
Dependencies.Http4s.emberClient.value % Test
)
},
Test / genSmithyDependencies ++= Seq(
Test / smithy4sDependencies ++= Seq(
Dependencies.Smithy.waiters,
Dependencies.Smithy.awsTraits
),
Expand Down Expand Up @@ -710,7 +710,7 @@ lazy val complianceTests = projectMatrix
.settings(
name := "compliance-tests",
Compile / allowedNamespaces := Seq("smithy.test", "smithy4s.example.test"),
Compile / genSmithyDependencies ++= Seq(Dependencies.Smithy.testTraits),
Compile / smithy4sDependencies ++= Seq(Dependencies.Smithy.testTraits),
Compile / sourceGenerators := Seq(genSmithyScala(Compile).taskValue),
isCE3 := virtualAxes.value.contains(CatsEffect3Axis),
libraryDependencies ++= {
Expand Down Expand Up @@ -825,146 +825,6 @@ lazy val benchmark = projectMatrix
.jvmPlatform(List(Scala213), jvmDimSettings)
.settings(Smithy4sBuildPlugin.doNotPublishArtifact)

val isCE3 = settingKey[Boolean]("Is the current build using CE3?")

lazy val Dependencies = new {

val collectionsCompat =
Def.setting(
"org.scala-lang.modules" %%% "scala-collection-compat" % "2.8.1"
)

val Jsoniter =
Def.setting(
"com.github.plokhotnyuk.jsoniter-scala" %%% "jsoniter-scala-core" % "2.17.9"
)

val Smithy = new {
val org = "software.amazon.smithy"
val version = "1.26.0"
val model = org % "smithy-model" % version
val testTraits = org % "smithy-protocol-test-traits" % version
val build = org % "smithy-build" % version
val awsTraits = org % "smithy-aws-traits" % version
val waiters = org % "smithy-waiters" % version
}

val Alloy = new {
val org = "com.disneystreaming.alloy"
val version = "0.1.0"
val core = org % "alloy-core" % version
val openapi = org %% "alloy-openapi" % version
}

val Cats = new {
val core: Def.Initialize[ModuleID] =
Def.setting("org.typelevel" %%% "cats-core" % "2.8.0")
}

object Decline {
val declineVersion = "2.3.1"

val core = Def.setting("com.monovore" %%% "decline" % declineVersion)
val effect =
Def.setting("com.monovore" %%% "decline-effect" % declineVersion)
}
object Fs2 {
val core: Def.Initialize[ModuleID] =
Def.setting("co.fs2" %%% "fs2-core" % "3.3.0")
}

object Mill {
val millVersion = "0.10.7"

val scalalib = "com.lihaoyi" %% "mill-scalalib" % millVersion
val main = "com.lihaoyi" %% "mill-main" % millVersion
val mainApi = "com.lihaoyi" %% "mill-main-api" % millVersion
val mainTestkit = "com.lihaoyi" %% "mill-main-testkit" % millVersion % Test
}

val Circe = new {
val generic: Def.Initialize[ModuleID] =
Def.setting("io.circe" %%% "circe-generic" % "0.14.3")
}

/*
* we override the version to use the fix included in
* https://github.com/typelevel/cats-effect/pull/2945
* it allows us to use UUIDGen instead of calling
* UUID.randomUUID manually
*
* we also provide a 2.12 shim under:
* modules/tests/src-ce2/UUIDGen.scala
*/
val CatsEffect3: Def.Initialize[ModuleID] =
Def.setting("org.typelevel" %%% "cats-effect" % "3.3.14")

object Http4s {
val http4sVersion = Def.setting(if (isCE3.value) "0.23.16" else "0.22.14")

val emberServer: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-ember-server" % http4sVersion.value)
val emberClient: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-ember-client" % http4sVersion.value)
val circe: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-circe" % http4sVersion.value)
val core: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-core" % http4sVersion.value)
val dsl: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-dsl" % http4sVersion.value)
val client: Def.Initialize[ModuleID] =
Def.setting("org.http4s" %%% "http4s-client" % http4sVersion.value)
}

object Weaver {

val weaverVersion = Def.setting(if (isCE3.value) "0.8.0" else "0.6.15")

val cats: Def.Initialize[ModuleID] =
Def.setting("com.disneystreaming" %%% "weaver-cats" % weaverVersion.value)

val scalacheck: Def.Initialize[ModuleID] =
Def.setting(
"com.disneystreaming" %%% "weaver-scalacheck" % weaverVersion.value
)
}

class MunitCross(munitVersion: String) {
val core: Def.Initialize[ModuleID] =
Def.setting("org.scalameta" %%% "munit" % munitVersion)
val scalacheck: Def.Initialize[ModuleID] =
Def.setting("org.scalameta" %%% "munit-scalacheck" % munitVersion)
}
object Munit extends MunitCross("0.7.29")
object MunitMilestone extends MunitCross("1.0.0-M6")

val Scalacheck = new {
val version = "1.16.0"
val scalacheck =
Def.setting("org.scalacheck" %%% "scalacheck" % version)
}

object Webjars {
val swaggerUi: ModuleID = "org.webjars.npm" % "swagger-ui-dist" % "4.15.2"

val webjarsLocator: ModuleID = "org.webjars" % "webjars-locator" % "0.42"
}

}

lazy val smithySpecs = SettingKey[Seq[File]]("smithySpecs")
lazy val genSmithyOutput = SettingKey[File]("genSmithyOutput")
lazy val genSmithyResourcesOutput = SettingKey[File]("genSmithyResourcesOutput")
lazy val allowedNamespaces = SettingKey[Seq[String]]("allowedNamespaces")
lazy val genSmithyDependencies =
SettingKey[Seq[ModuleID]]("genSmithyDependencies")

(ThisBuild / genSmithyDependencies) := Seq(Dependencies.Alloy.core)

lazy val smithy4sSkip = SettingKey[Seq[String]]("smithy4sSkip")

(ThisBuild / smithySpecs) := Seq.empty

def genSmithyScala(config: Configuration) = genSmithyImpl(config).map(_._1)
def genSmithyResources(config: Configuration) = genSmithyImpl(config).map(_._2)

Expand All @@ -983,8 +843,8 @@ def genSmithyImpl(config: Configuration) = Def.task {
.getAbsolutePath()
val allowedNS = (config / allowedNamespaces).?.value.filterNot(_.isEmpty)
val skip = (config / smithy4sSkip).?.value.getOrElse(Seq.empty)
val smithy4sDependencies =
(config / genSmithyDependencies).?.value.getOrElse(Seq.empty).map {
val smithy4sDeps =
(config / smithy4sDependencies).?.value.getOrElse(Seq.empty).map {
moduleId =>
s"${moduleId.organization}:${moduleId.name}:${moduleId.revision}"
}
Expand Down Expand Up @@ -1063,8 +923,8 @@ def genSmithyImpl(config: Configuration) = Def.task {
else Nil
val skipOpt = skip.flatMap(s => List("--skip", s))
val dependenciesOpt =
if (smithy4sDependencies.nonEmpty)
List("--dependencies", smithy4sDependencies.mkString(","))
if (smithy4sDeps.nonEmpty)
List("--dependencies", smithy4sDeps.mkString(","))
else Nil
val args = outputOpt ++
resourceOutputOpt ++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ lazy val foo = (project in file("foo"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
libraryDependencies ++= Seq(
"software.amazon.smithy" % "smithy-aws-traits" % smithyVersion % Smithy4s,
"com.disneystreaming.smithy4s" %% "smithy4s-aws-kernel" % smithy4sVersion.value
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ lazy val foo = (project in file("foo"))

lazy val bar = (project in file("bar"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(Compile / smithy4sLocalJars := Nil)
.settings(Compile / smithy4sInternalDependenciesAsJars := Nil)
.dependsOn(foo)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import foo._

object BarTest {

def main(args: Array[String]): Unit = println(Bar(Some(Foo(Some(1)))))
def main(args: Array[String]): Unit = {
println(Bar(Some(Foo(Some(1)))))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021-2022 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 baz

import foo._
import bar._

object BarTest {

def main(args: Array[String]): Unit = {
println(Baz(Some(Foo(Some(1)))))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$version: "2.0"

namespace baz

use foo#Foo

// Checking that Foo can be found by virtue of the upstream `bar` project
// defined as a compile-scope library dependency was published with an indication
// in the manifest that it used the `foo` project for code generation.
structure Baz {
foo: Foo
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import smithy4s.codegen.BuildInfo.smithyVersion

ThisBuild / scalaVersion := "2.13.10"
ThisBuild / version := "0.0.1-SNAPSHOT"
ThisBuild / organization := "foobar"
Expand All @@ -13,7 +15,21 @@ lazy val foo = (project in file("foo"))
lazy val bar = (project in file("bar"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
// Bar refers to foo explicitly in its ivy deps, and upon publishing,
// this information is stored in the manifest of bar's jar, for downstream
// consumption
libraryDependencies ++= Seq(
"foobar" %% "foo" % version.value % Smithy4sCompile
)
)

lazy val baz = (project in file("baz"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
// baz depend on bar, and an assumption is made that baz may depend on the same smithy models
// that bar depended on for its own codegen. Therefore, these are retrieved from bar's manifest,
// resolved and added to the list of jars to seek smithy models from during code generation
libraryDependencies ++= Seq(
"foobar" %% "bar" % version.value
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
> bar/compile
$ exists bar/target/scala-2.13/src_managed/main/bar/Bar.scala
$ absent bar/target/scala-2.13/src_managed/main/foo/Foo.scala
> bar/publishLocal
> baz/compile
$ exists baz/target/scala-2.13/src_managed/main/baz/Baz.scala
$ absent baz/target/scala-2.13/src_managed/main/foo/Foo.scala
$ absent baz/target/scala-2.13/src_managed/main/bar/Bar.scala

# check if code can run, this can reveal runtime issues# such as initialization errors
> bar/run
> baz/run
Loading