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 | 33 | 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 | 27 | doDependencyResolution( executedProject, localRepo ); |
52 | |
} |
53 | 0 | catch ( Exception e ) |
54 | |
{ |
55 | 0 | throw new MojoExecutionException( "Unable to build project dependencies.", e ); |
56 | 27 | } |
57 | |
|
58 | 27 | rewriteWorkspace(); |
59 | 27 | } |
60 | |
|
61 | |
public void rewriteWorkspace() |
62 | |
throws MojoExecutionException |
63 | |
{ |
64 | 33 | File workspaceFile = new File( executedProject.getBasedir(), executedProject.getArtifactId() + ".iws" ); |
65 | |
|
66 | |
try |
67 | |
{ |
68 | 33 | Document document = readXmlDocument( workspaceFile, "workspace.xml" ); |
69 | |
|
70 | 33 | Element module = document.getRootElement(); |
71 | |
|
72 | 33 | setProjectScmType( module ); |
73 | |
|
74 | 33 | 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 | 33 | } |
85 | 33 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private void setProjectScmType( Element content ) |
91 | |
{ |
92 | 33 | String scmType = getScmType(); |
93 | |
|
94 | 33 | if ( scmType != null ) |
95 | |
{ |
96 | 24 | Element component = findComponent( content, "VcsManagerConfiguration" ); |
97 | |
|
98 | 24 | Element element = findElement( component, "option", "ACTIVE_VCS_NAME" ); |
99 | |
|
100 | 24 | element.addAttribute( "value", scmType ); |
101 | |
} |
102 | 33 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
protected String getScmType() |
110 | |
{ |
111 | |
String scmType; |
112 | |
|
113 | 33 | if ( executedProject.getScm() == null ) |
114 | |
{ |
115 | 9 | return null; |
116 | |
} |
117 | 24 | scmType = getScmType( executedProject.getScm().getConnection() ); |
118 | |
|
119 | 24 | if ( scmType != null ) |
120 | |
{ |
121 | 21 | return scmType; |
122 | |
} |
123 | 3 | scmType = getScmType( executedProject.getScm().getDeveloperConnection() ); |
124 | |
|
125 | 3 | return scmType; |
126 | |
} |
127 | |
|
128 | |
protected String getScmType( String connection ) |
129 | |
{ |
130 | |
String scmType; |
131 | |
|
132 | 27 | if ( connection != null ) |
133 | |
{ |
134 | 24 | if ( connection.length() > 0 ) |
135 | |
{ |
136 | 24 | int startIndex = connection.indexOf( ":" ); |
137 | |
|
138 | 24 | int endIndex = connection.indexOf( "|", startIndex + 1 ); |
139 | |
|
140 | 24 | if ( endIndex == -1 ) |
141 | |
{ |
142 | 18 | endIndex = connection.indexOf( ":", startIndex + 1 ); |
143 | |
} |
144 | |
|
145 | 24 | if ( startIndex < endIndex ) |
146 | |
{ |
147 | 24 | scmType = connection.substring( startIndex + 1, endIndex ); |
148 | |
|
149 | 24 | scmType = translateScmType( scmType ); |
150 | |
|
151 | 24 | return scmType; |
152 | |
} |
153 | |
} |
154 | |
} |
155 | 3 | return null; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
protected String translateScmType( String scmType ) |
163 | |
{ |
164 | 24 | if ( "cvs".equals( scmType ) ) |
165 | |
{ |
166 | 3 | return "CVS"; |
167 | |
} |
168 | 21 | else if ( "perforce".equals( scmType ) ) |
169 | |
{ |
170 | 3 | return "Perforce"; |
171 | |
} |
172 | 18 | else if ( "starteam".equals( scmType ) ) |
173 | |
{ |
174 | 3 | return "StarTeam"; |
175 | |
} |
176 | 15 | else if ( "vss".equals( scmType ) ) |
177 | |
{ |
178 | 3 | return "SourceSafe"; |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | 12 | return scmType; |
183 | |
} |
184 | |
} |
185 | |
} |