Skip to content

Commit

Permalink
Merge pull request #676 from exoego/update-organizationId
Browse files Browse the repository at this point in the history
Change groupId when dependencies published under a different groupId
  • Loading branch information
fthomas committed Aug 19, 2019
2 parents 03b93e0 + b51a779 commit cea21c0
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 16 deletions.
3 changes: 2 additions & 1 deletion modules/core/src/main/resources/StewardPlugin.scala
Expand Up @@ -64,7 +64,8 @@ object StewardPlugin extends AutoPlugin {
sourcePositions.get(moduleId) match {
case Some(fp: FilePosition) if fp.path.startsWith("(sbt.Classpaths") => true
case Some(fp: FilePosition) if fp.path.startsWith("(") => false
case Some(fp: FilePosition) if fp.path.startsWith("Defaults.scala") => false
case Some(fp: FilePosition) if fp.path.startsWith("Defaults.scala")
&& !moduleId.configurations.contains("plugin->default(compile)") => false
case _ => true
}
}
Expand Up @@ -62,7 +62,8 @@ object Update {
artifactId: String,
currentVersion: String,
newerVersions: Nel[String],
configurations: Option[String] = None
configurations: Option[String] = None,
newerGroupId: Option[String] = None
) extends Update {
override def artifactIds: Nel[String] =
Nel.one(artifactId)
Expand Down
Expand Up @@ -61,6 +61,13 @@ object UpdateHeuristic {
}

def replaceF(update: Update): String => Option[String] =
target =>
for {
s <- replaceVersionF(update)(target)
s <- replaceGroupF(update)(s)
} yield s

def replaceVersionF(update: Update): String => Option[String] =
mkRegex(update).fold((_: String) => Option.empty[String]) { regex => target =>
replaceSomeInAllowedParts(
regex,
Expand All @@ -79,6 +86,21 @@ object UpdateHeuristic {
).someIfChanged
}

def replaceGroupF(update: Update): String => Option[String] = { target =>
update match {
case Update.Single(groupId, artifactId, _, _, _, Some(newerGroupId)) =>
val currentGroupId = Regex.quote(groupId)
val currentArtifactId = Regex.quote(artifactId)
val regex = s"""(?i)(.*)${currentGroupId}(.*${currentArtifactId})""".r
replaceSomeInAllowedParts(regex, target, match0 => {
val group1 = match0.group(1)
val group2 = match0.group(2)
Some(s"""$group1$newerGroupId$group2""")
}).someIfChanged
case _ => Some(target)
}
}

replaceF
}

Expand Down Expand Up @@ -114,8 +136,8 @@ object UpdateHeuristic {
val specific = UpdateHeuristic(
name = "specific",
replaceVersion = defaultReplaceVersion {
case Update.Single("org.scalameta", "scalafmt-core", _, _, _) => List("version")
case _ => List.empty
case Update.Single("org.scalameta", "scalafmt-core", _, _, _, _) => List("version")
case _ => List.empty
}
)

Expand Down
Expand Up @@ -32,8 +32,9 @@ package object io {

def isFileSpecificTo(update: Update)(f: File): Boolean =
update match {
case Update.Single("org.scala-sbt", "sbt", _, _, _) => f.name === "build.properties"
case Update.Single("org.scalameta", "scalafmt-core", _, _, _) => f.name === ".scalafmt.conf"
case _ => true
case Update.Single("org.scala-sbt", "sbt", _, _, _, _) => f.name === "build.properties"
case Update.Single("org.scalameta", "scalafmt-core", _, _, _, _) =>
f.name === ".scalafmt.conf"
case _ => true
}
}
Expand Up @@ -167,7 +167,7 @@ final class NurtureAlg[F[_]](
update: Update
): List[Dependency] =
update match {
case Update.Single(groupId, artifactId, _, _, _) =>
case Update.Single(groupId, artifactId, _, _, _, _) =>
dependencies.filter(dep => dep.groupId === groupId && dep.artifactId === artifactId)
case Update.Group(groupId, artifactIds, _, _) =>
val artifactIdSet = artifactIds.toList.toSet
Expand Down
22 changes: 17 additions & 5 deletions modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala
Expand Up @@ -23,10 +23,12 @@ import io.chrisdavenport.log4cats.Logger
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.{Dependency, Update}
import org.scalasteward.core.io.{FileAlg, FileData, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.repocache.RepoCacheRepository
import org.scalasteward.core.sbt.command._
import org.scalasteward.core.sbt.data.{ArtificialProject, SbtVersion}
import org.scalasteward.core.scalafix.Migration
import org.scalasteward.core.scalafmt.{scalafmtDependency, ScalafmtAlg}
import org.scalasteward.core.update.UpdateService
import org.scalasteward.core.util.Nel
import org.scalasteward.core.vcs.data.Repo

Expand Down Expand Up @@ -57,6 +59,7 @@ object SbtAlg {
processAlg: ProcessAlg[F],
workspaceAlg: WorkspaceAlg[F],
scalafmtAlg: ScalafmtAlg[F],
cacheRepository: RepoCacheRepository[F],
F: Monad[F]
): SbtAlg[F] =
new SbtAlg[F] {
Expand Down Expand Up @@ -91,17 +94,22 @@ object SbtAlg {

override def getDependencies(repo: Repo): F[List[Dependency]] =
for {
repoDir <- workspaceAlg.repoDir(repo)
cmd = sbtCmd(List(libraryDependenciesAsJson, reloadPlugins, libraryDependenciesAsJson))
lines <- exec(cmd, repoDir)
originalDependencies <- getOriginalDependencies(repo)
maybeSbtVersion <- getSbtVersion(repo)
maybeSbtDependency = maybeSbtVersion.flatMap(sbtDependency)
maybeScalafmtVersion <- scalafmtAlg.getScalafmtVersion(repo)
maybeScalafmtDependency = maybeScalafmtVersion.map(
scalafmtDependency(defaultScalaBinaryVersion)
)
} yield (maybeSbtDependency.toList ++ maybeScalafmtDependency.toList ++
parser.parseDependencies(lines)).distinct
originalDependencies).distinct

def getOriginalDependencies(repo: Repo): F[List[Dependency]] =
for {
repoDir <- workspaceAlg.repoDir(repo)
cmd = sbtCmd(List(libraryDependenciesAsJson, reloadPlugins, libraryDependenciesAsJson))
lines <- exec(cmd, repoDir)
} yield parser.parseDependencies(lines)

override def getUpdatesForProject(project: ArtificialProject): F[List[Update.Single]] =
for {
Expand Down Expand Up @@ -130,7 +138,11 @@ object SbtAlg {
updates <- withTemporarySbtDependency(repo) {
exec(sbtCmd(commands), repoDir).map(parser.parseSingleUpdates)
}
} yield updates
originalDependencies <- cacheRepository.getDependencies(List(repo))
updatesUnderNewGroupId = originalDependencies.flatMap(
UpdateService.findUpdateUnderNewGroup
)
} yield updates ++ updatesUnderNewGroupId

override def runMigrations(repo: Repo, migrations: Nel[Migration]): F[Unit] =
addGlobalPluginTemporarily(scalaStewardScalafixSbt) {
Expand Down
Expand Up @@ -177,4 +177,17 @@ object UpdateService {
case ("org.xerial.sbt", "sbt-pack") => false
case _ => true
}

def getNewerGroupId(currentGroupId: String, artifactId: String): Option[(String, String)] =
Option((currentGroupId, artifactId) match {
case ("org.spire-math", "kind-projector") => ("org.typelevel", "0.10.0")
case ("com.geirsson", "sbt-scalafmt") => ("org.scalameta", "2.0.0")
case _ => ("", "")
}).filter { case (groupId, _) => groupId.nonEmpty }

def findUpdateUnderNewGroup(dep: Dependency): Option[Update.Single] =
getNewerGroupId(dep.groupId, dep.artifactId).map {
case (newId, fromVersion) =>
dep.toUpdate.copy(newerGroupId = Some(newId), newerVersions = util.Nel.of(fromVersion))
}
}
Expand Up @@ -109,6 +109,18 @@ class UpdateHeuristicTest extends FunSuite with Matchers {
).replaceVersionIn(original) shouldBe (Some(expected) -> UpdateHeuristic.original.name)
}

test("update under different group id") {
val original = """ "org.spire-math" %% "kind-projector" % "0.9.0""""
val expected = """ "org.typelevel" %% "kind-projector" % "0.10.0""""
Single(
"org.spire-math",
"kind-projector",
"0.9.0",
Nel.of("0.10.0"),
newerGroupId = Some("org.typelevel")
).replaceVersionIn(original) shouldBe (Some(expected) -> UpdateHeuristic.strict.name)
}

test("group with repeated version") {
val original =
""" "com.pepegar" %% "hammock-core" % "0.8.1",
Expand Down
Expand Up @@ -9,6 +9,8 @@ import org.scalasteward.core.coursier.CoursierAlg
import org.scalasteward.core.edit.EditAlg
import org.scalasteward.core.git.{Author, GitAlg}
import org.scalasteward.core.io.{MockFileAlg, MockProcessAlg, MockWorkspaceAlg}
import org.scalasteward.core.repocache.RepoCacheRepository
import org.scalasteward.core.repocache.json.JsonRepoCacheRepository
import org.scalasteward.core.repoconfig.RepoConfigAlg
import org.scalasteward.core.sbt.SbtAlg
import org.scalasteward.core.scalafmt.ScalafmtAlg
Expand Down Expand Up @@ -56,6 +58,8 @@ object MockContext {
implicit val gitHubRepoAlg: VCSRepoAlg[MockEff] = VCSRepoAlg.create(config, gitAlg)
implicit val logAlg: LogAlg[MockEff] = new LogAlg[MockEff]
implicit val scalafmtAlg: ScalafmtAlg[MockEff] = ScalafmtAlg.create
implicit val cacheRepository: RepoCacheRepository[MockEff] =
new JsonRepoCacheRepository[MockEff]()
implicit val sbtAlg: SbtAlg[MockEff] = SbtAlg.create
implicit val editAlg: EditAlg[MockEff] = new EditAlg[MockEff]
implicit val repoConfigAlg: RepoConfigAlg[MockEff] = new RepoConfigAlg[MockEff]
Expand Down
Expand Up @@ -59,7 +59,8 @@ class SbtAlgTest extends FunSuite with Matchers {
"-no-colors",
";set every credentials := Nil;dependencyUpdates;reload plugins;dependencyUpdates"
),
List("rm", s"$repoDir/project/tmp-sbt-dep.sbt")
List("rm", s"$repoDir/project/tmp-sbt-dep.sbt"),
List("read", s"/tmp/ws/repos_v05.json")
)
)
}
Expand Down Expand Up @@ -91,7 +92,8 @@ class SbtAlgTest extends FunSuite with Matchers {
";set every credentials := Nil;dependencyUpdates;reload plugins;dependencyUpdates"
),
List("restore", (repoDir / ".sbtopts").toString),
List("restore", (repoDir / ".jvmopts").toString)
List("restore", (repoDir / ".jvmopts").toString),
List("read", s"/tmp/ws/repos_v05.json")
)
)
}
Expand All @@ -118,7 +120,8 @@ class SbtAlgTest extends FunSuite with Matchers {
"-batch",
"-no-colors",
";dependencyUpdates;reload plugins;dependencyUpdates"
)
),
List("read", s"/tmp/ws/repos_v05.json")
)
)
}
Expand Down
@@ -0,0 +1,27 @@
package org.scalasteward.core.update

import org.scalasteward.core.data.{Dependency, Update}
import org.scalasteward.core.util.Nel
import org.scalatest.{FunSuite, Matchers}

class UpdateServiceTest extends FunSuite with Matchers {

test("findUpdateUnderNewGroup: returns empty if dep is not listed") {
val original = new Dependency("org.spire-math", "UNKNOWN", "_2.12", "1.0.0")
UpdateService.findUpdateUnderNewGroup(original) shouldBe None
}

test("findUpdateUnderNewGroup: returns Update.Single for updateing groupId") {
val original = new Dependency("org.spire-math", "kind-projector", "_2.12", "0.9.0")
UpdateService.findUpdateUnderNewGroup(original) shouldBe Some(
Update.Single(
"org.spire-math",
"kind-projector",
"0.9.0",
Nel.of("0.10.0"),
newerGroupId = Some("org.typelevel")
)
)
}

}

0 comments on commit cea21c0

Please sign in to comment.