Skip to content

Commit

Permalink
can use build/system replacements in resolvers in bleep.yaml
Browse files Browse the repository at this point in the history
support somewhat niche case with resolving from a file repo relative to temp folder or home folder
  • Loading branch information
oyvindberg committed Nov 6, 2022
1 parent 627420e commit c7fbcca
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
10 changes: 9 additions & 1 deletion bleep-cli-test/src/scala/bleep/TestResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ object TestResolver {
}

val factory: CoursierResolver.Factory = { (pre, _, buildFile) =>
lazy val replacements = model.Replacements.paths(pre.buildPaths.buildDir)

val resolvers = buildFile.resolvers.values.map {
case model.Repository.Maven(name, uri) => model.Repository.Maven(name, replacements.fill.uri(uri))
case model.Repository.Folder(name, path) => model.Repository.Folder(name, replacements.fill.path(path))
case model.Repository.Ivy(name, uri) => model.Repository.Ivy(name, replacements.fill.uri(uri))
}

val params = CoursierResolver.Params(
overrideCacheFolder = None,
downloadSources = false,
authentications = None,
repos = buildFile.resolvers.values
repos = resolvers
)
val underlying = if (isCi) NoDownloadInCI(params) else new CoursierResolver.Direct(new BleepCacheLogger(pre.logger), params)
val cached = new TestResolver(underlying, inMemoryCache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ object importBloopFilesFromSbt {
val originalTarget = findOriginalTargetDir.force(crossName, bloopProject)

val replacements =
model.Replacements.paths(sbtBuildDir, directory) ++
model.Replacements.paths(sbtBuildDir) ++
model.Replacements.projectPaths(directory) ++
model.Replacements.targetDir(originalTarget) ++
model.Replacements.scope(projectType.sbtScope)

Expand Down
43 changes: 17 additions & 26 deletions bleep-core/src/scala/bleep/CoursierResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,20 @@ object CoursierResolver {

object Factory {
object default extends Factory {
def apply(pre: Prebootstrapped, config: model.BleepConfig, buildFile: model.BuildFile): CoursierResolver =
CoursierResolver(
repos = buildFile.resolvers.values,
logger = pre.logger,
downloadSources = true,
resolveCachePath = pre.userPaths.resolveCachePath,
authentications = config.authentications,
wantedBleepVersion = Some(buildFile.$version)
)
}
}
def apply(pre: Prebootstrapped, config: model.BleepConfig, buildFile: model.BuildFile): CoursierResolver = {
lazy val replacements = model.Replacements.paths(pre.buildPaths.buildDir)

def apply(
repos: List[model.Repository],
logger: Logger,
downloadSources: Boolean,
resolveCachePath: Path,
authentications: Option[model.Authentications],
wantedBleepVersion: Option[model.BleepVersion],
overrideCacheFolder: Option[File] = None
): CoursierResolver = {
val params = Params(overrideCacheFolder, downloadSources, authentications, repos)
val direct = new Direct(new BleepCacheLogger(logger), params)
val cached = new Cached(logger, direct, resolveCachePath)
new TemplatedVersions(cached, wantedBleepVersion)
val resolvers = buildFile.resolvers.values.map {
case model.Repository.Maven(name, uri) => model.Repository.Maven(name, replacements.fill.uri(uri))
case model.Repository.Folder(name, path) => model.Repository.Folder(name, replacements.fill.path(path))
case model.Repository.Ivy(name, uri) => model.Repository.Ivy(name, replacements.fill.uri(uri))
}
val params = Params(None, downloadSources = true, config.authentications, resolvers)
val direct = new Direct(new BleepCacheLogger(pre.logger), params)
val cached = new Cached(pre.logger, direct, pre.userPaths.resolveCachePath)
new TemplatedVersions(cached, Some(buildFile.$version))
}
}
}

// this is a simplified version of the original `Fetch.Result` with a json codec
Expand Down Expand Up @@ -149,14 +138,16 @@ object CoursierResolver {
class Direct(val logger: BleepCacheLogger, val params: Params) extends CoursierResolver {

val fileCache = FileCache[Task](params.overrideCacheFolder.getOrElse(CacheDefaults.location)).withLogger(logger)
val repos = coursierRepos(params.repos, params.authentications)

override def resolve(deps: SortedSet[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
val coursierDependencies = deps.toList.map(_.asDependency(versionCombo).orThrowText)

Fetch[Task](fileCache)
.withRepositories(coursierRepos(params.repos, params.authentications))
.withDependencies(deps.toList.map(_.asDependency(versionCombo).orThrowText))
.withRepositories(repos)
.withDependencies(coursierDependencies)
.withResolutionParams(
ResolutionParams()
.withForceScalaVersion(versionCombo.asScala.nonEmpty)
Expand Down
3 changes: 2 additions & 1 deletion bleep-core/src/scala/bleep/GenBloopFiles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ object GenBloopFiles {
model.VersionCombo.fromExplodedProject(explodedProject).orThrowTextWithContext(crossName)

val templateDirs =
model.Replacements.paths(build = buildPaths.buildDir, project = projectPaths.dir) ++
model.Replacements.paths(build = buildPaths.buildDir) ++
model.Replacements.projectPaths(project = projectPaths.dir) ++
model.Replacements.targetDir(projectPaths.targetDir) ++
model.Replacements.versions(Some(build.$version), versionCombo, includeEpoch = true, includeBinVersion = true)

Expand Down
10 changes: 6 additions & 4 deletions bleep-model/src/scala/bleep/BuildPaths.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ case class BuildPaths(cwd: Path, bleepYamlFile: Path, mode: BuildPaths.Mode, wan
val maybePlatformVersion = p.platform.flatMap(p => p.jsVersion.map(_.scalaJsVersion).orElse(p.nativeVersion.map(_.scalaNativeVersion)))

val targetDir = bleepBloopDir / crossName.name.value / crossName.crossId.fold("")(_.value)
val replacements = model.Replacements.paths(dir, buildDir) ++
model.Replacements.targetDir(targetDir) ++
model.Replacements.scope(p.`sbt-scope`.getOrElse("")) ++
model.Replacements.versions(wantedBleepVersion, scalaVersion, maybePlatformId, maybePlatformVersion, includeEpoch = true, includeBinVersion = true)
val replacements =
model.Replacements.paths(buildDir) ++
model.Replacements.projectPaths(dir) ++
model.Replacements.targetDir(targetDir) ++
model.Replacements.scope(p.`sbt-scope`.getOrElse("")) ++
model.Replacements.versions(wantedBleepVersion, scalaVersion, maybePlatformId, maybePlatformVersion, includeEpoch = true, includeBinVersion = true)

def sourceLayout = p.`source-layout` match {
case Some(sourceLayout) => sourceLayout
Expand Down
9 changes: 7 additions & 2 deletions bleep-model/src/scala/bleep/model/Replacements.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import coursier.cache.CacheDefaults
import coursier.paths.CoursierPaths

import java.io.File
import java.net.URI
import java.nio.file.Path

// unfortunately we'll need to handle absolute paths in scalacOptions
Expand Down Expand Up @@ -69,6 +70,8 @@ object Replacements {
Path.of(string(path.toString))
def file(file: File): File =
new File(string(file.toString))
def uri(uri: URI): URI =
new URI(string(uri.toString))

def dep(dep: Dep): Dep = dep.withVersion(string(dep.version))

Expand Down Expand Up @@ -106,10 +109,9 @@ object Replacements {
List(target.toString -> known.TargetDir)
)

def paths(build: Path, project: Path): Replacements =
def paths(build: Path): Replacements =
ofReplacements(
List(
project.toString -> known.ProjectDir,
build.toString -> known.BuildDir,
System.getProperty("java.io.tmpdir") -> known.TempDir,
System.getProperty("user.home") -> known.HomeDir,
Expand All @@ -118,6 +120,9 @@ object Replacements {
)
)

def projectPaths(project: Path): Replacements =
ofReplacements(List(project.toString -> known.ProjectDir))

def scope(scope: String): Replacements =
ofReplacements(List(scope -> known.Scope))

Expand Down

0 comments on commit c7fbcca

Please sign in to comment.