Hi,
 
As promissed, here is a first shot at geting the environment and propagating it properly
during the <exec*> operations.
 
The patch modifies Execute.java which is the lowest level. Added a new attribute to ExecTask
"newenvironment='true'" if you do not want propagation.
 
Added attribute "environment='prefix'" to <property/> to indicate to load the properties from the
environment:
 
   <property environment="ant.env" />
 
I have tested <property> which calls the methods on Execute to do the job, so most should be
tested at this point. But I only have NT, someone needs to verify other environments and in particular
set the correct values for the command on the MAC if there are any.
 
Hope is useful....
 
Jose Alberto

 

Attachment: Jose Alberto Fernandez.vcf
Description: Binary data

Index: docs/index.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v
retrieving revision 1.154
diff -u -r1.154 index.html
--- docs/index.html     2000/11/27 07:54:34     1.154
+++ docs/index.html     2000/11/29 06:48:57
@@ -1701,6 +1701,12 @@
       returncode other than 0.</td>
     <td align="center" valign="top">No</td>
   </tr>
+  <tr>
+    <td valign="top">newenvironment</td>
+    <td valign="top">Do not propagate old environment when new environment
+      variables are specified.</td>
+    <td align="center" valign="top">No, default is <em>false</em></td>
+  </tr>
 </table>
 <h3>Examples</h3>
 <blockquote>
@@ -1820,6 +1826,12 @@
     <td align="center" valign="top">No</td>
   </tr>
   <tr>
+    <td valign="top">newenvironment</td>
+    <td valign="top">Do not propagate old environment when new environment
+      variables are specified.</td>
+    <td align="center" valign="top">No, default is <em>false</em></td>
+  </tr>
+  <tr>
     <td valign="top">parallel</td>
     <td valign="top">Run the command only once, appending all files as
       arguments. Defaults to true. If false, command will be executed
@@ -3456,7 +3468,7 @@
 property cannot be set, and will be ignored. This means that properties set
 outside the current project always override the properties of the current
 project.</p>
-<p>There are four ways to set properties:</p>
+<p>There are five ways to set properties:</p>
 <ul>
   <li>By supplying both the <i>name</i> and <i>value</i> attribute.</li>
   <li>By supplying both the <i>name</i> and <i>refid</i> attribute.</li>
@@ -3466,6 +3478,9 @@
   <li>By setting the <i>resource</i> attribute with the resource name of the
     property file to load. This property file has the format as defined by the
     file used in the class java.util.Properties.</li>
+  <li>By setting the <i>environment</i> attribute with a prefix to use.
+    Properties will be defined for every environment variable by
+    prefixing the suplied name and a period to the name of the variable.</li>
 </ul>
 <p>Although combinations of the three ways are possible, only one should be 
used
 at a time. Problems might occur with the order in which properties are set, for
Index: src/main/org/apache/tools/ant/taskdefs/ExecTask.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v
retrieving revision 1.9
diff -u -r1.9 ExecTask.java
--- src/main/org/apache/tools/ant/taskdefs/ExecTask.java        2000/10/31 
11:12:04     1.9
+++ src/main/org/apache/tools/ant/taskdefs/ExecTask.java        2000/11/29 
06:49:02
@@ -74,6 +74,7 @@
     private File out;
     private File dir;
     protected boolean failOnError = false;
+    protected boolean newEnvironment = false;
     private Integer timeout = null;
     private Environment env = new Environment();
     protected Commandline cmdl = new Commandline();
@@ -132,6 +133,13 @@
     }
 
     /**
+     * Use a completely new environment
+     */
+    public void setNewenvironment(boolean newenv) {
+        newEnvironment = newenv;
+    }
+
+    /**
      * Add a nested env element - an environment variable.
      */
     public void addEnv(Environment.Variable var) {
@@ -198,6 +206,7 @@
                     Project.MSG_VERBOSE);
             }
         }
+        exe.setNewenvironment(newEnvironment);
         exe.setEnvironment(environment);
         return exe;
     }
Index: src/main/org/apache/tools/ant/taskdefs/Execute.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v
retrieving revision 1.9
diff -u -r1.9 Execute.java
--- src/main/org/apache/tools/ant/taskdefs/Execute.java 2000/11/25 02:38:36     
1.9
+++ src/main/org/apache/tools/ant/taskdefs/Execute.java 2000/11/29 06:49:02
@@ -63,8 +63,12 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.BufferedReader;
+import java.io.StringReader;
+import java.io.ByteArrayOutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.Vector;
 
 
 /**
@@ -84,9 +88,11 @@
     private ExecuteWatchdog watchdog;
     private File workingDirectory = null;
     private Project project = null;
+    private boolean newEnvironment = false;
 
     private static String antWorkingDirectory = System.getProperty("user.dir");
     private static CommandLauncher launcher = createCommandLauncher();
+    private static Vector procEnvironment = null;
 
     /** 
      * Builds a command launcher for the OS and JVM we are running under
@@ -139,6 +145,71 @@
     }
 
     /**
+     * Find the list of environment variables for this process.
+     */
+    public static synchronized Vector getProcEnvironment() {
+        if (procEnvironment != null) return procEnvironment;
+
+        procEnvironment = new Vector();
+        try {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            Execute exe = new Execute(new PumpStreamHandler(out));
+            exe.setCommandline(getProcEnvCommand());
+            // Make sure we do not recurse forever
+            exe.setNewenvironment(true);
+            int retval = exe.execute();
+            if ( retval != 0 ) {
+                // Just try to use what we got
+            }
+            BufferedReader in = 
+                new BufferedReader(new StringReader(out.toString()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                procEnvironment.addElement(line);
+            }
+        } 
+        catch (java.io.IOException exc) {
+            // Just try to see how much we got
+        }
+        return procEnvironment;
+    }
+
+    private static String[] getProcEnvCommand() {
+        String osname = System.getProperty("os.name").toLowerCase();
+        if ( osname.indexOf("mac os") >= 0 ) {
+            // Mac
+            // TODO: I have no idea how to get it, someone must fix it
+            String[] cmd = null;
+            return cmd;
+        }
+        else if ( osname.indexOf("os/2") >= 0 ) {
+            // OS/2 - use same mechanism as Windows 2000
+            // Not sure
+            String[] cmd = {"cmd", "/c", "set" };
+            return cmd;
+        }
+        else if ( osname.indexOf("windows") >= 0 ) {
+            // Determine if we're running under 2000/NT or 98/95
+            if ( osname.indexOf("nt") >= 0 || osname.indexOf("2000") >= 0 ) {
+                // Windows 2000/NT
+                String[] cmd = {"cmd", "/c", "set" };
+                return cmd;
+            }
+            else {
+                // Windows 98/95 - need to use an auxiliary script
+                String[] cmd = {"command", "/c", "set" };
+                return cmd;
+            }
+        }
+        else {
+            // Generic UNIX
+            // Alternatively one could use: /bin/sh -c env
+            String[] cmd = {"/usr/bin/env"};
+            return cmd;
+        }
+    }
+
+    /**
      * Creates a new execute object using <code>PumpStreamHandler</code> for
      * stream handling.
      */
@@ -191,12 +262,22 @@
     }
 
     /**
-     * Returns the commandline used to create a subprocess.
+     * Set whether to propagate the default environment or not.
      *
-     * @return the commandline used to create a subprocess
+     * @param newenv whether to propagate the process environment.
      */
+    public void setNewenvironment(boolean newenv) {
+        newEnvironment = newenv;
+    }
+
+    /**
+     * Returns the environment used to create a subprocess.
+     *
+     * @return the environment used to create a subprocess
+     */
     public String[] getEnvironment() {
-        return env;
+        if (env == null || newEnvironment) return env;
+        return patchEnvironment();
     }
 
 
@@ -275,6 +356,30 @@
 
     protected int getExitValue() {
         return exitValue;
+    }
+
+    /**
+     * Patch the current environment with the new values from the user.
+     * @return the patched environment
+     */
+    private String[] patchEnvironment() {
+        Vector osEnv = (Vector) getProcEnvironment().clone();
+        for (int i = 0; i < env.length; i++) {
+            int pos = env[i].indexOf('=');
+            // Get key including "="
+            String key = env[i].substring(0, pos+1);
+            int size = osEnv.size();
+            for (int j = 0; j < size; j++) {
+                if (((String)osEnv.elementAt(j)).startsWith(key)) {
+                    osEnv.removeElementAt(j);
+                    break;
+                }
+            }
+            osEnv.addElement(env[i]);
+        }
+        String[] result = new String[osEnv.size()];
+        osEnv.copyInto(result);
+        return result;
     }
 
     /**
Index: src/main/org/apache/tools/ant/taskdefs/Property.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Property.java,v
retrieving revision 1.19
diff -u -r1.19 Property.java
--- src/main/org/apache/tools/ant/taskdefs/Property.java        2000/10/13 
09:14:37     1.19
+++ src/main/org/apache/tools/ant/taskdefs/Property.java        2000/11/29 
06:49:02
@@ -73,6 +73,7 @@
     protected String value;
     protected File file;
     protected String resource;
+    protected String env;
     protected Reference ref = null;
 
     protected boolean userProperty=false; // set read-only properties
@@ -121,6 +122,14 @@
         return resource;
     }
 
+    public void setEnvironment(String env) {
+        this.env = env;
+    }
+
+    public String getEnvironment() {
+        return env;
+    }
+
     public void setUserProperty(boolean userProperty) {
         this.userProperty = userProperty;
     }
@@ -139,6 +148,8 @@
 
             if (resource != null) loadResource(resource);
 
+            if (env != null) loadEnvironment(env);
+
             if ((name != null) && (ref != null)) {
                 Object obj = ref.getReferencedObject(getProject());
                 if (obj != null) {
@@ -183,6 +194,24 @@
                 props.load(is);
                 addProperties(props);
             }
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    protected void loadEnvironment( String prefix ) {
+        Properties props = new Properties();
+        if (!prefix.endsWith(".")) prefix += ".";
+        log("Loading Environment " + prefix, Project.MSG_VERBOSE);
+        try {
+            Vector osEnv = Execute.getProcEnvironment();
+            for (Enumeration e = osEnv.elements(); e.hasMoreElements(); ) {
+                String entry = (String)e.nextElement();
+                int pos = entry.indexOf('=');
+                props.put(prefix + entry.substring(0, pos), 
+                          entry.substring(pos + 1));
+            }
+            addProperties(props);
         } catch (Exception ex) {
             ex.printStackTrace();
         }

Reply via email to