Skip to content

Commit

Permalink
Merge pull request #224 from rtimush/custom-dependency-positions
Browse files Browse the repository at this point in the history
Custom dependency positions
  • Loading branch information
rtimush authored Apr 5, 2021
2 parents 6f49194 + 84bb3cf commit 5320fe9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.4.9
sbt.version=1.5.0
34 changes: 34 additions & 0 deletions src/main/scala/com/timushev/sbt/updates/DependencyPositions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.timushev.sbt.updates

import sbt.Keys._
import sbt._

import scala.util.control.NonFatal

object DependencyPositions {

def dependencyPositionsTask: Def.Initialize[Task[Map[ModuleID, Set[SourcePosition]]]] =
Def.task {
try {
val projRef = thisProjectRef.value
val st = state.value
val sk = libraryDependencies.in(GlobalScope.in(projRef)).scopedKey
val extracted = Project.extract(st)
val empty = extracted.structure.data.set(sk.scope, sk.key, Nil)
val settings = extracted.structure.settings.filter { s =>
(s.key.key == libraryDependencies.key) && (s.key.scope.project == Select(projRef))
}
settings
.flatMap { case s: Setting[Seq[ModuleID]] @unchecked =>
s.init.evaluate(empty).map(_ -> s.pos)
}
.groupBy(_._1)
.map { case (k, v) => k -> v.map(_._2).toSet }
} catch {
case NonFatal(_) =>
dependencyPositions.value
.map { case (k, v) => k -> Set(v) }
}
}

}
28 changes: 18 additions & 10 deletions src/main/scala/com/timushev/sbt/updates/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ object Reporter {
import com.timushev.sbt.updates.UpdatesFinder._

def dependencyUpdatesData(
project: ModuleID,
dependencies: Seq[ModuleID],
dependenciesOverrides: Iterable[ModuleID],
dependencyPositions: Map[ModuleID, SourcePosition],
dependencyPositions: Map[ModuleID, Set[SourcePosition]],
resolvers: Seq[Resolver],
credentials: Seq[Credentials],
scalaVersions: Seq[String],
Expand Down Expand Up @@ -159,16 +158,25 @@ object Reporter {

def excludeDependenciesFromPlugins(
dependencies: Seq[ModuleID],
dependencyPositions: Map[ModuleID, SourcePosition],
dependencyPositions: Map[ModuleID, Set[SourcePosition]],
buildRoot: File
): Seq[ModuleID] =
dependencies.filter { moduleId =>
dependencyPositions.get(moduleId) match {
case Some(fp: FilePosition) if fp.path.startsWith("(sbt.Classpaths") => true
case Some(fp: FilePosition) if fp.path.startsWith("(") =>
extractFileName(fp.path).exists(fileExists(buildRoot, _))
case _ => true
}
dependencies.filter {
case moduleId
if moduleId.organization == "org.scala-lang" &&
moduleId.name == "scala-library" =>
true
case moduleId =>
dependencyPositions.get(moduleId).fold(true) {
_.exists {
case fp: FilePosition if fp.path.startsWith("(sbt.Classpaths") =>
false
case fp: FilePosition if fp.path.startsWith("(") =>
extractFileName(fp.path).exists(fileExists(buildRoot, _))
case _ =>
true
}
}
}

val FileNamePattern: Regex = "^\\([^\\)]+\\) (.*)$".r
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/timushev/sbt/updates/UpdatesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.timushev.sbt.updates.UpdatesKeys._
import sbt.Keys._
import sbt._
import com.timushev.sbt.updates.Compat._
import com.timushev.sbt.updates.DependencyPositions.dependencyPositionsTask

object UpdatesPlugin extends AutoPlugin {
object autoImport extends UpdatesKeys with Implicits
Expand All @@ -18,10 +19,9 @@ object UpdatesPlugin extends AutoPlugin {
dependencyAllowPreRelease := false,
dependencyUpdatesData := {
Reporter.dependencyUpdatesData(
projectID.value,
libraryDependencies.value,
dependencyOverrides.value,
dependencyPositions.value,
dependencyPositionsTask.value,
fullResolvers.value,
credentials.value,
crossScalaVersions.value,
Expand Down

0 comments on commit 5320fe9

Please sign in to comment.