Fun with Ant and Pulpcore

The company I currently work for uses Maven as their build tool, and it's been quite a while since I've actually used Ant.

Which is sad! Ant is an awesome tool.

I'll be honest, I haven't really delved into Maven enough to say I have the knowledge of it as I do Ant so I don't know if everything that is available in Ant, is available with Maven.

However, I was recently pointed to a great little project started by David Brackeen called PulpCore.

I joined the mailing list and came across a post with a tutorial for setting up a "Quick" PulpCore project of your own.

David has done something really cool with his binary downloads for PulpCore. When you download a binary for PulpCore and unzip it, you get the PulpCore framework, and a couple of quick setup templates which you can use to create your own projects.

Having an itch to play around with Ant again, I thought I'd try to see if I could create an Ant script to essentially do what the tutorial did.

I had a ton of fun, and I think I'll be bringing this up with my superiors at my current company to see if we can use Ant in other areas (since they're pretty stuck on Maven).

Anyway, my script only handles creating the project, you'll need to refer to the tutorial if you want to go further.

Here is the script

<?xml version="1.0"?>
<project name="PulpCoreTemplates" basedir="." default="usage">
   <property file="build.properties" />
 
   <!-- PulpCore properties -->
   <property name="pulpcore.home.dir" value=".." />
   <property name="pulpcore.jars.dir" value="${pulpcore.home.dir}/build" />
   <property name="pulpcore.templates.dir" value="${pulpcore.home.dir}/templates" />
   <property name="pulpcore.templates.quick.dir" value="${pulpcore.templates.dir}/quick" />
   <property name="pulpcore.templates.project.dir" value="${pulpcore.templates.dir}/project" />
   <!-- this is a hack to get the actual path to pulpcore home and pulpcore jars directories -->
   <!-- These temp files aren't actually created -->
   <tempfile property="pulpcore.home.dir.tempfile" destDir="${pulpcore.home.dir}" />
   <tempfile property="pulpcore.jars.dir.tempfile" destDir="${pulpcore.jars.dir}" />
 
   <dirname property="pulpcore.home.path" file="${pulpcore.home.dir.tempfile}" />
   <dirname property="pulpcore.jars.path" file="${pulpcore.jars.dir.tempfile}" />
   <dirname property="pulpcore.templates.quick.path" file="${pulpcore.templates.quick.dir}/build.xml" />
   <dirname property="pulpcore.templates.project.path" file="${pulpcore.templates.project.dir}/build.xml" />
 
   <target name="pulpcore-directories">
      <echo>   pulpcore.home.path: ${pulpcore.home.path}</echo>
      <echo>   pulpcore.jars.path: ${pulpcore.jars.path}</echo>
   </target>
 
   <target name="usage" depends="pulpcore-directories">
      <echo>   quick            - Sets up a new project using the "quick" template.</echo>
      <echo>   project          - Sets up a new project using the "project" template.</echo>
   </target>
 
   <!-- Quick Project Target -->
   <target name="quick" depends="pulpcore-directories">
      <!-- Ask user for Project Name. -->
      <input message="Project Name" addproperty="project.name" />
      <!-- and their project directory - default will use ../Project Name -->
      <input message="Project Directory (../${project.name})" addproperty="project.dir" defaultvalue="../${project.name}"/>
      <!-- Ask user for project scene - default will be Project Name -->
      <input message="Project Scene (${project.name})" addproperty="project.scene" defaultvalue="${project.name}"/>
      <!-- Create the project directory -->
      <mkdir dir="${project.dir}" />
      <!-- Copy the quick template build.xml into the project directory. -->
      <cop  y todir="${project.dir}">
      <fileset dir="${pulpcore.templates.quick.dir}" />
      </copy>
      <!-- Create a simple build.xml that just uses the quick template build.xml -->
      <echo file="${project.dir}/build.xml"><![CDATA[<?xml version="1.0"?>
<project name="${project.name}" default="build-and-run" basedir=".">
<!--
This is a generated file which simply points to the PulpCore
quick template build file.
 
If you want to see the entire file look here:
${pulpcore.templates.quick.path}/build.xml
 
To over write this file with that one, copy it to this directory
and replace the properties found here:
http://code.google.com/p/pulpcore/wiki/GettingStarted
-->
 
<property name="project.scene" value="${project.scene}" />
<property name="pulpcore.path" value="${pulpcore.jars.path}" />
<import file="${pulpcore.templates.quick.path}/build.xml" />
</project>]]>
      </echo>
      <!-- Do a manual clean of new project directory -->
      <echo>Clean new project directory</echo>
      <delete dir="${project.dir}/build" />
      <delete>
         <fileset dir="${project.dir}/src" excludes="background.png" />
      </delete>
      <!-- Create simple java file for user to start with -->
      <echo file="${project.dir}/src/${project.scene}.java">
/*
* This is an ugly generated file, edit it to make it your own and
* make it beautiful.
*/
import pulpcore.scene.Scene2D;
import pulpcore.sprite.ImageSprite;
 
public class ${project.scene} extends Scene2D {
   public void load() {
      add(new ImageSprite("background.png", 0, 0));
   }
}
      </echo>
      <!-- Provide user with some final instructions on how to build and run their new project -->
      <dirname property="project.path" file="${project.dir}/build.xml" />
      <echo>To build and run your new project:</echo>
      <echo>   1: CD (Change Directory) into your project directory (${project.path}).</echo>
      <echo>   2: Run the build command "ant build".</echo>
      <echo>   3: Run the run command "ant run".</echo>
   </target>
</project>

Download the current PulpCore binary, unzip it somewhere, and then copy and paste the above script into the templates directory (as build.xml). Afterwards, open up a console (command) and navigate into the templates directory. Type "ant quick" and then follow the on-screen instructions.

Currently, there is only a target to create a "Quick" project, but I'll repost when I have some more time to put together a target for the "Project" project.

I'm a huge jEdit fan, so I have to give props to my favorite text editor! Which I used to create this Ant Script.

Cheers, and have fun!