Skip to content

Commit

Permalink
Support multiple input dirs (sbt, mill) (#587)
Browse files Browse the repository at this point in the history
Closes #464
  • Loading branch information
lewisjkl authored Nov 11, 2022
1 parent 1a22e68 commit 539b3b2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
lazy val commonSettings = Def.settings(
scalaVersion := "2.13.6",
Compile / smithy4sInputDir := (ThisBuild / baseDirectory).value / "smithy_input",
Compile / smithy4sInputDirs := Seq(
(ThisBuild / baseDirectory).value / "smithy_input"
),
Compile / smithy4sAllowedNamespaces := List(
"aws.iam",
"smithy4s.example"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
val smithy4sVersion =
settingKey[String]("Smithy4sVersion")

val smithy4sInputDir =
settingKey[File](
"Input directory for smithy specs (.smithy or .json files)"
)
val smithy4sInputDirs = settingKey[Seq[File]](
"Input directories for smithy specs (.smithy or .json files)"
)

val smithy4sOutputDir =
settingKey[File](
Expand Down Expand Up @@ -98,7 +97,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {

// Use this with any configuration to enable the codegen in it.
def defaultSettings(config: Configuration) = Seq(
config / smithy4sInputDir := (config / sourceDirectory).value / "smithy",
config / smithy4sInputDirs := (config / unmanagedSourceDirectories).value
.map(_.getParentFile() / "smithy"),
config / smithy4sOutputDir := (config / sourceManaged).value,
config / smithy4sResourceDir := (config / resourceManaged).value,
config / smithy4sCodegen := cachedSmithyCodegen(config).value,
Expand Down Expand Up @@ -139,7 +139,7 @@ object Smithy4sCodegenPlugin extends AutoPlugin {

def cachedSmithyCodegen(conf: Configuration) = Def.task {
val inputFiles =
Option((conf / smithy4sInputDir).value)
Option((conf / smithy4sInputDirs).value).toSeq.flatten
.filter(_.exists())
.toList
val outputPath = (conf / smithy4sOutputDir).value
Expand Down
12 changes: 6 additions & 6 deletions modules/docs/markdown/01-overview/03-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ val myModule = project

This will enable the plugin on `myModule`. We also need to add `smithy4s-core ` here since it is needed for compiling the generated code.

By default, the plugin will look in the `$MY_MODULE/src/main/smithy` directory and will write scala code in `$MY_MODULE/target/scala-<version>/src_managed/` when invoking `compile`. The paths are configurable via the `smithy4sInputDir` and `smithy4sOutputDir` settings keys.
By default, the plugin will look in the `$MY_MODULE/src/main/smithy` directory and will write scala code in `$MY_MODULE/target/scala-<version>/src_managed/` when invoking `compile`. The paths are configurable via the `smithy4sInputDirs` and `smithy4sOutputDir` settings keys.

For example, in order for the plugin to source `.smithy` specs from `./smithy_input` (inside the folder where our `build.sbt` is) and output the generated files into `./smithy_output`.

Expand All @@ -41,8 +41,8 @@ val myModule = project
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
scalaVersion := "@SCALA_VERSION@",
smithy4sInputDir in Compile := (ThisBuild / baseDirectory).value / "smithy_input",
smithy4sOutputDir in Compile := (ThisBuild / baseDirectory).value / "smithy_output",
Compile / smithy4sInputDirs := Seq((ThisBuild / baseDirectory).value / "smithy_input"),
Compile / smithy4sOutputDir := (ThisBuild / baseDirectory).value / "smithy_output",
libraryDependencies += "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value
)
```
Expand All @@ -66,7 +66,7 @@ object example extends ScalaModule with Smithy4sModule {
}
```

By default, the `mill` plugin will look for Smithy files under the `$MY_MODULE/smithy` directory. The generated code ends up in `out/$MY_MODULE/smithy4sOutputDir.dest/scala/`, again, by default. Code generation happens automatically when you before you `compile` the module. The paths are configurable via the `smithy4sInputDir` and `smithy4sOutputDir` tasks.
By default, the `mill` plugin will look for Smithy files under the `$MY_MODULE/smithy` directory. The generated code ends up in `out/$MY_MODULE/smithy4sOutputDir.dest/scala/`, again, by default. Code generation happens automatically when you before you `compile` the module. The paths are configurable via the `smithy4sInputDirs` and `smithy4sOutputDir` tasks.

For example, here we'll read Smithy files from `smithy_input` and write to `smithy_output`.

Expand All @@ -81,8 +81,8 @@ object example extends ScalaModule with Smithy4sModule {
ivy"com.disneystreaming.smithy4s::smithy4s-core:${smithy4sVersion()}"
)

override def smithy4sInputDir = T.source {
PathRef(T.ctx().workspace / "smithy_input")
override def smithy4sInputDirs = T.sources {
Seq(PathRef(T.ctx().workspace / "smithy_input"))
}
override def smithy4sOutputDir = T {
PathRef(T.ctx().workspace / "smithy_output")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package smithy4s.codegen.mill
import coursier.maven.MavenRepository
import mill._
import mill.api.PathRef
import mill.define.Source
import mill.define.Sources
import mill.scalalib._
import smithy4s.codegen.{CodegenArgs, Codegen => Smithy4s, FileType}
import smithy4s.codegen.BuildInfo

trait Smithy4sModule extends ScalaModule {

/** Input directory for .smithy files */
protected def smithy4sInputDir: Source = T.source {
PathRef(millSourcePath / "smithy")
protected def smithy4sInputDirs: Sources = T.sources {
Seq(PathRef(millSourcePath / "smithy"))
}

protected def smithy4sOutputDir: T[PathRef] = T {
Expand Down Expand Up @@ -85,7 +85,7 @@ trait Smithy4sModule extends ScalaModule {

def smithy4sCodegen: T[(PathRef, PathRef)] = T {

val specFiles = List(smithy4sInputDir().path).filter(os.exists(_))
val specFiles = smithy4sInputDirs().map(_.path).filter(os.exists(_))

val scalaOutput = smithy4sOutputDir().path
val resourcesOutput = smithy4sResourceOutputDir().path
Expand Down

0 comments on commit 539b3b2

Please sign in to comment.