Made implicit the cache policy arguments

This commit is contained in:
Alexandre Archambault 2015-07-04 15:19:39 +01:00
parent e2c654eb88
commit 6eeb792829
8 changed files with 51 additions and 19 deletions

View File

@ -6,12 +6,14 @@ import java.io.File
import caseapp._ import caseapp._
import coursier.core.{ MavenRepository, Parse, Repository, CachePolicy } import coursier.core.{ MavenRepository, Parse, Repository, CachePolicy }
import scalaz.{\/-, -\/}
import scalaz.concurrent.Task import scalaz.concurrent.Task
case class Coursier( case class Coursier(
scope: List[String], scope: List[String],
keepOptional: Boolean, keepOptional: Boolean,
fetch: Boolean, fetch: Boolean,
@ExtraName("P") @ExtraName("cp") classpath: Boolean,
@ExtraName("c") offline: Boolean, @ExtraName("c") offline: Boolean,
@ExtraName("v") verbose: List[Unit], @ExtraName("v") verbose: List[Unit],
@ExtraName("N") maxIterations: Int = 100 @ExtraName("N") maxIterations: Int = 100
@ -29,10 +31,11 @@ case class Coursier(
def fileRepr(f: File) = f.toString def fileRepr(f: File) = f.toString
def println(s: String) = Console.err.println(s)
val logger: MavenRepository.Logger with Files.Logger = val logger: MavenRepository.Logger with Files.Logger =
new MavenRepository.Logger with Files.Logger { new MavenRepository.Logger with Files.Logger {
def println(s: String) = Console.err.println(s)
def downloading(url: String) = def downloading(url: String) =
println(s"Downloading $url") println(s"Downloading $url")
def downloaded(url: String, success: Boolean) = def downloaded(url: String, success: Boolean) =
@ -57,6 +60,12 @@ case class Coursier(
) )
} }
implicit val cachePolicy =
if (offline)
CachePolicy.LocalOnly
else
CachePolicy.Default
val cachedMavenCentral = Repository.mavenCentral.copy( val cachedMavenCentral = Repository.mavenCentral.copy(
cache = Some(centralCacheDir), cache = Some(centralCacheDir),
logger = if (verbose0 <= 1) None else Some(logger) logger = if (verbose0 <= 1) None else Some(logger)
@ -78,7 +87,7 @@ case class Coursier(
} }
if (malformed.nonEmpty) { if (malformed.nonEmpty) {
Console.err.println(s"Malformed dependencies:\n${malformed.map(_.mkString(":")).mkString("\n")}") println(s"Malformed dependencies:\n${malformed.map(_.mkString(":")).mkString("\n")}")
sys exit 1 sys exit 1
} }
@ -114,7 +123,7 @@ case class Coursier(
.run .run
if (!res.isDone) { if (!res.isDone) {
Console.err.println(s"Maximum number of iteration reached!") println(s"Maximum number of iteration reached!")
sys exit 1 sys exit 1
} }
@ -145,14 +154,8 @@ case class Coursier(
} }
} }
if (fetch) { if (fetch || classpath) {
println() println("")
val cachePolicy: CachePolicy =
if (offline)
CachePolicy.LocalOnly
else
CachePolicy.Default
val artifacts = res.artifacts val artifacts = res.artifacts
@ -164,10 +167,25 @@ case class Coursier(
if (verbose0 <= 0) None else Some(logger) if (verbose0 <= 0) None else Some(logger)
) )
val tasks = artifacts.map(files.file(_, cachePolicy).run) val tasks = artifacts.map(artifact => files.file(artifact, cachePolicy).run.map(artifact.->))
val task = Task.gatherUnordered(tasks) val task = Task.gatherUnordered(tasks)
task.run val results = task.run
val errors = results.collect{case (artifact, -\/(err)) => artifact -> err }
val files0 = results.collect{case (artifact, \/-(f)) => f }
if (errors.nonEmpty) {
println(s"${errors.size} error(s):")
for ((artifact, error) <- errors) {
println(s" ${artifact.url}: $error")
}
}
Console.out.println(
files0
.map(_.toString)
.mkString(File.pathSeparator)
)
} }
} }

View File

@ -26,6 +26,8 @@ object JsTests extends TestSuite {
} }
'getProj{ 'getProj{
implicit val cachePolicy = CachePolicy.Default
Repository.mavenCentral Repository.mavenCentral
.find(Module("ch.qos.logback", "logback-classic"), "1.1.3") .find(Module("ch.qos.logback", "logback-classic"), "1.1.3")
.map{case (_, proj) => .map{case (_, proj) =>

View File

@ -8,8 +8,9 @@ import coursier.core.compatibility.encodeURIComponent
trait Repository { trait Repository {
def find( def find(
module: Module, module: Module,
version: String, version: String
cachePolicy: CachePolicy = CachePolicy.Default )(implicit
cachePolicy: CachePolicy
): EitherT[Task, String, (Artifact.Source, Project)] ): EitherT[Task, String, (Artifact.Source, Project)]
} }
@ -38,6 +39,8 @@ object Repository {
repositories: Seq[Repository], repositories: Seq[Repository],
module: Module, module: Module,
version: String version: String
)(implicit
cachePolicy: CachePolicy
): EitherT[Task, Seq[String], (Artifact.Source, Project)] = { ): EitherT[Task, Seq[String], (Artifact.Source, Project)] = {
val lookups = repositories val lookups = repositories
@ -295,7 +298,8 @@ abstract class BaseMavenRepository(
def find( def find(
module: Module, module: Module,
version: String, version: String
)(implicit
cachePolicy: CachePolicy cachePolicy: CachePolicy
): EitherT[Task, String, (Artifact.Source, Project)] = { ): EitherT[Task, String, (Artifact.Source, Project)] = {

View File

@ -94,6 +94,8 @@ package object coursier {
implicit def fetch( implicit def fetch(
repositories: Seq[core.Repository] repositories: Seq[core.Repository]
)(implicit
cachePolicy: CachePolicy
): ResolutionProcess.Fetch[Task] = { ): ResolutionProcess.Fetch[Task] = {
modVers => modVers =>

View File

@ -13,6 +13,8 @@ object CentralTests extends TestSuite {
Repository.mavenCentral Repository.mavenCentral
) )
implicit val cachePolicy = CachePolicy.Default
def resolve(deps: Set[Dependency], filter: Option[Dependency => Boolean] = None, extraRepo: Option[Repository] = None) = { def resolve(deps: Set[Dependency], filter: Option[Dependency => Boolean] = None, extraRepo: Option[Repository] = None) = {
val repositories0 = extraRepo.toSeq ++ repositories val repositories0 = extraRepo.toSeq ++ repositories

View File

@ -9,6 +9,8 @@ import coursier.test.compatibility._
object ResolutionTests extends TestSuite { object ResolutionTests extends TestSuite {
implicit val cachePolicy = CachePolicy.Default
def resolve0(deps: Set[Dependency], filter: Option[Dependency => Boolean] = None) = { def resolve0(deps: Set[Dependency], filter: Option[Dependency => Boolean] = None) = {
Resolution(deps, filter = filter) Resolution(deps, filter = filter)
.process .process

View File

@ -11,7 +11,7 @@ class TestRepository(projects: Map[(Module, String), Project]) extends Repositor
val source = new core.Artifact.Source { val source = new core.Artifact.Source {
def artifacts(dependency: Dependency, project: Project) = ??? def artifacts(dependency: Dependency, project: Project) = ???
} }
def find(module: Module, version: String, cachePolicy: CachePolicy) = def find(module: Module, version: String)(implicit cachePolicy: CachePolicy) =
EitherT(Task.now( EitherT(Task.now(
projects.get((module, version)).map((source, _)).toRightDisjunction("Not found") projects.get((module, version)).map((source, _)).toRightDisjunction("Not found")
)) ))

View File

@ -133,9 +133,11 @@ class Backend($: BackendScope[Unit, State]) {
filter = Some(dep => (s.options.followOptional || !dep.optional) && (s.options.keepTest || dep.scope != Scope.Test)) filter = Some(dep => (s.options.followOptional || !dep.optional) && (s.options.keepTest || dep.scope != Scope.Test))
) )
implicit val cachePolicy = CachePolicy.Default
res res
.process .process
.run(fetch(s.repositories.map(r => r.copy(logger = Some(logger)))), 100) .run(s.repositories.map(r => r.copy(logger = Some(logger))), 100)
} }
// For reasons that are unclear to me, not delaying this when using the runNow execution context // For reasons that are unclear to me, not delaying this when using the runNow execution context