From e74356f96daf49fcfcf35983014342161baa921a Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Fri, 17 Aug 2018 14:41:16 -0600 Subject: Add workspace plugin --- README.md | 27 +++++++++++++----- build.sbt | 1 - .../scala/xyz.driver.sbt/WorkspacePlugin.scala | 32 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/main/scala/xyz.driver.sbt/WorkspacePlugin.scala diff --git a/README.md b/README.md index 5406e8d..1900e81 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ activated out-of-the-box. | LibraryPlugin | manual | | ServicePlugin | manual | | IntegrationTestPackaging | automatic, if ServicePlugin is enabled | +| WorkspacePlugin | automatic | ### Lint Plugin @@ -46,13 +47,6 @@ activated out-of-the-box. This plugin can get in the way of developer productivity. If that is the case, it can simply be disabled. -### Integration Test Packaging - -*[source](src/main/scala/xyz.driver.sbt/IntegrationTestPackaging.scala)* - -Augments the packaging configuration of ServicePlugin, to include -integration tests in deployed applications images. - ### Library Plugin *[source](src/main/scala/xyz.driver.sbt/LibraryPlugin.scala)* @@ -73,6 +67,22 @@ It also includes some utility commands such as `start` and `stop` (based on [sbt-revolver](https://github.com/spray/sbt-revolver), to enable rapid local development cycles of applications. +### Integration Test Packaging + +*[source](src/main/scala/xyz.driver.sbt/IntegrationTestPackaging.scala)* + +Augments the packaging configuration of ServicePlugin, to include +integration tests in deployed applications images. + +### Workspace Plugin + +*[source](src/main/scala/xyz.driver.sbt/WorkspacePlugin.scala)* + +Adds a `dependsOn` extension method to projects, that allows depending +on both source and binary versions of a project. It is similar to +sbt-sriracha and is intended to increase productivity in multi-project +workflows. + ## Canonical Use Case sbt-settings provides many plugins, which may be used in various ways. Typically however, a project is either a Library or a Service, @@ -89,6 +99,9 @@ lazy val myservice = project .enablePlugin(ServicePlugin) .disablePlugins(IntegrationTestPackaging) // we don't need integration tests .disablePlugins(LintPlugin) // I don't need style check during development! + // use published artifact by default, and ../domain-model if SBT_WORKSPACE + // is defined as .. + .dependsOn("xyz.driver" %% "domain-model" % "1.2.3", "domain-model") .settings( /* custom settings */) ``` diff --git a/build.sbt b/build.sbt index ae42a23..60a922c 100644 --- a/build.sbt +++ b/build.sbt @@ -10,7 +10,6 @@ addSbtPlugin("io.spray" %% "sbt-revolver" % "0.9.1") addSbtPlugin("com.eed3si9n" %% "sbt-buildinfo" % "0.9.0") addSbtPlugin("com.typesafe.sbt" %% "sbt-git" % "1.0.0") addSbtPlugin("com.typesafe.sbt" %% "sbt-native-packager" % "1.3.6") -addSbtPlugin("com.github.gseitz" %% "sbt-release" % "1.0.8") // the following prevents thousands of meaningless stacktraces by docker plugin on JDK 9 libraryDependencies += "javax.activation" % "activation" % "1.1.1" % Test diff --git a/src/main/scala/xyz.driver.sbt/WorkspacePlugin.scala b/src/main/scala/xyz.driver.sbt/WorkspacePlugin.scala new file mode 100644 index 0000000..9a66eb7 --- /dev/null +++ b/src/main/scala/xyz.driver.sbt/WorkspacePlugin.scala @@ -0,0 +1,32 @@ +package xyz.driver.sbt + +import java.nio.file.{Path, Paths} + +import sbt._ + +/** Enables using both source and binary dependencies for the same module, + * for faster development cycles in multi-project workflows. + * Adapted from https://github.com/sbt/sbt-sriracha. */ +object WorkspacePlugin extends AutoPlugin { + + private var _workspace = sys.props.get("sbt.workspace").orElse(sys.env.get("SBT_WORKSPACE")).map { base => + Paths.get(base) + } + def workspace: Option[Path] = synchronized(_workspace) + + override val requires = plugins.JvmPlugin + override val trigger = allRequirements + + object autoImport { + implicit class WorkspaceProject(project: Project) { + def dependsOn(binary: ModuleID, projectName: String, directory: Option[String] = None): Project = + WorkspacePlugin.workspace match { + case Some(base) => + project.dependsOn( + ProjectRef(base.resolve(directory.getOrElse(projectName)).toUri, projectName) + ) + case None => project.settings(Keys.libraryDependencies += binary) + } + } + } +} -- cgit v1.2.3