Skip to content

Commit 17238cf

Browse files
committed
Add ability to filter configurations
Similar to filtering modules (stringbean#32), this allows you to exclude certain configurations from the lockfile, such as the Test or Optional scopes.
1 parent 6c6d509 commit 17238cf

File tree

12 files changed

+183
-6
lines changed

12 files changed

+183
-6
lines changed

src/main/paradox/settings.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ The different levels of checking available are:
2626

2727
* **Description:** Excludes the specified dependencies from the lockfile.
2828
* **Accepts:** `sbt.librarymanagement.ModuleFilter`
29-
* **Default:** `DependencyFilter.fnToModuleFilter(_ => false)` (no exclusions)
29+
* **Default:** `DependencyFilter.fnToModuleFilter(_ => false)` (no exclusions)
30+
31+
### dependencyLockConfigurationFilter
32+
33+
* **Description:** Excludes the specified configurations from the lockfile.
34+
* **Accepts:** `sbt.librarymanagement.ConfigurationFilter`
35+
* **Default:** `DependencyFilter.fnToConfigurationFilter(_ => false)` (no exclusions)

src/main/scala/software/purpledragon/sbt/lock/DependencyLockPlugin.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package software.purpledragon.sbt.lock
1919
import sbt._
2020
import sbt.Keys._
2121
import sbt.internal.util.ManagedLogger
22-
import sbt.librarymanagement.{DependencyFilter, ModuleFilter}
22+
import sbt.librarymanagement.{DependencyFilter, ModuleFilter, ConfigurationFilter}
2323
import software.purpledragon.sbt.lock.DependencyLockUpdateMode._
2424
import software.purpledragon.sbt.lock.model.{DependencyLockFile, LockFileMatches}
2525
import software.purpledragon.sbt.lock.util.MessageUtil
@@ -32,6 +32,7 @@ object DependencyLockPlugin extends AutoPlugin {
3232
val dependencyLockWrite = taskKey[File]("write dependencies to lockfile")
3333
val dependencyLockRead = taskKey[Option[DependencyLockFile]]("read dependencies from lockfile")
3434
val dependencyLockModuleFilter = settingKey[ModuleFilter]("exclusion filter for dependencies")
35+
val dependencyLockConfigurationFilter = settingKey[ConfigurationFilter]("exclusion filter for configurations")
3536

3637
val dependencyLockCheck = taskKey[Unit]("check if dependency lock is up to date")
3738

@@ -51,8 +52,10 @@ object DependencyLockPlugin extends AutoPlugin {
5152
val dest = dependencyLockFile.value
5253
val updateReport = update.value
5354
val exclusionFilter = dependencyLockModuleFilter.value
55+
val configFilter = dependencyLockConfigurationFilter.value
56+
val configurations = thisProject.value.configurations.filterNot(c => configFilter(c)).map(_.toConfigRef)
5457

55-
val lockFile = DependencyUtils.resolve(updateReport, exclusionFilter, thisProject.value.configurations.map(_.toConfigRef))
58+
val lockFile = DependencyUtils.resolve(updateReport, exclusionFilter, configurations)
5659

5760
val updateStatus = DependencyLockIO
5861
.readLockFile(dest)
@@ -72,9 +75,11 @@ object DependencyLockPlugin extends AutoPlugin {
7275
val logger: ManagedLogger = streams.value.log
7376
val updateReport: UpdateReport = update.value
7477
val exclusionFilter = dependencyLockModuleFilter.value
78+
val configFilter = dependencyLockConfigurationFilter.value
79+
val configurations = thisProject.value.configurations.filterNot(c => configFilter(c)).map(_.toConfigRef)
7580

7681
val currentFile = dependencyLockRead.value.getOrElse(sys.error(MessageUtil.formatMessage("lock.status.missing")))
77-
val updatedFile = DependencyUtils.resolve(updateReport, exclusionFilter, thisProject.value.configurations.map(_.toConfigRef))
82+
val updatedFile = DependencyUtils.resolve(updateReport, exclusionFilter, configurations)
7883

7984
val changes = currentFile.findChanges(updatedFile)
8085

@@ -93,13 +98,15 @@ object DependencyLockPlugin extends AutoPlugin {
9398
val skipCheck = state.value.currentCommand.map(_.commandLine).exists(PluginTasks.contains)
9499
val checkMode = dependencyLockAutoCheck.value
95100
val exclusionFilter = dependencyLockModuleFilter.value
101+
val configFilter = dependencyLockConfigurationFilter.value
102+
val configurations = thisProject.value.configurations.filterNot(c => configFilter(c)).map(_.toConfigRef)
96103

97104
if (checkMode != DependencyLockUpdateMode.CheckDisabled && !skipCheck) {
98105
logger.debug("Automatically checking lockfile")
99106

100107
dependencyLockRead.value match {
101108
case Some(currentFile) =>
102-
val updatedFile = DependencyUtils.resolve(report, exclusionFilter, thisProject.value.configurations.map(_.toConfigRef))
109+
val updatedFile = DependencyUtils.resolve(report, exclusionFilter, configurations)
103110

104111
val changes = currentFile.findChanges(updatedFile)
105112

@@ -136,6 +143,7 @@ object DependencyLockPlugin extends AutoPlugin {
136143

137144
override def globalSettings: Seq[Def.Setting[_]] = Seq(
138145
dependencyLockAutoCheck := DependencyLockUpdateMode.WarnOnError,
139-
dependencyLockModuleFilter := DependencyFilter.fnToModuleFilter(_ => false)
146+
dependencyLockModuleFilter := DependencyFilter.fnToModuleFilter(_ => false),
147+
dependencyLockConfigurationFilter := DependencyFilter.fnToConfigurationFilter(_ => false)
140148
)
141149
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
scalaVersion := "2.12.10"
2+
3+
libraryDependencies ++= Seq(
4+
"org.apache.commons" % "commons-lang3" % "3.9",
5+
"org.scalatest" %% "scalatest" % "3.0.8" % Test,
6+
)
7+
8+
// One ineffective filter and one for the Test scope.
9+
dependencyLockConfigurationFilter := configurationFilter(name = "fake") | configurationFilter(name = "test")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"lockVersion" : 1,
3+
"timestamp" : "2022-08-11T18:02:07.891395Z",
4+
"configurations" : [
5+
"compile",
6+
"optional",
7+
"provided",
8+
"runtime"
9+
],
10+
"dependencies" : [
11+
{
12+
"org" : "org.apache.commons",
13+
"name" : "commons-lang3",
14+
"version" : "3.9",
15+
"artifacts" : [
16+
{
17+
"name" : "commons-lang3.jar",
18+
"hash" : "sha1:0122c7cee69b53ed4a7681c03d4ee4c0e2765da5"
19+
}
20+
],
21+
"configurations" : [
22+
"compile",
23+
"runtime"
24+
]
25+
},
26+
{
27+
"org" : "org.scala-lang",
28+
"name" : "scala-library",
29+
"version" : "2.12.10",
30+
"artifacts" : [
31+
{
32+
"name" : "scala-library.jar",
33+
"hash" : "sha1:3509860bc2e5b3da001ed45aca94ffbe5694dbda"
34+
}
35+
],
36+
"configurations" : [
37+
"compile",
38+
"runtime"
39+
]
40+
}
41+
]
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.7.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
val pluginVersion = System.getProperty("plugin.version")
3+
if (pluginVersion == null)
4+
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
5+
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
6+
else addSbtPlugin("software.purpledragon" % "sbt-dependency-lock" % pluginVersion)
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> dependencyLockCheck
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
scalaVersion := "2.12.10"
2+
3+
libraryDependencies ++= Seq(
4+
"org.apache.commons" % "commons-lang3" % "3.9",
5+
"org.scalatest" %% "scalatest" % "3.0.8" % Test,
6+
)
7+
8+
// One ineffective filter and one for the Test scope.
9+
dependencyLockConfigurationFilter := configurationFilter(name = "fake") | configurationFilter(name = "test")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"lockVersion" : 1,
3+
"timestamp" : "2022-08-11T18:02:07.891395Z",
4+
"configurations" : [
5+
"compile",
6+
"optional",
7+
"provided",
8+
"runtime"
9+
],
10+
"dependencies" : [
11+
{
12+
"org" : "org.apache.commons",
13+
"name" : "commons-lang3",
14+
"version" : "3.9",
15+
"artifacts" : [
16+
{
17+
"name" : "commons-lang3.jar",
18+
"hash" : "sha1:0122c7cee69b53ed4a7681c03d4ee4c0e2765da5"
19+
}
20+
],
21+
"configurations" : [
22+
"compile",
23+
"runtime"
24+
]
25+
},
26+
{
27+
"org" : "org.scala-lang",
28+
"name" : "scala-library",
29+
"version" : "2.12.10",
30+
"artifacts" : [
31+
{
32+
"name" : "scala-library.jar",
33+
"hash" : "sha1:3509860bc2e5b3da001ed45aca94ffbe5694dbda"
34+
}
35+
],
36+
"configurations" : [
37+
"compile",
38+
"runtime"
39+
]
40+
}
41+
]
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"lockVersion" : 1,
3+
"timestamp" : "2022-08-11T18:02:07.891395Z",
4+
"configurations" : [
5+
"compile",
6+
"optional",
7+
"provided",
8+
"runtime"
9+
],
10+
"dependencies" : [
11+
{
12+
"org" : "org.apache.commons",
13+
"name" : "commons-lang3",
14+
"version" : "3.9",
15+
"artifacts" : [
16+
{
17+
"name" : "commons-lang3.jar",
18+
"hash" : "sha1:0122c7cee69b53ed4a7681c03d4ee4c0e2765da5"
19+
}
20+
],
21+
"configurations" : [
22+
"compile",
23+
"runtime"
24+
]
25+
},
26+
{
27+
"org" : "org.scala-lang",
28+
"name" : "scala-library",
29+
"version" : "2.12.10",
30+
"artifacts" : [
31+
{
32+
"name" : "scala-library.jar",
33+
"hash" : "sha1:3509860bc2e5b3da001ed45aca94ffbe5694dbda"
34+
}
35+
],
36+
"configurations" : [
37+
"compile",
38+
"runtime"
39+
]
40+
}
41+
]
42+
}

0 commit comments

Comments
 (0)