Skip to content

Commit

Permalink
Refactor dependencies
Browse files Browse the repository at this point in the history
- `model.VersionScalaPlatform` => `model.VersionCombo`
- use `model.Dep` in `CoursierResolver` instead of `coursier.Dependency`
- refactor and move scala suffix calculation to `Dep` from `VersionScalaPlatform`
  • Loading branch information
oyvindberg committed Aug 7, 2022
1 parent d071c64 commit 6da7eee
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 235 deletions.
6 changes: 3 additions & 3 deletions bleep-cli/src/scala/bleep/commands/BuildUpdateDeps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ object BuildUpdateDeps {
def instantiateAllDependencies(build: model.ExplodedBuild): Map[model.Dep, List[Dependency]] =
build.projects.toList
.flatMap { case (crossName, p) =>
model.VersionScalaPlatform.fromExplodedProject(p) match {
model.VersionCombo.fromExplodedProject(p) match {
case Left(err) =>
throw new BleepException.Text(crossName, err)
case Right(scalaPlatform) =>
case Right(versionCombo) =>
p.dependencies.values.iterator.collect {
case dep if !dep.version.contains("$") => (dep, dep.dependencyForce(crossName, scalaPlatform))
case dep if !dep.version.contains("$") => (dep, dep.asDependencyForce(Some(crossName), versionCombo))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions bleep-cli/src/scala/bleep/internal/importBloopFilesFromSbt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object importBloopFilesFromSbt {
val configuredPlatform: Option[model.Platform] =
bloopProject.platform.map(translatePlatform(_, replacements, bloopProject.resolution))

val scalaPlatform = model.VersionScalaPlatform.fromExplodedScalaAndPlatform(scalaVersion, configuredPlatform) match {
val versionCombo = model.VersionCombo.fromExplodedScalaAndPlatform(scalaVersion, configuredPlatform) match {
case Left(value) => throw new BleepException.Text(crossName, value)
case Right(value) => value
}
Expand Down Expand Up @@ -130,15 +130,15 @@ object importBloopFilesFromSbt {
}

val (compilerPlugins, dependencies) = {
val providedDeps = scalaPlatform.libraries(isTest = projectType.testLike)
val providedDeps = versionCombo.libraries(isTest = projectType.testLike)
importDeps(logger, inputProject, crossName, configuredPlatform.flatMap(_.name), providedDeps)
}

val configuredJava: Option[model.Java] =
bloopProject.java.map(translateJava(replacements))

val configuredScala: Option[model.Scala] =
bloopProject.scala.map(translateScala(compilerPlugins, replacements, scalaPlatform))
bloopProject.scala.map(translateScala(compilerPlugins, replacements, versionCombo))

val testFrameworks: model.JsonSet[model.TestFrameworkName] =
if (projectType.testLike) {
Expand Down Expand Up @@ -340,7 +340,7 @@ object importBloopFilesFromSbt {
translatedPlatform
}

def translateScala(compilerPlugins: Seq[model.Dep], replacements: model.Replacements, scalaPlatform: model.VersionScalaPlatform)(
def translateScala(compilerPlugins: Seq[model.Dep], replacements: model.Replacements, versionCombo: model.VersionCombo)(
s: Config.Scala
): model.Scala = {
val options = parseOptionsDropSemanticDb(s.options, Some(replacements))
Expand All @@ -352,7 +352,7 @@ object importBloopFilesFromSbt {

val filteredCompilerPlugins: Seq[model.Dep] =
compilerOptionsDropSemanticDb {
scalaPlatform.compilerPlugin.foldLeft(compilerPlugins) { case (all, fromPlatform) => all.filterNot(_ == fromPlatform) }
versionCombo.compilerPlugin.foldLeft(compilerPlugins) { case (all, fromPlatform) => all.filterNot(_ == fromPlatform) }
}

val (strict, remainingOptions) = {
Expand Down
45 changes: 22 additions & 23 deletions bleep-core/src/scala/bleep/CoursierResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.nio.file.{Files, Path}
trait CoursierResolver {
val params: CoursierResolver.Params

def resolve(deps: Set[Dependency], forceScalaVersion: Option[model.VersionScala]): Either[CoursierError, CoursierResolver.Result]
def resolve(deps: Set[model.Dep], versionCombo: model.VersionCombo): Either[CoursierError, CoursierResolver.Result]
}

object CoursierResolver {
Expand Down Expand Up @@ -176,17 +176,17 @@ object CoursierResolver {

val fileCache = FileCache[Task]().withLogger(logger)

override def resolve(deps: Set[Dependency], forceScalaVersion: Option[model.VersionScala]): Either[CoursierError, CoursierResolver.Result] = {
override def resolve(deps: Set[model.Dep], versionCombo: model.VersionCombo): Either[CoursierError, CoursierResolver.Result] = {
def go(remainingAttempts: Int): Either[CoursierError, Fetch.Result] = {
val newClassifiers = if (params.downloadSources) List(Classifier.sources) else Nil

Fetch[Task](fileCache)
.withRepositories(coursierRepos(params.repos, params.authentications))
.withDependencies(deps.toList.sortBy(_.toString()))
.withDependencies(deps.toList.sorted.map(_.asDependencyForce(None, versionCombo)))
.withResolutionParams(
ResolutionParams()
.withForceScalaVersion(forceScalaVersion.nonEmpty)
.withScalaVersionOpt(forceScalaVersion.map(_.scalaVersion))
.withForceScalaVersion(versionCombo.asScala.nonEmpty)
.withScalaVersionOpt(versionCombo.asScala.map(_.scalaVersion.scalaVersion))
)
.withMainArtifacts(true)
.addClassifiers(newClassifiers: _*)
Expand All @@ -207,10 +207,10 @@ object CoursierResolver {

private class Cached(logger: Logger, underlying: Direct, in: Path) extends CoursierResolver {
override val params = underlying.params
override def resolve(deps: Set[Dependency], forceScalaVersion: Option[model.VersionScala]): Either[CoursierError, CoursierResolver.Result] =
if (deps.exists(_.version.endsWith("-SNAPSHOT"))) underlying.resolve(deps, forceScalaVersion)
override def resolve(deps: Set[model.Dep], versionCombo: model.VersionCombo): Either[CoursierError, CoursierResolver.Result] =
if (deps.exists(_.version.endsWith("-SNAPSHOT"))) underlying.resolve(deps, versionCombo)
else {
val request = Cached.Request(underlying.fileCache.location, deps.toList.sortBy(_.toString()), underlying.params, forceScalaVersion.map(_.scalaVersion))
val request = Cached.Request(underlying.fileCache.location, deps.toList.sortBy(_.toString()), underlying.params, versionCombo)
val digest = request.asJson.noSpaces.hashCode // both `noSpaces` and `String.hashCode` should hopefully be stable
val cachePath = in / s"$digest.json"

Expand All @@ -229,10 +229,10 @@ object CoursierResolver {
cachedResult match {
case Some(value) => Right(value)
case None =>
val depNames = deps.map(_.module.name.value)
val depNames = deps.map(_.baseModuleName.value)
val ctxLogger = logger.withContext(cachePath).withContext(depNames)
ctxLogger.debug(s"coursier cache miss")
underlying.resolve(deps, forceScalaVersion).map {
underlying.resolve(deps, versionCombo).map {
case changingResult if changingResult.fullDetailedArtifacts.exists { case (_, _, artifact, _) => artifact.changing } =>
ctxLogger.info("Not caching because result is changing")
changingResult
Expand All @@ -245,16 +245,14 @@ object CoursierResolver {
}

private object Cached {
case class Request(cacheLocation: File, wanted: List[Dependency], params: Params, forceScalaVersion: Option[String])
case class Request(cacheLocation: File, wanted: List[model.Dep], params: Params, versionCombo: model.VersionCombo)

object Request {

import Result.codecDependency

implicit val codec: Codec[Request] =
Codec.forProduct4[Request, File, List[Dependency], Params, Option[String]]("cacheLocation", "wanted", "params", "forceScalaVersion")(
Request.apply
)(x => (x.cacheLocation, x.wanted, x.params, x.forceScalaVersion))
Codec.forProduct4[Request, File, List[model.Dep], Params, model.VersionCombo]("cacheLocation", "wanted", "params", "forceScalaVersion")(Request.apply)(
x => (x.cacheLocation, x.wanted, x.params, x.versionCombo)
)
}

case class Both(request: Request, result: Result)
Expand All @@ -265,20 +263,21 @@ object CoursierResolver {

class TemplatedVersions(outer: CoursierResolver, maybeWantedBleepVersion: Option[model.BleepVersion]) extends CoursierResolver {
override val params = outer.params
override def resolve(deps: Set[Dependency], forceScalaVersion: Option[model.VersionScala]): Either[CoursierError, Result] = {
override def resolve(deps: Set[model.Dep], versionCombo: model.VersionCombo): Either[CoursierError, Result] = {
val rewrittenDeps =
deps.map {
case dep if dep.version == constants.BleepVersionTemplate =>
dep.withVersion(maybeWantedBleepVersion.getOrElse(sys.error("expected to have a bleep version")).value)

case dep if dep.version == constants.ScalaVersionTemplate =>
dep.withVersion(forceScalaVersion.getOrElse(sys.error("Expected to have a scala version")).scalaVersion)
val version = versionCombo.asScala match {
case Some(scalaCombo) => scalaCombo.scalaVersion.scalaVersion
case None => throw new BleepException.Text(s"You can only use ${constants.ScalaVersionTemplate} in scala projects")
}
dep.withVersion(version)
case dep => dep
}
maybeWantedBleepVersion match {
case Some(wantedBleepVersion) =>
case None => deps
}
outer.resolve(rewrittenDeps, forceScalaVersion)
outer.resolve(rewrittenDeps, versionCombo)
}
}
}
41 changes: 16 additions & 25 deletions bleep-core/src/scala/bleep/GenBloopFiles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import bleep.logging.Logger
import bleep.rewrites.Defaults
import bloop.config.{Config, ConfigCodecs}
import com.github.plokhotnyuk.jsoniter_scala.core.{readFromString, writeToString, WriterConfig}
import coursier.Classifier
import coursier.core.{Configuration, Extension}
import coursier.{Classifier, Dependency}
import io.github.davidgregory084.{DevMode, TpolecatPlugin}

import java.nio.charset.StandardCharsets.UTF_8
Expand Down Expand Up @@ -155,8 +155,8 @@ object GenBloopFiles {
case platform => platform
}

val scalaPlatform: model.VersionScalaPlatform =
model.VersionScalaPlatform.fromExplodedProject(explodedProject) match {
val versionCombo: model.VersionCombo =
model.VersionCombo.fromExplodedProject(explodedProject) match {
case Left(err) => throw new BleepException.Text(crossName, err)
case Right(versions) => versions
}
Expand Down Expand Up @@ -224,28 +224,19 @@ object GenBloopFiles {

val (resolvedDependencies, resolvedRuntimeDependencies) = {
val fromPlatform =
scalaPlatform.libraries(isTest = explodedProject.isTestProject.getOrElse(false))
versionCombo.libraries(isTest = explodedProject.isTestProject.getOrElse(false))

val inherited =
build.transitiveDependenciesFor(crossName).flatMap { case (_, p) => p.dependencies.values }

def providedOrOptional(dep: model.Dep): Boolean =
dep.configuration == Configuration.provided || dep.configuration == Configuration.optional

def resolve(all: model.JsonSet[model.Dep]) = {
val concreteDeps: Set[Dependency] =
all.values.toSet[model.Dep].map { dep =>
dep.dependency(scalaPlatform) match {
case Left(err) => throw new BleepException.Text(crossName, err)
case Right(value) => value
}
}

resolver.resolve(concreteDeps, forceScalaVersion = scalaVersion) match {
def resolve(all: model.JsonSet[model.Dep]) =
resolver.resolve(all.values, versionCombo) match {
case Left(coursierError) => throw BleepException.ResolveError(coursierError, crossName)
case Right(value) => value
}
}

// drop provided/optional from inherited deps
val filteredInherited = inherited.filterNot(providedOrOptional)
Expand Down Expand Up @@ -305,11 +296,12 @@ object GenBloopFiles {

val configuredScala: Option[Config.Scala] =
scalaVersion.map { scalaVersion =>
val scalaCompiler: Dependency =
scalaVersion.compiler.forceDependency(model.VersionScalaPlatform.Jvm(scalaVersion))
val jvmCombo = model.VersionCombo.Jvm(scalaVersion)

val compiler = scalaVersion.compiler.asJava(jvmCombo).getOrElse(sys.error("unexpected"))

val resolvedScalaCompiler: List[Path] =
resolver.resolve(Set(scalaCompiler), forceScalaVersion = Some(scalaVersion)) match {
resolver.resolve(Set(compiler), versionCombo = jvmCombo) match {
case Left(coursierError) => throw BleepException.ResolveError(coursierError, crossName)
case Right(res) => res.jars
}
Expand All @@ -327,16 +319,15 @@ object GenBloopFiles {
}

val compilerPlugins: model.Options = {
val fromPlatform = scalaPlatform.compilerPlugin
val fromPlatform = versionCombo.compilerPlugin
val fromScala = maybeScala.fold(Set.empty[model.Dep])(_.compilerPlugins.values)
val specified: Set[model.Dep] =
fromPlatform.foldLeft(fromScala) { case (all, dep) => all + dep }

val deps: Set[Dependency] =
specified.map(_.withTransitive(false).forceDependency(model.VersionScalaPlatform.Jvm(scalaVersion)))
val deps = specified.map(_.withTransitive(false))

val jars: Seq[Path] =
resolver.resolve(deps, forceScalaVersion = Some(scalaVersion)) match {
resolver.resolve(deps, jvmCombo) match {
case Left(coursierError) => throw BleepException.ResolveError(coursierError, crossName)
case Right(res) =>
res.fullDetailedArtifacts.collect {
Expand All @@ -362,9 +353,9 @@ object GenBloopFiles {
}

Config.Scala(
organization = scalaCompiler.module.organization.value,
name = scalaCompiler.module.name.value,
version = scalaCompiler.version,
organization = compiler.organization.value,
name = compiler.moduleName.value,
version = compiler.version,
options = templateDirs.fill.opts(scalacOptions).render,
jars = resolvedScalaCompiler,
analysis = Some(projectPaths.incrementalAnalysis),
Expand Down
4 changes: 2 additions & 2 deletions bleep-core/src/scala/bleep/bsp/SetupBloopRifle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import bleep.bsp.CompileServerMode.{NewEachInvocation, Shared}
import bleep.internal.FileUtils
import bleep.logging.Logger
import bleep.{BleepException, Lazy}
import coursier.core.Dependency
import coursier.parse.ModuleParser

import java.io.File
Expand Down Expand Up @@ -47,7 +46,8 @@ object SetupBloopRifle {
.left
.map(msg => new BleepException.ModuleFormatError(modString, msg))
.flatMap { mod =>
resolver.forceGet.resolve(Set(Dependency(mod, bloopVersion)), forceScalaVersion = None) match {
val dep = model.Dep.Java(mod.organization.value, mod.name.value, bloopVersion)
resolver.forceGet.resolve(Set(dep), model.VersionCombo.Java) match {
case Left(coursierError) => Left(new BleepException.ResolveError(coursierError, "installing bloop"))
case Right(res) => Right((res.jarFiles, true))
}
Expand Down
Loading

0 comments on commit 6da7eee

Please sign in to comment.