Cache now only designated by a simple directory

This commit is contained in:
Alexandre Archambault 2016-03-06 14:45:57 +01:00
parent af5275c014
commit a7a34320df
7 changed files with 40 additions and 45 deletions

View File

@ -220,6 +220,13 @@ lazy val cache = project
import com.typesafe.tools.mima.core.ProblemFilters._ import com.typesafe.tools.mima.core.ProblemFilters._
Seq( Seq(
// Since 1.0.0-M10
// cache argument type changed from `Seq[(String, File)]` to `File`
ProblemFilters.exclude[IncompatibleMethTypeProblem]("coursier.Cache.file"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("coursier.Cache.fetch"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("coursier.Cache.default"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("coursier.Cache.validateChecksum"),
ProblemFilters.exclude[MissingMethodProblem]("coursier.Cache.defaultBase"),
// Since 1.0.0-M9 // Since 1.0.0-M9
// Added an optional extra parameter to FileError.NotFound - only // Added an optional extra parameter to FileError.NotFound - only
// its unapply method should break compatibility at the source level. // its unapply method should break compatibility at the source level.

View File

@ -43,24 +43,29 @@ object Cache {
} }
} }
private def withLocal(artifact: Artifact, cache: Seq[(String, File)]): Artifact = { private def withLocal(artifact: Artifact, cache: File): Artifact = {
def local(url: String) = def local(url: String) =
if (url.startsWith("file:///")) if (url.startsWith("file:///"))
url.stripPrefix("file://") url.stripPrefix("file://")
else if (url.startsWith("file:/")) else if (url.startsWith("file:/"))
url.stripPrefix("file:") url.stripPrefix("file:")
else { else
val localPathOpt = cache.collectFirst { // FIXME Should we fully parse the URL here?
case (base, cacheDir) if url.startsWith(base) => // FIXME Should some safeguards be added against '..' components in paths?
cacheDir.toString + "/" + escape(url.stripPrefix(base)) url.split(":", 2) match {
} case Array(protocol, remaining) =>
val remaining0 =
if (remaining.startsWith("///"))
remaining.stripPrefix("///")
else if (remaining.startsWith("/"))
remaining.stripPrefix("/")
else
throw new Exception(s"URL $url doesn't contain an absolute path")
localPathOpt.getOrElse { new File(cache, escape(protocol + "/" + remaining0)) .toString
// FIXME Means we were handed an artifact from repositories other than the known ones
println(cache.mkString("\n")) case _ =>
println(url) throw new Exception(s"No protocol found in URL $url")
???
}
} }
if (artifact.extra.contains("local")) if (artifact.extra.contains("local"))
@ -180,7 +185,7 @@ object Cache {
private def download( private def download(
artifact: Artifact, artifact: Artifact,
cache: Seq[(String, File)], cache: File,
checksums: Set[String], checksums: Set[String],
cachePolicy: CachePolicy, cachePolicy: CachePolicy,
pool: ExecutorService, pool: ExecutorService,
@ -460,7 +465,7 @@ object Cache {
def validateChecksum( def validateChecksum(
artifact: Artifact, artifact: Artifact,
sumType: String, sumType: String,
cache: Seq[(String, File)], cache: File,
pool: ExecutorService pool: ExecutorService
): EitherT[Task, FileError, Unit] = { ): EitherT[Task, FileError, Unit] = {
@ -514,7 +519,7 @@ object Cache {
def file( def file(
artifact: Artifact, artifact: Artifact,
cache: Seq[(String, File)] = default, cache: File = default,
cachePolicy: CachePolicy = CachePolicy.FetchMissing, cachePolicy: CachePolicy = CachePolicy.FetchMissing,
checksums: Seq[Option[String]] = defaultChecksums, checksums: Seq[Option[String]] = defaultChecksums,
logger: Option[Logger] = None, logger: Option[Logger] = None,
@ -565,7 +570,7 @@ object Cache {
} }
def fetch( def fetch(
cache: Seq[(String, File)] = default, cache: File = default,
cachePolicy: CachePolicy = CachePolicy.FetchMissing, cachePolicy: CachePolicy = CachePolicy.FetchMissing,
checksums: Seq[Option[String]] = defaultChecksums, checksums: Seq[Option[String]] = defaultChecksums,
logger: Option[Logger] = None, logger: Option[Logger] = None,
@ -613,18 +618,13 @@ object Cache {
dropInfoAttributes = true dropInfoAttributes = true
) )
lazy val defaultBase = new File( lazy val default = new File(
sys.env.getOrElse( sys.env.getOrElse(
"COURSIER_CACHE", "COURSIER_CACHE",
sys.props("user.home") + "/.coursier/cache/v1" sys.props("user.home") + "/.coursier/cache/v1"
) )
).getAbsoluteFile ).getAbsoluteFile
lazy val default = Seq(
"http://" -> new File(defaultBase, "http"),
"https://" -> new File(defaultBase, "https")
)
val defaultConcurrentDownloadCount = 6 val defaultConcurrentDownloadCount = 6
lazy val defaultPool = lazy val defaultPool =

View File

@ -74,7 +74,7 @@ case class CommonOptions(
case class CacheOptions( case class CacheOptions(
@Help("Cache directory (defaults to environment variable COURSIER_CACHE or ~/.coursier/cache/v1)") @Help("Cache directory (defaults to environment variable COURSIER_CACHE or ~/.coursier/cache/v1)")
@Short("C") @Short("C")
cache: String = Cache.defaultBase.toString cache: String = Cache.default.toString
) )
sealed abstract class CoursierCommand extends Command sealed abstract class CoursierCommand extends Command

View File

@ -77,11 +77,7 @@ class Helper(
) )
} }
val caches = val cache = new File(cacheOptions.cache)
Seq(
"http://" -> new File(new File(cacheOptions.cache), "http"),
"https://" -> new File(new File(cacheOptions.cache), "https")
)
val pool = Executors.newFixedThreadPool(parallel, Strategy.DefaultDaemonThreadFactory) val pool = Executors.newFixedThreadPool(parallel, Strategy.DefaultDaemonThreadFactory)
@ -200,7 +196,7 @@ class Helper(
None None
val fetchs = cachePolicies.map(p => val fetchs = cachePolicies.map(p =>
Cache.fetch(caches, p, checksums = checksums, logger = logger, pool = pool) Cache.fetch(cache, p, checksums = checksums, logger = logger, pool = pool)
) )
val fetchQuiet = coursier.Fetch.from( val fetchQuiet = coursier.Fetch.from(
repositories, repositories,
@ -332,8 +328,8 @@ class Helper(
println(s" Found ${artifacts0.length} artifacts") println(s" Found ${artifacts0.length} artifacts")
val tasks = artifacts0.map(artifact => val tasks = artifacts0.map(artifact =>
(Cache.file(artifact, caches, cachePolicies.head, checksums = checksums, logger = logger, pool = pool) /: cachePolicies.tail)( (Cache.file(artifact, cache, cachePolicies.head, checksums = checksums, logger = logger, pool = pool) /: cachePolicies.tail)(
_ orElse Cache.file(artifact, caches, _, checksums = checksums, logger = logger, pool = pool) _ orElse Cache.file(artifact, cache, _, checksums = checksums, logger = logger, pool = pool)
).run.map(artifact.->) ).run.map(artifact.->)
) )

View File

@ -39,7 +39,7 @@ object CoursierPlugin extends AutoPlugin {
coursierVerbosity := 1, coursierVerbosity := 1,
coursierResolvers <<= Tasks.coursierResolversTask, coursierResolvers <<= Tasks.coursierResolversTask,
coursierSbtResolvers <<= externalResolvers in updateSbtClassifiers, coursierSbtResolvers <<= externalResolvers in updateSbtClassifiers,
coursierCache := Cache.defaultBase, coursierCache := Cache.default,
update <<= Tasks.updateTask(withClassifiers = false), update <<= Tasks.updateTask(withClassifiers = false),
updateClassifiers <<= Tasks.updateTask(withClassifiers = true), updateClassifiers <<= Tasks.updateTask(withClassifiers = true),
updateSbtClassifiers in Defaults.TaskGlobal <<= Tasks.updateTask(withClassifiers = true, sbtClassifiers = true), updateSbtClassifiers in Defaults.TaskGlobal <<= Tasks.updateTask(withClassifiers = true, sbtClassifiers = true),

View File

@ -215,7 +215,7 @@ object Tasks {
val artifactsChecksums = coursierArtifactsChecksums.value val artifactsChecksums = coursierArtifactsChecksums.value
val maxIterations = coursierMaxIterations.value val maxIterations = coursierMaxIterations.value
val cachePolicy = coursierCachePolicy.value val cachePolicy = coursierCachePolicy.value
val cacheDir = coursierCache.value val cache = coursierCache.value
val sv = scalaVersion.value // is this always defined? (e.g. for Java only projects?) val sv = scalaVersion.value // is this always defined? (e.g. for Java only projects?)
@ -273,11 +273,6 @@ object Tasks {
val repositories = Seq(globalPluginsRepo, interProjectRepo) ++ resolvers.flatMap(FromSbt.repository(_, ivyProperties)) val repositories = Seq(globalPluginsRepo, interProjectRepo) ++ resolvers.flatMap(FromSbt.repository(_, ivyProperties))
val caches = Seq(
"http://" -> new File(cacheDir, "http"),
"https://" -> new File(cacheDir, "https")
)
val pool = Executors.newFixedThreadPool(parallelDownloads, Strategy.DefaultDaemonThreadFactory) val pool = Executors.newFixedThreadPool(parallelDownloads, Strategy.DefaultDaemonThreadFactory)
def createLogger() = new TermDisplay(new OutputStreamWriter(System.err)) def createLogger() = new TermDisplay(new OutputStreamWriter(System.err))
@ -286,8 +281,8 @@ object Tasks {
val fetch = Fetch.from( val fetch = Fetch.from(
repositories, repositories,
Cache.fetch(caches, CachePolicy.LocalOnly, checksums = checksums, logger = Some(resLogger), pool = pool), Cache.fetch(cache, CachePolicy.LocalOnly, checksums = checksums, logger = Some(resLogger), pool = pool),
Cache.fetch(caches, cachePolicy, checksums = checksums, logger = Some(resLogger), pool = pool) Cache.fetch(cache, cachePolicy, checksums = checksums, logger = Some(resLogger), pool = pool)
) )
def depsRepr(deps: Seq[(String, Dependency)]) = def depsRepr(deps: Seq[(String, Dependency)]) =
@ -411,7 +406,7 @@ object Tasks {
val artifactsLogger = createLogger() val artifactsLogger = createLogger()
val artifactFileOrErrorTasks = allArtifacts.toVector.map { a => val artifactFileOrErrorTasks = allArtifacts.toVector.map { a =>
Cache.file(a, caches, cachePolicy, checksums = artifactsChecksums, logger = Some(artifactsLogger), pool = pool).run.map((a, _)) Cache.file(a, cache, cachePolicy, checksums = artifactsChecksums, logger = Some(artifactsLogger), pool = pool).run.map((a, _))
} }
if (verbosity >= 0) if (verbosity >= 0)

View File

@ -37,10 +37,7 @@ object ChecksumTests extends TestSuite {
val cachePath = getClass.getResource("/checksums").getPath val cachePath = getClass.getResource("/checksums").getPath
val cache = Seq( val cache = new File(cachePath)
"http://" -> new File(cachePath),
"https://" -> new File(cachePath)
)
def validate(artifact: Artifact, sumType: String) = def validate(artifact: Artifact, sumType: String) =
Cache.validateChecksum( Cache.validateChecksum(