From 571c177da2d5b9acfee0e494f0328c817d3f5de3 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Tue, 13 Mar 2001 03:04:06 +0000 Subject: [PATCH] Change Chmod task so that the usage works. This was being used in the Alexandria project and seems reasonable usage but was not in fact supported. Catching this usage is somewhat messy however. I factored out the code for executing the command into a final method runExecute so that I could invoke it from chmod. This means ExecTask has runExec and runExecute which may be confusing. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268815 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Chmod.java | 30 ++++++++++++++++++- .../apache/tools/ant/taskdefs/ExecTask.java | 29 +++++++++++------- .../apache/tools/ant/taskdefs/ExecuteOn.java | 20 ++----------- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Chmod.java b/src/main/org/apache/tools/ant/taskdefs/Chmod.java index 21d07e416..c84ce1002 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Chmod.java +++ b/src/main/org/apache/tools/ant/taskdefs/Chmod.java @@ -71,6 +71,7 @@ import java.util.*; public class Chmod extends ExecuteOn { private FileSet defaultSet = new FileSet(); + private boolean defaultSetDefined = false; private boolean havePerm = false; public Chmod() { @@ -98,6 +99,7 @@ public class Chmod extends ExecuteOn { * add a name entry on the include list */ public PatternSet.NameEntry createInclude() { + defaultSetDefined = true; return defaultSet.createInclude(); } @@ -105,6 +107,7 @@ public class Chmod extends ExecuteOn { * add a name entry on the exclude list */ public PatternSet.NameEntry createExclude() { + defaultSetDefined = true; return defaultSet.createExclude(); } @@ -112,6 +115,7 @@ public class Chmod extends ExecuteOn { * add a set of patterns */ public PatternSet createPatternSet() { + defaultSetDefined = true; return defaultSet.createPatternSet(); } @@ -122,6 +126,7 @@ public class Chmod extends ExecuteOn { * @param includes the string containing the include patterns */ public void setIncludes(String includes) { + defaultSetDefined = true; defaultSet.setIncludes(includes); } @@ -132,6 +137,7 @@ public class Chmod extends ExecuteOn { * @param excludes the string containing the exclude patterns */ public void setExcludes(String excludes) { + defaultSetDefined = true; defaultSet.setExcludes(excludes); } @@ -143,6 +149,7 @@ public class Chmod extends ExecuteOn { * shouldn't be used. */ public void setDefaultexcludes(boolean useDefaultExcludes) { + defaultSetDefined = true; defaultSet.setDefaultexcludes(useDefaultExcludes); } @@ -152,12 +159,33 @@ public class Chmod extends ExecuteOn { location); } - if (defaultSet.getDir(project) != null) { + if (defaultSetDefined && defaultSet.getDir(project) != null) { addFileset(defaultSet); } super.checkConfiguration(); } + public void execute() throws BuildException { + if (defaultSetDefined) { + super.execute(); + } + else if (!defaultSetDefined && defaultSet.getDir(project) != null) { + // we are chmodding the given directory + createArg().setValue(defaultSet.getDir(project).getPath()); + Execute execute = prepareExec(); + try { + execute.setCommandline(cmdl.getCommandline()); + runExecute(execute); + } catch (IOException e) { + throw new BuildException("Execute failed: " + e, e, location); + } finally { + // close the output file if required + logFlush(); + } + } + } + + public void setExecutable(String e) { throw new BuildException(taskType+" doesn\'t support the executable attribute", location); } diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index 0a4fcaaae..1aa106ec4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -218,21 +218,28 @@ public class ExecTask extends Task { } /** - * Run the command using the given Execute instance. + * A Utility method for this classes and subclasses to run an Execute instance (an external command). */ - protected void runExec(Execute exe) throws BuildException { + protected final void runExecute(Execute exe) throws IOException { int err = -1; // assume the worst - try { - exe.setCommandline(cmdl.getCommandline()); - err = exe.execute(); - if (err != 0) { - if (failOnError) { - throw new BuildException("Exec returned: "+err, location); - } else { - log("Result: " + err, Project.MSG_ERR); - } + err = exe.execute(); + if (err != 0) { + if (failOnError) { + throw new BuildException(taskType + " returned: "+err, location); + } else { + log("Result: " + err, Project.MSG_ERR); } + } + } + + /** + * Run the command using the given Execute instance. This may be overidden by subclasses + */ + protected void runExec(Execute exe) throws BuildException { + exe.setCommandline(cmdl.getCommandline()); + try { + runExecute(exe); } catch (IOException e) { throw new BuildException("Execute failed: " + e, e, location); } finally { diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java index 154ef0ca2..fecbabaf3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java @@ -148,15 +148,7 @@ public class ExecuteOn extends ExecTask { log("Executing " + Commandline.toString(command), Project.MSG_VERBOSE); exe.setCommandline(command); - err = exe.execute(); - if (err != 0) { - if (failOnError) { - throw new BuildException("Exec returned: "+err, - location); - } else { - log("Result: " + err, Project.MSG_ERR); - } - } + runExecute(exe); } else { for (int j=0; j