buildframework/helium/sf/java/quality/src/com/nokia/helium/quality/ant/taskdefs/CodeScannerTask.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.helium.quality.ant.taskdefs;
    18 package com.nokia.helium.quality.ant.taskdefs;
    19 
    19 
    20 import java.io.*;
    20 import java.io.File;
    21 import java.util.Vector;
    21 import java.util.Vector;
    22 
    22 
       
    23 import org.apache.tools.ant.BuildException;
    23 import org.apache.tools.ant.Task;
    24 import org.apache.tools.ant.Task;
    24 import org.apache.tools.ant.BuildException;
    25 import org.apache.tools.ant.taskdefs.ExecTask;
    25 import org.apache.tools.ant.types.Path;
    26 import org.apache.tools.ant.types.Path;
    26 import org.apache.tools.ant.taskdefs.ExecTask;
       
    27 
    27 
    28 /**
    28 /**
    29  * This task executes codescanner - and writes the results to the output directory.
    29  * This task executes codescanner - and writes the results to the output directory. Codescanner
    30  * Codescanner parses C++ code and flags any inconsistencies or errors in output files. 
    30  * parses C++ code and flags any inconsistencies or errors in output files. Configuration files are
    31  * Configuration files are used to determine what passes and fails the checking e.g. maximum length of lines,
    31  * used to determine what passes and fails the checking e.g. maximum length of lines, whether 'C'
    32  * whether 'C' type comments are allowed as well as C++ comments, does it adhere to the company coding 
    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 
    33  * guidelines and much more. Every person writing any C++ code should run codescanner on their code
    34  * should run codescanner on their code to ensure it follows the coding guidelines. The output logs 
    34  * to ensure it follows the coding guidelines. The output logs should have no errors and preferably
    35  * should have no errors and preferably no warnings before the code should be checked into SCM, e.g. synergy or SVN.
    35  * no warnings before the code should be checked into SCM, e.g. synergy or SVN.
    36  *
    36  * 
    37  * Below is an example of how to use the target to run codescanner.
    37  * Below is an example of how to use the target to run codescanner.
    38  *
    38  * 
    39  * <pre>
    39  * <pre>
    40  * &lt;property name="codescanner.output.dir" location="./cs" /&gt;
    40  * &lt;property name="codescanner.output.dir" location="./cs" /&gt;
    41  * &lt;property name="codescanner.output.type" value="html" /&gt;
    41  * &lt;property name="codescanner.output.type" value="html" /&gt;
    42  * &lt;property name="codescanner.config" location="./codescanner_config.xml" /&gt;
    42  * &lt;property name="codescanner.config" location="./codescanner_config.xml" /&gt;
    43  *
    43  * 
    44  * &lt;hlm:codescanner dest="${codescanner.output.dir}"
    44  * &lt;hlm:codescanner dest="${codescanner.output.dir}"
    45  *      format="${codescanner.output.type}"
    45  *      format="${codescanner.output.type}"
    46  *      failonerror="true"
    46  *      failonerror="true"
    47  *      configuration="${codescanner.config}"&gt;
    47  *      configuration="${codescanner.config}"&gt;
    48  *      &lt;path refid="src.path"/&gt;
    48  *      &lt;path refid="src.path"/&gt;
    62     private boolean auto;
    62     private boolean auto;
    63     private File log;
    63     private File log;
    64     private boolean failonerror;
    64     private boolean failonerror;
    65 
    65 
    66     /**
    66     /**
    67      * This defines if the task should fails in case of error while 
    67      * This defines if the task should fails in case of error while executing codescanner.
    68      * executing codescanner.
       
    69      * 
    68      * 
    70      * @param failonerror
    69      * @param failonerror
    71      * @ant.not-required Default is false for backward compatibility.
    70      * @ant.not-required Default is false for backward compatibility.
    72      */
    71      */
    73     public void setFailonerror(boolean failonerror) {
    72     public void setFailonerror(boolean failonerror) {
   161      * {@inheritDoc}
   160      * {@inheritDoc}
   162      */
   161      */
   163     @Override
   162     @Override
   164     public void execute() {
   163     public void execute() {
   165         // creating the exec subtask
   164         // creating the exec subtask
   166         String osType = System.getProperty("os.name");
       
   167         if (!osType.toLowerCase().startsWith("win")) {
       
   168             this.log("CODESCANNER: run in windows only");
       
   169             return;
       
   170         }
       
   171         ExecTask task = new ExecTask();
   165         ExecTask task = new ExecTask();
   172         task.setProject(getProject());
   166         task.setProject(getProject());
   173         task.setTaskName(this.getTaskName());
   167         task.setTaskName(this.getTaskName());
   174         task.setFailonerror(failonerror);
   168         task.setFailonerror(failonerror);
   175         task.setExecutable("codescanner");
   169         task.setExecutable("codescanner");
   177         if (dest == null) {
   171         if (dest == null) {
   178             throw new BuildException("'dest' attribute must be defined");
   172             throw new BuildException("'dest' attribute must be defined");
   179         }
   173         }
   180         if (configuration != null) {
   174         if (configuration != null) {
   181             if (!configuration.exists()) {
   175             if (!configuration.exists()) {
   182                 throw new BuildException("Could not find the file "
   176                 throw new BuildException("Could not find the file " + configuration);
   183                         + configuration);
   177             }
   184             } else {
   178             else {
   185                 task.createArg().setValue("-c");
   179                 task.createArg().setValue("-c");
   186                 task.createArg().setValue(configuration.getAbsolutePath());
   180                 task.createArg().setValue(configuration.getAbsolutePath());
   187             }
   181             }
   188         } else {
   182         }
   189             throw new BuildException(
   183         else {
   190                     "'configuration' attribute must be defined");
   184             throw new BuildException("'configuration' attribute must be defined");
   191         }
   185         }
   192         if (!format.contains("xml")) {
   186         if (!format.contains("xml")) {
   193            setFormat("xml," + format);
   187             setFormat("xml," + format);
   194         }
   188         }
   195         this.log("Output format: " + format);
   189         this.log("Output format: " + format);
   196         // -t off
   190         // -t off
   197         task.createArg().setValue("-t");
   191         task.createArg().setValue("-t");
   198         task.createArg().setValue(auto ? "on" : "off");
   192         task.createArg().setValue(auto ? "on" : "off");
   222         }
   216         }
   223         for (int i = 0; i < srcs.size(); i++) {
   217         for (int i = 0; i < srcs.size(); i++) {
   224             if (i != srcs.size() - 1) {
   218             if (i != srcs.size() - 1) {
   225                 task.createArg().setValue("-i");
   219                 task.createArg().setValue("-i");
   226                 task.createArg().setValue(srcs.elementAt(i));
   220                 task.createArg().setValue(srcs.elementAt(i));
   227             } else {
   221             }
       
   222             else {
   228                 task.createArg().setValue(srcs.elementAt(i));
   223                 task.createArg().setValue(srcs.elementAt(i));
   229                 task.createArg().setValue(dest.toString());
   224                 task.createArg().setValue(dest.toString());
   230             }
   225             }
   231         }
   226         }
   232         // output path
   227         // output path