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 |
|
|
20 |
import java.io.BufferedWriter;
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.FileWriter;
|
|
23 |
import java.io.IOException;
|
|
24 |
import java.util.ArrayList;
|
|
25 |
import java.util.List;
|
|
26 |
|
|
27 |
import org.apache.tools.ant.BuildException;
|
|
28 |
import org.apache.tools.ant.DirectoryScanner;
|
|
29 |
import org.apache.tools.ant.Project;
|
|
30 |
import org.apache.tools.ant.Task;
|
|
31 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
|
32 |
import org.apache.tools.ant.taskdefs.condition.Os;
|
|
33 |
import org.apache.tools.ant.types.FileSet;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* CMTTool is the task used to execute the Complexity Measures Tool for C/C++.
|
|
37 |
*
|
|
38 |
* <pre>
|
|
39 |
* Usage:
|
|
40 |
*
|
|
41 |
* <hlm:cmt output="${cmt.output.dir}\logs\${build.id}_${ido.name}_${componentbase}_${cmt.id}.txt>
|
|
42 |
* <fileset id="input" dir="${test.data.src}">
|
|
43 |
* <include name="**/*.h"/>
|
|
44 |
* <include name="**/*.cpp"/>
|
|
45 |
* </fileset>
|
|
46 |
* </hlm:cmt>
|
|
47 |
*
|
|
48 |
* output : Name of the file to contain the results of Complexity Measures Tool.
|
|
49 |
* fileset: Location of the source files whose complexity is to be measured.
|
|
50 |
* </pre>
|
|
51 |
*
|
|
52 |
* @ant.task name="cmt" category="Quality".
|
|
53 |
*/
|
|
54 |
public class CMTToolTask extends Task {
|
|
55 |
|
|
56 |
private List<FileSet> cmtFileSetList = new ArrayList<FileSet>();
|
|
57 |
private File input;
|
|
58 |
private File output;
|
|
59 |
private File htmlOutputDir;
|
|
60 |
private boolean failOnError = true;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Set the fileset.
|
|
64 |
*
|
|
65 |
* @param fileSet
|
|
66 |
* is the fileset which points to the location of source files to
|
|
67 |
* be measured.
|
|
68 |
* @ant.required
|
|
69 |
*/
|
|
70 |
public void addFileset(FileSet fileSet) {
|
|
71 |
cmtFileSetList.add(fileSet);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Set the name and location of the output file to contain the result of CMT
|
|
76 |
* tool.
|
|
77 |
*
|
|
78 |
* @param output
|
|
79 |
* is the name of the result file to be generated.
|
|
80 |
* @ant.required
|
|
81 |
*/
|
|
82 |
public void setOutput(File output) {
|
|
83 |
this.output = output;
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* @param failOnError the failOnError to set
|
|
88 |
*/
|
|
89 |
public void setFailOnError(boolean failOnError) {
|
|
90 |
this.failOnError = failOnError;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* @param htmlOutputDir the htmlOutputDir to set
|
|
95 |
*/
|
|
96 |
public void setHtmlOutputDir(File htmlOutputDir) {
|
|
97 |
this.htmlOutputDir = htmlOutputDir;
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Run the CMT task.
|
|
102 |
*/
|
|
103 |
public void execute() {
|
|
104 |
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
|
|
105 |
throw new BuildException("CMT supported only on windows platforms.");
|
|
106 |
}
|
|
107 |
validate();
|
|
108 |
try {
|
|
109 |
input = File.createTempFile("Files", ".list");
|
|
110 |
generateFileList();
|
|
111 |
runCmtCommand();
|
|
112 |
if (this.htmlOutputDir != null) {
|
|
113 |
runCmt2HtmlCommand();
|
|
114 |
}
|
|
115 |
} catch (IOException ioe) {
|
|
116 |
throw new BuildException("Not able to generate file list for 'cmt'. ", ioe);
|
|
117 |
} catch (BuildException be) {
|
|
118 |
if (failOnError) {
|
|
119 |
throw new BuildException("Exception occured while running 'cmt' tool. ", be);
|
|
120 |
}
|
|
121 |
log("Exception occured while running 'cmt' tool. ", Project.MSG_ERR);
|
|
122 |
} finally {
|
|
123 |
if (input != null) {
|
|
124 |
input.delete();
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Execute the cmt.exe command.
|
|
131 |
*/
|
|
132 |
private void runCmtCommand() {
|
|
133 |
String command = null;
|
|
134 |
if (!this.output.getParentFile().exists()) {
|
|
135 |
this.output.getParentFile().mkdirs();
|
|
136 |
}
|
|
137 |
ExecTask task = getExecTask("cmttool", "cmt", new File("."));
|
|
138 |
command = "cmt";
|
|
139 |
task.createArg().setValue("-f");
|
|
140 |
command += " " + "-f";
|
|
141 |
task.createArg().setValue(input.toString());
|
|
142 |
command += " " + input.toString();
|
|
143 |
task.createArg().setValue("-o");
|
|
144 |
command += " " + "-o";
|
|
145 |
task.createArg().setValue(output.toString());
|
|
146 |
command += " " + output;
|
|
147 |
task.setProject(getProject());
|
|
148 |
log("run command: " + command);
|
|
149 |
task.execute();
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Execute the cmt2html.bat command.
|
|
154 |
*/
|
|
155 |
private void runCmt2HtmlCommand() {
|
|
156 |
String command = null;
|
|
157 |
if (!this.htmlOutputDir.exists()) {
|
|
158 |
this.htmlOutputDir.mkdirs();
|
|
159 |
}
|
|
160 |
ExecTask task = getExecTask("cmt2html", "cmt2html.bat", this.htmlOutputDir);
|
|
161 |
command = "cmt2html.bat";
|
|
162 |
task.createArg().setValue("-i");
|
|
163 |
command += " " + "-i";
|
|
164 |
task.createArg().setValue(output.toString());
|
|
165 |
command += " " + output;
|
|
166 |
task.createArg().setValue("-nsb");
|
|
167 |
command += " " + "-nsb";
|
|
168 |
task.setProject(getProject());
|
|
169 |
log("run command: " + command);
|
|
170 |
task.execute();
|
|
171 |
}
|
|
172 |
|
|
173 |
/**
|
|
174 |
* Return an execute task with the given inputs.
|
|
175 |
*
|
|
176 |
* @param taskName
|
|
177 |
* is the name of the execute task.
|
|
178 |
* @param cmd
|
|
179 |
* is the command to be executed by the task.
|
|
180 |
* @param outDir
|
|
181 |
* is the working directory for the task.
|
|
182 |
* @return an instance of execute task.
|
|
183 |
*/
|
|
184 |
private ExecTask getExecTask(String taskName, String cmd, File outDir) {
|
|
185 |
ExecTask task = new ExecTask();
|
628
|
186 |
task.bindToOwner(this);
|
588
|
187 |
task.setDir(outDir);
|
|
188 |
task.setExecutable(cmd);
|
|
189 |
return task;
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Method validates the required parameters and elements are set.
|
|
194 |
*/
|
|
195 |
private void validate() {
|
|
196 |
if (cmtFileSetList.size() == 0) {
|
|
197 |
throw new BuildException("Nested Element 'fileset' missing for task 'cmt'.");
|
|
198 |
}
|
|
199 |
if (output == null) {
|
|
200 |
throw new BuildException("Parameter 'output' missing for task 'cmt'.");
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Generates a file containing a list of files.
|
|
206 |
*/
|
|
207 |
private void generateFileList() {
|
|
208 |
|
|
209 |
for (FileSet fileSet : cmtFileSetList) {
|
|
210 |
DirectoryScanner dirScanner = fileSet.getDirectoryScanner();
|
|
211 |
dirScanner.scan();
|
|
212 |
String[] filelist = dirScanner.getIncludedFiles();
|
|
213 |
String lineSeparator = System.getProperty("line.separator");
|
628
|
214 |
String fileSeparator = System.getProperty("file.separator");
|
|
215 |
|
588
|
216 |
BufferedWriter outputFile = null;
|
|
217 |
try {
|
|
218 |
outputFile = new BufferedWriter(new FileWriter(input));
|
|
219 |
for (String file : filelist) {
|
628
|
220 |
outputFile.write(dirScanner.getBasedir().toString() + fileSeparator + file
|
588
|
221 |
+ lineSeparator);
|
|
222 |
}
|
|
223 |
} catch (IOException e) {
|
|
224 |
throw new BuildException("Not able to generate file list for 'cmt'. ", e);
|
|
225 |
} finally {
|
|
226 |
try {
|
628
|
227 |
if (outputFile != null) {
|
588
|
228 |
outputFile.close();
|
628
|
229 |
}
|
588
|
230 |
} catch (IOException ex) {
|
|
231 |
// ignore exception
|
|
232 |
ex = null;
|
|
233 |
}
|
|
234 |
}
|
|
235 |
}
|
|
236 |
}
|
|
237 |
}
|