Skip to content

Commit

Permalink
Merge pull request #1 from alejandrohdezma/feature/all-modules
Browse files Browse the repository at this point in the history
Add `allModules` to plugin
  • Loading branch information
alejandrohdezma authored Jun 4, 2020
2 parents 853e2ce + 7ffe1f8 commit 178903c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ addCommandAlias("ci-publish", "github; ci-release")

lazy val documentation = project
.enablePlugins(MdocPlugin)
.dependsOn(allModules: _*)
.settings(mdocOut := file("."))

lazy val `sbt-modules` = module
Expand Down
21 changes: 20 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

```diff
lazy val docs = project
- .dependsOn(allProjects: _*)
+ .dependsOn(allModules: _*)
.in(file("docs"))

+ lazy val `my-library-core` = module
Expand All @@ -15,6 +17,11 @@ lazy val docs = project
- lazy val plugin = project
- .in(file("modules/plugin"))
- .settings(name := "my-library-plugin")
-
- lazy val allProjects: Seq[ClasspathDep[ProjectReference]] = Seq(
- core,
- plugin
- )
```

## Installation
Expand All @@ -31,7 +38,7 @@ Use `module` instead of `project` to create your SBT modules. Unlike `project`,

For example, the following SBT configuration:

```scala
```sbt
lazy val `my-library-core` = module

lazy val `my-library-plugin` = module
Expand All @@ -50,6 +57,18 @@ Would expect the following directory structure:
+-- project
```

### Retrieveing all modules created with `module`

`sbt-modules` creates a special variable called `allModules` that aggregates all the modules created with `module`, so you can pass it along as a dependency to other projects in your build, like:

```sbt
lazy val documentation = project.dependsOn(allModules: _*)

lazy val `my-library-core` = module

lazy val `my-library-plugin` = module
```

[github-action]: https://github.com/alejandrohdezma/sbt-modules/actions
[github-action-badge]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Falejandrohdezma%2Fsbt-modules%2Fbadge%3Fref%3Dmaster&style=flat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@

package com.alejandrohdezma.sbt.modules

import scala.collection.mutable
import scala.reflect.macros._

import sbt._

import scala.language.experimental.macros

@SuppressWarnings(Array("scalafix:Disable.scala.collection.mutable", "scalafix:DisableSyntax.implicitConversion"))
object ModulesPlugin extends AutoPlugin {

override def trigger = allRequirements

object autoImport {

/** List of all modules created with [[module]] */
val allModules: mutable.MutableList[Project] = mutable.MutableList[Project]()

implicit def MutableListProject2ListClasspathDependency(
list: mutable.MutableList[Project]
): List[ClasspathDep[ProjectReference]] =
list.map(classpathDependency(_)).toList

/**
* Creates a new Project with `modules` as base directory.
*
* This is a macro that expects to be assigned directly to a `val`.
* This is a macro that expects to be assigned directly to a `val`.
*
* The name of the val is used as the project ID and the name of its base
* The name of the val is used as the project ID and the name of its base
* directory inside `modules`.
*/
def module: Project = macro Macros.projectMacroImpl
Expand All @@ -54,7 +64,11 @@ object ModulesPlugin extends AutoPlugin {
val name = c.Expr[String](Literal(Constant(enclosingValName)))

reify {
Project(name.splice, file("modules") / name.splice)
val project = Project(name.splice, file("modules") / name.splice)

ModulesPlugin.autoImport.allModules += project

project
}
}

Expand Down
13 changes: 13 additions & 0 deletions modules/sbt-modules/src/sbt-test/sbt-modules/all-modules/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
lazy val a = module
lazy val b = module

lazy val c = project.dependsOn(allModules: _*)

TaskKey[Unit]("check", "Checks c depends on a & b using `allModules`") := {
val expected = List(
ClasspathDependency(LocalProject("b"), None),
ClasspathDependency(LocalProject("a"), None)
)

assert(c.dependencies == expected, s"Found: ${c.dependencies}\nExpected: $expected")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.alejandrohdezma" % "sbt-modules" % sys.props("plugin.version"))
2 changes: 2 additions & 0 deletions modules/sbt-modules/src/sbt-test/sbt-modules/all-modules/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this test ensures `allModules` contains the list of modules
> check

0 comments on commit 178903c

Please sign in to comment.