All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cedarsoft.devtools.modules.IgnoreFileModule Maven / Gradle / Ivy

The newest version!
package eu.cedarsoft.devtools.modules;

import eu.cedarsoft.devtools.FileTools;
import eu.cedarsoft.devtools.Module;
import eu.cedarsoft.devtools.SubversionMessageHandler;
import eu.cedarsoft.utils.CmdLine;
import org.jetbrains.annotations.NotNull;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.ISVNPropertyHandler;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.SVNPropertyData;
import org.tmatesoft.svn.core.wc.SVNRevision;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Adds a file to the ignore property
 */
public class IgnoreFileModule implements Module {
  @NotNull
  public String getDescription() {
    return SubversionMessageHandler.get( "module.ignore.file" );
  }

  public void process( @NotNull final CmdLine cmdLine ) throws Exception {
    cmdLine.out( SubversionMessageHandler.get( "module.edit.ignore" ) );

    final SVNClientManager manager = SVNClientManager.newInstance();
    File currentDir = new File( "." );
    SVNPropertyData property = manager.getWCClient().doGetProperty( currentDir, SVNProperty.IGNORE, SVNRevision.WORKING, SVNRevision.WORKING, false );

    //Current ignores

    final Set ignored = new HashSet();

    String propertyValue;
    if ( property != null ) {
      propertyValue = property.getValue();

      cmdLine.out( SubversionMessageHandler.get( "current.ignore" ) );
      String[] lines = propertyValue.trim().split( "\n" );
      for ( String line : lines ) {
        cmdLine.out( SubversionMessageHandler.get( "ignored.file", line ) );
      }
      ignored.addAll( Arrays.asList( lines ) );
    } else {
      cmdLine.out( SubversionMessageHandler.get( "no.current.ignored" ) );
      propertyValue = "";
    }

    cmdLine.outNl();

    File fileToIgnore = FileTools.readFile( cmdLine, SubversionMessageHandler.get( "select.file.to.ignore" ), currentDir.listFiles( new FileFilter() {
      public boolean accept( File pathname ) {
        if ( pathname.getName().equals( ".svn" ) ) {
          return false;
        }

        try {
          SVNInfo info = manager.getWCClient().doInfo( pathname, SVNRevision.HEAD );
          if ( info != null ) {
            return false;
          }
          return !ignored.contains( pathname.getName() );
        } catch ( SVNException ignore ) {
          return true;
        }
      }
    } )
    );
    cmdLine.out( SubversionMessageHandler.get( "will.ignore.file", fileToIgnore.getAbsolutePath() ) );
    cmdLine.pause( 3 );

    String newPropertyValue = propertyValue.trim() + "\n" + fileToIgnore.getName();

    manager.getWCClient().doSetProperty( currentDir, SVNProperty.IGNORE, newPropertyValue.trim() + "\n", false, false, new ISVNPropertyHandler() {
      public void handleProperty( File path, SVNPropertyData property ) throws SVNException {
        debug( path.getAbsolutePath(), property.getName(), property.getValue() );
      }

      public void handleProperty( SVNURL url, SVNPropertyData property ) throws SVNException {
        debug( url.getURIEncodedPath(), property.getName(), property.getValue() );
      }

      public void handleProperty( long revision, SVNPropertyData property ) throws SVNException {
      }

      public void debug( @NotNull String path, @NotNull String name, @NotNull String value ) {
        cmdLine.out( SubversionMessageHandler.get( "setting.property.path", path, name, value ) );
      }
    } );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy