Skip to content

Commit

Permalink
Added type coercions to the Ant code.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetzner committed Nov 30, 2013
1 parent 6332493 commit 98644cc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 35 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/bovinegenius/forge/Build.java
Expand Up @@ -6,7 +6,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugin.logging.Log;
import org.bovinegenius.forge.Forge;
import org.bovinegenius.forge.Forge$;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
Expand All @@ -27,12 +27,12 @@ public class Build extends AbstractMojo {
private String artifactCoords;

public void execute() throws MojoExecutionException {
Log log = getLog();
log.info("Building...");
log.info(String.format("context: %s", getPluginContext()));
log.info(String.format("artifactCode: %s", artifactCoords));
log.info(String.format("repoSystem: %s", repoSystem));
Forge.apply().build(repoSystem);
// Log log = getLog();
// log.info("Building...");
// log.info(String.format("context: %s", getPluginContext()));
// log.info(String.format("artifactCode: %s", artifactCoords));
// log.info(String.format("repoSystem: %s", repoSystem));
// Forge$.apply().build(repoSystem);
}
}

47 changes: 46 additions & 1 deletion src/main/scala/org/bovinegenius/forge/Ant.scala
@@ -1,5 +1,6 @@
package org.bovinegenius.forge

import java.io.File
import java.net.URLClassLoader
import org.apache.tools.ant.Project
import org.apache.tools.ant.NoBannerLogger
Expand All @@ -9,6 +10,48 @@ import java.lang.reflect.Method
import java.beans._
import scala.collection.JavaConverters._

sealed trait AntException
case class CoercionException(val message: String)
extends Exception(message) with AntException

private object Coercions {
def coerceString[T](value: String, cls: Class[T]): Any = {
val FileClass = classOf[java.io.File]
val StringClass = classOf[java.lang.String]
val BooleanClass = classOf[java.lang.Boolean]
val CharClass = classOf[java.lang.Character]
cls match {
case FileClass => new File(value)
case StringClass => value
case BooleanClass => (value == "true" || value == "yes" || value == "on")
case CharClass => value(0)
case _ => throw CoercionException("Unknown String coercion to type: %s".format(cls.getName))
}
}

def coerceFile[T](value: File, cls: Class[T]): Any = {
val FileClass = classOf[java.io.File]
val StringClass = classOf[java.lang.String]
val BooleanClass = classOf[java.lang.Boolean]
val CharClass = classOf[java.lang.Character]
cls match {
case FileClass => value
case StringClass => value.toString
case BooleanClass => false
case CharClass => value.toString()(0)
case _ => throw CoercionException("Unknown File coercion to type: %s".format(cls.getName))
}
}

def coerce[T](value: Any, cls: Class[T]): Any = {
value match {
case v: String => coerceString(v, cls)
case f: File => coerceFile(f, cls)
case _ => value
}
}
}

class Tasks(ant: Ant) extends Dynamic {
def applyDynamicNamed(name: String)(args: (String, Any)*) = {
val task = ant.task(name)
Expand Down Expand Up @@ -41,7 +84,9 @@ class Task(task: AntTask) {
def setProperties(props: Seq[(String,Any)]) {
val descs = descriptors
props.foreach { case (name, value) =>
descs(name).invoke(task, value.asInstanceOf[Object])
val desc = descs(name)
val input = Coercions.coerce(value, desc.getParameterTypes()(0))
desc.invoke(task, input.asInstanceOf[Object])
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/org/bovinegenius/forge/Fetcher.scala
Expand Up @@ -34,11 +34,11 @@ import org.eclipse.aether.util.artifact.JavaScopes
import org.eclipse.aether.util.filter.DependencyFilterUtils
import org.eclipse.aether.collection.CollectRequest

case class Jar(val groupId: String, val artifactId: String, val version: String) {
case class Jar(val group: String, val artifact: String, val version: String) {
private var classloader: ClassLoader = null

def install() = {
Fetcher.fetch(groupId, artifactId, version)
Fetcher.fetch(group, artifact, version)
}

def load() = {
Expand Down
25 changes: 10 additions & 15 deletions src/main/scala/org/bovinegenius/forge/forge.scala
@@ -1,23 +1,18 @@
package org.bovinegenius.forge

import org.bovinegenius.forge.impl.ForgeFactory
import org.eclipse.aether.RepositorySystem

trait Forge {
def build(repoSystem: RepositorySystem = null)
}

object Forge {
def apply(): org.bovinegenius.forge.impl.Forge = {
// val parent = getClass.getClassLoader
// val classLoader = new DynamicClassLoader(parent)
// println("classloader: %s".format(classLoader))
// val forgeClass = classLoader.loadClass("org.bovinegenius.forge.impl.ForgeFactory")
// val forgeObj = forgeClass.newInstance() //.asInstanceOf[ForgeFactory]
// forgeClass.getMethod("defaultForge").invoke(forgeObj).asInstanceOf[org.bovinegenius.forge.impl.Forge]
// Jar("org.apache.ant", "ant", "1.9.2").load()
new org.bovinegenius.forge.impl.ForgeFactory().defaultForge
// forgeObj.defaultForge
}
def apply(): Forge = ForgeFactory.defaultForge
}

def main(args: Array[String]) {
val forge = Forge()
forge.build(null)
}
object Main {
def main(args: Array[String]): Unit = Forge().build()
}


19 changes: 9 additions & 10 deletions src/main/scala/org/bovinegenius/forge/impl/Forge.scala
@@ -1,21 +1,20 @@
package org.bovinegenius.forge.impl

import org.bovinegenius.forge.Ant
import org.bovinegenius.forge.Forge
import org.bovinegenius.forge.Jar
import org.eclipse.aether.RepositorySystem

trait Forge {
def build(repoSystem: RepositorySystem)
}

private class DefaultForge extends Forge {
override def build(repoSystem: RepositorySystem) {
val rhino = Jar("org.mozilla", "rhino", "1.7R4").load()
override def build(repoSystem: RepositorySystem): Unit = {
val rhino = Jar (
group = "org.mozilla",
artifact = "rhino",
version = "1.7R4"
).load()
val proguard = Jar("net.sf.proguard", "proguard-anttask", "4.10").load()
// println(rhino.loadClass("org.mozilla.javascript.Context"))
// println(Ant.taskNames)
Ant.tasks.echo(message = "Some message")
val dir1 = new java.io.File("/Users/walter/some-test-dir")
val dir1 = "/Users/walter/some-test-dir"
val dir2 = new java.io.File("/Users/walter/some-test-dir2")
Ant.tasks.mkdir(dir = dir1)
Ant.tasks.mkdir(dir = dir2)
Expand All @@ -25,7 +24,7 @@ private class DefaultForge extends Forge {
}
}

class ForgeFactory {
object ForgeFactory {
def defaultForge: Forge = new DefaultForge()
}

4 changes: 4 additions & 0 deletions src/main/scala/org/bovinegenius/forge/prolog.scala
@@ -0,0 +1,4 @@
package org.bovinegenius.forge



0 comments on commit 98644cc

Please sign in to comment.