From d6936032517c2a859de268301ca38b893c94a2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Tue, 15 Jan 2019 20:29:20 +0700 Subject: [PATCH] Collect all the module trees in the plugin and query that from directive (#10) One downside is that it now requires to populate `paradoxDependenciesModules` on the use side. But on the other hand, the access to the dependency tree is now done without touching the state directly. Fixes #9 --- README.md | 4 +++ .../ParadoxDependenciesPlugin.scala | 27 ++++++++++++++----- .../ParadoxDependenciesPluginKeys.scala | 9 ++++++- .../dependencies/multi-module/build.sbt | 13 ++++----- .../docs/src/main/paradox/_template/page.st | 1 + .../docs/src/main/paradox/index.md | 3 +++ src/sbt-test/dependencies/multi-module/test | 8 +++++- 7 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 src/sbt-test/dependencies/multi-module/docs/src/main/paradox/_template/page.st create mode 100644 src/sbt-test/dependencies/multi-module/docs/src/main/paradox/index.md diff --git a/README.md b/README.md index e511e24..02eb25b 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,14 @@ addSbtPlugin("com.lightbend.paradox" % "sbt-paradox-dependencies" % ) ``` Use the directive in a Paradox markdown file and specify the sbt project id. + ``` @@dependencies { projectId="core" } ``` +Any project from the build root project aggregates can be specified. If the project is not among the aggregates, then it needs +to be added to the `paradoxDependenciesProjects` setting value. + ## License The license is Apache 2.0, see LICENSE. diff --git a/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPlugin.scala b/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPlugin.scala index 4875ddb..9b9ebdd 100644 --- a/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPlugin.scala +++ b/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPlugin.scala @@ -33,16 +33,31 @@ object ParadoxDependenciesPlugin extends AutoPlugin { override def projectSettings: Seq[Setting[_]] = dependenciesSettings(Compile) - def dependenciesGlobalSettings: Seq[Setting[_]] = Seq( + def dependenciesZeroSettings: Seq[Setting[_]] = Seq( + paradoxDependenciesModuleTrees := Def.taskDyn { + val projectsToFilter = paradoxDependenciesProjects.?.value + .map(inProjects) + .getOrElse { + inAggregates(LocalRootProject, includeRoot = true) + } + + val filter: ScopeFilter = ScopeFilter(projectsToFilter, inConfigurations(Compile)) + + val projectIdWithTree = Def.task { + (thisProject.value.id, ModuleTree(DependencyGraphKeys.moduleGraphSbt.value)) + } + + projectIdWithTree.all(filter).map(_.toMap) + }.value, paradoxDirectives ++= Def.taskDyn { Def.task { - val s = state.value + val trees = paradoxDependenciesModuleTrees.value Seq( { _: Writer.Context ⇒ new DependenciesDirective(projectId => { - Project.runTask(LocalProject(projectId) / Compile / DependencyGraphKeys.moduleGraphSbt, s) match { - case Some((_, Value(deps))) => ModuleTree(deps) - case _ => throw new Error(s"Could not retrieve dependency information for projectId [$projectId]") + trees.get(projectId) match { + case Some(deps) => deps + case _ => throw new Error(s"Could not retrieve dependency information for project [$projectId]") } }) } @@ -52,7 +67,7 @@ object ParadoxDependenciesPlugin extends AutoPlugin { ) def dependenciesSettings(config: Configuration): Seq[Setting[_]] = - dependenciesGlobalSettings ++ inConfig(config)( + dependenciesZeroSettings ++ inConfig(config)( Seq( // scoped settings here )) diff --git a/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPluginKeys.scala b/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPluginKeys.scala index b9280b6..1bd145a 100644 --- a/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPluginKeys.scala +++ b/src/main/scala/com/lightbend/paradox/dependencies/ParadoxDependenciesPluginKeys.scala @@ -16,4 +16,11 @@ package com.lightbend.paradox.dependencies -trait ParadoxDependenciesPluginKeys {} +import net.virtualvoid.sbt.graph.ModuleTree + +import sbt._ + +trait ParadoxDependenciesPluginKeys { + val paradoxDependenciesProjects = settingKey[Seq[ProjectReference]]("Projects to get the dependency information for") + val paradoxDependenciesModuleTrees = taskKey[Map[String, ModuleTree]]("Retrieved module trees") +} diff --git a/src/sbt-test/dependencies/multi-module/build.sbt b/src/sbt-test/dependencies/multi-module/build.sbt index 5086c57..3520781 100644 --- a/src/sbt-test/dependencies/multi-module/build.sbt +++ b/src/sbt-test/dependencies/multi-module/build.sbt @@ -1,8 +1,3 @@ -enablePlugins(ParadoxPlugin) -paradoxTheme := None - -libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.5.17" - lazy val projectA = project .settings( libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.5.17" @@ -16,7 +11,9 @@ lazy val projectB = Project("projectBee", file("bee")) libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.25" ) -lazy val multiModule = project - .aggregate( - projectB +lazy val multiModule = (project in file(".")) + .settings( + paradoxTheme := None ) + .enablePlugins(ParadoxPlugin) + .aggregate(projectB) diff --git a/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/_template/page.st b/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/_template/page.st new file mode 100644 index 0000000..ca90149 --- /dev/null +++ b/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/_template/page.st @@ -0,0 +1 @@ +$page.content$ \ No newline at end of file diff --git a/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/index.md b/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/index.md new file mode 100644 index 0000000..1d49e65 --- /dev/null +++ b/src/sbt-test/dependencies/multi-module/docs/src/main/paradox/index.md @@ -0,0 +1,3 @@ +This module has the following dependencies: + +@@dependencies { module="module1" } diff --git a/src/sbt-test/dependencies/multi-module/test b/src/sbt-test/dependencies/multi-module/test index e8f37a4..4ffe691 100644 --- a/src/sbt-test/dependencies/multi-module/test +++ b/src/sbt-test/dependencies/multi-module/test @@ -1,3 +1,9 @@ +# expect to fail, because projectA is not part of the root aggregate +-> paradox + +> set paradoxDependenciesProjects := Seq(projectA, projectB) + +# now plugin knows about all projects and is able to generate dependency info for all > paradox -$ must-mirror target/paradox/site/main/index.html expected/index.html \ No newline at end of file +$ must-mirror target/paradox/site/main/index.html expected/index.html