1   package org.apache.maven.plugin.idea;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
32  
33  
34  
35  
36  
37  public class IdeaWorkspaceMojo
38      extends AbstractIdeaMojo
39  {
40      
41  
42  
43  
44  
45  
46      public void execute()
47          throws MojoExecutionException
48      {
49          try
50          {
51              doDependencyResolution( executedProject, localRepo );
52          }
53          catch ( Exception e )
54          {
55              throw new MojoExecutionException( "Unable to build project dependencies.", e );
56          }
57  
58          rewriteWorkspace();
59      }
60  
61      public void rewriteWorkspace()
62          throws MojoExecutionException
63      {
64          File workspaceFile = new File( executedProject.getBasedir(), executedProject.getArtifactId() + ".iws" );
65  
66          try
67          {
68              Document document = readXmlDocument( workspaceFile, "workspace.xml" );
69  
70              Element module = document.getRootElement();
71  
72              setProjectScmType( module );
73  
74              writeXmlDocument( workspaceFile, document );
75          }
76          catch ( DocumentException e )
77          {
78              throw new MojoExecutionException( "Error parsing existing IWS file: " + workspaceFile.getAbsolutePath(),
79                                                e );
80          }
81          catch ( IOException e )
82          {
83              throw new MojoExecutionException( "Unable to create workspace file", e );
84          }
85      }
86  
87      
88  
89  
90      private void setProjectScmType( Element content )
91      {
92          String scmType = getScmType();
93  
94          if ( scmType != null )
95          {
96              Element component = findComponent( content, "VcsManagerConfiguration" );
97  
98              Element element = findElement( component, "option", "ACTIVE_VCS_NAME" );
99  
100             element.addAttribute( "value", scmType );
101         }
102     }
103 
104     
105 
106 
107 
108 
109     protected String getScmType()
110     {
111         String scmType;
112 
113         if ( executedProject.getScm() == null )
114         {
115             return null;
116         }
117         scmType = getScmType( executedProject.getScm().getConnection() );
118 
119         if ( scmType != null )
120         {
121             return scmType;
122         }
123         scmType = getScmType( executedProject.getScm().getDeveloperConnection() );
124 
125         return scmType;
126     }
127 
128     protected String getScmType( String connection )
129     {
130         String scmType;
131 
132         if ( connection != null )
133         {
134             if ( connection.length() > 0 )
135             {
136                 int startIndex = connection.indexOf( ":" );
137 
138                 int endIndex = connection.indexOf( "|", startIndex + 1 );
139 
140                 if ( endIndex == -1 )
141                 {
142                     endIndex = connection.indexOf( ":", startIndex + 1 );
143                 }
144 
145                 if ( startIndex < endIndex )
146                 {
147                     scmType = connection.substring( startIndex + 1, endIndex );
148 
149                     scmType = translateScmType( scmType );
150 
151                     return scmType;
152                 }
153             }
154         }
155         return null;
156     }
157 
158     
159 
160 
161 
162     protected String translateScmType( String scmType )
163     {
164         if ( "cvs".equals( scmType ) )
165         {
166             return "CVS";
167         }
168         else if ( "perforce".equals( scmType ) )
169         {
170             return "Perforce";
171         }
172         else if ( "starteam".equals( scmType ) )
173         {
174             return "StarTeam";
175         }
176         else if ( "vss".equals( scmType ) )
177         {
178             return "SourceSafe";
179         }
180         else
181         {
182             return scmType;
183         }
184     }
185 }