sbt/USAGE.md

65 lines
1.7 KiB
Markdown
Raw Normal View History

2015-06-16 20:17:07 +02:00
# API
Ensure you have a dependency on its artifact, e.g. add in `build.sbt`,
```scala
resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies +=
2015-06-22 23:34:18 +02:00
"com.github.alexarchambault" %% "coursier" % "0.1.0-SNAPSHOT"
2015-06-16 20:17:07 +02:00
```
Then,
```scala
import coursier._
2015-06-26 03:11:56 +02:00
// Cache for metadata can be setup here, see cli/src/main/scala/coursier/Coursier.scala
2015-06-16 20:17:07 +02:00
val repositories = Seq(
2015-06-26 03:11:56 +02:00
Repository.ivy2Local,
Repository.mavenCentral
2015-06-16 20:17:07 +02:00
)
val dependencies = Set(
Dependency(Module("com.lihaoyi", "ammonite-pprint_2.11"), "0.3.2"),
Dependency(Module("org.scala-lang", "scala-reflect"), "2.11.6")
2015-06-16 20:17:07 +02:00
)
val resolution =
2015-06-26 03:11:56 +02:00
Resolution(dependencies)
.process
.run(repositories)
.run
// Note that only metadata are downloaded during resolution
2015-06-16 20:17:07 +02:00
assert(resolution.isDone) // Check that resolution converged
2015-06-26 03:11:56 +02:00
// Errors in
resolution.errors // Seq[(Dependency, Seq[String])], the Seq[String] contains the errors returned by each repository
// Artifact URLs in
resolution.artifacts // Seq[Artifact]
// Artifact has in particular a field url: String
2015-06-16 20:17:07 +02:00
2015-06-26 03:11:56 +02:00
// Now if we want to download or cache the artifacts, add in build.sbt
// "com.github.alexarchambault" %% "coursier-files" % "0.1.0-SNAPSHOT"
2015-06-16 20:17:07 +02:00
2015-06-26 03:11:56 +02:00
val files = Files(
Seq(),
() => ???, // TODO Tmp directory for URLs with no cache
Some(logger) // Optional, logger: FilesLogger
)
val cachePolicy = Repository.CachePolicy.Default
for (artifact <- artifacts)
files.file(artifact, cachePolicy).run match {
case -\/(err) => // Download failed, err: String
case \/-(file) => // Success, file: java.io.File
2015-06-16 20:17:07 +02:00
}
2015-06-26 03:11:56 +02:00
// Artifacts can be downloaded in parallel thanks to Task.gatherUnordered
// See the example in cli/src/main/scala/coursier/Coursier.scala
2015-06-16 20:17:07 +02:00
```