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

Allow modifying generated sources (eg to hook up scalafmt), closes #235 #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ Compile / guardrailTasks := (Compile / guardrailDiscoveredOpenApiFiles).value.fl
)
}
```
Formatting generated sources with scalafmt:
```
Compile / scalafmt / unmanagedSources ++= (Compile / guardrail).value
```
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ ThisBuild / developers := List(
)
)

ThisBuild / scalaVersion := "2.12.18"
ThisBuild / scalacOptions := Seq("-target:jvm-1.8")
//ThisBuild / pluginCrossBuild / sbtVersion := "1.5.0" //breaks on windows https://github.com/sbt/sbt/issues/7082
ThisBuild / scalacOptions ++= List("-feature", "-Xexperimental")

libraryDependencies ++= Seq(
Expand All @@ -44,14 +45,16 @@ git.gitDescribedVersion := git.gitDescribedVersion(v => {

git.gitUncommittedChanges := git.gitCurrentTags.value.isEmpty

val commonSettings = Seq(
val commonSettings: SettingsDefinition = Seq(
// Release
publishMavenStyle := true,
sonatypeCredentialHost := "s01.oss.sonatype.org",
evictionErrorLevel := Level.Debug
)


scriptedBufferLog := false

scriptedLaunchOpts := { scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
}
Expand Down
38 changes: 18 additions & 20 deletions modules/core/src/main/scala/AbstractCodegenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sbt

import _root_.sbt.{Keys => SbtKeys, Types => _, _}
import _root_.sbt.plugins.JvmPlugin
import _root_.sbt.nio.{Keys => SbtNioKeys}
import dev.guardrail.runner.GuardrailRunner
import dev.guardrail.terms.protocol.PropertyRequirement
import dev.guardrail.{
Expand Down Expand Up @@ -187,36 +188,33 @@ trait AbstractGuardrailPlugin { self: AutoPlugin =>
lazy val GuardrailHelpers = _root_.dev.guardrail.sbt.GuardrailHelpers
}

private def cachedGuardrailTask(projectName: String, scope: String, scalaBinaryVersion: String)(kind: String, streams: _root_.sbt.Keys.TaskStreams)(tasks: List[(String, Args)], sources: Seq[java.io.File]) = {
val inputFiles = tasks.flatMap(_._2.specPath).map(file(_)).toSet
val cacheDir = streams.cacheDirectory / "guardrail" / scalaBinaryVersion / kind

val cachedFn = _root_.sbt.util.FileFunction
.cached(cacheDir, inStyle = FilesInfo.hash, outStyle = FilesInfo.hash) {
_ =>
GuardrailAnalysis(
BuildInfo.version,
Tasks.guardrailTask(runner.guardrailRunner)(tasks, sources.head)
).products
}

cachedFn(inputFiles).toSeq
}

def scopedSettings(name: String, scope: Configuration) = {
def scopedSettings(scope: Configuration): SettingsDefinition = {
import _root_.sbt.Keys.{resourceDirectory, sourceDirectory, unmanagedResourceDirectories, unmanagedSourceDirectories}
Seq(
scope / unmanagedSourceDirectories += (scope / sourceDirectory).value / "openapi",
scope / unmanagedResourceDirectories += (scope / resourceDirectory).value / "openapi",
scope / Keys.guardrailDiscoveredOpenApiFiles := GuardrailHelpers.discoverOpenApiFiles((scope / sourceDirectory).value / "openapi"),
scope / Keys.guardrailTasks := List.empty,
scope / Keys.guardrail := cachedGuardrailTask(SbtKeys.name.value, scope.name, SbtKeys.scalaBinaryVersion.value)(name, _root_.sbt.Keys.streams.value)((scope / Keys.guardrailTasks).value, (scope / SbtKeys.managedSourceDirectories).value),

scope / Keys.guardrail / SbtNioKeys.fileInputs := (scope / Keys.guardrailTasks).value.flatMap(_._2.specPath).map( p => Glob(file(p).getAbsoluteFile)),
scope / Keys.guardrail / SbtKeys.sourceManaged := (scope / SbtKeys.sourceManaged).value / "guardrail",
scope / Keys.guardrail := {
val tasks = (scope / Keys.guardrailTasks).value
val csf = (scope / Keys.guardrail / SbtKeys.streams).value.cacheStoreFactory
.sub(SbtKeys.scalaBinaryVersion.value)
.sub(BuildInfo.version)
.sub(Hash.toHex(Hash(tasks.toString)))
val output = (scope / Keys.guardrail / SbtKeys.sourceManaged).value
val cached = FileFunction.cached(csf, inStyle = FileInfo.hash, outStyle = FileInfo.exists) { (_, _) =>
Tasks.guardrailTask(runner.guardrailRunner)(tasks, output)
}
cached((scope / Keys.guardrail / SbtNioKeys.allInputFiles).value.map(_.toFile).toSet).toSeq
},
scope / SbtKeys.sourceGenerators += (scope / Keys.guardrail).taskValue,
scope / SbtKeys.watchSources ++= Tasks.watchSources((scope / Keys.guardrailTasks).value),
)
}

override lazy val projectSettings = {
scopedSettings("compile", Compile) ++ scopedSettings("test", Test)
scopedSettings(Compile) ++ scopedSettings(Test)
}
}
21 changes: 0 additions & 21 deletions modules/core/src/main/scala/GuardrailAnalysis.scala

This file was deleted.

4 changes: 0 additions & 4 deletions modules/core/src/main/scala/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,4 @@ object Tasks {
paths.map(_.toFile).toSet
} finally Thread.currentThread().setContextClassLoader(oldClassLoader)
}

def watchSources(tasks: List[Types.Args]): Seq[WatchSource] = {
tasks.flatMap(_._2.specPath.map(new File(_)).map(WatchSource(_))).toSeq
}
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.8
sbt.version=1.10.1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.8
sbt.version=1.5.0
6 changes: 3 additions & 3 deletions src/sbt-test/sbt-guardrail/java-codegen-app/test
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
> clean
$ absent target/scala-2.12/src_managed/main/com/example/clients/petstore/user/UserClient.java
$ absent target/scala-2.12/src_managed/main/guardrail/com/example/clients/petstore/user/UserClient.java
$ absent target/scala-2.12/classes/com/example/clients/petstore/user/UserClient.class
> compile
$ exists target/scala-2.12/src_managed/main/com/example/clients/petstore/user/UserClient.java
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/clients/petstore/user/UserClient.java
$ exists target/scala-2.12/classes/com/example/clients/petstore/user/UserClient.class
> compile
$ exists target/scala-2.12/src_managed/main/com/example/clients/petstore/user/UserClient.java
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/clients/petstore/user/UserClient.java
$ exists target/scala-2.12/classes/com/example/clients/petstore/user/UserClient.class
> test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.8
sbt.version=1.5.0
10 changes: 5 additions & 5 deletions src/sbt-test/sbt-guardrail/scala-client-codegen-app/test
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
> clean
$ absent target/scala-2.12/src_managed/main/com/example/petstore/client/user/UserClient.scala
$ absent target/scala-2.12/src_managed/main/guardrail/com/example/petstore/client/user/UserClient.scala
$ absent target/scala-2.12/classes/com/example/petstore/client/user/UserClient.class
> compile
$ exists target/scala-2.12/src_managed/main/com/example/petstore/client/user/UserClient.scala
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/petstore/client/user/UserClient.scala
$ exists target/scala-2.12/classes/com/example/petstore/client/user/UserClient.class
$ exists target/scala-2.12/src_managed/main/com/example/petstore/server/user/Routes.scala
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/petstore/server/user/Routes.scala
$ exists target/scala-2.12/classes/com/example/petstore/server/user/UserHandler.class
> compile
$ exists target/scala-2.12/src_managed/main/com/example/petstore/client/user/UserClient.scala
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/petstore/client/user/UserClient.scala
$ exists target/scala-2.12/classes/com/example/petstore/client/user/UserClient.class
$ exists target/scala-2.12/src_managed/main/com/example/petstore/server/user/Routes.scala
$ exists target/scala-2.12/src_managed/main/guardrail/com/example/petstore/server/user/Routes.scala
$ exists target/scala-2.12/classes/com/example/petstore/server/user/UserHandler.class
> test
1 change: 1 addition & 0 deletions src/sbt-test/sbt-guardrail/scalafmt/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = 2.5.2
16 changes: 16 additions & 0 deletions src/sbt-test/sbt-guardrail/scalafmt/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

name := "sbt-guardrail-scala-client-test-app"

version := "1.0." + System.currentTimeMillis

scalaVersion := "2.12.18"

scalacOptions += "-Xexperimental"

Compile / guardrailTasks := GuardrailHelpers.createGuardrailTasks((Compile / sourceDirectory).value / "openapi") { openApiFile =>
List(
ScalaClient(openApiFile.file),
)
}

Compile / scalafmt / unmanagedSources ++= (Compile / guardrail).value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.5.0
12 changes: 12 additions & 0 deletions src/sbt-test/sbt-guardrail/scalafmt/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("dev.guardrail" % "sbt-guardrail" % pluginVersion)
}

libraryDependencies += "dev.guardrail" %% "guardrail-scala-support" % "1.0.0-M1"
libraryDependencies += "dev.guardrail" %% "guardrail-scala-akka-http" % "1.0.0-M1"

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"swagger": "2.0",
"host": "petstore.swagger.io",
"basePath": "/v2",
"schemes": [
"http"
],
"definitions": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
},
"quantity": {
"type": "integer",
"format": "int32"
},
"shipDate": {
"type": "string",
"format": "date-time"
},
"status": {
"type": "string",
"description": "Order Status",
"x-scala-type": "OrderStatus"
},
"complete": {
"type": "boolean",
"default": false
}
},
"xml": {
"name": "Order"
}
}
}
}
10 changes: 10 additions & 0 deletions src/sbt-test/sbt-guardrail/scalafmt/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
> clean
$ absent target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala
> Compile / guardrail
$ exists target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala
$ copy-file target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala created
> Compile / scalafmt
$ newer target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala created
$ copy-file target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala formatted
> Compile / guardrail
-$ newer target/scala-2.12/src_managed/main/guardrail/swagger/definitions/Order.scala formatted
Loading