1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
package fr.paris.lutece.plugins.releaser.service; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.IOException; |
38 | |
import java.io.PrintWriter; |
39 | |
import java.io.StringWriter; |
40 | |
|
41 | |
import org.apache.commons.lang.ObjectUtils; |
42 | |
import org.apache.commons.lang.StringUtils; |
43 | |
import org.tmatesoft.svn.core.SVNCancelException; |
44 | |
import org.tmatesoft.svn.core.SVNException; |
45 | |
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; |
46 | |
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; |
47 | |
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory; |
48 | |
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; |
49 | |
import org.tmatesoft.svn.core.wc.ISVNEventHandler; |
50 | |
import org.tmatesoft.svn.core.wc.SVNCommitPacket; |
51 | |
import org.tmatesoft.svn.core.wc.SVNEvent; |
52 | |
import org.tmatesoft.svn.core.wc.SVNEventAction; |
53 | |
import org.tmatesoft.svn.core.wc.SVNWCUtil; |
54 | |
|
55 | |
import fr.paris.lutece.plugins.releaser.business.Component; |
56 | |
import fr.paris.lutece.plugins.releaser.business.Site; |
57 | |
import fr.paris.lutece.plugins.releaser.util.CommandResult; |
58 | |
import fr.paris.lutece.plugins.releaser.util.ConstanteUtils; |
59 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
60 | |
import fr.paris.lutece.plugins.releaser.util.pom.PomUpdater; |
61 | |
import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCheckoutClient; |
62 | |
import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCommitClient; |
63 | |
import fr.paris.lutece.plugins.releaser.util.svn.ReleaseSvnCopyClient; |
64 | |
import fr.paris.lutece.plugins.releaser.util.svn.SvnUtils; |
65 | |
import fr.paris.lutece.plugins.releaser.util.svn.SvnUser; |
66 | |
import fr.paris.lutece.portal.service.spring.SpringContextService; |
67 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
68 | |
|
69 | |
|
70 | |
public class SvnService implements ISvnService |
71 | |
{ |
72 | |
|
73 | |
private static ISvnService _instance; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
private SvnService( ) |
86 | 1 | { |
87 | |
|
88 | 1 | } |
89 | |
|
90 | |
|
91 | |
public static ISvnService getService() |
92 | |
{ |
93 | 0 | if(_instance==null) |
94 | |
{ |
95 | 0 | _instance=SpringContextService.getBean(ConstanteUtils.BEAN_SVN_SERVICE ); |
96 | 0 | _instance.init( ); |
97 | |
} |
98 | |
|
99 | 0 | return _instance; |
100 | |
|
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void init( ) |
107 | |
{ |
108 | |
|
109 | |
|
110 | |
|
111 | 0 | DAVRepositoryFactory.setup( ); |
112 | |
|
113 | |
|
114 | |
|
115 | 0 | SVNRepositoryFactoryImpl.setup( ); |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | 0 | FSRepositoryFactory.setup( ); |
121 | 0 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
public String doSvnCheckoutSite( Site site, String strSvnLogin,String strSvnPassword, CommandResult commandResult) |
126 | |
{ |
127 | 0 | ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( strSvnLogin, |
128 | |
strSvnPassword ); |
129 | |
|
130 | 0 | String strSiteLocalBasePath = ReleaserUtils.getLocalSitePath( site.getArtifactId( ) ); |
131 | |
|
132 | 0 | if ( StringUtils.isNotBlank( strSiteLocalBasePath ) && ( strSvnLogin != null ) ) |
133 | |
{ |
134 | 0 | ReleaseSvnCheckoutClient updateClient = new ReleaseSvnCheckoutClient( authManager, |
135 | 0 | SVNWCUtil.createDefaultOptions( false ) ); |
136 | |
|
137 | 0 | String strError = null; |
138 | |
|
139 | |
try |
140 | |
{ |
141 | 0 | strError = SvnUtils.doSvnCheckout( site.getScmUrl( ), strSiteLocalBasePath, updateClient, |
142 | |
commandResult ); |
143 | |
} |
144 | 0 | catch ( Exception e ) |
145 | |
{ |
146 | 0 | ReleaserUtils.addTechnicalError(commandResult,"errreur lors du checkout du site "+ e.getMessage(),e); |
147 | 0 | } |
148 | |
} |
149 | |
|
150 | |
|
151 | 0 | return ConstanteUtils.CONSTANTE_EMPTY_STRING; |
152 | |
} |
153 | |
|
154 | |
public String doSvnCheckoutComponent( Component component, String strSvnLogin,String strSvnPassword, CommandResult commandResult) |
155 | |
{ |
156 | 0 | ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( strSvnLogin, |
157 | |
strSvnPassword ); |
158 | |
|
159 | 0 | String strLocalBasePath = ReleaserUtils.getLocalComponentPath(component.getArtifactId( ) ); |
160 | |
|
161 | 0 | if ( StringUtils.isNotBlank( strLocalBasePath ) && ( strSvnLogin != null ) ) |
162 | |
{ |
163 | 0 | ReleaseSvnCheckoutClient updateClient = new ReleaseSvnCheckoutClient( authManager, |
164 | 0 | SVNWCUtil.createDefaultOptions( false ) ); |
165 | |
|
166 | 0 | String strError = null; |
167 | |
|
168 | |
try |
169 | |
{ |
170 | 0 | strError = SvnUtils.doSvnCheckout( SvnUtils.getRepoUrl( component.getScmDeveloperConnection( )), strLocalBasePath, updateClient, |
171 | |
commandResult ); |
172 | |
} |
173 | 0 | catch ( Exception e ) |
174 | |
{ |
175 | 0 | ReleaserUtils.addTechnicalError(commandResult,"errreur lors du checkout du composant"+ e.getMessage(),e); |
176 | 0 | } |
177 | |
} |
178 | |
|
179 | |
|
180 | 0 | return ConstanteUtils.CONSTANTE_EMPTY_STRING; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
public String doReleaseSite( Site site, String strSvnLogin,String strSvnPassword, CommandResult commandResult ) |
187 | |
{ |
188 | |
|
189 | 0 | String strSitePomLocalBasePath = ReleaserUtils.getLocalSitePomPath( site.getArtifactId( ) ); |
190 | |
|
191 | 0 | ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(strSvnLogin, |
192 | |
strSvnPassword); |
193 | |
|
194 | 0 | String strSrcURL = site.getScmUrl( ); |
195 | 0 | String strDstURL = SvnUtils.getSvnUrlTagSite( site.getScmUrl( ), ReleaserUtils.getSiteTagName( site ) ); |
196 | |
|
197 | 0 | ReleaseSvnCommitClient commitClient = new ReleaseSvnCommitClient( authManager, |
198 | 0 | SVNWCUtil.createDefaultOptions( false ) ); |
199 | 0 | ReleaseSvnCopyClient copyClient = new ReleaseSvnCopyClient( authManager, SVNWCUtil.createDefaultOptions( false ) ); |
200 | |
|
201 | |
try |
202 | |
{ |
203 | 0 | final StringBuffer sbLog = commandResult.getLog( ); |
204 | |
|
205 | |
|
206 | 0 | copyClient.setEventHandler( new ISVNEventHandler( ) |
207 | 0 | { |
208 | |
public void checkCancelled( ) throws SVNCancelException |
209 | |
{ |
210 | |
|
211 | 0 | } |
212 | |
|
213 | |
public void handleEvent( SVNEvent event, double progress ) |
214 | |
throws SVNException |
215 | |
{ |
216 | 0 | sbLog.append( ( ( event.getAction( ) == SVNEventAction.COPY ) ? "Tag " : event.getAction( ) ) + |
217 | 0 | " " + ObjectUtils.toString( event.getFile( ) ) + "\n" ); |
218 | 0 | } |
219 | |
} ); |
220 | |
|
221 | 0 | sbLog.append( "Preparing release site\n" ); |
222 | 0 | sbLog.append( "Updating pom version to " + site.getNextReleaseVersion( ) + "...\n" ); |
223 | 0 | sbLog.append( "Updating dependency version ...\n" ); |
224 | 0 | PomUpdater.updateSiteBeforeTag( site ); |
225 | |
|
226 | 0 | SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update pom before tag", commitClient ); |
227 | |
|
228 | 0 | commandResult.setProgressValue( commandResult.getProgressValue( ) + 30 ); |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | 0 | sbLog.append( "Pom updated\n" ); |
234 | |
|
235 | 0 | if(!site.isTheme( ) ) |
236 | |
{ |
237 | |
|
238 | 0 | sbLog.append( "Tagging site to " + site.getNextReleaseVersion( ) + "...\n" ); |
239 | |
|
240 | 0 | String strErrorDuringTag=SvnUtils.doTagSite( site.getArtifactId( ), site.getArtifactId( )+"-"+site.getNextReleaseVersion( ), strSrcURL, strDstURL, copyClient ); |
241 | |
|
242 | |
|
243 | 0 | commandResult.setProgressValue( commandResult.getProgressValue( ) + 20 ); |
244 | |
|
245 | |
|
246 | 0 | if(StringUtils.isEmpty(strErrorDuringTag)) |
247 | |
{ |
248 | 0 | sbLog.append( "Tag done\n" ); |
249 | |
|
250 | 0 | sbLog.append( "Updating pom to next development " + site.getNextSnapshotVersion( ) + "\n" ); |
251 | |
|
252 | 0 | PomUpdater.updateSiteAfterTag( site ); |
253 | |
|
254 | 0 | SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update Updating pom to next development ", commitClient ); |
255 | |
|
256 | 0 | sbLog.append( "Pom updated\n" ); |
257 | |
} |
258 | |
else |
259 | |
{ |
260 | 0 | ReleaserUtils.addTechnicalError(commandResult, strErrorDuringTag); |
261 | |
} |
262 | 0 | } |
263 | |
else |
264 | |
{ |
265 | 0 | sbLog.append( "Release prepare theme " + site.getNextReleaseVersion( ) + "...\n" ); |
266 | |
|
267 | 0 | MavenService.getService( ).mvnReleasePrepare( strSitePomLocalBasePath, site.getNextReleaseVersion( ), site.getArtifactId( )+"-"+site.getNextReleaseVersion( ), |
268 | 0 | site.getNextSnapshotVersion( ), strSvnLogin, strSvnPassword, commandResult ); |
269 | 0 | sbLog.append( "End Release prepare theme " + site.getNextReleaseVersion( ) + "...\n" ); |
270 | 0 | sbLog.append( "Release perform theme " + site.getNextReleaseVersion( ) + "...\n" ); |
271 | 0 | MavenService.getService( ).mvnReleasePerform( strSitePomLocalBasePath, strSvnLogin, strSvnPassword, commandResult ); |
272 | 0 | sbLog.append( "End Release perform theme " + site.getNextReleaseVersion( ) + "...\n" ); |
273 | 0 | sbLog.append( "Updating pom to next development " + site.getNextSnapshotVersion( ) + "\n" ); |
274 | |
|
275 | 0 | PomUpdater.updateSiteAfterTag( site ); |
276 | |
|
277 | 0 | SvnUtils.doCommit( strSitePomLocalBasePath, "[site-release] update Updating pom to next development ", commitClient ); |
278 | |
|
279 | 0 | sbLog.append( "Pom updated\n" ); |
280 | |
|
281 | |
} |
282 | |
|
283 | |
} |
284 | 0 | catch ( Exception e ) |
285 | |
{ |
286 | |
|
287 | 0 | StringWriter sw = new StringWriter( ); |
288 | 0 | PrintWriter pw = new PrintWriter( sw ); |
289 | 0 | e.printStackTrace( pw ); |
290 | |
|
291 | 0 | String errorLog = sw.toString( ); |
292 | 0 | pw.flush( ); |
293 | 0 | pw.close( ); |
294 | |
|
295 | |
try |
296 | |
{ |
297 | 0 | sw.flush( ); |
298 | 0 | sw.close( ); |
299 | |
} |
300 | 0 | catch ( IOException e1 ) |
301 | |
{ |
302 | |
|
303 | 0 | AppLogService.error(e1); |
304 | 0 | } |
305 | |
|
306 | 0 | commandResult.getLog( ).append( errorLog ); |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | 0 | ReleaserUtils.addTechnicalError(commandResult, errorLog,e); |
314 | 0 | } |
315 | |
|
316 | 0 | return ConstanteUtils.CONSTANTE_EMPTY_STRING; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
} |