DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7397>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7397
Explicitly choose line separator in copy task for token filtering
Summary: Explicitly choose line separator in copy task for token
filtering
Product: Ant
Version: 1.5 alpha (nightly)
Platform: All
OS/Version: All
Status: NEW
Severity: Enhancement
Priority: Other
Component: Core tasks
AssignedTo: ant-dev@jakarta.apache.org
ReportedBy: haui@haumacher.de
Ant promises a platform independent build process. But this build
process may require the generation of platform dependent files. Such
files e.g. are shell scripts. One requirement for shell scripts on
Unix and DOS are line separators corresponding to the target platform
of the script. So neither a scripts with \r\n line separator will work
on Unix, nor does a batch file with \n line separator in a DOS shell.
With the current ant distribution up to 1.5 alpha from the CVS it is
not possible to create shell scripts using the token filtering for
another platform than the one used for build (without faking the
line.separator property). To create shell scripts for various
platforms, one would like to specify the line separator used in the
copy task. This even does not require that the editor used for
creating the sources supports the handling of different line separator
styles.
I propose two additional attributes for the copy task, one to specify
the line separator explicitly (lineSeparator) and one
(lineSeparatorStyle) to select a line separator style form a
predefined list of styles (at least unix, dos, and mac).
To create a batch file for dos, one may use a task definition like the
following. This is independent of the build platform and does not
require editor support of the dos line separator style to create the
sources.
<copy todir="${build}" filtering="true" lineSeparatorStyle="dos">
<fileset dir="${basedir}">
<include name="bin/*.bat"/>
</fileset>
</copy>
The creation of Unix scripts on any platform may look like this:
<copy todir="${build}" filtering="true" lineSeparatorStyle="unix">
<fileset dir="${basedir}">
<include name="bin/*.sh"/>
</fileset>
</copy>
I attach a patch against the latest development sources 1.5alpha that
implements the proposed feature. I'm also willing to prepare a patch
against the 1.4 branch, if I find a person willing to commit it. The
move task may also be a candidate for supporting these two new
attributes.
Best regards, Bernhard Haumacher.
--- src/main/org/apache/tools/ant/taskdefs/Copy.java 6 Mar 2002 14:29:18 -0000
1.37
+++ src/main/org/apache/tools/ant/taskdefs/Copy.java 23 Mar 2002 19:05:46 -0000
@@ -95,6 +95,14 @@
* @ant.task category="filesystem"
*/
public class Copy extends Task {
+ public static final String LINE_SEPARATOR_STYLE_UNIX = "unix";
+ public static final String LINE_SEPARATOR_STYLE_DOS = "dos";
+ public static final String LINE_SEPARATOR_STYLE_MAC = "mac";
+
+ public static final String LINE_SEPARATOR_UNIX = "\n";
+ public static final String LINE_SEPARATOR_DOS = "\r\n";
+ public static final String LINE_SEPARATOR_MAC = "\r";
+
protected File file = null; // the source file
protected File destFile = null; // the destination file
protected File destDir = null; // the destination directory
@@ -117,6 +125,8 @@
private FileUtils fileUtils;
private String encoding = null;
+ private String lineSeparator = null;
+
public Copy() {
fileUtils = FileUtils.newFileUtils();
}
@@ -288,6 +298,37 @@
}
/**
+ * Sets the line separator for this copy operation. Has only an
+ * effect, if token filtering is performed.
+ *
+ * @since 1.32, Ant 1.5
+ */
+ public void setLineSeparator (String lineSeparator) {
+ this.lineSeparator = lineSeparator;
+ }
+
+ /**
+ * @return the line separator, <code>null</code> if not set.
+ *
+ * @since 1.32, Ant 1.5
+ */
+ public String getLineSeparator() {
+ return lineSeparator;
+ }
+
+ public void setLineSeparatorStyle (String lineSeparatorStyle) {
+ if (lineSeparatorStyle.equals(LINE_SEPARATOR_STYLE_UNIX)) {
+ setLineSeparator(LINE_SEPARATOR_UNIX);
+ } else if (lineSeparatorStyle.equals(LINE_SEPARATOR_STYLE_DOS)) {
+ setLineSeparator(LINE_SEPARATOR_DOS);
+ } else if (lineSeparatorStyle.equals(LINE_SEPARATOR_STYLE_MAC)) {
+ setLineSeparator(LINE_SEPARATOR_MAC);
+ } else {
+ throw new BuildException("Unknown line separator style '" +
lineSeparatorStyle + "'");
+ }
+ }
+
+ /**
* Performs the copy operation.
*/
public void execute() throws BuildException {
@@ -479,7 +520,7 @@
}
fileUtils.copyFile(fromFile, toFile, executionFilters,
filterChains,
forceOverwrite, preserveLastModified,
- encoding, project);
+ encoding, project, lineSeparator);
} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + ioe.getMessage();
--- src/main/org/apache/tools/ant/util/FileUtils.java 21 Mar 2002 08:09:19 -0000
1.20
+++ src/main/org/apache/tools/ant/util/FileUtils.java 23 Mar 2002 19:05:46 -0000
@@ -225,7 +225,30 @@
throws IOException {
copyFile(new File(sourceFile), new File(destFile), filters,
filterChains, overwrite, preserveLastModified,
- encoding, project);
+ encoding, project, null);
+ }
+
+ /**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * filter chains must be used, if source files may overwrite
+ * newer destination files, if the last modified time of
+ * <code>destFile</code> file should be made equal
+ * to the last modified time of <code>sourceFile</code> and
+ * a custom line separator should be used.
+ *
+ * @throws IOException
+ *
+ * @since 1.21, Ant 1.5
+ */
+ public void copyFile(String sourceFile, String destFile,
+ FilterSetCollection filters, Vector filterChains,
+ boolean overwrite, boolean preserveLastModified,
+ String encoding, Project project, String
lineSeparator)
+ throws IOException {
+ copyFile(new File(sourceFile), new File(destFile), filters,
+ filterChains, overwrite, preserveLastModified,
+ encoding, project, lineSeparator);
}
/**
@@ -314,6 +337,28 @@
boolean overwrite, boolean preserveLastModified,
String encoding, Project project)
throws IOException {
+ copyFile(sourceFile, destFile, filters, filterChains, overwrite,
+ preserveLastModified, encoding, project, null);
+ }
+
+ /**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * filter chains must be used, if source files may overwrite
+ * newer destination files, if the last modified time of
+ * <code>destFile</code> file should be made equal
+ * to the last modified time of <code>sourceFile</code> and a
+ * custome line separator should be used.
+ *
+ * @throws IOException
+ *
+ * @since 1.21, Ant 1.5
+ */
+ public void copyFile(File sourceFile, File destFile,
+ FilterSetCollection filters, Vector filterChains,
+ boolean overwrite, boolean preserveLastModified,
+ String encoding, Project project, String
lineSeparator)
+ throws IOException {
if (overwrite || !destFile.exists() ||
destFile.lastModified() < sourceFile.lastModified()) {
@@ -335,6 +380,10 @@
&& filterChains.size() > 0);
if (filterSetsAvailable || filterChainsAvailable) {
+ if (lineSeparator == null) {
+ lineSeparator = System.getProperty("line.separator");
+ }
+
BufferedReader in = null;
BufferedWriter out = null;
@@ -368,7 +417,7 @@
String line = in.readLine();
while (line != null) {
if (line.length() == 0) {
- out.newLine();
+ out.write(lineSeparator); // out.newLine();
} else {
if (filterSetsAvailable) {
newline = filters.replaceTokens(line);
@@ -376,7 +425,7 @@
newline = line;
}
out.write(newline);
- out.newLine();
+ out.write(lineSeparator); // out.newLine();
}
line = in.readLine();
}
--
To unsubscribe, e-mail: <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>
|