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

eu.cedarsoft.devtools.MavenSupport Maven / Gradle / Ivy

The newest version!
package eu.cedarsoft.devtools;

import com.google.inject.Inject;
import eu.cedarsoft.utils.CmdLine;
import eu.cedarsoft.utils.Renderer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Special module that offers methods related to maven.
 */
public class MavenSupport {
  @NotNull
  @NonNls
  public static final String SRC_DIR_NAME = "src";

  @NotNull
  @NonNls
  public static final String RESOURCES_DIR_NAME = "resources";

  @NotNull
  @NonNls
  public static final String SITE_DIR_NAME = "site";

  @NotNull
  @NonNls
  public static final String SITE_XML_NAME = "site.xml";
  @NotNull
  @NonNls
  public static final String GROUP_ID = "groupId";
  @NotNull
  @NonNls
  public static final String ARTIFACT_ID = "artifactId";

  @NotNull
  @NonNls
  public static final String VERSION = "version";

  @NonNls
  @NotNull
  private static final String POM_XML = "pom.xml";
  @NotNull
  private final DirectorySupport directorySupport;

  @NotNull
  private final List archetypes = new ArrayList();
  @NotNull
  @NonNls
  private final String mavenBin;
  @NotNull
  private static final FilenameFilter POM_FILENAME_FILTER = new FilenameFilter() {
    public boolean accept( File dir, String name ) {
      return name.equals( POM_XML );
    }
  };

  /**
   * Creates a new maven support
   *
   * @param directorySupport the directory support
   * @param mavenBin         the maven bin
   */
  @Inject
  public MavenSupport( @NotNull DirectorySupport directorySupport, @MavenBin @NotNull String mavenBin ) {
    this.directorySupport = directorySupport;
    this.mavenBin = mavenBin;
  }

  /**
   * Returns the directory for a given GroupId
   *
   * @param groupId the group id
   * @return the directory for the given groupId
   *
   * @throws FileNotFoundException if the group id is not valid
   */
  @NotNull
  public File getDirectory( @NotNull String groupId ) throws FileNotFoundException {
    File projectRoot = directorySupport.getProjectRoot();
    String[] parts = groupId.split( "\\." );

    File runner = new File( projectRoot.getAbsolutePath() );
    for ( String part : parts ) {
      runner = new File( runner, part );
      if ( !runner.exists() ) {
        throw new FileNotFoundException( runner.getAbsolutePath() );
      }
      if ( !runner.isDirectory() ) {
        throw new IllegalStateException( "Is not a directory: " + runner.getAbsolutePath() );
      }
    }
    return runner;
  }

  /**
   * Returns the directory for a given groupId and artifactId
   *
   * @param groupId    the groupId
   * @param artifactId the artifactId
   * @return the directory for the given group and artifact id
   *
   * @throws FileNotFoundException
   */
  public File getDirectory( @NotNull String groupId, @NotNull String artifactId ) throws FileNotFoundException {
    File groupDir = getDirectory( groupId );
    File projectDir = new File( groupDir, artifactId );
    if ( !projectDir.exists() ) {
      throw new FileNotFoundException( projectDir.getAbsolutePath() );
    }
    if ( !projectDir.isDirectory() ) {
      throw new IllegalStateException( "Is not a directory: " + projectDir.getAbsolutePath() );
    }
    return projectDir;
  }

  /**
   * Let the user select an archetype
   *
   * @param cmdLine the cmd line
   * @return the archetype
   *
   * @throws IOException
   */
  @NotNull
  public Archetype readArchetype( @NotNull CmdLine cmdLine ) throws IOException {
    return cmdLine.readSelection( MavenMessageHandler.get( "select.archetype" ), archetypes, new ArchetypePresenter() );
  }

  /**
   * Read the artifact id
   *
   * @param cmdLine the command line
   * @return the artifact id
   *
   * @throws IOException
   */
  @NotNull
  public String readArtifactId( @NotNull CmdLine cmdLine ) throws IOException {
    return cmdLine.read( MavenMessageHandler.get( "select.artifact.id" ) );
  }

  /**
   * Reads the version
   *
   * @param cmdLine the command line
   * @return the version
   *
   * @throws IOException
   */
  @NotNull
  public String readVersion( @NotNull CmdLine cmdLine ) throws IOException {
    return cmdLine.read( MavenMessageHandler.get( "select.version" ) );
  }

  /**
   * Read in the artifact id
   *
   * @param cmdLine           the command line
   * @param useLastAsArtifact whether the actual directory should be used as suggestion
   * @return the artifact id
   *
   * @throws IOException
   */
  public String readArtifactId( @NotNull CmdLine cmdLine, boolean useLastAsArtifact ) throws IOException {
    if ( useLastAsArtifact ) {
      File current = new File( "." );
      String defaultValue = null;
      if ( directorySupport.isInProjectRoot( current ) ) {
        String subPath = directorySupport.getProjectSubPath( current );
        defaultValue = subPath.replace( '/', '.' );

        if ( defaultValue.indexOf( '.' ) < 0 ) {
          throw new IllegalStateException( "Uuups, no point found " + defaultValue );
        }
        int lastIndex = defaultValue.lastIndexOf( '.' );
        defaultValue = defaultValue.substring( lastIndex + 1, defaultValue.length() );
      }
      return cmdLine.read( MavenMessageHandler.get( "select.artifact.id" ), defaultValue );
    } else {
      return readArtifactId( cmdLine );
    }
  }

  /**
   * Reads the group id. The id is verified.
   *
   * @param cmdLine the command line
   * @return the group id
   *
   * @throws IOException
   */
  @NotNull
  public String readGroupId( @NotNull CmdLine cmdLine ) throws IOException {
    return readGroupId( cmdLine, false );
  }

  /**
   * Reads the group id but does not verify the value
   *
   * @param cmdLine the command line
   * @return the group id
   *
   * @throws IOException
   */
  @NotNull
  public String readGroupIdUnverified( @NotNull CmdLine cmdLine ) throws IOException {
    return readGroupId( cmdLine, false, MavenMessageHandler.get( "select.project.id" ), false );
  }

  /**
   * Read the project id
   *
   * @param cmdLine the command line
   * @return the project id
   *
   * @throws IOException
   */
  @NotNull
  public String readProjectId( @NotNull CmdLine cmdLine ) throws IOException {
    return readGroupId( cmdLine, false, MavenMessageHandler.get( "select.project.id" ), true );
  }

  /**
   * Reads the group id
   *
   * @param cmdLine           the command line
   * @param useLastAsArtifact whether the last directory shall be used as group id
   * @return the group id
   *
   * @throws IOException
   */
  @NotNull
  public String readGroupId( @NotNull CmdLine cmdLine, boolean useLastAsArtifact ) throws IOException {
    return readGroupId( cmdLine, useLastAsArtifact, MavenMessageHandler.get( "select.group.id" ), false );
  }

  /**
   * Reads the grou pid
   *
   * @param cmdLine               the command line
   * @param lastElementIsArtefact whether the last directory shall be treated as artefact
   * @param message               the message
   * @param verify                whether the value shall be verified
   * @return the group id
   *
   * @throws IOException
   */
  @NotNull
  protected String readGroupId( @NotNull CmdLine cmdLine, boolean lastElementIsArtefact, @NotNull String message, boolean verify ) throws IOException {
    File current = new File( "." );
    String defaultValue = null;
    if ( directorySupport.isInProjectRoot( current ) ) {
      String subPath = directorySupport.getProjectSubPath( current );
      defaultValue = subPath.replace( '/', '.' );

      if ( lastElementIsArtefact ) {
        if ( defaultValue.indexOf( '.' ) < 0 ) {
          throw new IllegalStateException( "Uuups, no point found " + defaultValue );
        }
        int lastIndex = defaultValue.lastIndexOf( '.' );
        defaultValue = defaultValue.substring( 0, lastIndex );
      }
    }

    //Without verification
    if ( !verify ) {
      return cmdLine.read( message, defaultValue );
    }

    //With verification
    while ( true ) {
      String value = cmdLine.read( message, defaultValue );
      try {
        getDirectory( value );
      } catch ( FileNotFoundException e ) {
        cmdLine.warning( MavenMessageHandler.get( "invalid.groupid.file", e.getMessage() ) );
        continue;
      }
      //exists?
      return value;
    }
  }

  /**
   * Sets the archetypes
   *
   * @param archetypes the archetypes
   */
  public void setArchetypes( @NotNull List archetypes ) {
    this.archetypes.clear();
    this.archetypes.addAll( archetypes );
  }

  /**
   * Returns the archetypes
   *
   * @return the archetypes
   */
  @NotNull
  public List getArchetypes() {
    return Collections.unmodifiableList( archetypes );
  }

  /**
   * Adds an archetype
   *
   * @param archetype the archetype
   */
  public void addArchetype( @NotNull Archetype archetype ) {
    this.archetypes.add( archetype );
  }

  /**
   * Ensures that the pom in the current directory exists
   *
   * @param currentDir the directory
   */
  public void ensurePomExists( @NotNull File currentDir ) {
    if ( !doesPomExist( currentDir ) ) {
      throw new IllegalStateException( "No pom.xml found in directory " + currentDir.getAbsolutePath() );
    }
  }

  public boolean doesPomExist( @NotNull File currentDir ) {
    return currentDir.listFiles( POM_FILENAME_FILTER ).length == 1;
  }

  /**
   * Returns the maven bin
   *
   * @return the maven bin
   */
  @NotNull
  @NonNls
  public String getMavenBin() {
    return mavenBin;
  }

  /**
   * Returns the source dir. This method creates the directory if it does not exist yet.
   *
   * @param projectDir the project dir
   * @return the source dir
   */
  @NotNull
  public File getSourceDir( @NotNull File projectDir ) {
    ensurePomExists( projectDir );
    File srcDir = new File( projectDir, SRC_DIR_NAME );
    if ( !srcDir.exists() ) {
      srcDir.mkdir();
    }
    return srcDir;
  }

  /**
   * Returns the site source dir
   *
   * @param projectDir the project dir
   * @return the site source dir ("src/site")
   */
  @NotNull
  public File getSiteDir( @NotNull File projectDir ) {
    File siteDir = new File( getSourceDir( projectDir ), SITE_DIR_NAME );
    if ( !siteDir.exists() ) {
      siteDir.mkdir();
    }
    return siteDir;
  }

  @NotNull
  public File getSiteXml( @NotNull File projectDir ) {
    return new File( getSiteDir( projectDir ), SITE_XML_NAME );
  }

  /**
   * An archetype
   */
  public static class Archetype {
    private final String descriptionKey;
    private final String groupId;
    private final String artifactId;
    private final String version;

    public Archetype( @NonNls @NotNull String groupId, @NonNls @NotNull String artifactId, @NonNls @NotNull String descriptionKey ) {
      this( groupId, artifactId, descriptionKey, null );
    }

    public Archetype( @NonNls @NotNull String groupId, @NonNls @NotNull String artifactId, @NonNls @NotNull String descriptionKey, @NonNls @Nullable String version ) {
      this.version = version;
      this.groupId = groupId;
      this.artifactId = artifactId;
      this.descriptionKey = descriptionKey;
    }

    @NotNull
    @NonNls
    public String getDescriptionKey() {
      return descriptionKey;
    }

    @Nullable
    public String getVersion() {
      return version;
    }

    @NotNull
    public String getGroupId() {
      return groupId;
    }

    @NotNull
    public String getArtifactId() {
      return artifactId;
    }

    @Override
    @NonNls
    public String toString() {
      return "Archetype: " + descriptionKey;
    }
  }

  public static class ArchetypePresenter implements Renderer {
    @NotNull
    public String render( @NotNull Archetype object, @Nullable Object context ) {
      return " (" + MavenMessageHandler.get( object.getDescriptionKey() ) + ')' + "\t" + object.getGroupId() + ':' + object.getArtifactId();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy