Browse Source

add prefix and suffix attributes to apply's srcfile and targetfile elements. PR 45625

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@789271 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
0ce2545f6c
5 changed files with 141 additions and 11 deletions
  1. +4
    -0
      WHATSNEW
  2. +38
    -0
      docs/manual/CoreTasks/apply.html
  3. +32
    -8
      src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java
  4. +44
    -3
      src/main/org/apache/tools/ant/types/Commandline.java
  5. +23
    -0
      src/tests/antunit/taskdefs/exec/apply-test.xml

+ 4
- 0
WHATSNEW View File

@@ -741,6 +741,10 @@ Other changes:
optional prefix and suffix attributes.
Bugzilla Report 47365

* <apply>'s srcfile and targetfile child elements can now have
optional prefix and suffix attributes.
Bugzilla Report 45625

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 38
- 0
docs/manual/CoreTasks/apply.html View File

@@ -333,12 +333,50 @@ end of the command line (unless you set addsourcefile to
<code>false</code>). If you need to place it somewhere different,
use a nested <code>&lt;srcfile&gt;</code> element between your
<code>&lt;arg&gt;</code> elements to mark the insertion point.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">prefix</td>
<td valign="top">a prefix to place in front of the file name when
building the command line argument. <em>Since Ant 1.8.0</em></td>
<td align="center" valign="top">No.</td>
</tr>
<tr>
<td valign="top">suffix</td>
<td valign="top">a suffix to append to the file name when
building the command line argument. <em>Since Ant 1.8.0</em></td>
<td align="center" valign="top">No.</td>
</tr>
</table>
<h4>targetfile</h4>
<p><code>&lt;targetfile&gt;</code> is similar to
<code>&lt;srcfile&gt;</code> and marks the position of the target
filename on the command line. If omitted, the target filenames will
not be added to the command line at all. This element can only be
specified if you also define a nested mapper.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">prefix</td>
<td valign="top">a prefix to place in front of the file name when
building the command line argument. <em>Since Ant 1.8.0</em></td>
<td align="center" valign="top">No.</td>
</tr>
<tr>
<td valign="top">suffix</td>
<td valign="top">a suffix to append to the file name when
building the command line argument. <em>Since Ant 1.8.0</em></td>
<td align="center" valign="top">No.</td>
</tr>
</table>
<h4>env</h4>
<p>It is possible to specify environment variables to pass to the
system command via nested <code>&lt;env&gt;</code> elements. See the


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

@@ -576,10 +576,10 @@ public class ExecuteOn extends ExecTask {
srcIndex + srcFiles.length,
targetIndex - srcIndex);

// targets are already absolute file names
System.arraycopy(targetFiles, 0, result,
targetIndex + srcFiles.length,
targetFiles.length);
insertTargetFiles(targetFiles, result,
targetIndex + srcFiles.length,
targetFilePos.getPrefix(),
targetFilePos.getSuffix());

// targetIndex --> end
System.arraycopy(orig, targetIndex, result,
@@ -589,10 +589,9 @@ public class ExecuteOn extends ExecTask {
// 0 --> targetIndex
System.arraycopy(orig, 0, result, 0, targetIndex);

// targets are already absolute file names
System.arraycopy(targetFiles, 0, result,
targetIndex,
targetFiles.length);
insertTargetFiles(targetFiles, result, targetIndex,
targetFilePos.getPrefix(),
targetFilePos.getSuffix());

// targetIndex --> srcIndex
System.arraycopy(orig, targetIndex, result,
@@ -626,6 +625,11 @@ public class ExecuteOn extends ExecTask {
if (forwardSlash && fileSeparator != '/') {
src = src.replace(fileSeparator, '/');
}
if (srcFilePos != null &&
(srcFilePos.getPrefix().length() > 0
|| srcFilePos.getSuffix().length() > 0)) {
src = srcFilePos.getPrefix() + src + srcFilePos.getSuffix();
}
result[srcIndex + i] = src;
}
return result;
@@ -735,6 +739,26 @@ public class ExecuteOn extends ExecTask {
}
}

/**
* Inserts target file names (which are already absolute paths)
* into the list of arguments, taking prefix and postfix into
* account.
*/
private static void insertTargetFiles(String[] targetFiles,
String[] arguments,
int insertPosition,
String prefix, String suffix) {
if (prefix.length() == 0 && suffix.length() == 0) {
System.arraycopy(targetFiles, 0, arguments, insertPosition,
targetFiles.length);
} else {
for (int i = 0; i < targetFiles.length; i++) {
arguments[insertPosition + i] =
prefix + targetFiles[i] + suffix;
}
}
}

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


+ 44
- 3
src/main/org/apache/tools/ant/types/Commandline.java View File

@@ -202,14 +202,16 @@ 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
*
* <p>This class is there to support the srcfile and targetfile
* elements of &lt;apply&gt;.</p>
*/
public class Marker {

private int position;
private int realPos = -1;
private String prefix = "";
private String suffix = "";

/**
* Construct a marker for the specified position.
@@ -236,6 +238,45 @@ public class Commandline implements Cloneable {
}
return realPos;
}

/**
* Set the prefix to be placed in front of the inserted argument.
*
* @param prefix fixed prefix string.
* @since Ant 1.8.0
*/
public void setPrefix(String prefix) {
this.prefix = prefix != null ? prefix : "";
}

/**
* Get the prefix to be placed in front of the inserted argument.
*
* @since Ant 1.8.0
*/
public String getPrefix() {
return prefix;
}

/**
* Set the suffix to be placed at the end of the inserted argument.
*
* @param suffix fixed suffix string.
* @since Ant 1.8.0
*/
public void setSuffix(String suffix) {
this.suffix = suffix != null ? suffix : "";
}

/**
* Get the suffix to be placed at the end of the inserted argument.
*
* @since Ant 1.8.0
*/
public String getSuffix() {
return suffix;
}

}

/**


+ 23
- 0
src/tests/antunit/taskdefs/exec/apply-test.xml View File

@@ -731,6 +731,29 @@
</au:assertTrue>
</target>

<target name="testSrcfilePrefix" if="test.can.run" depends="xyz">
<apply executable="sh" force="true">
<arg value="parrot.sh" />
<srcfile prefix="-Dfoo="/>
<fileset refid="xyz" />
</apply>
<au:assertLogContains text="-Dfoo=${x} out" />
<au:assertLogContains text="-Dfoo=${y} out" />
<au:assertLogContains text="-Dfoo=${z} out" />
</target>

<target name="testTargetfileSuffix" if="test.can.run" depends="xyz">
<apply executable="sh" addsourcefile="false" dest="${basedir}">
<arg value="parrot.sh" />
<targetfile suffix=",x"/>
<fileset refid="xyz" />
<globmapper from="*" to="*.bar"/>
</apply>
<au:assertLogContains text="${x}.bar,x out" />
<au:assertLogContains text="${y}.bar,x out" />
<au:assertLogContains text="${z}.bar,x out" />
</target>

<target name="tearDown">
<delete>
<fileset refid="xyz" />


Loading…
Cancel
Save