Coverage Report - org.apache.maven.plugin.idea.IdeaWorkspaceMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
IdeaWorkspaceMojo
88%
45/51
91%
20/22
5
 
 1  
 package org.apache.maven.plugin.idea;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.plugin.MojoExecutionException;
 23  
 import org.dom4j.Element;
 24  
 import org.dom4j.Document;
 25  
 import org.dom4j.DocumentException;
 26  
 
 27  
 import java.io.File;
 28  
 import java.io.IOException;
 29  
 
 30  
 /**
 31  
  * Creates the workspace file (*.iws) for IntelliJ IDEA.
 32  
  *
 33  
  * @author Edwin Punzalan
 34  
  * @goal workspace
 35  
  * @execute phase="generate-sources"
 36  
  */
 37  22
 public class IdeaWorkspaceMojo
 38  
     extends AbstractIdeaMojo
 39  
 {
 40  
     /**
 41  
      * Create IDEA workspace (.iws) file.
 42  
      *
 43  
      * @throws org.apache.maven.plugin.MojoExecutionException
 44  
      *
 45  
      */
 46  
     public void execute()
 47  
         throws MojoExecutionException
 48  
     {
 49  
         try
 50  
         {
 51  18
             doDependencyResolution( executedProject, localRepo );
 52  
         }
 53  0
         catch ( Exception e )
 54  
         {
 55  0
             throw new MojoExecutionException( "Unable to build project dependencies.", e );
 56  18
         }
 57  
 
 58  18
         rewriteWorkspace();
 59  18
     }
 60  
 
 61  
     public void rewriteWorkspace()
 62  
         throws MojoExecutionException
 63  
     {
 64  22
         File workspaceFile = new File( executedProject.getBasedir(), executedProject.getArtifactId() + ".iws" );
 65  
 
 66  
         try
 67  
         {
 68  22
             Document document = readXmlDocument( workspaceFile, "workspace.xml" );
 69  
 
 70  22
             Element module = document.getRootElement();
 71  
 
 72  22
             setProjectScmType( module );
 73  
 
 74  22
             writeXmlDocument( workspaceFile, document );
 75  
         }
 76  0
         catch ( DocumentException e )
 77  
         {
 78  0
             throw new MojoExecutionException( "Error parsing existing IWS file: " + workspaceFile.getAbsolutePath(),
 79  
                                               e );
 80  
         }
 81  0
         catch ( IOException e )
 82  
         {
 83  0
             throw new MojoExecutionException( "Unable to create workspace file", e );
 84  22
         }
 85  22
     }
 86  
 
 87  
     /**
 88  
      * Sets the SCM type of the project
 89  
      */
 90  
     private void setProjectScmType( Element content )
 91  
     {
 92  22
         String scmType = getScmType();
 93  
 
 94  22
         if ( scmType != null )
 95  
         {
 96  16
             Element component = findComponent( content, "VcsManagerConfiguration" );
 97  
 
 98  16
             Element element = findElement( component, "option", "ACTIVE_VCS_NAME" );
 99  
 
 100  16
             element.addAttribute( "value", scmType );
 101  
         }
 102  22
     }
 103  
 
 104  
     /**
 105  
      * used to retrieve the SCM Type
 106  
      *
 107  
      * @return the Scm Type string used to connect to the SCM
 108  
      */
 109  
     protected String getScmType()
 110  
     {
 111  
         String scmType;
 112  
 
 113  22
         if ( executedProject.getScm() == null )
 114  
         {
 115  6
             return null;
 116  
         }
 117  16
         scmType = getScmType( executedProject.getScm().getConnection() );
 118  
 
 119  16
         if ( scmType != null )
 120  
         {
 121  14
             return scmType;
 122  
         }
 123  2
         scmType = getScmType( executedProject.getScm().getDeveloperConnection() );
 124  
 
 125  2
         return scmType;
 126  
     }
 127  
 
 128  
     protected String getScmType( String connection )
 129  
     {
 130  
         String scmType;
 131  
 
 132  18
         if ( connection != null )
 133  
         {
 134  16
             if ( connection.length() > 0 )
 135  
             {
 136  16
                 int startIndex = connection.indexOf( ":" );
 137  
 
 138  16
                 int endIndex = connection.indexOf( "|", startIndex + 1 );
 139  
 
 140  16
                 if ( endIndex == -1 )
 141  
                 {
 142  12
                     endIndex = connection.indexOf( ":", startIndex + 1 );
 143  
                 }
 144  
 
 145  16
                 if ( startIndex < endIndex )
 146  
                 {
 147  16
                     scmType = connection.substring( startIndex + 1, endIndex );
 148  
 
 149  16
                     scmType = translateScmType( scmType );
 150  
 
 151  16
                     return scmType;
 152  
                 }
 153  
             }
 154  
         }
 155  2
         return null;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Translate the SCM type from the SCM connection URL to the format used by
 160  
      * IDEA as the value for ACTIVE_VCS_NAME.
 161  
      */
 162  
     protected String translateScmType( String scmType )
 163  
     {
 164  16
         if ( "cvs".equals( scmType ) )
 165  
         {
 166  2
             return "CVS";
 167  
         }
 168  14
         else if ( "perforce".equals( scmType ) )
 169  
         {
 170  2
             return "Perforce";
 171  
         }
 172  12
         else if ( "starteam".equals( scmType ) )
 173  
         {
 174  2
             return "StarTeam";
 175  
         }
 176  10
         else if ( "vss".equals( scmType ) )
 177  
         {
 178  2
             return "SourceSafe";
 179  
         }
 180  
         else
 181  
         {
 182  8
             return scmType;
 183  
         }
 184  
     }
 185  
 }