/* __ ______________ *\ ** / |/ / ____/ ____/ ** ** / | | /___ / /___ ** ** /_/|__/_____/_____/ Copyright 2005-2006 LAMP/EPFL ** ** ** $Id$ \* */ package scala.tools.ant { import scala.collection.Map import scala.collection.mutable.HashMap import java.io.{File, FileInputStream, FileOutputStream, FileWriter, StringReader} import java.net.{URL, URLClassLoader} import java.util.{ArrayList, Vector} import java.util.zip.{ZipOutputStream, ZipEntry} import org.apache.tools.ant.{AntClassLoader, BuildException, DirectoryScanner, Project} import org.apache.tools.ant.Task import org.apache.tools.ant.types.Path import org.apache.tools.ant.util.{FileUtils, MergingMapper, SourceFileScanner} import org.apache.tools.ant.types.{EnumeratedAttribute, Reference, FileSet} /** An Ant task that generates a Scala Bazaars package (sbp file) along * with an advertisement of that package. * * This task can take the following parameters as attributes:
file
. */
def setFile(input: File) =
file = Some(input)
/** Sets the advertisement file attribute. Used by Ant.
* @param input The value of adfile
. */
def setAdfile(input: File) =
adfile = Some(input)
/** Sets the name attribute of this package. Used by Ant.
* @param input The value of name
. */
def setName(input: String) =
name = Some(input)
/** Sets the version attribute of this package. Used by Ant.
* @param input The value of version
. */
def setVersion(input: String) =
version = Some(input)
/** Sets the depends attribute. Used by Ant.
* @param input The value for depends
. */
def setDepends(input: String) = {
depends = List.fromArray(input.split(",")).flatMap(s: String => {
val st = s.trim()
(if (st != "") List(st) else Nil)
})
}
/** Sets the description attribute of this package. Used by Ant.
* @param input The value of description
. */
def setDesc(input: String) =
desc = Some(input)
/** Sets the link attribute of this package. Used by Ant.
* @param input The value of link
. */
def setLink(input: String) =
link = Some(input)
def addConfiguredLibset(input: FileSet) =
fileSetsMap.update("lib", input)
def addConfiguredBinset(input: FileSet) =
fileSetsMap.update("bin", input)
def addConfiguredSrcset(input: FileSet) =
fileSetsMap.update("src", input)
def addConfiguredManset(input: FileSet) =
fileSetsMap.update("man", input)
def addConfiguredDocset(input: FileSet) =
fileSetsMap.update("doc/" + getName, input)
def addConfiguredMiscset(input: FileSet) =
fileSetsMap.update("misc/" + getName, input)
/******************************************************************************\
** Properties getters **
\******************************************************************************/
/** Gets the value of the file attribute in a Scala-friendly form.
* @returns The file as a file. */
private def getName: String =
if (name.isEmpty) error("Name attribute must be defined first.")
else name.get
/** Gets the value of the file attribute in a Scala-friendly form.
* @returns The file as a file. */
private def getFile: File =
if (file.isEmpty) error("Member 'file' is empty.")
else getProject().resolveFile(file.get.toString())
/** Gets the value of the adfile attribute in a Scala-friendly form.
* @returns The adfile as a file. */
private def getAdfile: File =
if (adfile.isEmpty) error("Member 'adfile' is empty.")
else getProject().resolveFile(adfile.get.toString())
/******************************************************************************\
** Compilation and support methods **
\******************************************************************************/
/** Transforms a string name into a file relative to the provided base
* directory.
* @param base A file pointing to the location relative to which the name
* will be resolved.
* @param name A relative or absolute path to the file as a string.
* @return A file created from the name and the base file. */
private def nameToFile(base: File)(name: String): File =
existing(fileUtils.resolveFile(base, name))
/** Transforms a string name into a file relative to the build root
* directory.
* @param name A relative or absolute path to the file as a string.
* @return A file created from the name. */
private def nameToFile(name: String): File =
existing(getProject().resolveFile(name))
/** Tests if a file exists and prints a warning in case it doesn't. Always
* returns the file, even if it doesn't exist.
* @param file A file to test for existance.
* @return The same file. */
private def existing(file: File): File = {
if (!file.exists())
log("Element '" + file.toString() + "' does not exist.",
Project.MSG_WARN)
file
}
/** Generates a build error. Error location will be the current task in the
* ant file.
* @param message A message describing the error.
* @throws BuildException A build error exception thrown in every case. */
private def error(message: String): Nothing =
throw new BuildException(message, getLocation())
private def writeFile(file: File, content: String) =
if (file.exists() && !file.canWrite())
error("File " + file + " is not writable")
else {
val writer = new FileWriter(file, false)
writer.write(content)
writer.close()
}
/******************************************************************************\
** The big execute method **
\******************************************************************************/
/** Performs the compilation. */
override def execute() = {
// Tests if all mandatory attributes are set and valid.
if (file.isEmpty) error("Attribute 'file' is not set.")
if (name.isEmpty) error("Attribute 'name' is not set.")
if (version.isEmpty) error("Attribute 'version' is not set.")
val pack = {