Skip to content

Commit

Permalink
Merge pull request #827 from fthomas/topic/tmp-sbt-dep
Browse files Browse the repository at this point in the history
Add sbt temporarily as dependency
  • Loading branch information
fthomas committed Aug 14, 2019
2 parents 2d9b5ed + ace1a4d commit 742b362
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 79 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Expand Up @@ -230,7 +230,8 @@ addCommandAlias(
Seq("--whitelist", s"$home/.cache/coursier"),
Seq("--whitelist", s"$home/.coursier"),
Seq("--whitelist", s"$home/.ivy2"),
Seq("--whitelist", s"$home/.sbt")
Seq("--whitelist", s"$home/.sbt"),
Seq("--prune-repos=false")
).flatten.mkString(" ")
}
)
26 changes: 18 additions & 8 deletions modules/core/src/main/scala/org/scalasteward/core/sbt/SbtAlg.scala
Expand Up @@ -17,8 +17,8 @@
package org.scalasteward.core.sbt

import better.files.File
import cats.Monad
import cats.implicits._
import cats.{Functor, Monad}
import io.chrisdavenport.log4cats.Logger
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.{Dependency, Update}
Expand All @@ -45,9 +45,6 @@ trait SbtAlg[F[_]] {
def getUpdatesForRepo(repo: Repo): F[List[Update.Single]]

def runMigrations(repo: Repo, migrations: Nel[Migration]): F[Unit]

final def getSbtUpdate(repo: Repo)(implicit F: Functor[F]): F[Option[Update.Single]] =
getSbtVersion(repo).map(_.flatMap(findSbtUpdate))
}

object SbtAlg {
Expand Down Expand Up @@ -95,7 +92,9 @@ object SbtAlg {
repoDir <- workspaceAlg.repoDir(repo)
cmd = sbtCmd(List(libraryDependenciesAsJson, reloadPlugins, libraryDependenciesAsJson))
lines <- exec(cmd, repoDir)
} yield parser.parseDependencies(lines)
maybeSbtVersion <- getSbtVersion(repo)
maybeSbtDependency = maybeSbtVersion.flatMap(sbtDependency)
} yield maybeSbtDependency.toList ++ parser.parseDependencies(lines)

override def getUpdatesForProject(project: ArtificialProject): F[List[Update.Single]] =
for {
Expand All @@ -121,9 +120,10 @@ object SbtAlg {
maybeClearCredentials = if (config.keepCredentials) Nil else List(setCredentialsToNil)
commands = maybeClearCredentials ++
List(dependencyUpdates, reloadPlugins, dependencyUpdates)
updates <- exec(sbtCmd(commands), repoDir).map(parser.parseSingleUpdates)
maybeSbtUpdate <- getSbtUpdate(repo)
} yield maybeSbtUpdate.toList ::: updates
updates <- withTemporarySbtDependency(repo) {
exec(sbtCmd(commands), repoDir).map(parser.parseSingleUpdates)
}
} yield updates

override def runMigrations(repo: Repo, migrations: Nel[Migration]): F[Unit] =
addGlobalPluginTemporarily(scalaStewardScalafixSbt) {
Expand Down Expand Up @@ -162,5 +162,15 @@ object SbtAlg {
}
}
}

def withTemporarySbtDependency[A](repo: Repo)(fa: F[A]): F[A] =
getSbtVersion(repo).flatMap {
_.flatMap(sbtDependency).fold(fa) { dependency =>
workspaceAlg.repoDir(repo).flatMap { repoDir =>
val content = s"libraryDependencies += ${dependency.formatAsModuleId}"
fileAlg.createTemporarily(repoDir / "project" / "tmp-sbt-dep.sbt", content)(fa)
}
}
}
}
}
Expand Up @@ -16,12 +16,11 @@

package org.scalasteward.core

import cats.effect.{IO, Resource}
import cats.implicits._
import org.scalasteward.core.data.Update
import cats.effect.{IO, Resource}
import org.scalasteward.core.data.{Dependency, Version}
import org.scalasteward.core.io.FileData
import org.scalasteward.core.sbt.data.{SbtVersion, ScalaVersion}
import org.scalasteward.core.util.Nel
import scala.io.Source

package object sbt {
Expand All @@ -35,26 +34,19 @@ package object sbt {
val defaultScalaVersion: ScalaVersion =
ScalaVersion(BuildInfo.scalaVersion)

def findNewerSbtVersion(sbtVersion: SbtVersion): Option[SbtVersion] =
(sbtVersion.value match {
case v if v.startsWith("0.13.") => Some(latestSbtVersion_0_13)
case v if v.startsWith("1.3.0-RC") => Some(SbtVersion("1.3.0-RC3"))
case v if v.startsWith("1.") => Some(defaultSbtVersion)
case _ => None
}).filter(_.toVersion > sbtVersion.toVersion)

def findSbtUpdate(currentVersion: SbtVersion): Option[Update.Single] =
findNewerSbtVersion(currentVersion).map { newerVersion =>
Update.Single("org.scala-sbt", "sbt", currentVersion.value, Nel.of(newerVersion.value))
}

def seriesToSpecificVersion(sbtSeries: SbtVersion): SbtVersion =
sbtSeries.value match {
case "0.13" => latestSbtVersion_0_13
case "1.0" => defaultSbtVersion
case _ => defaultSbtVersion
}

def sbtDependency(sbtVersion: SbtVersion): Option[Dependency] =
if (sbtVersion.toVersion >= Version("1.0.0"))
Some(Dependency("org.scala-sbt", "sbt", "sbt", sbtVersion.value))
else
None

val scalaStewardSbt: FileData =
FileData(
"scala-steward.sbt",
Expand Down
Expand Up @@ -29,7 +29,7 @@ import org.scalasteward.core.update.data.UpdateState._
import org.scalasteward.core.util.MonadThrowable
import org.scalasteward.core.vcs.data.PullRequestState.Closed
import org.scalasteward.core.vcs.data.Repo
import org.scalasteward.core.{sbt, scalafmt, util}
import org.scalasteward.core.{scalafmt, util}

final class UpdateService[F[_]](
implicit
Expand Down Expand Up @@ -121,14 +121,10 @@ final class UpdateService[F[_]](
def findAllUpdateStates(repo: Repo, updates: List[Update.Single]): F[List[UpdateState]] =
repoCacheRepository.findCache(repo).flatMap {
case Some(repoCache) =>
val maybeSbtUpdate = repoCache.maybeSbtVersion.flatMap(sbt.findSbtUpdate)
val maybeScalafmtUpdate =
repoCache.maybeScalafmtVersion.flatMap(scalafmt.findScalafmtUpdate)
val updates1 = maybeSbtUpdate.toList ++ maybeScalafmtUpdate.toList ++ updates
val maybeSbtDependency = maybeSbtUpdate.map { update =>
Dependency(update.groupId, update.artifactId, update.artifactId, update.currentVersion)
}
val dependencies = maybeSbtDependency.toList ++ repoCache.dependencies
val updates1 = maybeScalafmtUpdate.toList ++ updates
val dependencies = repoCache.dependencies

dependencies.traverse { dependency =>
findUpdateState(repo, repoCache.sha1, dependency, updates1)
Expand Down
Expand Up @@ -2,9 +2,9 @@ package org.scalasteward.core.sbt

import better.files.File
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.Version
import org.scalasteward.core.mock.MockContext._
import org.scalasteward.core.mock.{MockContext, MockState}
import org.scalasteward.core.data.{Update, Version}
import org.scalasteward.core.scalafix.Migration
import org.scalasteward.core.util.Nel
import org.scalasteward.core.vcs.data.Repo
Expand Down Expand Up @@ -33,10 +33,15 @@ class SbtAlgTest extends FunSuite with Matchers {
test("getUpdatesForRepo") {
val repo = Repo("fthomas", "refined")
val repoDir = config.workspace / "fthomas/refined"
val state = sbtAlg.getUpdatesForRepo(repo).runS(MockState.empty).unsafeRunSync()
val buildProperties = repoDir / "project" / "build.properties"
val initialState = MockState.empty.copy(files = Map(buildProperties -> "sbt.version=1.2.6"))
val state = sbtAlg.getUpdatesForRepo(repo).runS(initialState).unsafeRunSync()

state shouldBe MockState.empty.copy(
files = Map(buildProperties -> "sbt.version=1.2.6"),
commands = Vector(
List("read", s"$repoDir/project/build.properties"),
List("create", s"$repoDir/project/tmp-sbt-dep.sbt"),
List(
"TEST_VAR=GREAT",
"ANOTHER_TEST_VAR=ALSO_GREAT",
Expand All @@ -48,7 +53,7 @@ class SbtAlgTest extends FunSuite with Matchers {
"-no-colors",
";set every credentials := Nil;dependencyUpdates;reload plugins;dependencyUpdates"
),
List("read", s"$repoDir/project/build.properties")
List("rm", s"$repoDir/project/tmp-sbt-dep.sbt")
)
)
}
Expand All @@ -63,6 +68,7 @@ class SbtAlgTest extends FunSuite with Matchers {

state shouldBe MockState.empty.copy(
commands = Vector(
List("read", s"$repoDir/project/build.properties"),
List("rm", (repoDir / ".jvmopts").toString),
List("rm", (repoDir / ".sbtopts").toString),
List("create", (repoDir / ".jvmopts").toString),
Expand All @@ -79,8 +85,7 @@ class SbtAlgTest extends FunSuite with Matchers {
),
List("rm", (repoDir / ".jvmopts").toString),
List("restore", (repoDir / ".sbtopts").toString),
List("restore", (repoDir / ".jvmopts").toString),
List("read", s"$repoDir/project/build.properties")
List("restore", (repoDir / ".jvmopts").toString)
)
)
}
Expand All @@ -95,6 +100,7 @@ class SbtAlgTest extends FunSuite with Matchers {

state shouldBe MockState.empty.copy(
commands = Vector(
List("read", s"$repoDir/project/build.properties"),
List(
"TEST_VAR=GREAT",
"ANOTHER_TEST_VAR=ALSO_GREAT",
Expand All @@ -105,8 +111,7 @@ class SbtAlgTest extends FunSuite with Matchers {
"-batch",
"-no-colors",
";dependencyUpdates;reload plugins;dependencyUpdates"
),
List("read", s"$repoDir/project/build.properties")
)
)
)
}
Expand Down Expand Up @@ -182,20 +187,4 @@ class SbtAlgTest extends FunSuite with Matchers {
)
)
}

test("getSbtUpdate") {
val repo = Repo("fthomas", "scala-steward")
val repoDir = config.workspace / repo.owner / repo.repo
val buildProperties = repoDir / "project" / "build.properties"
val initialState = MockState.empty.add(buildProperties, "sbt.version=1.2.6")
val (state, maybeUpdate) = sbtAlg.getSbtUpdate(repo).run(initialState).unsafeRunSync()

maybeUpdate shouldBe Some(
Update.Single("org.scala-sbt", "sbt", "1.2.6", Nel.of(defaultSbtVersion.value))
)
state shouldBe MockState.empty.copy(
commands = Vector(List("read", s"$repoDir/project/build.properties")),
files = Map(buildProperties -> "sbt.version=1.2.6")
)
}
}

This file was deleted.

0 comments on commit 742b362

Please sign in to comment.