Skip to content

Commit ab72937

Browse files
authored
Merge pull request #102 from eed3si9n/wip/cache-dir
Avoid shelling out to Powershell on Windows
2 parents d9e0cbd + f503ce6 commit ab72937

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

launcher-implementation/src/main/scala/xsbt/boot/CoursierUpdate.scala

+36-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package xsbt.boot
22

33
import Pre._
44
import coursier._
5-
import coursier.cache.FileCache
5+
import coursier.cache.{ CacheDefaults, FileCache }
66
import coursier.core.{ Publication, Repository }
77
import coursier.credentials.DirectCredentials
88
import coursier.ivy.IvyRepository
@@ -26,9 +26,43 @@ class CousierUpdate(config: UpdateConfiguration) {
2626

2727
private def logFile = new File(bootDirectory, UpdateLogName)
2828
private val logWriter = new PrintWriter(new FileWriter(logFile))
29+
30+
private def defaultCacheLocation: File = {
31+
def absoluteFile(path: String): File = new File(path).getAbsoluteFile()
32+
def windowsCacheDirectory: File = {
33+
// Per discussion in https://github.com/dirs-dev/directories-jvm/issues/43,
34+
// LOCALAPPDATA environment variable may NOT represent the one-true
35+
// Known Folders API (https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid)
36+
// in case the user happened to have set the LOCALAPPDATA environmental variable.
37+
// Given that there's no reliable way of accessing this API from JVM, I think it's actually
38+
// better to use the LOCALAPPDATA as the first place to look.
39+
// When it is not found, it will fall back to $HOME/AppData/Local.
40+
// For the purpose of picking the Coursier cache directory, it's better to be
41+
// fast, reliable, and predictable rather than strict adherence to Microsoft.
42+
val base =
43+
sys.env
44+
.get("LOCALAPPDATA")
45+
.map(absoluteFile)
46+
.getOrElse(new File(new File(absoluteFile(sys.props("user.home")), "AppData"), "Local"))
47+
new File(new File(new File(base, "Coursier"), "Cache"), "v1")
48+
}
49+
sys.props
50+
.get("sbt.coursier.home")
51+
.map(home => new File(absoluteFile(home), "cache"))
52+
.orElse(sys.env.get("COURSIER_CACHE").map(absoluteFile))
53+
.orElse(sys.props.get("coursier.cache").map(absoluteFile)) match {
54+
case Some(dir) => dir
55+
case _ =>
56+
if (isWindows) windowsCacheDirectory
57+
else CacheDefaults.location
58+
}
59+
}
2960
private lazy val coursierCache = {
61+
import coursier.util.Task
3062
val credentials = bootCredentials
31-
val cache = credentials.foldLeft(FileCache()) { _.addCredentials(_) }
63+
val cache = credentials.foldLeft(FileCache(defaultCacheLocation)(Task.sync)) {
64+
_.addCredentials(_)
65+
}
3266
cache
3367
}
3468
private lazy val coursierRepos: Seq[Repository] =

0 commit comments

Comments
 (0)