ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Simon <jonathan_s_si...@yahoo.com>
Subject Ant scripting with Jython (Phase 2)
Date Fri, 20 Jun 2003 14:48:53 GMT
All-

First off, Special thanks to Peter Reilly for his last post. Im still
relatively new to Jython (and Ant dev for that matter!) so his last code
snippet got me thinking in the right direction about the extreme reflection in
Jython!

Allright. So I took Peter's code and tried to mimic the Ant Main() as much as
possible. This consisted of creating and configuring a project and logging.
Additionally, I changed the the reflection to use the Ant introspector to
create and configure the tasks. This means that instead of having a line like
this (Notice the destFile is case sensitive and the actual java.io.File object
being instantiated)

ant.jar(destFile=File("D:\\java\\jython_ant\\temp\\test.jar"),
basedir=File("D:\\java\\jython_ant\\src\\"))

you can write this (notice that destfile is NOT case sensitive due to the use
of the introspector. Also, notice that you can pass in a simple string instead
of a java.io.File instance)

ant.jar(destfile="D:\\java\\jython_ant\\temp\\test.jar",
BASEDIR="D:\\java\\jython_ant\\src\\")

So here is the Jython code (all of it) to get this all working: 

----------------------------------------------------------------------------

from java.io import File
from org.apache.tools.ant import *
from org.apache.tools.ant.util import *
from java.lang import *
from java.util import Locale
from org.apache.tools.ant.types import *

class _Method:
  def __init__(self, name, project):
    self.name = name
    self.project = project
  def __call__(self, **args):
    target = self.project.createTask(self.name)
    ih = IntrospectionHelper.getHelper(target.class)
    ph = ProjectHelper()
    for a in args.keys():
        value = ph.replaceProperties(self.project,
                        args[a],
                        self.project.properties)
        ih.setAttribute(self.project,
                        target,
                        a.lower(),
                        value)
    target.execute()

class Ant:
  myProject = Project()
  myProject.init()
  myProject.setUserProperty("ant.version", Main.getAntVersion());
  logger = DefaultLogger()
  logger.outputPrintStream = System.out
  logger.errorPrintStream = System.out
  logger.messageOutputLevel = 3
  myProject.addBuildListener(logger)
  def __getattr__(self, name):
    return _Method(name, self.myProject)
  def setLogLevel(logLevel):
    self.logger.messageOutputLevel = logLevel

---------------------------------------------------------------------------

Here are the outstading issues that I would like some feedback on. 

1) Sub-elements. I know that we need them. Just not sure how to do it. I am
having more trouble figuring out the introspector works with the subelements
than with the attributes ;-( If the introspectionworks, then you can just pass
a fileset instance in along with the otehr attributes. 

2) Properties. Properties are still good to have in XML. I want to take a look
at the best way to be able to reference XML property file from the Jython
scripts. 

So there you go. Any suggestions or comments? Give a shout!

Thanks.
-jonathan

  









=====
Jonathan Simon
Home - 732.777.0377  <----------- New!!!
Work - 646.674.2060
Cell - 732.718.8844
Music - www.mp3.com/jonathan_simon

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message