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/unix Rpm.java
Date Sun, 06 Jan 2002 01:58:11 GMT
donaldp     02/01/05 17:58:11

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix
                        Rpm.java
  Log:
  Refactor and clean code
  
  Also start using Execute2 abstraction
  
  Revision  Changes    Path
  1.6       +57 -139   jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java
  
  Index: Rpm.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Rpm.java	30 Dec 2001 10:46:59 -0000	1.5
  +++ Rpm.java	6 Jan 2002 01:58:11 -0000	1.6
  @@ -7,220 +7,138 @@
    */
   package org.apache.tools.ant.taskdefs.unix;
   
  -import java.io.BufferedOutputStream;
   import java.io.File;
  -import java.io.FileOutputStream;
   import java.io.IOException;
  -import java.io.OutputStream;
  -import java.io.PrintStream;
  +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.Execute;
  -import org.apache.tools.ant.taskdefs.exec.LogOutputStream;
  +import org.apache.tools.ant.taskdefs.exec.Execute2;
   import org.apache.tools.ant.types.Commandline;
   
   /**
    * @author lucas@collab.net
    */
  -public class Rpm extends Task
  +public class Rpm
  +    extends AbstractTask
   {
  -
       /**
        * the rpm command to use
        */
  -    private String command = "-bb";
  +    private String m_command = "-bb";
   
       /**
        * clean BUILD directory
        */
  -    private boolean cleanBuildDir = false;
  +    private boolean m_cleanBuildDir;
   
       /**
        * remove spec file
        */
  -    private boolean removeSpec = false;
  +    private boolean m_removeSpec;
   
       /**
        * remove sources
        */
  -    private boolean removeSource = false;
  -
  -    /**
  -     * the file to direct standard error from the command
  -     */
  -    private File error;
  -
  -    /**
  -     * the file to direct standard output from the command
  -     */
  -    private File output;
  +    private boolean m_removeSource;
   
       /**
        * the spec file
        */
  -    private String specFile;
  +    private String m_specFile;
   
       /**
        * the rpm top dir
        */
  -    private File topDir;
  -
  -    public void setCleanBuildDir( boolean cbd )
  -    {
  -        cleanBuildDir = cbd;
  -    }
  -
  -    public void setCommand( String c )
  -    {
  -        this.command = c;
  -    }
  +    private File m_topDir;
   
  -    public void setError( File error )
  +    public void setCleanBuildDir( boolean cleanBuildDir )
       {
  -        this.error = error;
  +        m_cleanBuildDir = cleanBuildDir;
       }
   
  -    public void setOutput( File output )
  +    public void setCommand( final String command )
       {
  -        this.output = output;
  +        m_command = command;
       }
   
  -    public void setRemoveSource( boolean rs )
  +    public void setRemoveSource( final boolean removeSource )
       {
  -        removeSource = rs;
  +        m_removeSource = removeSource;
       }
   
  -    public void setRemoveSpec( boolean rs )
  +    public void setRemoveSpec( final boolean removeSpec )
       {
  -        removeSpec = rs;
  +        m_removeSpec = removeSpec;
       }
   
  -    public void setSpecFile( String sf )
  +    public void setSpecFile( final String specFile )
           throws TaskException
       {
  -        if( ( sf == null ) || ( sf.trim().equals( "" ) ) )
  +        if( ( specFile == null ) || ( specFile.trim().equals( "" ) ) )
           {
               throw new TaskException( "You must specify a spec file" );
           }
  -        this.specFile = sf;
  +        m_specFile = specFile;
       }
   
  -    public void setTopDir( File td )
  +    public void setTopDir( final File topDir )
       {
  -        this.topDir = td;
  +        m_topDir = topDir;
       }
   
       public void execute()
           throws TaskException
       {
  +        final Commandline cmd = createCommand();
  +        final Execute2 exe = new Execute2();
  +        setupLogger( exe );
   
  -        Commandline toExecute = new Commandline();
  +        if( m_topDir == null ) m_topDir = getBaseDirectory();
  +        exe.setWorkingDirectory( m_topDir );
   
  -        toExecute.setExecutable( "rpm" );
  -        if( topDir != null )
  +        exe.setCommandline( cmd.getCommandline() );
  +        try
           {
  -            toExecute.createArgument().setValue( "--define" );
  -            toExecute.createArgument().setValue( "_topdir" + topDir );
  -        }
  +            final String message = "Building the RPM based on the " + m_specFile + " file";
  +            getLogger().info( message );
   
  -        toExecute.createArgument().setLine( command );
  -
  -        if( cleanBuildDir )
  -        {
  -            toExecute.createArgument().setValue( "--clean" );
  -        }
  -        if( removeSpec )
  -        {
  -            toExecute.createArgument().setValue( "--rmspec" );
  +            if( 0 != exe.execute() )
  +            {
  +                throw new TaskException( "Failed to execute rpm" );
  +            }
           }
  -        if( removeSource )
  +        catch( IOException e )
           {
  -            toExecute.createArgument().setValue( "--rmsource" );
  +            throw new TaskException( "Error", e );
           }
  +    }
   
  -        toExecute.createArgument().setValue( "SPECS/" + specFile );
  -
  -        OutputStream outputstream = null;
  -        OutputStream errorstream = null;
  -
  -        if( error == null && output == null )
  +    private Commandline createCommand()
  +        throws TaskException
  +    {
  +        final Commandline cmd = new Commandline();
  +        cmd.setExecutable( "rpm" );
  +        if( m_topDir != null )
           {
  -            outputstream = new LogOutputStream( getLogger(), false );
  -            errorstream = new LogOutputStream( getLogger(), true );
  +            cmd.createArgument().setValue( "--define" );
  +            cmd.createArgument().setValue( "_topdir" + m_topDir );
           }
  -        else
  -        {
  -            if( output != null )
  -            {
  -                try
  -                {
  -                    outputstream = new PrintStream( new BufferedOutputStream( new FileOutputStream(
output ) ) );
  -                }
  -                catch( IOException e )
  -                {
  -                    throw new TaskException( "Error", e );
  -                }
  -            }
  -            else
  -            {
  -                outputstream = new LogOutputStream( getLogger(), false );
  -            }
  -            if( error != null )
  -            {
  -                try
  -                {
  -                    errorstream = new PrintStream( new BufferedOutputStream( new FileOutputStream(
error ) ) );
  -                }
  -                catch( IOException e )
  -                {
  -                    throw new TaskException( "Error", e );
  -                }
  -            }
  -            else
  -            {
  -                errorstream = new LogOutputStream( getLogger(), true );
  -            }
  -        }
  -
  -        Execute exe = new Execute();
  -        exe.setOutput( outputstream );
  -        exe.setError( errorstream );
   
  -        if( topDir == null ) topDir = getBaseDirectory();
  -        exe.setWorkingDirectory( topDir );
  +        cmd.createArgument().setLine( m_command );
   
  -        exe.setCommandline( toExecute.getCommandline() );
  -        try
  +        if( m_cleanBuildDir )
           {
  -            exe.execute();
  -            getLogger().info( "Building the RPM based on the " + specFile + " file" );
  +            cmd.createArgument().setValue( "--clean" );
           }
  -        catch( IOException e )
  +        if( m_removeSpec )
           {
  -            throw new TaskException( "Error", e );
  +            cmd.createArgument().setValue( "--rmspec" );
           }
  -        finally
  +        if( m_removeSource )
           {
  -            if( output != null )
  -            {
  -                try
  -                {
  -                    outputstream.close();
  -                }
  -                catch( IOException e )
  -                {
  -                }
  -            }
  -            if( error != null )
  -            {
  -                try
  -                {
  -                    errorstream.close();
  -                }
  -                catch( IOException e )
  -                {
  -                }
  -            }
  +            cmd.createArgument().setValue( "--rmsource" );
           }
  +
  +        cmd.createArgument().setValue( "SPECS/" + m_specFile );
  +        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