ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs Patch.java
Date Sat, 12 Jan 2002 23:41:07 GMT
donaldp     02/01/12 15:41:07

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs
                        Patch.java
  Log:
  Cleaned up task and added attributes for all settable qualities. Create COmmandline in separate
method and reuse that
  
  Revision  Changes    Path
  1.12      +91 -58    jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Patch.java
  
  Index: Patch.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Patch.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Patch.java	6 Jan 2002 02:04:11 -0000	1.11
  +++ Patch.java	12 Jan 2002 23:41:07 -0000	1.12
  @@ -9,8 +9,8 @@
   
   import java.io.File;
   import java.io.IOException;
  +import org.apache.myrmidon.api.AbstractTask;
   import org.apache.myrmidon.api.TaskException;
  -import org.apache.tools.ant.Task;
   import org.apache.tools.ant.taskdefs.exec.Execute2;
   import org.apache.tools.ant.types.Commandline;
   
  @@ -18,25 +18,27 @@
    * Task as a layer on top of patch. Patch applies a diff file to an original.
    *
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  + * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    */
  -public class Patch extends Task
  +public class Patch
  +    extends AbstractTask
   {
  -    private boolean havePatchfile = false;
  -    private Commandline cmd = new Commandline();
  -
  -    private File originalFile;
  +    private File m_originalFile;
  +    private File m_patchFile;
  +    private boolean m_backups;
  +    private boolean m_ignorewhitespace;
  +    private boolean m_reverse;
  +    private boolean m_quiet;
  +    private Integer m_strip;
   
       /**
        * Shall patch write backups.
        *
        * @param backups The new Backups value
        */
  -    public void setBackups( boolean backups )
  +    public void setBackups( final boolean backups )
       {
  -        if( backups )
  -        {
  -            cmd.createArgument().setValue( "-b" );
  -        }
  +        m_backups = backups;
       }
   
       /**
  @@ -44,12 +46,9 @@
        *
        * @param ignore The new Ignorewhitespace value
        */
  -    public void setIgnorewhitespace( boolean ignore )
  +    public void setIgnorewhitespace( final boolean ignorewhitespace )
       {
  -        if( ignore )
  -        {
  -            cmd.createArgument().setValue( "-l" );
  -        }
  +        m_ignorewhitespace = ignorewhitespace;
       }
   
       /**
  @@ -57,9 +56,9 @@
        *
        * @param file The new Originalfile value
        */
  -    public void setOriginalfile( File file )
  +    public void setOriginalfile( final File originalFile )
       {
  -        originalFile = file;
  +        m_originalFile = originalFile;
       }
   
       /**
  @@ -67,16 +66,10 @@
        *
        * @param file The new Patchfile value
        */
  -    public void setPatchfile( File file )
  +    public void setPatchfile( final File patchFile )
           throws TaskException
       {
  -        if( !file.exists() )
  -        {
  -            throw new TaskException( "patchfile " + file + " doesn\'t exist" );
  -        }
  -        cmd.createArgument().setValue( "-i" );
  -        cmd.createArgument().setFile( file );
  -        havePatchfile = true;
  +        m_patchFile = patchFile;
       }
   
       /**
  @@ -84,12 +77,9 @@
        *
        * @param q The new Quiet value
        */
  -    public void setQuiet( boolean q )
  +    public void setQuiet( final boolean quiet )
       {
  -        if( q )
  -        {
  -            cmd.createArgument().setValue( "-s" );
  -        }
  +        m_quiet = quiet;
       }
   
       /**
  @@ -97,12 +87,9 @@
        *
        * @param r The new Reverse value
        */
  -    public void setReverse( boolean r )
  +    public void setReverse( final boolean reverse )
       {
  -        if( r )
  -        {
  -            cmd.createArgument().setValue( "-R" );
  -        }
  +        m_reverse = reverse;
       }
   
       /**
  @@ -111,46 +98,92 @@
        *
        * patch's <i>-p</i> option.
        *
  -     * @param num The new Strip value
  -     * @exception TaskException Description of Exception
  +     * @param strip The new Strip value
        */
  -    public void setStrip( int num )
  +    public void setStrip( final Integer strip )
  +    {
  +        m_strip = strip;
  +    }
  +
  +    public void execute()
           throws TaskException
       {
  -        if( num < 0 )
  +        validate();
  +
  +        final Execute2 exe = new Execute2();
  +        setupLogger( exe );
  +
  +        final Commandline cmd = buildCommand();
  +        exe.setCommandline( cmd.getCommandline() );
  +
  +        try
           {
  -            throw new TaskException( "strip has to be >= 0" );
  +            exe.execute();
  +        }
  +        catch( IOException e )
  +        {
  +            throw new TaskException( "Error", e );
           }
  -        cmd.createArgument().setValue( "-p" + num );
       }
   
  -    public void execute()
  +    private void validate()
           throws TaskException
       {
  -        if( !havePatchfile )
  +        if( null == m_patchFile )
           {
  -            throw new TaskException( "patchfile argument is required" );
  +            final String message = "patchfile argument is required";
  +            throw new TaskException( message );
           }
   
  -        Commandline toExecute = (Commandline)cmd.clone();
  -        toExecute.setExecutable( "patch" );
  +        if( !m_patchFile.exists() )
  +        {
  +            final String message = "patchfile " + m_patchFile + " doesn\'t exist";
  +            throw new TaskException( message );
  +        }
   
  -        if( originalFile != null )
  +        if( null != m_strip && m_strip.intValue() < 0 )
           {
  -            toExecute.createArgument().setFile( originalFile );
  +            throw new TaskException( "strip has to be >= 0" );
           }
  +    }
   
  -        final Execute2 exe = new Execute2();
  -        setupLogger( exe );
  -        exe.setCommandline( toExecute.getCommandline() );
  -        try
  +    private Commandline buildCommand()
  +    {
  +        final Commandline cmd = new Commandline();
  +        cmd.setExecutable( "patch" );
  +
  +        if( m_backups )
           {
  -            exe.execute();
  +            cmd.createArgument().setValue( "-b" );
           }
  -        catch( IOException e )
  +
  +        if( null != m_strip )
           {
  -            throw new TaskException( "Error", e );
  +            cmd.createArgument().setValue( "-p" + m_strip.intValue() );
  +        }
  +
  +        if( m_quiet )
  +        {
  +            cmd.createArgument().setValue( "-s" );
  +        }
  +
  +        if( m_reverse )
  +        {
  +            cmd.createArgument().setValue( "-R" );
  +        }
  +
  +        cmd.createArgument().setValue( "-i" );
  +        cmd.createArgument().setFile( m_patchFile );
  +
  +        if( m_ignorewhitespace )
  +        {
  +            cmd.createArgument().setValue( "-l" );
           }
  -    }
   
  -}// Patch
  +        if( null != m_originalFile )
  +        {
  +            cmd.createArgument().setFile( m_originalFile );
  +        }
  +        return cmd;
  +    }
  +}
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message