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.helium.antlint.ant.taskdefs;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.List;
|
|
23 |
import java.util.Vector;
|
628
|
24 |
|
587
|
25 |
import org.apache.tools.ant.BuildException;
|
|
26 |
import org.apache.tools.ant.DirectoryScanner;
|
|
27 |
import org.apache.tools.ant.Task;
|
|
28 |
import org.apache.tools.ant.types.FileSet;
|
628
|
29 |
|
588
|
30 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
31 |
import com.nokia.helium.antlint.ant.Reporter;
|
|
32 |
import com.nokia.helium.antlint.ant.Severity;
|
|
33 |
import com.nokia.helium.antlint.ant.types.Check;
|
|
34 |
import com.nokia.helium.antlint.ant.types.ConsoleReporter;
|
587
|
35 |
|
|
36 |
/**
|
|
37 |
* AntLint Task. This task checks for common coding conventions and errors in
|
|
38 |
* Ant XML script files.
|
|
39 |
*
|
|
40 |
* <p>
|
|
41 |
* The current checks include:
|
|
42 |
* <ul>
|
|
43 |
* <li>CheckAntCall : checks whether antcall is used with no param elements and
|
|
44 |
* calls target with no dependencies</li>
|
|
45 |
* <li>CheckDescription : checks for project description</li>
|
|
46 |
* <li>CheckDuplicateNames : checks for duplicate macros</li>
|
|
47 |
* <li>CheckFileName : checks the naming convention of ant xml files</li>
|
|
48 |
* <li>CheckIndentation : checks indentation</li>
|
|
49 |
* <li>CheckJepJythonScript : checks the coding convention in Jep and Jython
|
|
50 |
* scripts</li>
|
|
51 |
* <li>CheckPresetDefMacroDefName : checks the naming convention of presetdef
|
|
52 |
* and macrodef</li>
|
|
53 |
* <li>CheckProjectName : checks the naming convention of project</li>
|
|
54 |
* <li>CheckPropertyName : checks the naming convention of properties</li>
|
|
55 |
* </li>
|
|
56 |
* <li>CheckPythonTasks : checks the coding convention of python tasks</li>
|
|
57 |
* <li>CheckRunTarget : checks whether runtarget calls a target that has
|
|
58 |
* dependencies</li>
|
|
59 |
* <li>CheckScriptCondition : checks the coding convention in script condition</li>
|
|
60 |
* <li>CheckScriptDef : checks the coding convention in scriptdef</li>
|
|
61 |
* <li>CheckScriptDefNameAttributes - checks the naming convention of scriptdef
|
|
62 |
* name attributes</li></li>
|
|
63 |
* <li>CheckScriptDefStyle : checks the coding style of scriptdef</li>
|
|
64 |
* <li>CheckScriptSize : checks the size of scripts</li>
|
|
65 |
* <li>CheckTabCharacter : checks for tab characters</li>
|
|
66 |
* <li>CheckTargetName : checks the naming convention of targets</li>
|
|
67 |
* <li>CheckUseOfEqualsTask : checks the usage of equals task</li>
|
|
68 |
* <li>CheckUseOfIfInTargets : checks the usage of if task inside targets</li>
|
|
69 |
* </ul>
|
|
70 |
* </pre>
|
|
71 |
*
|
|
72 |
* <p>
|
|
73 |
* Checks to be added:
|
|
74 |
* <ul>
|
|
75 |
* <li>Help target is defined.</li>
|
|
76 |
* <li>Optional to thrown warnings about deprecated targets (rename, copydir,
|
|
77 |
* copyfile).</li>
|
|
78 |
* </ul>
|
|
79 |
* </p>
|
|
80 |
*
|
|
81 |
* <pre>
|
|
82 |
* Usage:
|
|
83 |
*
|
|
84 |
* <antlint>
|
|
85 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
86 |
* <include name="*.ant.xml"/>
|
|
87 |
* <include name="*build.xml"/>
|
|
88 |
* <include name="*.antlib.xml"/>
|
|
89 |
* </fileset>
|
588
|
90 |
* <CheckTabCharacter" severity="error" enabled="true"/>
|
|
91 |
* <CheckTargetName" severity="warning&quot enabled="true" regexp="([a-z0-9[\\d\\-]]*)"/>
|
|
92 |
* <CheckScriptDef" severity="error" enabled="true" outputDir="${antlint.test.dir}/output"/>
|
587
|
93 |
* </antlint>
|
|
94 |
* </pre>
|
|
95 |
*
|
|
96 |
* @ant.task name="antlint" category="AntLint"
|
|
97 |
*
|
|
98 |
*/
|
588
|
99 |
public class AntLintTask extends Task implements Reporter {
|
587
|
100 |
|
588
|
101 |
private List<Check> checkerList = new Vector<Check>();
|
|
102 |
private List<FileSet> antFileSetList = new ArrayList<FileSet>();
|
|
103 |
private List<Reporter> reporters = new ArrayList<Reporter>();
|
|
104 |
private int errorCount;
|
|
105 |
private boolean failOnError = true;
|
|
106 |
private ConsoleReporter consoleReporter = new ConsoleReporter();
|
587
|
107 |
|
|
108 |
/**
|
|
109 |
* Add a set of files to copy.
|
|
110 |
*
|
|
111 |
* @param set
|
|
112 |
* a set of files to AntLintTask.
|
|
113 |
* @ant.required
|
|
114 |
*/
|
|
115 |
public void addFileset(FileSet set) {
|
|
116 |
antFileSetList.add(set);
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Execute the antlint task.
|
|
121 |
*/
|
|
122 |
public final void execute() {
|
588
|
123 |
if (checkerList.size() == 0) {
|
|
124 |
throw new BuildException("No antlint checks are defined.");
|
|
125 |
}
|
587
|
126 |
try {
|
588
|
127 |
// Adding console reported by default if no
|
|
128 |
// other reporter are mentioned.
|
|
129 |
if (reporters.size() == 0) {
|
|
130 |
reporters.add(consoleReporter);
|
|
131 |
}
|
|
132 |
setTask(this);
|
|
133 |
open();
|
|
134 |
doAntLintCheck();
|
|
135 |
} catch (AntlintException e) {
|
587
|
136 |
throw new BuildException(
|
|
137 |
"Exception occured while running AntLint task "
|
|
138 |
+ e.getMessage());
|
588
|
139 |
} finally {
|
|
140 |
// Closing all reporter session.
|
|
141 |
close();
|
587
|
142 |
}
|
|
143 |
|
588
|
144 |
if (failOnError && (errorCount > 0)) {
|
|
145 |
throw new BuildException("Build failed because of AntLint errors.");
|
587
|
146 |
}
|
|
147 |
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Triggers the antlint checking.
|
|
152 |
*
|
588
|
153 |
* @throws AntlintException
|
|
154 |
*
|
587
|
155 |
* @throws Exception
|
|
156 |
* if the checking fails.
|
|
157 |
*/
|
588
|
158 |
private void doAntLintCheck() throws AntlintException {
|
587
|
159 |
|
|
160 |
for (FileSet fs : antFileSetList) {
|
|
161 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
|
162 |
String[] srcFiles = ds.getIncludedFiles();
|
|
163 |
String basedir = ds.getBasedir().getPath();
|
|
164 |
for (int i = 0; i < srcFiles.length; i++) {
|
588
|
165 |
String antFilename = basedir + File.separator + srcFiles[i];
|
|
166 |
runChecks(new File(antFilename));
|
587
|
167 |
}
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
|
172 |
* Runs antlint checks for the given ant file.
|
|
173 |
*
|
|
174 |
* @param antFileName
|
|
175 |
* is the name of the ant file to be checked.
|
588
|
176 |
* @throws AntlintException
|
587
|
177 |
*/
|
588
|
178 |
private void runChecks(File antFilename) throws AntlintException {
|
|
179 |
for (Check check : checkerList) {
|
587
|
180 |
if (check.isEnabled()) {
|
588
|
181 |
check.validateAttributes();
|
|
182 |
check.setReporter(this);
|
|
183 |
check.run(antFilename);
|
587
|
184 |
}
|
|
185 |
}
|
588
|
186 |
|
587
|
187 |
}
|
588
|
188 |
|
|
189 |
/**
|
|
190 |
* To add Antlint checkers.
|
|
191 |
*
|
|
192 |
* @param c
|
|
193 |
*/
|
|
194 |
public void add(Check c) {
|
|
195 |
checkerList.add(c);
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* To add reporters.
|
|
200 |
*
|
|
201 |
* @param reporter
|
|
202 |
*/
|
|
203 |
public void add(Reporter reporter) {
|
|
204 |
reporter.setTask(this);
|
|
205 |
reporters.add(reporter);
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* @param failOnError
|
|
210 |
* the failOnError to set
|
|
211 |
*/
|
|
212 |
public void setFailOnError(boolean failOnError) {
|
|
213 |
this.failOnError = failOnError;
|
|
214 |
}
|
|
215 |
|
|
216 |
/*
|
|
217 |
* (non-Javadoc)
|
|
218 |
*
|
|
219 |
* @see
|
|
220 |
* com.nokia.helium.antlint.ant.Reporter#report(com.nokia.helium.antlint
|
|
221 |
* .ant.Severity, java.lang.String, java.io.File, int)
|
|
222 |
*/
|
|
223 |
public void report(Severity severity, String message, File filename,
|
|
224 |
int lineNo) {
|
|
225 |
if (severity.getValue().toUpperCase().equals("ERROR")) {
|
|
226 |
errorCount++;
|
|
227 |
}
|
|
228 |
|
|
229 |
for (Reporter reporter : reporters) {
|
|
230 |
reporter.report(severity, message, filename, lineNo);
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
/*
|
|
235 |
* (non-Javadoc)
|
|
236 |
*
|
|
237 |
* @see
|
|
238 |
* com.nokia.helium.antlint.ant.Reporter#setTask(org.apache.tools.ant.Task)
|
|
239 |
*/
|
|
240 |
@Override
|
|
241 |
public void setTask(Task task) {
|
|
242 |
for (Reporter reporter : reporters) {
|
|
243 |
reporter.setTask(task);
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
@Override
|
|
248 |
public void close() {
|
|
249 |
for (Reporter reporter : reporters) {
|
|
250 |
reporter.close();
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
@Override
|
|
255 |
public void open() {
|
|
256 |
for (Reporter reporter : reporters) {
|
|
257 |
reporter.open();
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
587
|
261 |
} |