The code I currently have is only "proof of concept" quality... meaning that in my view it is not ready to be commited. Here are some code snippets of the main parts. The parts that are left out are my own implementations of the apache ProjectHelperImpl xml parsing classes. (My custom ones are mostly the same but work on a stream rather than a file) If you want to see more let me know. (Also note package names are just placeholders) For this you should be able to see the idea. The Ruby Server is still under heavy work. Currently it is just a basic example to prove that the plumbing works.
<begin AntWrapper.java>
package org.codehaus.antserver;
import org.apache.tools.ant.Project;
import java.io.IOException;
import java.io.InputStream;
public class AntWrapper {
private Project project;
private InputStream inputStream;
private boolean running = false;
private Thread serverThread;
public AntWrapper(String projectName, String baseDirectory, InputStream inStream)
{
project = new Project();
project.setBasedir(baseDirectory);
project.setName(projectName);
project.init();
inputStream = inStream;
}
public boolean isRunning()
{
return running;
}
public void stop() {
running = false;
try
{
inputStream.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void start() {
running = true;
if (inputStream != null)
{
StreamProjectHelper projectHelper = new StreamProjectHelper(project, inputStream);
serverThread = new Thread(projectHelper);
serverThread.setName("AntServerThread");
serverThread.setDaemon(true);
serverThread.setPriority(Thread.MAX_PRIORITY);
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
serverThread.start();
}
}
}
<end AntWrapper.java>
<begin AntWrapperTest.java>
package org.codehaus.antserver;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.tools.ant.filters.StringInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class AntWrapperTest extends TestCase {
private String projectName = "TestProject";
private String baseDirectory = "./testproject";
protected void setUp() throws Exception {
super.setUp();
File testFile = new File("./testProject/testbuild/HelloWorld.class");
if (testFile.exists())
{
testFile.delete();
}
}
public AntWrapperTest() {
}
public AntWrapperTest(String name)
{
super(name);
}
public void testStartAntServer()
{
AntWrapper antServer = new AntWrapper(projectName, baseDirectory,null);
antServer.start();
assertTrue(antServer.isRunning());
antServer.stop();
}
public void testAntServerCompile() throws Exception
{
String xmlFragement = "<project><target name=\"compile\"> <javac srcdir=\"./test\" destdir=\"./testbuild\"/> </target></project>";
InputStream xmlInputStream = new StringInputStream(xmlFragement);
AntWrapper antServer = new AntWrapper(projectName, baseDirectory,xmlInputStream);
antServer.start();
assertExists("./testProject/testbuild/HelloWorld.class");
antServer.stop();
}
public void testSeveralTargets() throws Exception
{
PipedOutputStream outputStream = new PipedOutputStream();
InputStream xmlInputStream = new PipedInputStream(outputStream);
OutputStreamWriter out = new OutputStreamWriter(outputStream);
AntWrapper antServer = new AntWrapper(projectName,baseDirectory,xmlInputStream);
antServer.start();
out.write("<project>");
out.write("<target name=\"makeDir\"> <mkdir dir=\"testDir\"/> </target>");
out.flush();
//Thread.currentThread().sleep(30);
assertExists("./testProject/testDir");
out.write("<target name=\"deleteDir\"> <delete dir=\"testDir\"/> </target>");
out.flush();
//Thread.currentThread().sleep(30);
assertNotExists("./testProject/testDir");
out.write("</project>");
out.close();
antServer.stop();
}
private void assertExists(String location) throws AssertionFailedError {
if (location == null || location.trim().length() == 0)
{
throw new AssertionFailedError("The location is null");
}
File locationFile = new File(location);
if (!locationFile.exists()) {
throw new AssertionFailedError("The location " + locationFile.getAbsolutePath() + " does not exist");
}
}
private void assertNotExists(String location) throws AssertionFailedError {
if (location == null || location.trim().length() == 0) { throw new AssertionFailedError("The location is null"); }
File locationFile = new File(location);
if (locationFile.exists())
{
throw new AssertionFailedError("The location " + locationFile.getAbsolutePath() + " does exist");
}
}
}
<end AntWrapperTest.java>
<begin AntServer.java>
package org.codehaus.antserver;
import java.io.InputStream;
public class AntServer {
public static void main(String[] args) {
String projectName = null;
String baseDir = null;
if (args != null && args.length >= 2) {
projectName = args[0];
baseDir = args[1];
}
if (projectName == null || baseDir == null) {
System.out.println("You need to pass the projectName and the baseDir as arguments");
System.out.println("For example AntServer testProject .");
System.exit(1);
}
InputStream in = System.in;
AntWrapper antWrapper = new AntWrapper(projectName,baseDir,in);
antWrapper.start();
while (antWrapper.isRunning()) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException ie) {
ie.printStackTrace();
System.exit(1);
}
}
}
}
<end AntServer.java>
<start AntServer.java (warning hard coded paths)>
package org.codehaus.antserver;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class AntServerTest extends TestCase {
public void setUp() throws Exception {
super.setUp();
File testDir = new File("./testProject/testDir");
File testDir2 = new File ("./testProject/testDir2");
if (testDir.exists()) {
testDir.delete();
}
if (testDir2.exists()) {
testDir2.delete();
}
}
public void testServer() throws Exception{
String[] args = {"testProject","."};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java -cp C:\\.ideabuild\\antserver;C:\\IdeaProjects\\antserver\\lib\\ant.jar;C:\\IdeaProjects\\antserver\\lib\\xercesImpl.jar;C:\\IdeaProjects\\antserver\\lib
xml-apis.jar org.codehaus.antserver.AntServer testProject C:\\IdeaProjects\\AntServer
testProject");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());
InputStreamReader in = new InputStreamReader(process.getInputStream());
InputStreamReader err = new InputStreamReader(process.getErrorStream());
out.write("<project>");
out.write("<target name=\"makeDir\"> <mkdir dir=\"testDir\"/> </target>");
out.flush();
out.write("<target name=\"makeDir2\"> <mkdir dir=\"testDir2\"/> </target>");
out.flush();
out.write("</project>");
out.flush();
System.out.println("This is std out for the process");
while (in.ready()) {
System.out.write(in.read());
}
System.out.println("This is std error for the process");
while (err.ready()) {
System.out.write(err.read());
}
Thread.currentThread().sleep(10);
assertExists("/IdeaProjects/AntServer/testProject/testDir");
assertExists("/IdeaProjects/AntServer/testProject/testDir2");
process.destroy();
}
private void assertExists(String location) throws AssertionFailedError {
if (location == null || location.trim().length() == 0) {
throw new AssertionFailedError("The location is null");
}
File locationFile = new File(location);
if (!locationFile.exists()) {
throw new AssertionFailedError("The location " + locationFile.getAbsolutePath() + " does not exist");
}
}
private void assertNotExists(String location) throws AssertionFailedError {
if (location == null || location.trim().length() == 0) { throw new AssertionFailedError("The location is null"); }
File locationFile = new File(location);
if (locationFile.exists()) { throw new AssertionFailedError("The location " + locationFile.getAbsolutePath() + " does exist"); }
}
}
<end AntServerTest.java>
<begin AntServer.rb>
antServer = IO.popen("java -jar antserver.jar testProject testProject","w+");
antServer.sync = true
antServer.puts("<project>")
antServer.puts('<target name="compile"> <javac srcdir="./test" destdir="./testbuild"/> </target>')
antServer.puts("</project>")
antServer.close()
<end AntServer.rb>
Just out of curiosity: What Codehaus project are you referring to? Damagecontrol? Something else?
What are the features you have been working on? On IRC you mentioned something about a ruby-ant bridge. Any chance you could upload your code to DC's JIRA (if it is DC at all).
Aslak