Skip to content

Commit

Permalink
- Extra Settings are now available to control project information, vi…
Browse files Browse the repository at this point in the history
…z., name, description, homepage, organization name, organization homepage, licenses

- Modified name/signature of some private types/methods to reduce confusion (all in limited scope, so nothing should change from end user's pov)
- Enriched Ivy and Maven descriptors produced out of the box (see Keys.scala and Defaults.scala for more)
- Projects do not need to create custom Ivy <info/> block anymore, there is more settings-specific control instead
  • Loading branch information
indrajitr committed Aug 3, 2011
1 parent e67b3be commit 67102aa
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 47 deletions.
50 changes: 26 additions & 24 deletions ivy/Ivy.scala
Expand Up @@ -14,7 +14,7 @@ import org.apache.ivy.{core, plugins, util, Ivy}
import core.IvyPatternHelper
import core.cache.{CacheMetadataOptions, DefaultRepositoryCacheManager}
import core.module.descriptor.{Artifact => IArtifact, DefaultArtifact, DefaultDependencyArtifactDescriptor, MDArtifact}
import core.module.descriptor.{DefaultDependencyDescriptor, DefaultModuleDescriptor, DependencyDescriptor, ModuleDescriptor}
import core.module.descriptor.{DefaultDependencyDescriptor, DefaultModuleDescriptor, DependencyDescriptor, ModuleDescriptor, License}
import core.module.id.{ArtifactId,ModuleId, ModuleRevisionId}
import core.resolve.IvyNode
import core.settings.IvySettings
Expand All @@ -26,7 +26,7 @@ import plugins.resolver.{ChainResolver, DependencyResolver}
import util.{Message, MessageLogger}
import util.extendable.ExtendableItem

import scala.xml.NodeSeq
import scala.xml.{NodeSeq, Text}

final class IvySbt(val configuration: IvyConfiguration)
{
Expand Down Expand Up @@ -124,9 +124,9 @@ final class IvySbt(val configuration: IvyConfiguration)
moduleSettings match
{
case ic: InlineConfiguration => configureInline(ic, configuration.log)
case ec: EmptyConfiguration => configureEmpty(ec.module)
case pc: PomConfiguration => readPom(pc.file, pc.validate)
case ifc: IvyFileConfiguration => readIvyFile(ifc.file, ifc.validate)
case ec: EmptyConfiguration => configureEmpty(ec)
case pc: PomConfiguration => configurePom(pc)
case ifc: IvyFileConfiguration => configureIvyFile(ifc)
}
moduleSettings.ivyScala.foreach(IvyScala.checkModule(baseModule, baseConfiguration))
baseModule.getExtraAttributesNamespaces.asInstanceOf[java.util.Map[String,String]].put("e", "http://ant.apache.org/ivy/extra")
Expand All @@ -135,7 +135,7 @@ final class IvySbt(val configuration: IvyConfiguration)
private def configureInline(ic: InlineConfiguration, log: Logger) =
{
import ic._
val moduleID = newConfiguredModuleID(module, configurations)
val moduleID = newConfiguredModuleID(module, moduleInfo, configurations)
val defaultConf = defaultConfiguration getOrElse Configurations.config(ModuleDescriptor.DEFAULT_CONFIGURATION)
log.debug("Using inline dependencies specified in Scala" + (if(ivyXML.isEmpty) "." else " and XML."))

Expand All @@ -144,44 +144,43 @@ final class IvySbt(val configuration: IvyConfiguration)
IvySbt.addMainArtifact(moduleID)
(moduleID, parser.getDefaultConf)
}
private def newConfiguredModuleID(module: ModuleID, configurations: Iterable[Configuration]) =
private def newConfiguredModuleID(module: ModuleID, moduleInfo: ModuleInfo, configurations: Iterable[Configuration]) =
{
val mod = new DefaultModuleDescriptor(IvySbt.toID(module), "release", null, false)
mod.setLastModified(System.currentTimeMillis)
mod.setDescription(moduleInfo.description)
moduleInfo.homepage foreach { h => mod.setHomePage(h.toString) }
moduleInfo.licenses foreach { l => mod.addLicense(new License(l._1, l._2.toString)) }
IvySbt.addConfigurations(mod, configurations)
IvySbt.addArtifacts(mod, module.explicitArtifacts)
mod
}

/** Parses the given Maven pom 'pomFile'.*/
private def readPom(pomFile: File, validate: Boolean) =
/** Parses the Maven pom 'pomFile' from the given `PomConfiguration`.*/
private def configurePom(pc: PomConfiguration) =
{
val md = PomModuleDescriptorParser.getInstance.parseDescriptor(settings, toURL(pomFile), validate)
val md = PomModuleDescriptorParser.getInstance.parseDescriptor(settings, toURL(pc.file), pc.validate)
val dmd = IvySbt.toDefaultModuleDescriptor(md)
IvySbt.addConfigurations(dmd, Configurations.defaultInternal)
(dmd, "compile")
}
/** Parses the given Ivy file 'ivyFile'.*/
private def readIvyFile(ivyFile: File, validate: Boolean) =
/** Parses the Ivy file 'ivyFile' from the given `IvyFileConfiguration`.*/
private def configureIvyFile(ifc: IvyFileConfiguration) =
{
val url = toURL(ivyFile)
val parser = new CustomXmlParser.CustomParser(settings, None)
parser.setValidate(validate)
parser.setSource(url)
parser.setValidate(ifc.validate)
parser.setSource(toURL(ifc.file))
parser.parse()
val md = parser.getModuleDescriptor()
(IvySbt.toDefaultModuleDescriptor(md), parser.getDefaultConf)
}
private def toURL(file: File) = file.toURI.toURL
private def configureEmpty(module: ModuleID) =
private def configureEmpty(ec: EmptyConfiguration) =
{
val defaultConf = ModuleDescriptor.DEFAULT_CONFIGURATION
val moduleID = new DefaultModuleDescriptor(IvySbt.toID(module), "release", null, false)
moduleID.setLastModified(System.currentTimeMillis)
moduleID.addConfiguration(IvySbt.toIvyConfiguration(Configurations.Default))
IvySbt.addArtifacts(moduleID, module.explicitArtifacts)
IvySbt.addMainArtifact(moduleID)
(moduleID, defaultConf)
val mod = newConfiguredModuleID(ec.module, ec.moduleInfo, Seq(Configurations.Default))
IvySbt.addMainArtifact(mod)
(mod, defaultConf)
}
}
}
Expand Down Expand Up @@ -348,12 +347,11 @@ private object IvySbt
/** Creates a full ivy file for 'module' using the 'dependencies' XML as the part after the &lt;info&gt;...&lt;/info&gt; section. */
private def wrapped(module: ModuleID, dependencies: NodeSeq) =
{
import module._
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
{ if(hasInfo(module, dependencies))
NodeSeq.Empty
else
addExtraAttributes(<info organisation={organization} module={name} revision={revision}/>, module.extraAttributes)
addExtraAttributes(defaultInfo(module), module.extraAttributes)
}
{dependencies}
{
Expand All @@ -362,6 +360,10 @@ private object IvySbt
}
</ivy-module>
}
private[this] def defaultInfo(module: ModuleID): scala.xml.Elem = {
import module._
<info organisation={organization} module={name} revision={revision}/>
}
private[this] def addExtraAttributes(elem: scala.xml.Elem, extra: Map[String, String]): scala.xml.Elem =
(elem /: extra) { case (e, (key,value) ) => e % new scala.xml.UnprefixedAttribute(key, value, scala.xml.Null) }
private def hasInfo(module: ModuleID, x: scala.xml.NodeSeq) =
Expand Down
10 changes: 5 additions & 5 deletions ivy/IvyActions.scala
Expand Up @@ -26,7 +26,7 @@ final class PublishConfiguration(val ivyFile: Option[File], val resolverName: St

final class UpdateConfiguration(val retrieve: Option[RetrieveConfiguration], val missingOk: Boolean, val logging: UpdateLogging.Value)
final class RetrieveConfiguration(val retrieveDirectory: File, val outputPattern: String)
final case class MakePomConfiguration(file: File, configurations: Option[Iterable[Configuration]] = None, extra: NodeSeq = NodeSeq.Empty, process: XNode => XNode = n => n, filterRepositories: MavenRepository => Boolean = _ => true, allRepositories: Boolean)
final case class MakePomConfiguration(file: File, moduleInfo: ModuleInfo, configurations: Option[Iterable[Configuration]] = None, extra: NodeSeq = NodeSeq.Empty, process: XNode => XNode = n => n, filterRepositories: MavenRepository => Boolean = _ => true, allRepositories: Boolean)
// exclude is a map on a restricted ModuleID
final case class GetClassifiersConfiguration(module: GetClassifiersModule, exclude: Map[ModuleID, Set[String]], configuration: UpdateConfiguration, ivyScala: Option[IvyScala])
final case class GetClassifiersModule(id: ModuleID, modules: Seq[ModuleID], configurations: Seq[Configuration], classifiers: Seq[String])
Expand Down Expand Up @@ -66,9 +66,9 @@ object IvyActions
/** Creates a Maven pom from the given Ivy configuration*/
def makePom(module: IvySbt#Module, configuration: MakePomConfiguration, log: Logger)
{
import configuration.{allRepositories, configurations, extra, file, filterRepositories, process}
import configuration.{allRepositories, moduleInfo, configurations, extra, file, filterRepositories, process}
module.withModule(log) { (ivy, md, default) =>
(new MakePom).write(ivy, md, configurations, extra, process, filterRepositories, allRepositories, file)
(new MakePom).write(ivy, md, moduleInfo, configurations, extra, process, filterRepositories, allRepositories, file)
log.info("Wrote " + file.getAbsolutePath)
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ object IvyActions
import config.{configuration => c, ivyScala, module => mod}
import mod.{configurations => confs, id, modules => deps}
val base = restrictedCopy(id).copy(name = id.name + "$" + label)
val module = new ivySbt.Module(InlineConfiguration(base, deps).copy(ivyScala = ivyScala))
val module = new ivySbt.Module(InlineConfiguration(base, ModuleInfo(base.name), deps).copy(ivyScala = ivyScala))
val report = update(module, c, log)
val newConfig = config.copy(module = mod.copy(modules = report.allModules))
updateClassifiers(ivySbt, newConfig, log)
Expand All @@ -183,7 +183,7 @@ object IvyActions
val baseModules = modules map restrictedCopy
val deps = baseModules.distinct flatMap classifiedArtifacts(classifiers, exclude)
val base = restrictedCopy(id).copy(name = id.name + classifiers.mkString("$","_",""))
val module = new ivySbt.Module(InlineConfiguration(base, deps).copy(ivyScala = ivyScala, configurations = confs))
val module = new ivySbt.Module(InlineConfiguration(base, ModuleInfo(base.name), deps).copy(ivyScala = ivyScala, configurations = confs))
val upConf = new UpdateConfiguration(c.retrieve, true, c.logging)
update(module, upConf, log)
}
Expand Down
14 changes: 5 additions & 9 deletions ivy/IvyConfigurations.scala
Expand Up @@ -62,21 +62,17 @@ final case class PomConfiguration(file: File, ivyScala: Option[IvyScala], valida
{
def noScala = copy(ivyScala = None)
}
final case class InlineConfiguration(module: ModuleID, dependencies: Seq[ModuleID], ivyXML: NodeSeq,
configurations: Seq[Configuration], defaultConfiguration: Option[Configuration], ivyScala: Option[IvyScala],
validate: Boolean) extends ModuleSettings
final case class InlineConfiguration(module: ModuleID, moduleInfo: ModuleInfo, dependencies: Seq[ModuleID], ivyXML: NodeSeq = NodeSeq.Empty, configurations: Seq[Configuration] = Nil, defaultConfiguration: Option[Configuration] = None, ivyScala: Option[IvyScala] = None, validate: Boolean = false) extends ModuleSettings
{
def withConfigurations(configurations: Seq[Configuration]) = copy(configurations = configurations)
def noScala = copy(ivyScala = None)
}
final case class EmptyConfiguration(module: ModuleID, ivyScala: Option[IvyScala], validate: Boolean) extends ModuleSettings
final case class EmptyConfiguration(module: ModuleID, moduleInfo: ModuleInfo, ivyScala: Option[IvyScala], validate: Boolean) extends ModuleSettings
{
def noScala = copy(ivyScala = None)
}
object InlineConfiguration
{
def apply(module: ModuleID, dependencies: Seq[ModuleID]) =
new InlineConfiguration(module, dependencies, NodeSeq.Empty, Nil, None, None, false)
def configurations(explicitConfigurations: Iterable[Configuration], defaultConfiguration: Option[Configuration]) =
if(explicitConfigurations.isEmpty)
{
Expand All @@ -92,7 +88,7 @@ object InlineConfiguration
}
object ModuleSettings
{
def apply(ivyScala: Option[IvyScala], validate: Boolean, module: => ModuleID)(baseDirectory: File, log: Logger) =
def apply(ivyScala: Option[IvyScala], validate: Boolean, module: => ModuleID, moduleInfo: => ModuleInfo)(baseDirectory: File, log: Logger) =
{
log.debug("Autodetecting dependencies.")
val defaultPOMFile = IvySbt.defaultPOM(baseDirectory)
Expand All @@ -106,8 +102,8 @@ object ModuleSettings
else
{
log.warn("No dependency configuration found, using defaults.")
new EmptyConfiguration(module, ivyScala, validate)
new EmptyConfiguration(module, moduleInfo, ivyScala, validate)
}
}
}
}
}
8 changes: 8 additions & 0 deletions ivy/IvyInterface.scala
Expand Up @@ -38,6 +38,14 @@ object ModuleID
for ( (key, value) <- attributes) yield
if(key.startsWith("e:")) (key, value) else ("e:" + key, value)
}
/** Additional information about a project module */
case class ModuleInfo(nameFormal: String, description: String = "", homepage: Option[URL] = None, licenses: Seq[(String, URL)] = Nil, organizationName: String = "", organizationHomepage: Option[URL] = None)
{
def formally(name: String) = copy(nameFormal = name)
def describing(desc: String, home: Option[URL]) = copy(description = desc, homepage = home)
def licensed(lics: (String, URL)*) = copy(licenses = lics)
def organization(name: String, home: Option[URL]) = copy(organizationName = name, organizationHomepage = home)
}
sealed trait Resolver
{
def name: String
Expand Down
15 changes: 12 additions & 3 deletions ivy/MakePom.scala
Expand Up @@ -20,8 +20,8 @@ import plugins.resolver.{ChainResolver, DependencyResolver, IBiblioResolver}
class MakePom
{
def encoding = "UTF-8"
def write(ivy: Ivy, module: ModuleDescriptor, configurations: Option[Iterable[Configuration]], extra: NodeSeq, process: XNode => XNode, filterRepositories: MavenRepository => Boolean, allRepositories: Boolean, output: File): Unit =
write(process(toPom(ivy, module, configurations, extra, filterRepositories, allRepositories)), output)
def write(ivy: Ivy, module: ModuleDescriptor, moduleInfo: ModuleInfo, configurations: Option[Iterable[Configuration]], extra: NodeSeq, process: XNode => XNode, filterRepositories: MavenRepository => Boolean, allRepositories: Boolean, output: File): Unit =
write(process(toPom(ivy, module, moduleInfo, configurations, extra, filterRepositories, allRepositories)), output)
// use \n as newline because toString uses PrettyPrinter, which hard codes line endings to be \n
def write(node: XNode, output: File): Unit = write(toString(node), output, "\n")
def write(xmlString: String, output: File, newline: String)
Expand All @@ -37,10 +37,12 @@ class MakePom
}

def toString(node: XNode): String = new PrettyPrinter(1000, 4).format(node)
def toPom(ivy: Ivy, module: ModuleDescriptor, configurations: Option[Iterable[Configuration]], extra: NodeSeq, filterRepositories: MavenRepository => Boolean, allRepositories: Boolean): XNode =
def toPom(ivy: Ivy, module: ModuleDescriptor, moduleInfo: ModuleInfo, configurations: Option[Iterable[Configuration]], extra: NodeSeq, filterRepositories: MavenRepository => Boolean, allRepositories: Boolean): XNode =
(<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
{ makeModuleID(module) }
<name>{moduleInfo.nameFormal}</name>
{ makeOrganization(moduleInfo) }
{ extra }
{ makeProperties(module) }
{ makeDependencies(module, configurations) }
Expand All @@ -61,6 +63,13 @@ class MakePom
licenses(module.getLicenses)) : NodeSeq )
a ++ b
}
def makeOrganization(moduleInfo: ModuleInfo): NodeSeq =
{
<organization>
<name>{moduleInfo.organizationName}</name>
{ moduleInfo.organizationHomepage map { h => <url>{h}</url> } getOrElse NodeSeq.Empty }
</organization>
}
def makeProperties(module: ModuleDescriptor): NodeSeq =
{
val extra = IvySbt.getExtraAttributes(module)
Expand Down
14 changes: 10 additions & 4 deletions main/Defaults.scala
Expand Up @@ -595,7 +595,13 @@ object Classpaths
conflictWarning <<= (thisProjectRef, conflictWarning) { (ref, cw) => cw.copy(label = Project.display(ref)) },
unmanagedBase <<= baseDirectory / "lib",
normalizedName <<= name(StringUtilities.normalize),
description <<= description or name.identity,
homepage in GlobalScope :== None,
licenses in GlobalScope :== Nil,
organization <<= organization or normalizedName.identity,
organizationName in GlobalScope <<= organizationName or organization.identity,
organizationHomepage in GlobalScope <<= organizationHomepage or homepage.identity,
projectInfo <<= (name, description, homepage, licenses, organizationName, organizationHomepage) apply ModuleInfo,
classpathFilter in GlobalScope :== "*.jar",
externalResolvers <<= (externalResolvers.task.? zipWith resolvers.identity) {
case (Some(delegated), Seq()) => delegated
Expand Down Expand Up @@ -645,8 +651,8 @@ object Classpaths
},
ivyConfigurations ++= Configurations.auxiliary,
moduleSettings <<= moduleSettings0,
makePomConfiguration <<= (artifactPath in makePom, pomExtra, pomPostProcess, pomIncludeRepository, pomAllRepositories) {
(file, extra, process, include, all) => new MakePomConfiguration(file, None, extra, process, include, all)
makePomConfiguration <<= (artifactPath in makePom, projectInfo, pomExtra, pomPostProcess, pomIncludeRepository, pomAllRepositories) {
(file, minfo, extra, process, include, all) => new MakePomConfiguration(file, minfo, None, extra, process, include, all)
},
deliverLocalConfiguration <<= (crossTarget, ivyLoggingLevel) map { (outDir, level) => deliverConfig( outDir, logging = level ) },
deliverConfiguration <<= deliverLocalConfiguration.identity,
Expand Down Expand Up @@ -686,8 +692,8 @@ object Classpaths
new IvySbt(conf)
}
def moduleSettings0: Initialize[Task[ModuleSettings]] =
(projectID, allDependencies, ivyXML, ivyConfigurations, defaultConfiguration, ivyScala, ivyValidate) map {
(pid, deps, ivyXML, confs, defaultConf, ivyS, validate) => new InlineConfiguration(pid, deps, ivyXML, confs, defaultConf, ivyS, validate)
(projectID, allDependencies, ivyXML, ivyConfigurations, defaultConfiguration, ivyScala, ivyValidate, projectInfo) map {
(pid, deps, ivyXML, confs, defaultConf, ivyS, validate, pinfo) => new InlineConfiguration(pid, pinfo, deps, ivyXML, confs, defaultConf, ivyS, validate)
}

def sbtClassifiersTasks = inTask(updateSbtClassifiers)(Seq(
Expand Down
11 changes: 9 additions & 2 deletions main/Keys.scala
Expand Up @@ -4,6 +4,7 @@
package sbt

import java.io.File
import java.net.URL
import Project.ScopedKey
import complete._
import inc.Analysis
Expand Down Expand Up @@ -171,9 +172,15 @@ object Keys
// Classpath/Dependency Management Keys
type Classpath = Seq[Attributed[File]]

val name = SettingKey[String]("name", "Name.")
val normalizedName = SettingKey[String]("normalized-name", "Name transformed from mixed case and spaces to lowercase and dash-separated.")
val name = SettingKey[String]("name", "Project name.")
val normalizedName = SettingKey[String]("normalized-name", "Project name transformed from mixed case and spaces to lowercase and dash-separated.")
val description = SettingKey[String]("description", "Project description.")
val homepage = SettingKey[Option[URL]]("homepage", "Project homepage.")
val licenses = SettingKey[Seq[(String, URL)]]("licenses", "Project licenses as (name, url) pairs.")
val organization = SettingKey[String]("organization", "Organization/group ID.")
val organizationName = SettingKey[String]("organization-name", "Organization full/formal name.")
val organizationHomepage = SettingKey[Option[URL]]("organization-homepage", "Organization homepage.")
val projectInfo = SettingKey[ModuleInfo]("project-info", "Addition project information like formal name, homepage, licenses etc.")
val defaultConfiguration = SettingKey[Option[Configuration]]("default-configuration", "Defines the configuration used when none is specified for a dependency.")
val defaultConfigurationMapping = SettingKey[String]("default-configuration-mapping", "Defines the mapping used for a simple, unmapped configuration definition.")

Expand Down

0 comments on commit 67102aa

Please sign in to comment.