588
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.scm.ant.actions;
|
|
19 |
|
628
|
20 |
import java.io.BufferedWriter;
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.FileWriter;
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.text.ParseException;
|
|
25 |
import java.text.SimpleDateFormat;
|
588
|
26 |
import java.util.Date;
|
|
27 |
import java.util.Iterator;
|
|
28 |
|
628
|
29 |
import org.apache.maven.scm.ChangeSet;
|
588
|
30 |
import org.apache.maven.scm.ScmException;
|
|
31 |
import org.apache.maven.scm.ScmRevision;
|
|
32 |
import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
|
|
33 |
import org.apache.maven.scm.command.changelog.ChangeLogSet;
|
|
34 |
import org.apache.maven.scm.manager.ScmManager;
|
|
35 |
import org.apache.maven.scm.repository.ScmRepository;
|
|
36 |
import org.apache.tools.ant.BuildException;
|
628
|
37 |
import org.dom4j.Document;
|
|
38 |
import org.dom4j.DocumentException;
|
|
39 |
import org.dom4j.Element;
|
|
40 |
import org.dom4j.io.SAXReader;
|
588
|
41 |
|
|
42 |
/**
|
628
|
43 |
* Get change log within a date range i.e startDate and endDate; OR get change log of a number of
|
|
44 |
* days i.e numDays OR get change log within a range of starVersion and endVersion Parameters either
|
|
45 |
* startDate="20090317 18:49:31" endDate="20090318 24:49:31" datePattern="yyyyMMdd HH:mm:ss" Or
|
|
46 |
* numDays='1' Or startVersion="1" endVersion="2" Add logOutput="xml" to output log in xml format
|
588
|
47 |
*
|
|
48 |
* <pre>
|
|
49 |
* <hlm:scm verbose="false" scmUrl="scm:hg:${repo.dir}/changelog">
|
|
50 |
* <hlm:changelog basedir="${repo.dir}/changelog" startVersion="1" endVersion="2"/>
|
|
51 |
* <hlm:changelog baseDir="${repo.dir}/changelog" numDays='1' />
|
|
52 |
* </hlm:scm>
|
|
53 |
* </pre>
|
628
|
54 |
*
|
588
|
55 |
* @ant.type name="changelog" category="SCM"
|
|
56 |
*/
|
|
57 |
public class ChangelogAction extends BaseDirectoryScmAction {
|
|
58 |
|
|
59 |
private String startDate;
|
|
60 |
|
|
61 |
private String endDate;
|
|
62 |
|
|
63 |
private String datePattern;
|
|
64 |
|
|
65 |
private String logOutput;
|
628
|
66 |
|
588
|
67 |
private int numDays;
|
|
68 |
|
|
69 |
private String startVersion;
|
|
70 |
|
|
71 |
private String endVersion;
|
|
72 |
private File xmlbom;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Start version
|
|
76 |
*
|
|
77 |
* @ant.not-required
|
|
78 |
*/
|
|
79 |
public void setStartVersion(String startVersion) {
|
|
80 |
this.startVersion = startVersion;
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* End version
|
|
85 |
*
|
|
86 |
* @ant.not-required
|
|
87 |
*/
|
|
88 |
public void setEndVersion(String endVersion) {
|
|
89 |
this.endVersion = endVersion;
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Start date
|
|
94 |
*
|
|
95 |
* @ant.not-required
|
|
96 |
*/
|
|
97 |
public void setStartDate(String startDate) {
|
|
98 |
this.startDate = startDate;
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* End date
|
|
103 |
*
|
|
104 |
* @ant.not-required
|
|
105 |
*/
|
|
106 |
public void setEndDate(String endDate) {
|
|
107 |
this.endDate = endDate;
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Number of days
|
|
112 |
*
|
|
113 |
* @ant.not-required
|
|
114 |
*/
|
|
115 |
public void setNumDays(int numDays) {
|
|
116 |
this.numDays = numDays;
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Date pattern default is EEE MMM dd HH:mm:ss yyyy Z
|
|
121 |
*
|
|
122 |
* @ant.not-required
|
|
123 |
*/
|
|
124 |
public void setDatePattern(String datePattern) {
|
|
125 |
this.datePattern = datePattern;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Output pattern default is log output, to get xml output set logoutput=xml
|
|
130 |
*
|
|
131 |
* @ant.not-required
|
628
|
132 |
*/
|
|
133 |
public void setLogOutput(String logOutput) {
|
588
|
134 |
this.logOutput = logOutput;
|
|
135 |
}
|
628
|
136 |
|
588
|
137 |
/**
|
|
138 |
* File for xml output of changeset list
|
|
139 |
*
|
|
140 |
* @ant.not-required
|
628
|
141 |
*/
|
|
142 |
public void setXmlbom(File xmlbom) {
|
588
|
143 |
this.xmlbom = xmlbom;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* {@inheritDoc}
|
|
148 |
*/
|
|
149 |
@SuppressWarnings("unchecked")
|
|
150 |
@Override
|
628
|
151 |
public void execute(ScmRepository repository) throws ScmException {
|
588
|
152 |
ScmManager scmManager = getTask().getScmManager();
|
|
153 |
ScmRevision startRevision = new ScmRevision(startVersion);
|
|
154 |
ScmRevision endRevision = new ScmRevision(endVersion);
|
|
155 |
|
|
156 |
ChangeLogScmResult result;
|
|
157 |
Date start = null;
|
|
158 |
Date end = null;
|
|
159 |
|
628
|
160 |
if (numDays == 0 && (startDate != null || endDate != null)) {
|
588
|
161 |
try {
|
|
162 |
SimpleDateFormat format = new SimpleDateFormat(datePattern);
|
|
163 |
start = format.parse(startDate);
|
|
164 |
end = format.parse(endDate);
|
628
|
165 |
}
|
|
166 |
catch (ParseException e) {
|
588
|
167 |
throw new ScmException("Date Format not supported:" + e.getMessage());
|
|
168 |
}
|
|
169 |
}
|
628
|
170 |
else {
|
588
|
171 |
start = null;
|
|
172 |
end = null;
|
|
173 |
}
|
628
|
174 |
try {
|
|
175 |
if (startVersion == null) {
|
|
176 |
result = scmManager.changeLog(repository, getScmFileSet(), start, end, numDays, null, datePattern);
|
|
177 |
} else {
|
|
178 |
result = scmManager.changeLog(repository, getScmFileSet(), startRevision, endRevision, datePattern);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
catch (ScmException e) {
|
588
|
182 |
throw new BuildException("Execution of SCM changelog action failed.");
|
|
183 |
}
|
|
184 |
if (!result.isSuccess()) {
|
|
185 |
throw new BuildException("SCM changelog command unsuccessful.");
|
|
186 |
}
|
|
187 |
// Output changelog information
|
|
188 |
ChangeLogSet changelogSet = result.getChangeLog();
|
628
|
189 |
if (logOutput != null && logOutput.equals("xml")) {
|
588
|
190 |
getTask().log(changelogSet.toXML());
|
|
191 |
}
|
628
|
192 |
else if (xmlbom != null) {
|
588
|
193 |
String output = "";
|
628
|
194 |
for (Object object : changelogSet.getChangeSets()) {
|
588
|
195 |
String revision = "";
|
628
|
196 |
ChangeSet changeSet = (ChangeSet) object;
|
|
197 |
for (String line : changeSet.toString().split("\n")) {
|
|
198 |
if (line.contains("revision:")) {
|
588
|
199 |
revision = line.replace("revision:", "");
|
628
|
200 |
}
|
588
|
201 |
}
|
628
|
202 |
|
|
203 |
output = output + "<task><id>" + revision + "</id><synopsis>"
|
|
204 |
+ changeSet.getComment() + "</synopsis><completed>"
|
|
205 |
+ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(changeSet.getDate())
|
|
206 |
+ "</completed></task>";
|
588
|
207 |
}
|
|
208 |
String[] path = getTask().getScmUrl().split("/");
|
|
209 |
String xml = "<bom><build>untitled</build><content>\n";
|
628
|
210 |
String thisproject = "<project>" + "<name>" + path[path.length - 1] + "</name>"
|
|
211 |
+ "<baseline>" + getTask().getScmUrl() + "</baseline>"
|
|
212 |
+ "<database>mercurial</database>" + output + "</project>\n";
|
588
|
213 |
xml = xml + thisproject;
|
|
214 |
try {
|
628
|
215 |
if (xmlbom.exists()) {
|
588
|
216 |
SAXReader xmlReader = new SAXReader();
|
|
217 |
Document antDoc = xmlReader.read(xmlbom);
|
628
|
218 |
for (Iterator iterator = antDoc.selectNodes("//project").iterator(); iterator.hasNext();) {
|
588
|
219 |
boolean equal = false;
|
|
220 |
Element element = (Element) iterator.next();
|
628
|
221 |
for (Iterator iterator2 = antDoc.selectNodes("//baseline").iterator(); iterator2.hasNext();) {
|
|
222 |
Element e2 = (Element) iterator2.next();
|
|
223 |
if (e2.getText().equals(getTask().getScmUrl())) {
|
|
224 |
equal = true;
|
|
225 |
}
|
588
|
226 |
}
|
628
|
227 |
if (!equal) {
|
588
|
228 |
xml = xml + element.asXML() + "\n";
|
628
|
229 |
}
|
588
|
230 |
}
|
|
231 |
}
|
|
232 |
xml = xml + "</content></bom>";
|
628
|
233 |
|
588
|
234 |
FileWriter fstream = new FileWriter(xmlbom);
|
|
235 |
BufferedWriter out = new BufferedWriter(fstream);
|
|
236 |
out.write(xml);
|
|
237 |
out.close();
|
628
|
238 |
} catch (DocumentException e) {
|
|
239 |
e.printStackTrace();
|
|
240 |
} catch (IOException e) {
|
|
241 |
e.printStackTrace();
|
|
242 |
}
|
588
|
243 |
}
|
628
|
244 |
else {
|
588
|
245 |
Iterator iterator = changelogSet.getChangeSets().iterator();
|
628
|
246 |
while (iterator.hasNext()) {
|
588
|
247 |
getTask().log(iterator.next().toString());
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|