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.quality.ant.taskdefs;
|
|
19 |
|
628
|
20 |
import java.io.File;
|
588
|
21 |
import java.util.Vector;
|
|
22 |
|
628
|
23 |
import org.apache.tools.ant.BuildException;
|
588
|
24 |
import org.apache.tools.ant.Task;
|
628
|
25 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
588
|
26 |
import org.apache.tools.ant.types.Path;
|
|
27 |
|
|
28 |
/**
|
628
|
29 |
* This task executes codescanner - and writes the results to the output directory. Codescanner
|
|
30 |
* parses C++ code and flags any inconsistencies or errors in output files. Configuration files are
|
|
31 |
* used to determine what passes and fails the checking e.g. maximum length of lines, whether 'C'
|
|
32 |
* type comments are allowed as well as C++ comments, does it adhere to the company coding
|
|
33 |
* guidelines and much more. Every person writing any C++ code should run codescanner on their code
|
|
34 |
* to ensure it follows the coding guidelines. The output logs should have no errors and preferably
|
|
35 |
* no warnings before the code should be checked into SCM, e.g. synergy or SVN.
|
|
36 |
*
|
588
|
37 |
* Below is an example of how to use the target to run codescanner.
|
628
|
38 |
*
|
588
|
39 |
* <pre>
|
|
40 |
* <property name="codescanner.output.dir" location="./cs" />
|
|
41 |
* <property name="codescanner.output.type" value="html" />
|
|
42 |
* <property name="codescanner.config" location="./codescanner_config.xml" />
|
628
|
43 |
*
|
588
|
44 |
* <hlm:codescanner dest="${codescanner.output.dir}"
|
|
45 |
* format="${codescanner.output.type}"
|
|
46 |
* failonerror="true"
|
|
47 |
* configuration="${codescanner.config}">
|
|
48 |
* <path refid="src.path"/>
|
|
49 |
* </hlm:codescanner>
|
|
50 |
* <au:assertLogContains text="Successfully executed codescanner"/>
|
|
51 |
* <au:assertLogContains text="Output format: xml,html"/>
|
|
52 |
* <au:assertFileExists file="${codescanner.output.dir}/problemIndex.xml"/>
|
|
53 |
* </pre>
|
|
54 |
*
|
|
55 |
* @ant.task name="codescanner" category="Quality"
|
|
56 |
*/
|
|
57 |
public class CodeScannerTask extends Task {
|
|
58 |
private Vector<Path> paths = new Vector<Path>();
|
|
59 |
private File configuration;
|
|
60 |
private String dest;
|
|
61 |
private String format = "xml,html";
|
|
62 |
private boolean auto;
|
|
63 |
private File log;
|
|
64 |
private boolean failonerror;
|
|
65 |
|
|
66 |
/**
|
628
|
67 |
* This defines if the task should fails in case of error while executing codescanner.
|
588
|
68 |
*
|
|
69 |
* @param failonerror
|
|
70 |
* @ant.not-required Default is false for backward compatibility.
|
|
71 |
*/
|
|
72 |
public void setFailonerror(boolean failonerror) {
|
|
73 |
this.failonerror = failonerror;
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Add path datatype to the task.
|
|
78 |
*
|
|
79 |
* @param path
|
|
80 |
*/
|
|
81 |
public void add(Path path) {
|
|
82 |
paths.add(path);
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Get dest attribute.
|
|
87 |
*
|
|
88 |
*/
|
|
89 |
public String getDest() {
|
|
90 |
return this.dest;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Set dest attribute.
|
|
95 |
*
|
|
96 |
* @param dest
|
|
97 |
* @ant.required
|
|
98 |
*/
|
|
99 |
public void setDest(String dest) {
|
|
100 |
this.dest = dest;
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Get format attribute.
|
|
105 |
*
|
|
106 |
*/
|
|
107 |
public String getFormat() {
|
|
108 |
return this.format;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Set format attribute.
|
|
113 |
*
|
|
114 |
* @param format
|
|
115 |
* @ant.not-required Default is xml,html
|
|
116 |
*/
|
|
117 |
public void setFormat(String format) {
|
|
118 |
this.format = format;
|
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* Get configuration attribute.
|
|
123 |
*
|
|
124 |
*/
|
|
125 |
public File getConfiguration() {
|
|
126 |
return this.configuration;
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Set configuration attribute.
|
|
131 |
*
|
|
132 |
* @param configuration
|
|
133 |
* @ant.required
|
|
134 |
*/
|
|
135 |
public void setConfiguration(File configuration) {
|
|
136 |
this.configuration = configuration;
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Set auto attribute.
|
|
141 |
*
|
|
142 |
* @param auto
|
|
143 |
* @ant.not-required Default is false.
|
|
144 |
*/
|
|
145 |
public void setAuto(boolean auto) {
|
|
146 |
this.auto = auto;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Set log attribute.
|
|
151 |
*
|
|
152 |
* @param log
|
|
153 |
* @ant.not-required
|
|
154 |
*/
|
|
155 |
public void setLog(File log) {
|
|
156 |
this.log = log;
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* {@inheritDoc}
|
|
161 |
*/
|
|
162 |
@Override
|
|
163 |
public void execute() {
|
|
164 |
// creating the exec subtask
|
|
165 |
ExecTask task = new ExecTask();
|
|
166 |
task.setProject(getProject());
|
|
167 |
task.setTaskName(this.getTaskName());
|
|
168 |
task.setFailonerror(failonerror);
|
|
169 |
task.setExecutable("codescanner");
|
|
170 |
task.setDir(new File("."));
|
|
171 |
if (dest == null) {
|
|
172 |
throw new BuildException("'dest' attribute must be defined");
|
|
173 |
}
|
|
174 |
if (configuration != null) {
|
|
175 |
if (!configuration.exists()) {
|
628
|
176 |
throw new BuildException("Could not find the file " + configuration);
|
|
177 |
}
|
|
178 |
else {
|
588
|
179 |
task.createArg().setValue("-c");
|
|
180 |
task.createArg().setValue(configuration.getAbsolutePath());
|
|
181 |
}
|
628
|
182 |
}
|
|
183 |
else {
|
|
184 |
throw new BuildException("'configuration' attribute must be defined");
|
588
|
185 |
}
|
|
186 |
if (!format.contains("xml")) {
|
628
|
187 |
setFormat("xml," + format);
|
588
|
188 |
}
|
|
189 |
this.log("Output format: " + format);
|
|
190 |
// -t off
|
|
191 |
task.createArg().setValue("-t");
|
|
192 |
task.createArg().setValue(auto ? "on" : "off");
|
|
193 |
|
|
194 |
// -l log
|
|
195 |
if (log != null) {
|
|
196 |
this.log("Output log: " + log.getAbsolutePath());
|
|
197 |
task.createArg().setValue("-l");
|
|
198 |
task.createArg().setValue(log.getAbsolutePath());
|
|
199 |
}
|
|
200 |
|
|
201 |
// -o type
|
|
202 |
task.createArg().setValue("-o");
|
|
203 |
task.createArg().setValue(format);
|
|
204 |
if (paths.isEmpty()) {
|
|
205 |
throw new BuildException("No input directory defined");
|
|
206 |
}
|
|
207 |
// Getting the list of source dir to scan
|
|
208 |
Vector<String> srcs = new Vector<String>();
|
|
209 |
for (Path path : paths) {
|
|
210 |
if (path.isReference()) {
|
|
211 |
path = (Path) path.getRefid().getReferencedObject();
|
|
212 |
}
|
|
213 |
for (String apath : path.list()) {
|
|
214 |
srcs.add(apath);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
for (int i = 0; i < srcs.size(); i++) {
|
|
218 |
if (i != srcs.size() - 1) {
|
|
219 |
task.createArg().setValue("-i");
|
|
220 |
task.createArg().setValue(srcs.elementAt(i));
|
628
|
221 |
}
|
|
222 |
else {
|
588
|
223 |
task.createArg().setValue(srcs.elementAt(i));
|
|
224 |
task.createArg().setValue(dest.toString());
|
|
225 |
}
|
|
226 |
}
|
|
227 |
// output path
|
|
228 |
this.log("Output dir " + dest);
|
|
229 |
|
|
230 |
// Run codescanner
|
|
231 |
task.execute();
|
|
232 |
this.log("Successfully executed codescanner");
|
|
233 |
}
|
|
234 |
}
|