Browse Source

Added a nested <srcfile> element to <execon> to give the user more

control over the command line. If no element has been specified, the
names for the source files will be put at the end.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268220 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
96501a8fea
2 changed files with 92 additions and 8 deletions
  1. +50
    -8
      src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java
  2. +42
    -0
      src/main/org/apache/tools/ant/types/Commandline.java

+ 50
- 8
src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java View File

@@ -64,7 +64,7 @@ import java.io.IOException;
/**
* Executes a given command, supplying a set of files as arguments.
*
* @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:mariusz@rakiura.org">Mariusz Nowostawski</a>
*/
public class ExecuteOn extends ExecTask {
@@ -72,6 +72,7 @@ public class ExecuteOn extends ExecTask {
protected Vector filesets = new Vector();
private boolean parallel = false;
protected String type = "file";
protected Commandline.Marker srcFilePos = null;

/**
* Adds a set of files (nested fileset attribute).
@@ -94,6 +95,19 @@ public class ExecuteOn extends ExecTask {
this.type = type.getValue();
}

/**
* Marker that indicates where the name of the source file should
* be put on the command line.
*/
public Commandline.Marker createSrcfile() {
if (srcFilePos != null) {
throw new BuildException(taskType + " doesn\'t support multiple srcfile elements.",
location);
}
srcFilePos = cmdl.createMarker();
return srcFilePos;
}

protected void checkConfiguration() {
super.checkConfiguration();
if (filesets.size() == 0) {
@@ -128,11 +142,11 @@ public class ExecuteOn extends ExecTask {
v.copyInto(s);

int err = -1;
String myos = System.getProperty("os.name");

if (parallel) {
cmdl.addArguments(s);
exe.setCommandline(cmdl.getCommandline());
String[] command = getCommandline(s);
log("Executing " + Commandline.toString(command), Project.MSG_VERBOSE);
exe.setCommandline(command);
err = exe.execute();
if (err != 0) {
if (failOnError) {
@@ -144,11 +158,10 @@ public class ExecuteOn extends ExecTask {
}

} else {
String[] cmd = new String[cmdl.size()+1];
System.arraycopy(cmdl.getCommandline(), 0, cmd, 0, cmdl.size());
for (int i=0; i<s.length; i++) {
cmd[cmdl.size()] = s[i];
exe.setCommandline(cmd);
String[] command = getCommandline(s[i]);
log("Executing " + Commandline.toString(command), Project.MSG_VERBOSE);
exe.setCommandline(command);
err = exe.execute();
if (err != 0) {
if (failOnError) {
@@ -169,6 +182,35 @@ public class ExecuteOn extends ExecTask {
}
}

/**
* Construct the command line for parallel execution.
*
* @param srcFiles The filenames to add to the commandline
*/
protected String[] getCommandline(String[] srcFiles) {
String[] orig = cmdl.getCommandline();
String[] result = new String[orig.length+srcFiles.length];

int index = orig.length;
if (srcFilePos != null) {
index = srcFilePos.getPosition();
}
System.arraycopy(orig, 0, result, 0, index);
System.arraycopy(srcFiles, 0, result, index, srcFiles.length);
System.arraycopy(orig, index, result, index+srcFiles.length,
orig.length-index);
return result;
}

/**
* Construct the command line for serial execution.
*
* @param srcFile The filename to add to the commandline
*/
protected String[] getCommandline(String srcFile) {
return getCommandline(new String[] {srcFile});
}

/**
* Enumerated attribute with the values "file", "dir" and "both"
* for the type attribute.


+ 42
- 0
src/main/org/apache/tools/ant/types/Commandline.java View File

@@ -156,6 +156,38 @@ public class Commandline implements Cloneable {
}
}

/**
* Class to keep track of the position of an Argument.
*/
// <p>This class is there to support the srcfile and targetfile
// elements of &lt;execon&gt; and &lt;transform&gt; - don't know
// whether there might be additional use cases.</p> --SB
public class Marker {

private int position;
private int realPos = -1;

Marker(int position) {
this.position = position;
}

/**
* Return the number of arguments that preceeded this marker.
*
* <p>The name of the executable - if set - is counted as the
* very first argument.</p>
*/
public int getPosition() {
if (realPos == -1) {
realPos = (executable == null ? 0 : 1);
for (int i=0; i<position; i++) {
Argument arg = (Argument) arguments.elementAt(i);
realPos += arg.getParts().length;
}
}
return realPos;
}
}

/**
* Creates an argument object.
@@ -352,4 +384,14 @@ public class Commandline implements Cloneable {
arguments.removeAllElements();
}
/**
* Return a marker.
*
* <p>This marker can be used to locate a position on the
* commandline - to insert something for example - when all
* parameters have been set.</p>
*/
public Marker createMarker() {
return new Marker(arguments.size());
}
}

Loading…
Cancel
Save