From 370ac32906a2af20459706db9e6e1e0888a9483f Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Tue, 19 Apr 2005 22:28:22 +0000 Subject: [PATCH] Now for the fun. Exec adds an osfamily attribute, so you can restrict execution to a platform such as nt, or unix. This propagates to execon, so into chmod, attrib, etc. We could therefore adapt them use osfamily as their way of being unix or windows only. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278155 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/apply.html | 6 +++ docs/manual/CoreTasks/exec.html | 11 +++++- src/etc/testcases/taskdefs/exec/exec.xml | 28 ++++++++++++++ .../apache/tools/ant/taskdefs/ExecTask.java | 37 ++++++++++++++++--- .../tools/ant/taskdefs/ExecTaskTest.java | 12 ++++++ 5 files changed, 86 insertions(+), 8 deletions(-) diff --git a/docs/manual/CoreTasks/apply.html b/docs/manual/CoreTasks/apply.html index 272bc2698..c82ae4523 100644 --- a/docs/manual/CoreTasks/apply.html +++ b/docs/manual/CoreTasks/apply.html @@ -91,6 +91,12 @@ to send input to it is via the input and inputstring attributes.

executed. No + + osfamily + OS family as used in the <os> condition. + since Ant 1.7 + No + output the file to which the output of the command diff --git a/docs/manual/CoreTasks/exec.html b/docs/manual/CoreTasks/exec.html index 77002c01a..fac1a90e8 100644 --- a/docs/manual/CoreTasks/exec.html +++ b/docs/manual/CoreTasks/exec.html @@ -17,7 +17,7 @@ systems.

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that -in Ant 1.6, any attempt to read input in the forked program will receive an +since Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.

@@ -115,7 +115,14 @@ or the task will fail as follows: list of Operating Systems on which the command may be executed. If the current OS's name is contained in this list, the command will be executed. The OS's name is determined by the Java Virtual machine and is set - in the "os.name" system property. + in the "os.name" system property. + + No + + + osfamily + OS family as used in the <os> condition. + since Ant 1.7 No diff --git a/src/etc/testcases/taskdefs/exec/exec.xml b/src/etc/testcases/taskdefs/exec/exec.xml index 6aaef2a86..3255c7396 100644 --- a/src/etc/testcases/taskdefs/exec/exec.xml +++ b/src/etc/testcases/taskdefs/exec/exec.xml @@ -329,6 +329,34 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index a5a5edf60..bb04330e9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -21,10 +21,13 @@ import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.Vector; +import java.util.Locale; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Path; @@ -43,6 +46,7 @@ public class ExecTask extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private String os; + private String osFamily; private File dir; protected boolean failOnError = false; @@ -382,6 +386,15 @@ public class ExecTask extends Task { } + /** + * Restrict this execution to a single OS Family + * @param osFamily + */ + public void setOsFamily(String osFamily) { + this.osFamily = osFamily.toLowerCase(Locale.US); + } + + /** * The method attempts to figure out where the executable is so that we can feed * the full path. We first try basedir, then the exec dir, and then @@ -516,21 +529,33 @@ public class ExecTask extends Task { * @return boolean. *
    *
  • - * true if the os under which Ant is running is - * matches one os in the os attribute - * or if the os attribute is null
  • + *
  • true if the os and osfamily attributes are null.
  • + *
  • true if osfamily is set, and the os family and must match + * that of the current OS, according to the logic of + * {@link Os#isOs(String, String, String, String)}, and the result of the + * os attribute must also evaluate true. + *
  • + *
  • + * true if os is set, and the system.property os.name + * is found in the os attribute,
  • *
  • false otherwise.
  • *
*/ protected boolean isValidOs() { - // test if os match + //hand osfamily off to Os class, if set + if(osFamily!=null && !Os.isOs(osFamily,null,null,null)) { + return false; + } + //the Exec OS check is different from Os.isOs(), which + //probes for a specific OS. Instead it searches the os field + //for the current os.name String myos = System.getProperty("os.name"); log("Current OS is " + myos, Project.MSG_VERBOSE); if ((os != null) && (os.indexOf(myos) < 0)) { // this command will be executed only on the specified OS log("This OS, " + myos - + " was not found in the specified list of valid OSes: " + os, - Project.MSG_VERBOSE); + + " was not found in the specified list of valid OSes: " + os, + Project.MSG_VERBOSE); return false; } return true; diff --git a/src/testcases/org/apache/tools/ant/taskdefs/ExecTaskTest.java b/src/testcases/org/apache/tools/ant/taskdefs/ExecTaskTest.java index c351b5b84..ef384e5c8 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/ExecTaskTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/ExecTaskTest.java @@ -383,6 +383,18 @@ public class ExecTaskTest extends BuildFileTest { assertTrue("log file found after spawn", logFile.exists()); } + public void testExecUnknownOS() { + executeTarget("testExecUnknownOS"); + } + + public void testExecOSFamily() { + executeTarget("testExecOSFamily"); + } + + public void testExecInconsistentSettings() { + executeTarget("testExecInconsistentSettings"); + } + private static class MonitoredBuild implements Runnable { private Thread worker; private File myBuildFile = null;