1 | |
package org.apache.maven.plugin.changes; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.maven.plugin.MojoExecutionException; |
28 | |
import org.apache.maven.plugin.logging.Log; |
29 | |
import org.apache.maven.plugins.changes.model.Action; |
30 | |
import org.apache.maven.plugins.changes.model.Release; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ReleaseUtils |
40 | |
{ |
41 | |
private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT"; |
42 | |
|
43 | |
private Log log; |
44 | |
|
45 | |
public ReleaseUtils( Log log ) |
46 | 2 | { |
47 | 2 | this.log = log; |
48 | 2 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public Release getLatestRelease( List<Release> releases, String pomVersion ) |
60 | |
throws MojoExecutionException |
61 | |
{ |
62 | |
|
63 | 1 | if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) ) |
64 | |
{ |
65 | 0 | pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() ); |
66 | |
} |
67 | 1 | getLog().debug( "Found " + releases.size() + " releases." ); |
68 | |
|
69 | 1 | Release release = getRelease( releases, pomVersion ); |
70 | |
|
71 | 1 | if ( release == null ) |
72 | |
{ |
73 | 0 | throw new MojoExecutionException( "Couldn't find the release '" + pomVersion |
74 | |
+ "' among the supplied releases." ); |
75 | |
} |
76 | |
|
77 | 1 | return release; |
78 | |
} |
79 | |
|
80 | |
private Log getLog() |
81 | |
{ |
82 | 20 | return log; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
protected Release getRelease( List<Release> releases, String version ) |
93 | |
{ |
94 | 11 | for ( Release release : releases ) |
95 | |
{ |
96 | 14 | if ( getLog().isDebugEnabled() ) |
97 | |
{ |
98 | 0 | getLog().debug( "The release: " + release.getVersion() |
99 | |
+ " has " + release.getActions().size() + " actions." ); |
100 | |
} |
101 | |
|
102 | 14 | if ( release.getVersion() != null && release.getVersion().equals( version ) ) |
103 | |
{ |
104 | 5 | if ( getLog().isDebugEnabled() ) |
105 | |
{ |
106 | 0 | getLog().debug( "Found the correct release: " + release.getVersion() ); |
107 | 0 | logRelease( release ); |
108 | |
} |
109 | 5 | return release; |
110 | |
} |
111 | |
} |
112 | 6 | return null; |
113 | |
} |
114 | |
|
115 | |
protected void logRelease( Release release ) |
116 | |
{ |
117 | |
Action action; |
118 | 0 | for ( Iterator iterator = release.getActions().iterator(); iterator.hasNext(); ) |
119 | |
{ |
120 | 0 | action = (Action) iterator.next(); |
121 | 0 | getLog().debug( "o " + action.getType() ); |
122 | 0 | getLog().debug( "issue : " + action.getIssue() ); |
123 | 0 | getLog().debug( "action : " + action.getAction() ); |
124 | 0 | getLog().debug( "dueTo : " + action.getDueTo() ); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public List<Release> mergeReleases( final List<Release> firstReleases, final List<Release> secondReleases ) |
138 | |
{ |
139 | 6 | if ( firstReleases == null && secondReleases == null ) |
140 | |
{ |
141 | 0 | return Collections.emptyList(); |
142 | |
} |
143 | 6 | if ( firstReleases == null ) |
144 | |
{ |
145 | 1 | return secondReleases; |
146 | |
} |
147 | 5 | if ( secondReleases == null ) |
148 | |
{ |
149 | 0 | return firstReleases; |
150 | |
} |
151 | |
|
152 | 5 | List<Release> mergedReleases = new ArrayList<Release>(); |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | 5 | for ( Release firstRelease : firstReleases ) |
158 | |
{ |
159 | 6 | Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() ); |
160 | 6 | if ( secondRelease != null ) |
161 | |
{ |
162 | 2 | if ( secondRelease.getActions() != null ) |
163 | |
{ |
164 | 2 | firstRelease.getActions().addAll( secondRelease.getActions() ); |
165 | |
} |
166 | |
} |
167 | 6 | mergedReleases.add( firstRelease ); |
168 | 6 | } |
169 | |
|
170 | |
|
171 | 5 | for ( Release secondRelease : secondReleases ) |
172 | |
{ |
173 | 4 | Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() ); |
174 | 4 | if ( mergedRelease == null ) |
175 | |
{ |
176 | 2 | mergedReleases.add( secondRelease ); |
177 | |
} |
178 | 4 | } |
179 | 5 | return mergedReleases; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public List<Release> convertReleaseList( List changesReleases ) { |
191 | 1 | List<Release> releases = new ArrayList<Release>(); |
192 | |
|
193 | |
|
194 | |
|
195 | 1 | for ( Iterator iterator = changesReleases.iterator(); iterator.hasNext(); ) |
196 | |
{ |
197 | 2 | Release release = (Release) iterator.next(); |
198 | 2 | releases.add( release ); |
199 | 2 | } |
200 | 1 | return releases; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public List mergeReleases( final List releases, final String componentName, final List componentReleases ) { |
215 | 0 | if ( releases == null && componentReleases == null ) |
216 | |
{ |
217 | 0 | return Collections.EMPTY_LIST; |
218 | |
} |
219 | 0 | if ( componentReleases == null ) |
220 | |
{ |
221 | 0 | return releases; |
222 | |
} |
223 | |
|
224 | 0 | final List mergedReleases = new ArrayList(); |
225 | |
|
226 | 0 | if ( releases != null ) |
227 | |
{ |
228 | 0 | for ( Iterator iterator = releases.iterator(); iterator.hasNext(); ) |
229 | |
{ |
230 | 0 | final Release release = (Release) iterator.next(); |
231 | 0 | final Release componentRelease = getRelease( componentReleases, release.getVersion() ); |
232 | 0 | if ( componentRelease != null ) { |
233 | 0 | release.addComponent( componentName, componentRelease ); |
234 | |
} |
235 | 0 | mergedReleases.add( release ); |
236 | 0 | } |
237 | |
} |
238 | |
|
239 | 0 | for ( Iterator iterator = componentReleases.iterator(); iterator.hasNext(); ) |
240 | |
{ |
241 | 0 | final Release release = (Release) iterator.next(); |
242 | 0 | final Release mergedRelease = getRelease( mergedReleases, release.getVersion() ); |
243 | 0 | if ( mergedRelease == null ) |
244 | |
{ |
245 | 0 | final Release componentRelease = new Release(); |
246 | 0 | componentRelease.setVersion( release.getVersion() ); |
247 | 0 | componentRelease.setDateRelease( release.getDateRelease() ); |
248 | 0 | componentRelease.addComponent( componentName, release ); |
249 | 0 | mergedReleases.add( componentRelease ); |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | 0 | return mergedReleases; |
254 | |
} |
255 | |
} |