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

add ability to filter modules from lock file #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package software.purpledragon.sbt.lock
import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
import sbt.librarymanagement.{DependencyFilter, ModuleFilter}
import software.purpledragon.sbt.lock.DependencyLockUpdateMode._
import software.purpledragon.sbt.lock.model.{DependencyLockFile, LockFileMatches}
import software.purpledragon.sbt.lock.util.MessageUtil
Expand All @@ -30,6 +31,7 @@ object DependencyLockPlugin extends AutoPlugin {
val dependencyLockFile = settingKey[File]("lockfile to generate")
val dependencyLockWrite = taskKey[File]("write dependencies to lockfile")
val dependencyLockRead = taskKey[Option[DependencyLockFile]]("read dependencies from lockfile")
val dependencyLockExclusion = settingKey[ModuleFilter]("lockfile module exclusion filter")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to:

Suggested change
val dependencyLockExclusion = settingKey[ModuleFilter]("lockfile module exclusion filter")
val dependencyLockModuleFilter = settingKey[ModuleFilter]("exclusion filter for dependencies")


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

Expand All @@ -46,11 +48,13 @@ object DependencyLockPlugin extends AutoPlugin {
override def projectSettings: Seq[Def.Setting[_]] = Seq(
dependencyLockFile := baseDirectory.value / "build.sbt.lock",
dependencyLockAutoCheck := DependencyLockUpdateMode.WarnOnError,
dependencyLockExclusion := DependencyFilter.fnToModuleFilter(_ => false),
dependencyLockWrite := {
val dest = dependencyLockFile.value
val updateReport = update.value
val exclusionFilter = dependencyLockExclusion.value

val lockFile = DependencyUtils.resolve(updateReport, thisProject.value.configurations.map(_.toConfigRef))
val lockFile = DependencyUtils.resolve(updateReport, exclusionFilter, thisProject.value.configurations.map(_.toConfigRef))
DependencyLockIO.writeLockFile(lockFile, dest)
dest
},
Expand All @@ -62,9 +66,10 @@ object DependencyLockPlugin extends AutoPlugin {
dependencyLockCheck := {
val logger: ManagedLogger = streams.value.log
val updateReport: UpdateReport = update.value
val exclusionFilter = dependencyLockExclusion.value

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

val changes = currentFile.findChanges(updatedFile)

Expand All @@ -82,13 +87,14 @@ object DependencyLockPlugin extends AutoPlugin {
// check to see if the current command/task is one of our internal ones
val skipCheck = state.value.currentCommand.map(_.commandLine).exists(PluginTasks.contains)
val checkMode = dependencyLockAutoCheck.value
val exclusionFilter = dependencyLockExclusion.value

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

dependencyLockRead.value match {
case Some(currentFile) =>
val updatedFile = DependencyUtils.resolve(report, thisProject.value.configurations.map(_.toConfigRef))
val updatedFile = DependencyUtils.resolve(report, exclusionFilter, thisProject.value.configurations.map(_.toConfigRef))

val changes = currentFile.findChanges(updatedFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package software.purpledragon.sbt.lock

import java.time.Instant

import sbt._
import sbt.librarymanagement.ModuleFilter
import software.purpledragon.sbt.lock.model.{DependencyLockFile, DependencyRef, ResolvedArtifact, ResolvedDependency}

import scala.collection.{immutable, mutable, SortedSet}
import scala.collection.{SortedSet, immutable, mutable}

object DependencyUtils {
def resolve(updateReport: UpdateReport, configs: Seq[ConfigRef]): DependencyLockFile = {
def resolve(updateReport: UpdateReport, exclusion: ModuleFilter, configs: Seq[ConfigRef]): DependencyLockFile = {
val configurations: immutable.Seq[ConfigurationReport] =
updateReport.configurations.filter(config => configs.contains(config.configuration))

Expand All @@ -34,7 +34,11 @@ object DependencyUtils {
configurations.foldLeft(Map.empty[DependencyRef, ResolvedDependency]) { (acc, conf) =>
val configName = conf.configuration.name

conf.modules.foldLeft(acc) { (acc2, module) =>
val filteredModules = conf.modules.filterNot { moduleReport =>
exclusion(moduleReport.module)
}

filteredModules.foldLeft(acc) { (acc2, module) =>
resolveModuleForConfig(acc2, configName, module, checksumCache)
}
}
Expand Down