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

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

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

import com.google.inject.Inject;
import eu.cedarsoft.devtools.DirectorySupport;
import eu.cedarsoft.devtools.Module;
import eu.cedarsoft.devtools.SubversionMessageHandler;
import eu.cedarsoft.devtools.SubversionSupport;
import eu.cedarsoft.utils.CmdLine;
import eu.cedarsoft.utils.io.FileCopyManager;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

/**
 * Imports a project into a subversion repository
 */
public class SubversionImportModule implements Module {
  @NotNull
  private final SubversionSupport subversionSupport;

  @Inject
  public SubversionImportModule( @NotNull SubversionSupport subversionSupport ) {
    this.subversionSupport = subversionSupport;
  }

  @NotNull
  public String getDescription() {
    return SubversionMessageHandler.get( "module.subversion.import" );
  }

  public void process( @NotNull CmdLine cmdLine ) throws IOException {
    cmdLine.out( SubversionMessageHandler.get( "import.directory.into.subversion" ) );

    File current = new File( "." );
    String importName = cmdLine.read( SubversionMessageHandler.get( "select.import.name" ), subversionSupport.guessImportName( current ) );

    String repositoryUrl = cmdLine.read( SubversionMessageHandler.get( "wich.repository.shall.be.used" ), subversionSupport.getRepositoryUrls(), null );
    subversionSupport.addRepositoryUrl( repositoryUrl );

    //Check whether the project is still in SVN!
    {
      File svnDir = new File( current, ".svn" );
      if ( svnDir.exists() ) {
        throw new IllegalStateException( current.getCanonicalPath() + " is still in svn" );
      }
    }

    cmdLine.warning( SubversionMessageHandler.get( "importing.directory.subversion" ) );
    cmdLine.out( SubversionMessageHandler.get( "import.name" ) );
    cmdLine.success( '\t' + importName );
    cmdLine.out( SubversionMessageHandler.get( "repository.url" ) );
    cmdLine.success( '\t' + repositoryUrl );

    cmdLine.pause( 5 );

    //Create the directory structure
    File tmpDir = createTmpDir();

    File proj = new File( tmpDir, importName );
    proj.mkdir();
    File trunk = new File( proj, "trunk" );
    trunk.mkdir();
    new File( proj, "branches" ).mkdir();
    new File( proj, "tags" ).mkdir();

    //Import project
    {
      cmdLine.out( SubversionMessageHandler.get( "importing.project" ) );

      Process process = Runtime.getRuntime().exec( subversionSupport.getSubversionBin() + " import . " + repositoryUrl + " --message 'Initial'", null, tmpDir );
      cmdLine.out( process );
      try {
        process.waitFor();
      } catch ( InterruptedException e ) {
        throw new RuntimeException( e );
      }
    }

    //clean up
    FileCopyManager.deleteForced( tmpDir );
    tmpDir = createTmpDir();

    //Checkout
    {
      cmdLine.out( SubversionMessageHandler.get( "checking.out.project" ) );
      Process process = Runtime.getRuntime().exec( subversionSupport.getSubversionBin() + " checkout " + repositoryUrl + "/" + importName + "/trunk " + importName, null, tmpDir );
      cmdLine.out( process );
      try {
        process.waitFor();
      } catch ( InterruptedException e ) {
        throw new RuntimeException( e );
      }
    }

    //Copy the .svn
    {
      File checkedOut = new File( tmpDir, importName );
      File svnDir = new File( checkedOut, ".svn" );
      File dstDir = new File( current, ".svn" );
      dstDir.mkdir();
      FileCopyManager.copy( svnDir, dstDir );
    }

    //clean up
    FileCopyManager.deleteForced( tmpDir );

    cmdLine.success( SubversionMessageHandler.get( "module.processed.successfully" ) );
  }

  protected File createTmpDir() {
    File tmpDir = new File( DirectorySupport.getTempDir(), "tmpDir" );
    if ( tmpDir.exists() ) {
      throw new IllegalStateException( "Uups. File still exists " + tmpDir.getAbsolutePath() );
    }
    tmpDir.mkdir();
    return tmpDir;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy