bodewig 2003/11/27 06:11:05
Modified: proposal/sandbox/dotnet/src/etc/testcases msbuild.xml
nant.xml
proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet
AbstractBuildTask.java MSBuildTask.java
NAntTask.java
proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet
MSBuildTaskTest.java NAntTaskTest.java
Log:
Add support for nesting buuild file snippets into the task
Revision Changes Path
1.3 +27 -0 ant/proposal/sandbox/dotnet/src/etc/testcases/msbuild.xml
Index: msbuild.xml
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/etc/testcases/msbuild.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- msbuild.xml 25 Nov 2003 11:57:14 -0000 1.2
+++ msbuild.xml 27 Nov 2003 14:11:05 -0000 1.3
@@ -27,4 +27,31 @@
<property name="foo" value="bar"/>
</msbuild>
</target>
+
+ <target name="nested-file">
+ <property name="foo" value="bar"/>
+ <msbuild
+ xmlns="antlib:org.apache.tools.ant.taskdefs.optional.dotnet"
+ >
+ <build>
+ <Project DefaultTargets="echo">
+ <Target Name="echo">
+ <Task Name="Echo" Message="foo is ${foo}"/>
+ </Target>
+ </Project>
+ </build>
+ </msbuild>
+ </target>
+
+ <target name="nested-task">
+ <property name="foo" value="bar"/>
+ <msbuild
+ xmlns="antlib:org.apache.tools.ant.taskdefs.optional.dotnet"
+ >
+ <build>
+ <Task Name="Echo" Message="foo is ${foo}"/>
+ </build>
+ </msbuild>
+ </target>
+
</project>
1.3 +26 -0 ant/proposal/sandbox/dotnet/src/etc/testcases/nant.xml
Index: nant.xml
===================================================================
RCS file: /home/cvs/ant/proposal/sandbox/dotnet/src/etc/testcases/nant.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- nant.xml 25 Nov 2003 11:57:14 -0000 1.2
+++ nant.xml 27 Nov 2003 14:11:05 -0000 1.3
@@ -27,4 +27,30 @@
<property name="foo" value="bar"/>
</nant>
</target>
+
+ <target name="nested-file">
+ <property name="foo" value="bar"/>
+ <nant
+ xmlns="antlib:org.apache.tools.ant.taskdefs.optional.dotnet"
+ >
+ <build>
+ <project basedir="." default="echo">
+ <target name="echo">
+ <echo message="foo is ${foo}"/>
+ </target>
+ </project>
+ </build>
+ </nant>
+ </target>
+
+ <target name="nested-task">
+ <property name="foo" value="bar"/>
+ <nant
+ xmlns="antlib:org.apache.tools.ant.taskdefs.optional.dotnet"
+ >
+ <build>
+ <echo message="foo is ${foo}"/>
+ </build>
+ </nant>
+ </target>
</project>
1.2 +81 -2
ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/AbstractBuildTask.java
Index: AbstractBuildTask.java
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/AbstractBuildTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractBuildTask.java 21 Nov 2003 13:53:57 -0000 1.1
+++ AbstractBuildTask.java 27 Nov 2003 14:11:05 -0000 1.2
@@ -54,9 +54,18 @@
package org.apache.tools.ant.taskdefs.optional.dotnet;
+import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.util.DOMElementWriter;
+import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.XMLFragment;
+
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Element;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -81,6 +90,11 @@
private List properties = new ArrayList(1);
/**
+ * Nested build file fragment.
+ */
+ private XMLFragment buildSnippet;
+
+ /**
* Empty constructor.
*/
protected AbstractBuildTask() {
@@ -94,6 +108,18 @@
}
/**
+ * Adds a build file fragment.
+ */
+ public void addBuild(XMLFragment f) {
+ if (buildSnippet == null) {
+ buildSnippet = f;
+ } else {
+ throw new BuildException("You must not specify more than one "
+ + "build element");
+ }
+ }
+
+ /**
* A target.
*/
public static class Target {
@@ -181,9 +207,24 @@
protected abstract String[] getPropertyArguments(List properties);
/**
+ * Turn the DoucmentFragment into a DOM tree suitable as a build
+ * file when serialized.
+ *
+ * <p>Must throw a BuildException if the snippet can not be turned
+ * into a build file.</p>
+ */
+ protected abstract Element makeTree(DocumentFragment f);
+
+ /**
* Perform the build.
*/
public void execute() {
+ if (buildFile != null && buildSnippet != null) {
+ throw new BuildException("You must not specify the build file"
+ + " attribute and a nested build at the"
+ + " same time");
+ }
+
DotNetExecTask exec = new DotNetExecTask();
exec.setProject(getProject());
exec.setExecutable(getExecutable());
@@ -196,10 +237,48 @@
for (int i = 0; i < args.length; i++) {
exec.createArg().setValue(args[i]);
}
- args = getBuildfileArguments(buildFile);
+
+ File generatedFile = null;
+ if (buildSnippet != null) {
+ try {
+ generatedFile = getBuildFile();
+ } catch (IOException e) {
+ throw new BuildException(e);
+ }
+ args = getBuildfileArguments(generatedFile);
+ } else {
+ args = getBuildfileArguments(buildFile);
+ }
+
for (int i = 0; i < args.length; i++) {
exec.createArg().setValue(args[i]);
}
- exec.execute();
+
+ try {
+ exec.execute();
+ } finally {
+ if (generatedFile != null) {
+ generatedFile.delete();
+ }
+ }
+ }
+
+ private File getBuildFile() throws IOException {
+ File f = null;
+ if (buildSnippet != null) {
+ Element e = makeTree(buildSnippet.getFragment());
+ f = FileUtils.newFileUtils().createTempFile("build", ".xml",
null);
+ f.deleteOnExit();
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(f);
+ (new DOMElementWriter()).write(e, out);
+ } finally {
+ if (out != null) {
+ out.close();
+ }
+ }
+ }
+ return f;
}
}
1.2 +35 -0
ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/MSBuildTask.java
Index: MSBuildTask.java
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/MSBuildTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MSBuildTask.java 21 Nov 2003 13:53:57 -0000 1.1
+++ MSBuildTask.java 27 Nov 2003 14:11:05 -0000 1.2
@@ -59,11 +59,18 @@
import java.util.ArrayList;
import java.util.List;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
/**
* Runs a MSBuild build process.
*/
public class MSBuildTask extends AbstractBuildTask {
+ private static final String TARGET = "generated-by-ant";
+
public MSBuildTask() {
super();
}
@@ -117,6 +124,34 @@
return new String[]{sb.toString()};
} else {
return new String[0];
+ }
+ }
+
+ /**
+ * Turn the DocumentFragment into a DOM tree suitable as a build
+ * file when serialized.
+ *
+ * <p>If we have exactly one <Project> child, return that.
+ * Otherwise if we have only <Task> children, wrap them into a
+ * <Target> which in turn gets wrapped into a <Project>.
+ * Otherwise, fail.</p>
+ */
+ protected Element makeTree(DocumentFragment f) {
+ NodeList nl = f.getChildNodes();
+ if (nl.getLength() == 1
+ && nl.item(0).getNodeType() == Node.ELEMENT_NODE
+ && nl.item(0).getNodeName().equals("Project")) {
+ return (Element) nl.item(0);
+ } else {
+ Element p = f.getOwnerDocument().createElement("Project");
+ p.setAttribute("DefaultTargets", TARGET);
+
+ Element t = f.getOwnerDocument().createElement("Target");
+ t.setAttribute("Name", TARGET);
+
+ p.appendChild(t);
+ t.appendChild(f);
+ return p;
}
}
}
1.2 +26 -0
ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NAntTask.java
Index: NAntTask.java
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NAntTask.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NAntTask.java 21 Nov 2003 13:53:57 -0000 1.1
+++ NAntTask.java 27 Nov 2003 14:11:05 -0000 1.2
@@ -59,6 +59,11 @@
import java.util.ArrayList;
import java.util.List;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
/**
* Runs a NAnt build process.
*/
@@ -101,5 +106,26 @@
al.add("-D:" + p.getName() + "=" + p.getValue());
}
return (String[]) al.toArray(new String[al.size()]);
+ }
+
+ /**
+ * Turn the DocumentFragment into a DOM tree suitable as a build
+ * file when serialized.
+ *
+ * <p>If we have exactly one <project> child, return that.
+ * Otherwise assume that this is a valid build file snippet that
+ * just needs an empty project wrapped around it.</p>
+ */
+ protected Element makeTree(DocumentFragment f) {
+ NodeList nl = f.getChildNodes();
+ if (nl.getLength() == 1
+ && nl.item(0).getNodeType() == Node.ELEMENT_NODE
+ && nl.item(0).getNodeName().equals("project")) {
+ return (Element) nl.item(0);
+ } else {
+ Element e = f.getOwnerDocument().createElement("project");
+ e.appendChild(f);
+ return e;
+ }
}
}
1.2 +12 -0
ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/MSBuildTaskTest.java
Index: MSBuildTaskTest.java
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/MSBuildTaskTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MSBuildTaskTest.java 25 Nov 2003 11:57:14 -0000 1.1
+++ MSBuildTaskTest.java 27 Nov 2003 14:11:05 -0000 1.2
@@ -86,4 +86,16 @@
expectLogContaining("echo", "foo is bar");
}
}
+
+ public void testNestedFile() throws Exception {
+ if (getProject().getProperty("msbuild.found") != null) {
+ expectLogContaining("nested-file", "foo is bar");
+ }
+ }
+
+ public void testNestedTask() throws Exception {
+ if (getProject().getProperty("msbuild.found") != null) {
+ expectLogContaining("nested-task", "foo is bar");
+ }
+ }
}
1.2 +12 -0
ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NAntTaskTest.java
Index: NAntTaskTest.java
===================================================================
RCS file:
/home/cvs/ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NAntTaskTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NAntTaskTest.java 25 Nov 2003 11:57:14 -0000 1.1
+++ NAntTaskTest.java 27 Nov 2003 14:11:05 -0000 1.2
@@ -86,4 +86,16 @@
expectLogContaining("echo", "foo is bar");
}
}
+
+ public void testNestedFile() throws Exception {
+ if (getProject().getProperty("nant.found") != null) {
+ expectLogContaining("nested-file", "foo is bar");
+ }
+ }
+
+ public void testNestedTask() throws Exception {
+ if (getProject().getProperty("nant.found") != null) {
+ expectLogContaining("nested-task", "foo is bar");
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]