From 0ce2545f6c212bf88eedcfcbc3ab22c908a73a26 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 29 Jun 2009 09:41:37 +0000 Subject: [PATCH] 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 --- WHATSNEW | 4 ++ docs/manual/CoreTasks/apply.html | 38 +++++++++++++++ .../apache/tools/ant/taskdefs/ExecuteOn.java | 40 ++++++++++++---- .../apache/tools/ant/types/Commandline.java | 47 +++++++++++++++++-- .../antunit/taskdefs/exec/apply-test.xml | 23 +++++++++ 5 files changed, 141 insertions(+), 11 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index f00f94c1c..a0cafee24 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -741,6 +741,10 @@ Other changes: optional prefix and suffix attributes. Bugzilla Report 47365 + * '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 ============================================= diff --git a/docs/manual/CoreTasks/apply.html b/docs/manual/CoreTasks/apply.html index 814d9ab6e..f5ee80ff9 100644 --- a/docs/manual/CoreTasks/apply.html +++ b/docs/manual/CoreTasks/apply.html @@ -333,12 +333,50 @@ end of the command line (unless you set addsourcefile to false). If you need to place it somewhere different, use a nested <srcfile> element between your <arg> elements to mark the insertion point.

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
prefixa prefix to place in front of the file name when + building the command line argument. Since Ant 1.8.0No.
suffixa suffix to append to the file name when + building the command line argument. Since Ant 1.8.0No.

targetfile

<targetfile> is similar to <srcfile> 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.

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
prefixa prefix to place in front of the file name when + building the command line argument. Since Ant 1.8.0No.
suffixa suffix to append to the file name when + building the command line argument. Since Ant 1.8.0No.

env

It is possible to specify environment variables to pass to the system command via nested <env> elements. See the diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java index 0e3147190..d2903eb59 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java @@ -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. diff --git a/src/main/org/apache/tools/ant/types/Commandline.java b/src/main/org/apache/tools/ant/types/Commandline.java index c53979093..0a800ae30 100644 --- a/src/main/org/apache/tools/ant/types/Commandline.java +++ b/src/main/org/apache/tools/ant/types/Commandline.java @@ -202,14 +202,16 @@ public class Commandline implements Cloneable { /** * Class to keep track of the position of an Argument. -

This class is there to support the srcfile and targetfile - elements of <execon> and <transform> - don't know - whether there might be additional use cases.

--SB + * + *

This class is there to support the srcfile and targetfile + * elements of <apply>.

*/ 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; + } + } /** diff --git a/src/tests/antunit/taskdefs/exec/apply-test.xml b/src/tests/antunit/taskdefs/exec/apply-test.xml index 89f384153..56067a798 100755 --- a/src/tests/antunit/taskdefs/exec/apply-test.xml +++ b/src/tests/antunit/taskdefs/exec/apply-test.xml @@ -731,6 +731,29 @@ + + + + + + + + + + + + + + + + + + + + + + +