You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2002/04/03 18:34:25 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/types Commandline.java

bodewig     02/04/03 08:34:25

  Modified:    src/etc/testcases/taskdefs abstractcvstask.xml
               src/main/org/apache/tools/ant/taskdefs AbstractCvsTask.java
               src/main/org/apache/tools/ant/types Commandline.java
  Log:
  Add nested <commandline> elements to <cvs>
  
  PR: 7433
  Submitted by:	stephan beal <st...@wanderinghorse.net>
  
  Revision  Changes    Path
  1.2       +5 -3      jakarta-ant/src/etc/testcases/taskdefs/abstractcvstask.xml
  
  Index: abstractcvstask.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/abstractcvstask.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- abstractcvstask.xml	3 Apr 2002 16:17:37 -0000	1.1
  +++ abstractcvstask.xml	3 Apr 2002 16:34:25 -0000	1.2
  @@ -6,7 +6,6 @@
     <property name="file" value="ant.properties.sample" />
     <target name="all">
       <cvs failonerror="true" command="status ${file}"/>
  -    <!--
       <cvs failonerror="true">
         <commandline>
           <argument value="up"/>
  @@ -16,8 +15,11 @@
         </commandline>
       </cvs>
       <cvs failonerror="true" command="status ${file}"/>
  -    <cvs failonerror="true">up -r HEAD ${file}</cvs>
  +    <cvs failonerror="true">
  +      <commandline>
  +        <argument line="up -r HEAD ${file}" />
  +      </commandline>
  +    </cvs>
       <cvs failonerror="true" command="status ${file}"/>
  -    -->
     </target>
   </project>
  
  
  
  1.3       +160 -48   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java
  
  Index: AbstractCvsTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractCvsTask.java	7 Feb 2002 02:15:47 -0000	1.2
  +++ AbstractCvsTask.java	3 Apr 2002 16:34:25 -0000	1.3
  @@ -60,11 +60,13 @@
   import java.io.IOException;
   import java.io.OutputStream;
   import java.io.PrintStream;
  +import java.util.Vector;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.Task;
   import org.apache.tools.ant.types.Commandline;
   import org.apache.tools.ant.types.Environment;
  +import org.apache.tools.ant.util.StringUtils;
   
   /**
    * original Cvs.java 1.20
  @@ -79,9 +81,13 @@
    * @author Kevin Ross <a href="mailto:kevin.ross@bredex.com">kevin.ross@bredex.com</a>
    */
   public abstract class AbstractCvsTask extends Task {
  -
  +    /** Default compression level to use, if compression is enabled via setCompression( true ). */
  +    public static final int DEFAULT_COMPRESSION_LEVEL = 3;
       private Commandline cmd = new Commandline();
   
  +    /** list of Commandline children */
  +    private Vector vecCommandlines = new Vector();
  +
       /**
        * the CVSROOT variable.
        */
  @@ -98,9 +104,13 @@
       private String cvsPackage;
   
       /**
  +     * the default command.
  +     */
  +    private static final String default_command = "checkout";
  +    /**
        * the CVS command to execute.
        */
  -    private String command = "checkout";
  +    private String command = null;
   
       /**
        * suppress information messages.
  @@ -108,6 +118,11 @@
       private boolean quiet = false;
   
       /**
  +     * compression level to use.
  +     */
  +    private int compression = 0;
  +
  +    /**
        * report only, don't change any files.
        */
       private boolean noexec = false;
  @@ -146,15 +161,19 @@
        */
       private boolean failOnError = false;
   
  -
       /**
        * Create accessors for the following, to allow different handling of
  -     *  the output.
  +     * the output.
        */
       private ExecuteStreamHandler executeStreamHandler;
       private OutputStream outputStream;
       private OutputStream errorStream;
   
  +    /** empty no-arg constructor*/
  +    public AbstractCvsTask() {
  +        super();
  +    }
  +
       public void setExecuteStreamHandler(ExecuteStreamHandler executeStreamHandler){
   
           this.executeStreamHandler = executeStreamHandler;
  @@ -222,42 +241,20 @@
           return this.errorStream;
       }
   
  -    public void execute() throws BuildException {
  -
  +    /**
  +     * Sets up the environment for toExecute and then runs it.
  +     * @throws BuildException
  +     */
  +    protected void runCommand( Commandline toExecute ) throws BuildException {
           // XXX: we should use JCVS (www.ice.com/JCVS) instead of command line
           // execution so that we don't rely on having native CVS stuff around (SM)
   
           // We can't do it ourselves as jCVS is GPLed, a third party task
           // outside of jakarta repositories would be possible though (SB).
   
  -        Commandline toExecute = new Commandline();
  -
  -        toExecute.setExecutable("cvs");
  -        if (cvsRoot != null) {
  -            toExecute.createArgument().setValue("-d");
  -            toExecute.createArgument().setValue(cvsRoot);
  -        }
  -        if (noexec) {
  -            toExecute.createArgument().setValue("-n");
  -        }
  -        if (quiet) {
  -            toExecute.createArgument().setValue("-q");
  -        }
  -
  -        toExecute.createArgument().setLine(command);
  -
  -        //
  -        // get the other arguments.
  -        //
  -        toExecute.addArguments(cmd.getCommandline());
  -
  -        if (cvsPackage != null) {
  -            toExecute.createArgument().setLine(cvsPackage);
  -        }
  -
           Environment env = new Environment();
   
  -        if(port>0){
  +        if (port>0) {
               Environment.Variable var = new Environment.Variable();
               var.setKey("CVS_CLIENT_PORT");
               var.setValue(String.valueOf(port));
  @@ -277,7 +274,7 @@
           }
            */
   
  -        if(passFile!=null){
  +        if (passFile!=null) {
               Environment.Variable var = new Environment.Variable();
               var.setKey("CVS_PASSFILE");
               var.setValue(String.valueOf(passFile));
  @@ -285,7 +282,7 @@
               log("Using cvs passfile: " + String.valueOf(passFile), Project.MSG_INFO);
           }
   
  -        if(cvsRsh!=null){
  +        if (cvsRsh!=null) {
               Environment.Variable var = new Environment.Variable();
               var.setKey("CVS_RSH");
               var.setValue(String.valueOf(cvsRsh));
  @@ -309,16 +306,47 @@
           exe.setEnvironment(env.getVariables());
   
           try {
  -            log("Executing: " + executeToString(exe), Project.MSG_DEBUG);
  -
  +            String actualCommandLine = executeToString(exe);
  +            log("running cvs command: " + actualCommandLine, 
  +                Project.MSG_DEBUG);
               int retCode = exe.execute();
  +            log( "retCode="+retCode, Project.MSG_DEBUG );
               /*Throw an exception if cvs exited with error. (Iulian)*/
               if(failOnError && retCode != 0) {
  -                throw new BuildException("cvs exited with error code "+ retCode);
  +                throw new BuildException("cvs exited with error code "
  +                                         + retCode 
  +                                         + StringUtils.LINE_SEP
  +                                         + "Command line was ["
  +                                         + actualCommandLine + "]", location );
               }
           }
           catch (IOException e) {
  -            throw new BuildException(e, location);
  +            if( failOnError ) {
  +                throw new BuildException(e, location);
  +            }
  +            else {
  +                log("Caught exception: "+e.getMessage(), Project.MSG_WARN);
  +            }
  +        }
  +        catch (BuildException e) {
  +            if( failOnError ) {
  +                throw( e );
  +            }
  +            else {
  +                Throwable t = e.getException();
  +                if (t == null) {
  +                    t = e;
  +                }
  +                log("Caught exception: "+t.getMessage(), Project.MSG_WARN);
  +            }
  +        }
  +        catch (Exception e) {
  +            if( failOnError ) {
  +                throw new BuildException(e, location);
  +            }
  +            else {
  +                log("Caught exception: "+e.getMessage(), Project.MSG_WARN);
  +            }
           }
           finally {
               //
  @@ -330,7 +358,6 @@
                       outputStream.close();
                   } catch (IOException e) {}
               }
  -
               if (errorStream != null) {
                   try {
                       errorStream.close();
  @@ -339,6 +366,26 @@
           }
       }
   
  +    public void execute() throws BuildException {
  +
  +
  +        if( this.getCommand() == null
  +            && vecCommandlines.size() == 0 ) {
  +            // re-implement legacy behaviour:
  +            this.setCommand( AbstractCvsTask.default_command );
  +        }
  +
  +        String c = this.getCommand();
  +        if( c != null ) {
  +            this.addConfiguredCommandline( this.cmd, true );
  +            this.cmd.createArgument().setLine(c);
  +        }
  +
  +        for( int i = 0; i < vecCommandlines.size(); i++ ) {
  +            this.runCommand( (Commandline)vecCommandlines.elementAt( i ) );
  +        }
  +    }
  +
       private String executeToString(Execute execute){
   
           StringBuffer stringBuffer = new StringBuffer(250);
  @@ -348,18 +395,16 @@
               stringBuffer.append(commandLine[i]);
               stringBuffer.append(" ");
           }
  -        String newLine = System.getProperty("line.separator");
  -        stringBuffer.append(newLine);
  -        stringBuffer.append(newLine);
  -        stringBuffer.append("environment:");
  -        stringBuffer.append(newLine);
  -
   
  +        String newLine = StringUtils.LINE_SEP;
           String[] variableArray = execute.getEnvironment();
   
           if(variableArray != null){
  +	    stringBuffer.append(newLine);
  +	    stringBuffer.append(newLine);
  +	    stringBuffer.append("environment:");
  +	    stringBuffer.append(newLine);
               for(int z=0; z<variableArray.length; z++){
  -
                   stringBuffer.append(newLine);
                   stringBuffer.append("\t");
                   stringBuffer.append(variableArray[z]);
  @@ -451,10 +496,14 @@
        *      of commands externally.
        */
       public void addCommandArgument(String arg){
  +        this.addCommandArgument( cmd, arg);
  +    }
   
  -        this.cmd.createArgument().setValue(arg);
  +    public void addCommandArgument(Commandline c, String arg){
  +        c.createArgument().setValue(arg);
       }
   
  +
       public void setDate(String p) {
           if(p != null && p.trim().length() > 0) {
               addCommandArgument("-D");
  @@ -465,6 +514,9 @@
       public void setCommand(String c) {
           this.command = c;
       }
  +    public String getCommand() {
  +        return this.command;
  +    }
   
       public void setQuiet(boolean q) {
           quiet = q;
  @@ -489,6 +541,66 @@
       public void setFailOnError(boolean failOnError) {
           this.failOnError = failOnError;
       }
  -}
   
  +    /**
  +     * Configure a commandline element for things like cvsRoot, quiet, etc.
  +     */
  +    protected void configureCommandline( Commandline c ) {
  +        if( c == null ) {
  +            return;
  +        }
  +        c.setExecutable( "cvs" );
  +        if (cvsPackage != null) {
  +            c.createArgument(true).setLine(cvsPackage);
  +        }
  +        if ( this.compression > 0 && this.compression < 10 ) {
  +            c.createArgument(true).setValue("-z"+this.compression);
  +        }
  +        if (quiet) {
  +            c.createArgument(true).setValue("-q");
  +        }
  +        if (noexec) {
  +            c.createArgument(true).setValue("-n");
  +        }
  +        if (cvsRoot != null) {
  +            c.createArgument(true).setLine("-d"+cvsRoot);
  +        }
  +    }
   
  +    public void addConfiguredCommandline( Commandline c ) {
  +        this.addConfiguredCommandline( c, false );
  +    }
  +
  +    /**
  +    * Configures and adds the given Commandline.
  +    * @param insertAtStart If true, c is
  +    */
  +    public void addConfiguredCommandline( Commandline c, boolean insertAtStart ) {
  +        if( c == null ) { return; }
  +        this.configureCommandline( c );
  +        if( insertAtStart ) {
  +            vecCommandlines.insertElementAt( c, 0 );
  +        }
  +        else {
  +            vecCommandlines.addElement( c );
  +        }
  +    }
  +
  +    /**
  +    * If set to a value 1-9 it adds -zN to the cvs command line, else
  +    * it disables compression.
  +    */
  +    public void setCompression( int level ) {
  +        this.compression = level;
  +    }
  +
  +    /**
  +     * @param usecomp If true, turns on compression using default
  +     * level, AbstractCvsTask.DEFAULT_COMPRESSION_LEVEL.
  +     */
  +    public void setCompression( boolean usecomp ) {
  +        this.setCompression( usecomp ? 
  +                             AbstractCvsTask.DEFAULT_COMPRESSION_LEVEL : 0 );
  +    }
  +
  +}
  
  
  
  1.19      +41 -14    jakarta-ant/src/main/org/apache/tools/ant/types/Commandline.java
  
  Index: Commandline.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/Commandline.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Commandline.java	10 Jan 2002 11:21:20 -0000	1.18
  +++ Commandline.java	3 Apr 2002 16:34:25 -0000	1.19
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -80,7 +80,7 @@
    * <code>createAcommandline</code> which returns an instance of this class.
    *
    * @author thomas.haas@softwired-inc.com
  - * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
  + * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    */
   public class Commandline implements Cloneable {
   
  @@ -124,6 +124,9 @@
            * @param line line to split into several commandline arguments
            */
           public void setLine(String line) {
  +            if( line == null ) {
  +                return;
  +            }
               parts = translateCommandline(line);
           }
   
  @@ -132,7 +135,7 @@
            * PATH - ensures the right separator for the local platform
            * is used.
            *
  -         * @param value a single commandline argument.  
  +         * @param value a single commandline argument.
            */
           public void setPath(Path value) {
               parts = new String[] {value.toString()};
  @@ -140,9 +143,9 @@
   
           /**
            * Sets a single commandline argument to the absolute filename
  -         * of the given file.  
  +         * of the given file.
            *
  -         * @param value a single commandline argument.  
  +         * @param value a single commandline argument.
            */
           public void setFile(File value) {
               parts = new String[] {value.getAbsolutePath()};
  @@ -191,16 +194,37 @@
   
       /**
        * Creates an argument object.
  -     * Each commandline object has at most one instance of the argument class.
  +     *
  +     * <p>Each commandline object has at most one instance of the
  +     * argument class.  This method calls
  +     * <code>this.createArgument(false)</code>.</p>
  +     *
  +     * @see #createArgument(boolean)
        * @return the argument object.
        */
       public Argument createArgument() {
  +        return this.createArgument( false );
  +    }
  +
  +    /**
  +     * Creates an argument object and adds it to our list of args.
  +     *
  +     * <p>Each commandline object has at most one instance of the
  +     * argument class.</p>
  +     *
  +     * @param insertAtStart if true, the argument is inserted at the
  +     * beginning of the list of args, otherwise it is appended.
  +     */
  +    public Argument createArgument( boolean insertAtStart ) {
           Argument argument = new Argument();
  -        arguments.addElement(argument);
  +        if(insertAtStart) {
  +            arguments.insertElementAt(argument,0);
  +        } else {
  +            arguments.addElement(argument);
  +        }
           return argument;
       }
   
  -
       /**
        * Sets the executable to run.
        */
  @@ -248,11 +272,13 @@
           for (int i=0; i<arguments.size(); i++) {
               Argument arg = (Argument) arguments.elementAt(i);
               String[] s = arg.getParts();
  -            for (int j=0; j<s.length; j++) {
  -                result.addElement(s[j]);
  +            if( s != null ) {
  +                for (int j=0; j<s.length; j++) {
  +                    result.addElement(s[j]);
  +                }
               }
           }
  -        
  +
           String [] res = new String[result.size()];
           result.copyInto(res);
           return res;
  @@ -271,7 +297,7 @@
        * surround the argument by double quotes.</p>
        *
        * @exception BuildException if the argument contains both, single
  -     *                           and double quotes.  
  +     *                           and double quotes.
        */
       public static String quoteArgument(String argument) {
           if (argument.indexOf("\"") > -1) {
  @@ -310,7 +336,7 @@
           }
   
           // parse with a simple finite state machine
  -        
  +
           final int normal = 0;
           final int inQuote = 1;
           final int inDoubleQuote = 2;
  @@ -390,7 +416,7 @@
       public void clearArgs() {
           arguments.removeAllElements();
       }
  -        
  +
       /**
        * Return a marker.
        *
  @@ -401,4 +427,5 @@
       public Marker createMarker() {
           return new Marker(arguments.size());
       }
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>