Browse Source

Cleaned up task and added attributes for all settable qualities. Create COmmandline in separate method and reuse that

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270700 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 24 years ago
parent
commit
0d2c8498ea
2 changed files with 182 additions and 116 deletions
  1. +91
    -58
      proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Patch.java
  2. +91
    -58
      proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Patch.java

+ 91
- 58
proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Patch.java View File

@@ -9,8 +9,8 @@ package org.apache.tools.ant.taskdefs;

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 @@ import org.apache.tools.ant.types.Commandline;
* 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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* 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;
}
}

+ 91
- 58
proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Patch.java View File

@@ -9,8 +9,8 @@ package org.apache.tools.ant.taskdefs;

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 @@ import org.apache.tools.ant.types.Commandline;
* 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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* @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 @@ public class Patch extends Task
*
* 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;
}
}

Loading…
Cancel
Save