587
|
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.ant;
|
|
19 |
|
|
20 |
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.util.ArrayList;
|
|
24 |
import java.util.Enumeration;
|
|
25 |
import java.util.HashSet;
|
|
26 |
import java.util.Hashtable;
|
|
27 |
import java.util.Iterator;
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.Vector;
|
|
30 |
|
|
31 |
import org.apache.tools.ant.BuildException;
|
|
32 |
import org.apache.tools.ant.Project;
|
|
33 |
import org.apache.tools.ant.Target;
|
|
34 |
import org.dom4j.Comment;
|
|
35 |
import org.dom4j.Document;
|
|
36 |
import org.dom4j.Element;
|
|
37 |
import org.dom4j.Node;
|
|
38 |
import org.dom4j.Visitor;
|
|
39 |
import org.dom4j.VisitorSupport;
|
|
40 |
import org.dom4j.io.SAXReader;
|
|
41 |
import org.dom4j.DocumentException;
|
|
42 |
|
|
43 |
import com.nokia.helium.core.ant.types.HlmPostDefImpl;
|
|
44 |
|
|
45 |
import com.nokia.helium.ant.data.PropertyMeta;
|
|
46 |
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Class to store the status of the signal of a particular target.
|
|
50 |
*/
|
|
51 |
public class BuildStatusDef extends HlmPostDefImpl
|
|
52 |
{
|
|
53 |
private HashSet<String> output = new HashSet<String>();
|
|
54 |
|
|
55 |
|
|
56 |
public void execute(Project prj, String module, String[] targetNames)
|
|
57 |
{
|
|
58 |
//Run after targets execute so dynamic target names are resolved
|
|
59 |
for (int i = 0; i < targetNames.length; i++)
|
|
60 |
{
|
|
61 |
String[] array = { targetNames[i] };
|
|
62 |
Target target = findTarget(targetNames[i], getProject(), array);
|
|
63 |
targetCallsHeliumTarget(target, getProject());
|
|
64 |
}
|
|
65 |
checkTargetsProperties(getProject());
|
|
66 |
|
|
67 |
if (!output.isEmpty())
|
|
68 |
{
|
|
69 |
log("*** Configuration report ***", Project.MSG_INFO);
|
|
70 |
for (String x : output)
|
|
71 |
log(x, Project.MSG_INFO);
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* @param desiredTarget
|
|
77 |
* Target name to search
|
|
78 |
* @param project
|
|
79 |
* Object of the project
|
|
80 |
* @param targetNames
|
|
81 |
* Array of target names
|
|
82 |
*
|
|
83 |
*/
|
|
84 |
@SuppressWarnings("unchecked")
|
|
85 |
public Target findTarget(String desiredTarget, Project project, String[] targetNames)
|
|
86 |
{
|
|
87 |
Hashtable<String, Target> targets;
|
|
88 |
Vector<Target> sorted;
|
|
89 |
|
|
90 |
// get all targets of the current project
|
|
91 |
targets = project.getTargets();
|
|
92 |
|
|
93 |
// sort all targets of the current project
|
|
94 |
sorted = project.topoSort(targetNames[0], targets);
|
|
95 |
|
|
96 |
// Find the desiredTarget Target object
|
|
97 |
for (Target t : sorted)
|
|
98 |
{
|
|
99 |
if (t.getName().equals(desiredTarget))
|
|
100 |
{
|
|
101 |
return t;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
throw new BuildException("Could not find target matching " + desiredTarget + "\n");
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* If a target defined outside helium are calling a private Helium target then print warning
|
|
109 |
*
|
|
110 |
*/
|
|
111 |
@SuppressWarnings("unchecked")
|
|
112 |
public void targetCallsHeliumTarget(Target target, Project project)
|
|
113 |
{
|
|
114 |
String location = target.getLocation().getFileName();
|
|
115 |
|
|
116 |
try {
|
|
117 |
String heliumpath = new File(project.getProperty("helium.dir")).getCanonicalPath();
|
|
118 |
String targetpath = new File(location).getCanonicalPath();
|
|
119 |
|
|
120 |
if (!targetpath.contains(heliumpath))
|
|
121 |
{
|
|
122 |
ArrayList<String> antcallTargets = new ArrayList<String>();
|
|
123 |
Visitor visitorTarget = new AntTargetVisitor(antcallTargets, project);
|
|
124 |
|
|
125 |
Element element = findTargetElement(target, project);
|
|
126 |
if (element != null)
|
|
127 |
element.accept(visitorTarget);
|
|
128 |
for (String depTargetString : antcallTargets)
|
|
129 |
{
|
|
130 |
String[] array = { depTargetString };
|
|
131 |
try {
|
|
132 |
Target depTarget = findTarget(depTargetString, project, array);
|
|
133 |
targetCallsHeliumTarget(depTarget, project);
|
|
134 |
} catch (BuildException x) {
|
|
135 |
// We are Ignoring the errors as no need to fail the build.
|
|
136 |
log("Exception occured while target defined outside helium are calling a private Helium target " + x.toString(), Project.MSG_DEBUG);
|
|
137 |
x = null;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
|
|
142 |
for (Enumeration<String> e = target.getDependencies(); e.hasMoreElements();)
|
|
143 |
{
|
|
144 |
String depTargetString = e.nextElement();
|
|
145 |
String[] array = { depTargetString };
|
|
146 |
try {
|
|
147 |
Target depTarget = findTarget(depTargetString, project, array);
|
|
148 |
targetCallsHeliumTarget(depTarget, project);
|
|
149 |
} catch (BuildException x) {
|
|
150 |
//We are Ignoring the errors as no need to fail the build.
|
|
151 |
log("Exception occured while target defined outside helium are calling a private Helium target " + x.toString(), Project.MSG_DEBUG);
|
|
152 |
x = null;
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|
|
156 |
else
|
|
157 |
{
|
|
158 |
checkIfTargetPrivate(target, project);
|
|
159 |
}
|
|
160 |
|
|
161 |
} catch (IOException e) {
|
|
162 |
//We are Ignoring the errors as no need to fail the build.
|
|
163 |
log("IOException occured while target defined outside helium are calling a private Helium target " + e.getMessage(), Project.MSG_DEBUG);
|
|
164 |
e.printStackTrace();
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
private class AntTargetVisitor extends VisitorSupport
|
|
169 |
{
|
|
170 |
private List<String> targetList;
|
|
171 |
private Project project;
|
|
172 |
|
|
173 |
public AntTargetVisitor(List<String> targetList, Project project)
|
|
174 |
{
|
|
175 |
this.targetList = targetList;
|
|
176 |
this.project = project;
|
|
177 |
}
|
|
178 |
|
|
179 |
public void visit(Element node)
|
|
180 |
{
|
|
181 |
String name = node.getName();
|
|
182 |
if (name.equals("antcall") || name.equals("runtarget"))
|
|
183 |
{
|
|
184 |
String text = node.attributeValue("target");
|
|
185 |
extractTarget(text);
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
private void extractTarget(String text)
|
|
190 |
{
|
|
191 |
String iText = project.replaceProperties(text);
|
|
192 |
targetList.add(iText);
|
|
193 |
}
|
|
194 |
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Find the xml Element for the target
|
|
199 |
*
|
|
200 |
*/
|
|
201 |
@SuppressWarnings("unchecked")
|
|
202 |
public Element findTargetElement(Target target, Project project)
|
|
203 |
{
|
|
204 |
SAXReader xmlReader = new SAXReader();
|
|
205 |
|
|
206 |
Document antDoc = null;
|
|
207 |
|
|
208 |
String location = target.getLocation().getFileName();
|
|
209 |
|
|
210 |
try {
|
|
211 |
File file = new File(location);
|
|
212 |
antDoc = xmlReader.read(file);
|
|
213 |
} catch (DocumentException e) {
|
|
214 |
// We are Ignoring the errors as no need to fail the build.
|
|
215 |
log("Not able read the XML file. " + e.getMessage(), Project.MSG_WARN);
|
|
216 |
}
|
|
217 |
|
|
218 |
String projectName = antDoc.valueOf("/project/@name");
|
|
219 |
for (Iterator<Element> iterator = antDoc.selectNodes("//target").iterator(); iterator.hasNext();)
|
|
220 |
{
|
|
221 |
Element e = iterator.next();
|
|
222 |
|
|
223 |
String targetName = e.attributeValue("name");
|
|
224 |
if (targetName.equals(target.getName()) || (projectName + "." + targetName).equals(target.getName()))
|
|
225 |
return e;
|
|
226 |
}
|
|
227 |
return null;
|
|
228 |
}
|
|
229 |
|
|
230 |
/**
|
|
231 |
* If target has comment that says it is private them print warning
|
|
232 |
*
|
|
233 |
*/
|
|
234 |
@SuppressWarnings("unchecked")
|
|
235 |
public void checkIfTargetPrivate(Target target, Project project)
|
|
236 |
{
|
|
237 |
Element targetElement = findTargetElement(target, project);
|
|
238 |
if (targetElement != null)
|
|
239 |
{
|
|
240 |
List<Node> children = targetElement.selectNodes("preceding-sibling::node()");
|
|
241 |
if (children.size() > 0)
|
|
242 |
{
|
|
243 |
// Scan past the text nodes, which are most likely whitespace
|
|
244 |
int index = children.size() - 1;
|
|
245 |
Node child = children.get(index);
|
|
246 |
while (index > 0 && child.getNodeType() == Node.TEXT_NODE)
|
|
247 |
{
|
|
248 |
index--;
|
|
249 |
child = (Node) children.get(index);
|
|
250 |
}
|
|
251 |
|
|
252 |
// Check if there is a comment node
|
|
253 |
String commentText = null;
|
|
254 |
if (child.getNodeType() == Node.COMMENT_NODE)
|
|
255 |
{
|
|
256 |
Comment macroComment = (Comment) child;
|
|
257 |
commentText = macroComment.getStringValue().trim();
|
|
258 |
//log(macroName + " comment: " + commentText, Project.MSG_DEBUG);
|
|
259 |
}
|
|
260 |
|
|
261 |
if (commentText != null)
|
|
262 |
{
|
|
263 |
if (commentText.contains("Private:"))
|
|
264 |
{
|
|
265 |
output.add("Warning: " + target.getName() + " is private and should only be called by helium");
|
|
266 |
}
|
|
267 |
if (commentText.contains("<deprecated>"))
|
|
268 |
{
|
|
269 |
output.add("Warning: " + target.getName() + "\n" + commentText);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
}
|
|
273 |
}
|
|
274 |
}
|
|
275 |
|
|
276 |
@SuppressWarnings("unchecked")
|
|
277 |
public void checkTargetsProperties(Project project)
|
|
278 |
{
|
|
279 |
try {
|
|
280 |
String heliumpath = new File(project.getProperty("helium.dir")).getCanonicalPath();
|
|
281 |
com.nokia.helium.ant.data.Database db = new com.nokia.helium.ant.data.Database(project, "private");
|
|
282 |
ArrayList<String> customerProps = getCustomerProperties(project);
|
|
283 |
|
|
284 |
for (PropertyMeta x : db.getProperties())
|
|
285 |
{
|
|
286 |
if (x.getLocation().contains(heliumpath) && x.getScope().equals("private") && customerProps.contains(x.getName()))
|
|
287 |
{
|
|
288 |
output.add("Warning: " + x.getName() + " property has been overridden");
|
|
289 |
}
|
|
290 |
}
|
|
291 |
} catch (IOException e) { e.printStackTrace(); }
|
|
292 |
}
|
|
293 |
|
|
294 |
@SuppressWarnings("unchecked")
|
|
295 |
public ArrayList<String> getCustomerProperties(Project project)
|
|
296 |
{
|
|
297 |
ArrayList<String> props = new ArrayList<String>();
|
|
298 |
Database db = new Database(null, null, null);
|
|
299 |
try {
|
|
300 |
String heliumpath = new File(project.getProperty("helium.dir")).getCanonicalPath();
|
|
301 |
|
|
302 |
for (Object o : db.getAntFiles(project))
|
|
303 |
{
|
|
304 |
String antFile = (String)o;
|
|
305 |
antFile = new File(antFile).getCanonicalPath();
|
|
306 |
|
|
307 |
if (!antFile.contains(heliumpath))
|
|
308 |
{
|
|
309 |
SAXReader xmlReader = new SAXReader();
|
|
310 |
Document antDoc = xmlReader.read(new File(antFile));
|
|
311 |
|
|
312 |
List<Element> propertyNodes = antDoc.selectNodes("//property | //param");
|
|
313 |
for (Element propertyNode : propertyNodes)
|
|
314 |
{
|
|
315 |
props.add(propertyNode.attributeValue("name"));
|
|
316 |
}
|
|
317 |
}
|
|
318 |
}
|
|
319 |
} catch (IOException e) {
|
|
320 |
// We are Ignoring the errors as no need to fail the build.
|
|
321 |
log("IOException: Not able read the Customer Properties " + e.getMessage(), Project.MSG_WARN);
|
|
322 |
} catch (DocumentException e) {
|
|
323 |
// We are Ignoring the errors as no need to fail the build.
|
|
324 |
log("DocumentException: Not able read the Customer Properties " + e.getMessage(), Project.MSG_WARN);
|
|
325 |
}
|
|
326 |
|
|
327 |
return props;
|
|
328 |
}
|
|
329 |
|
|
330 |
} |