2010-02-08 05:45:19 +01:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2008, 2009, 2010 Mark Harrah
|
|
|
|
|
*/
|
2010-01-16 01:05:23 +01:00
|
|
|
package sbt
|
2009-08-16 20:29:08 +02:00
|
|
|
|
|
|
|
|
import java.io.File
|
2010-04-24 03:20:07 +02:00
|
|
|
import scala.xml.{Node,NodeSeq}
|
2009-08-16 20:29:08 +02:00
|
|
|
|
2010-09-04 14:42:37 +02:00
|
|
|
import org.apache.ivy.{core, plugins, Ivy}
|
2009-08-16 20:29:08 +02:00
|
|
|
import core.cache.DefaultRepositoryCacheManager
|
|
|
|
|
import core.LogOptions
|
|
|
|
|
import core.deliver.DeliverOptions
|
2009-08-20 06:02:06 +02:00
|
|
|
import core.install.InstallOptions
|
2009-08-16 20:29:08 +02:00
|
|
|
import core.module.descriptor.{DefaultArtifact, DefaultDependencyArtifactDescriptor, MDArtifact}
|
|
|
|
|
import core.module.descriptor.{DefaultDependencyDescriptor, DefaultModuleDescriptor, DependencyDescriptor, ModuleDescriptor}
|
|
|
|
|
import core.module.id.{ArtifactId,ModuleId, ModuleRevisionId}
|
|
|
|
|
import core.publish.PublishOptions
|
2010-09-22 04:38:18 +02:00
|
|
|
import core.report.{ArtifactDownloadReport,ResolveReport}
|
2009-08-16 20:29:08 +02:00
|
|
|
import core.resolve.ResolveOptions
|
|
|
|
|
import core.retrieve.RetrieveOptions
|
|
|
|
|
import plugins.parser.m2.{PomModuleDescriptorParser,PomModuleDescriptorWriter}
|
|
|
|
|
|
2010-09-22 04:38:18 +02:00
|
|
|
final class UpdateConfiguration(val retrieve: Option[RetrieveConfiguration], val logging: UpdateLogging.Value)
|
|
|
|
|
final class RetrieveConfiguration(val retrieveDirectory: File, val outputPattern: String, val synchronize: Boolean)
|
2010-04-24 03:20:07 +02:00
|
|
|
final class MakePomConfiguration(val extraDependencies: Iterable[ModuleID], val configurations: Option[Iterable[Configuration]],
|
2010-09-22 04:38:18 +02:00
|
|
|
val extra: NodeSeq, val process: Node => Node, val filterRepositories: MavenRepository => Boolean)
|
2010-04-24 03:20:07 +02:00
|
|
|
object MakePomConfiguration
|
|
|
|
|
{
|
|
|
|
|
def apply(extraDependencies: Iterable[ModuleID], configurations: Option[Iterable[Configuration]], extra: NodeSeq) =
|
|
|
|
|
new MakePomConfiguration(extraDependencies, configurations, extra, identity, _ => true)
|
|
|
|
|
}
|
2010-05-03 01:15:01 +02:00
|
|
|
/** Configures logging during an 'update'. `level` determines the amount of other information logged.
|
|
|
|
|
* `Full` is the default and logs the most.
|
|
|
|
|
* `DownloadOnly` only logs what is downloaded.
|
|
|
|
|
* `Quiet` only displays errors.*/
|
|
|
|
|
object UpdateLogging extends Enumeration
|
|
|
|
|
{
|
|
|
|
|
val Full, DownloadOnly, Quiet = Value
|
|
|
|
|
}
|
2009-08-16 20:29:08 +02:00
|
|
|
|
|
|
|
|
object IvyActions
|
|
|
|
|
{
|
2009-08-20 06:02:06 +02:00
|
|
|
/** Installs the dependencies of the given 'module' from the resolver named 'from' to the resolver named 'to'.*/
|
|
|
|
|
def install(module: IvySbt#Module, from: String, to: String)
|
2009-08-18 06:51:08 +02:00
|
|
|
{
|
2009-08-20 06:02:06 +02:00
|
|
|
module.withModule { (ivy, md, default) =>
|
|
|
|
|
for(dependency <- md.getDependencies)
|
|
|
|
|
{
|
|
|
|
|
module.logger.info("Installing " + dependency)
|
|
|
|
|
val options = new InstallOptions
|
2009-09-27 20:39:26 +02:00
|
|
|
options.setValidate(module.moduleSettings.validate)
|
2009-08-20 06:02:06 +02:00
|
|
|
options.setTransitive(dependency.isTransitive)
|
|
|
|
|
ivy.install(dependency.getDependencyRevisionId, from, to, options)
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-18 06:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-16 20:29:08 +02:00
|
|
|
/** Clears the Ivy cache, as configured by 'config'. */
|
2010-01-30 02:31:07 +01:00
|
|
|
def cleanCache(ivy: IvySbt) = ivy.withIvy { iv =>
|
|
|
|
|
iv.getSettings.getResolutionCacheManager.clean()
|
|
|
|
|
iv.getSettings.getRepositoryCacheManagers.foreach(_.clean())
|
|
|
|
|
}
|
2009-08-20 06:02:06 +02:00
|
|
|
|
2009-08-16 20:29:08 +02:00
|
|
|
/** Creates a Maven pom from the given Ivy configuration*/
|
2010-04-24 03:20:07 +02:00
|
|
|
def makePom(module: IvySbt#Module, configuration: MakePomConfiguration, output: File)
|
2009-08-16 20:29:08 +02:00
|
|
|
{
|
2010-04-24 03:20:07 +02:00
|
|
|
import configuration.{configurations, extra, extraDependencies, filterRepositories, process}
|
2009-08-16 20:29:08 +02:00
|
|
|
module.withModule { (ivy, md, default) =>
|
|
|
|
|
addLateDependencies(ivy, md, default, extraDependencies)
|
2010-04-24 03:20:07 +02:00
|
|
|
(new MakePom).write(ivy, md, configurations, extra, process, filterRepositories, output)
|
2009-08-16 20:29:08 +02:00
|
|
|
module.logger.info("Wrote " + output.getAbsolutePath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// todo: correct default configuration for extra dependencies
|
|
|
|
|
private def addLateDependencies(ivy: Ivy, module: DefaultModuleDescriptor, defaultConfiguration: String, extraDependencies: Iterable[ModuleID])
|
|
|
|
|
{
|
2009-09-27 20:39:26 +02:00
|
|
|
val parser = new CustomXmlParser.CustomParser(ivy.getSettings, Some(defaultConfiguration))
|
2009-08-16 20:29:08 +02:00
|
|
|
parser.setMd(module)
|
|
|
|
|
IvySbt.addDependencies(module, extraDependencies, parser)
|
|
|
|
|
}
|
2009-08-20 06:02:06 +02:00
|
|
|
|
2010-05-03 01:15:01 +02:00
|
|
|
def deliver(module: IvySbt#Module, status: String, deliverIvyPattern: String, extraDependencies: Iterable[ModuleID], configurations: Option[Iterable[Configuration]], logging: UpdateLogging.Value)
|
2009-08-16 20:29:08 +02:00
|
|
|
{
|
|
|
|
|
module.withModule { case (ivy, md, default) =>
|
|
|
|
|
addLateDependencies(ivy, md, default, extraDependencies)
|
2010-05-03 01:15:01 +02:00
|
|
|
resolve(logging)(ivy, md, default) // todo: set download = false for resolve
|
2009-08-16 20:29:08 +02:00
|
|
|
val revID = md.getModuleRevisionId
|
|
|
|
|
val options = DeliverOptions.newInstance(ivy.getSettings).setStatus(status)
|
2010-03-30 15:19:36 +02:00
|
|
|
options.setConfs(IvySbt.getConfigurations(md, configurations))
|
2009-08-16 20:29:08 +02:00
|
|
|
ivy.deliver(revID, revID.getRevision, deliverIvyPattern, options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// todo: map configurations, extra dependencies
|
|
|
|
|
def publish(module: IvySbt#Module, resolverName: String, srcArtifactPatterns: Iterable[String], deliveredIvyPattern: Option[String], configurations: Option[Iterable[Configuration]])
|
|
|
|
|
{
|
|
|
|
|
module.withModule { case (ivy, md, default) =>
|
|
|
|
|
val revID = md.getModuleRevisionId
|
|
|
|
|
val patterns = new java.util.ArrayList[String]
|
|
|
|
|
srcArtifactPatterns.foreach(pattern => patterns.add(pattern))
|
|
|
|
|
val options = (new PublishOptions).setOverwrite(true)
|
|
|
|
|
deliveredIvyPattern.foreach(options.setSrcIvyPattern)
|
2010-03-30 15:19:36 +02:00
|
|
|
options.setConfs(IvySbt.getConfigurations(md, configurations))
|
2009-08-16 20:29:08 +02:00
|
|
|
ivy.publish(revID, patterns, resolverName, options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/** Resolves and retrieves dependencies. 'ivyConfig' is used to produce an Ivy file and configuration.
|
|
|
|
|
* 'updateConfig' configures the actual resolution and retrieval process. */
|
2010-09-04 14:42:37 +02:00
|
|
|
def update(module: IvySbt#Module, configuration: UpdateConfiguration) =
|
2009-08-16 20:29:08 +02:00
|
|
|
{
|
|
|
|
|
module.withModule { case (ivy, md, default) =>
|
2010-09-22 04:38:18 +02:00
|
|
|
import configuration.{retrieve => rConf, logging}
|
2010-09-04 14:42:37 +02:00
|
|
|
val report = resolve(logging)(ivy, md, default)
|
2010-09-22 04:38:18 +02:00
|
|
|
rConf match
|
|
|
|
|
{
|
|
|
|
|
case None => IvyRetrieve.cachePaths(report)
|
|
|
|
|
case Some(conf) => retrieve(ivy, md, conf, logging)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// doesn't work. perhaps replace retrieve/determineArtifactsToCopy with custom code
|
|
|
|
|
private def retrieve(ivy: Ivy, md: ModuleDescriptor, conf: RetrieveConfiguration, logging: UpdateLogging.Value) =
|
|
|
|
|
{
|
|
|
|
|
import conf._
|
|
|
|
|
val retrieveOptions = new RetrieveOptions
|
2009-08-16 20:29:08 +02:00
|
|
|
retrieveOptions.setSync(synchronize)
|
|
|
|
|
val patternBase = retrieveDirectory.getAbsolutePath
|
|
|
|
|
val pattern =
|
|
|
|
|
if(patternBase.endsWith(File.separator))
|
|
|
|
|
patternBase + outputPattern
|
|
|
|
|
else
|
|
|
|
|
patternBase + File.separatorChar + outputPattern
|
2010-09-22 04:38:18 +02:00
|
|
|
|
|
|
|
|
val engine = ivy.getRetrieveEngine
|
|
|
|
|
engine.retrieve(md.getModuleRevisionId, pattern, retrieveOptions)
|
|
|
|
|
|
|
|
|
|
//TODO: eliminate the duplication for better efficiency (retrieve already calls determineArtifactsToCopy once)
|
|
|
|
|
val rawMap = engine.determineArtifactsToCopy(md.getModuleRevisionId, pattern, retrieveOptions)
|
|
|
|
|
val map = rawMap.asInstanceOf[java.util.Map[ArtifactDownloadReport,java.util.Set[String]]]
|
|
|
|
|
val confMap = new collection.mutable.HashMap[String, Seq[File]]
|
|
|
|
|
|
|
|
|
|
import collection.JavaConversions.{asScalaMap,asScalaSet}
|
|
|
|
|
for( (report, all) <- map; retrieved <- all; val file = new File(retrieved); conf <- report.getArtifact.getConfigurations)
|
|
|
|
|
confMap.put(conf, file +: confMap.getOrElse(conf, Nil))
|
|
|
|
|
confMap.toMap
|
2009-08-16 20:29:08 +02:00
|
|
|
}
|
2010-09-04 14:42:37 +02:00
|
|
|
private def resolve(logging: UpdateLogging.Value)(ivy: Ivy, module: DefaultModuleDescriptor, defaultConf: String): ResolveReport =
|
2009-08-16 20:29:08 +02:00
|
|
|
{
|
|
|
|
|
val resolveOptions = new ResolveOptions
|
2010-05-03 01:15:01 +02:00
|
|
|
resolveOptions.setLog(ivyLogLevel(logging))
|
2009-08-16 20:29:08 +02:00
|
|
|
val resolveReport = ivy.resolve(module, resolveOptions)
|
|
|
|
|
if(resolveReport.hasError)
|
2010-06-16 02:38:18 +02:00
|
|
|
throw new ResolveException(resolveReport.getAllProblemMessages.toArray.map(_.toString).distinct)
|
2010-09-04 14:42:37 +02:00
|
|
|
resolveReport
|
2009-08-16 20:29:08 +02:00
|
|
|
}
|
2010-05-03 01:15:01 +02:00
|
|
|
|
|
|
|
|
import UpdateLogging.{Quiet, Full, DownloadOnly}
|
|
|
|
|
import LogOptions.{LOG_QUIET, LOG_DEFAULT, LOG_DOWNLOAD_ONLY}
|
|
|
|
|
private def ivyLogLevel(level: UpdateLogging.Value) =
|
|
|
|
|
level match
|
|
|
|
|
{
|
|
|
|
|
case Quiet => LOG_QUIET
|
|
|
|
|
case DownloadOnly => LOG_DOWNLOAD_ONLY
|
|
|
|
|
case Full => LOG_DEFAULT
|
|
|
|
|
}
|
2010-02-06 23:43:14 +01:00
|
|
|
}
|
2010-06-16 02:38:18 +02:00
|
|
|
final class ResolveException(messages: Seq[String]) extends RuntimeException(messages.mkString("\n"))
|