org.openscm.kundo.plugins.CheckstyleDelegate.groovy Maven / Gradle / Ivy
The newest version!
package org.openscm.kundo.plugins
/*
* Copyright (C) 2008 The Ultimate People Company Ltd ("UPCO").
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.openscm.kundo.plugins.context.BuildContext
/**
* Checkstyle Delegate
* @author Steve king
* @version 1.0.0
*
* Description: A Checkstyle Delegate that creates Checkstyle reports
*
* The Checkstyle plugin creates checkstyle reports on configurable coding standards
*
* The doCheckstyle target will run the checkstyle Ant task over your specified source folders.
* it is also possible to specify a comma separated list of extra folders to check using the
* checkstyle.optional.dirs property. Reports will be printed in XML by default but can also
* be generated in html by use of the ant properties below.
*/
/*
* @ant-target doCheckstyle
*/
class CheckstyleDelegate extends AbstractPluginTargetDelegate {
/*
* A flag if checkstyle reporting is enabled
* @ant-property-name checkstyle.enabled
*/
String enabled
/*
* A flag if checkstyle reporting is enabled
* @ant-property-name checkstyle.print.html.report
*/
String printHtmlReport
/*
* The directory to have the checkstyle xml report writtenm to
* @ant-property-name checkstyle.xmlreportdir
*/
String xmlreportdir
/*
* The directory to have the checkstyle html report writtenm to
* @ant-property-name checkstyle.htmlreportdir
*/
String htmlreportdir
/*
* The name to assign the checkstyle report
* @ant-property-name checkstyle.xmlreportname
*/
String xmlreportname
/*
* The name to assign the checkstyle report
* @ant-property-name checkstyle.htmlreportname
*/
String htmlreportname
/*
* The location of the checkstyle checkstyle_config.xml file
* @ant-property-name checkstyle.config
*/
String config
/*
* The location of the checkstyle xsl file
* @ant-property-name checkstyle.stylesheet
*/
String stylesheet
/*
* A flag to allow test directories
* @ant-property-name checkstyle.allow.testdir
*/
String allowTestdir
/*
* A flag to determine if a build should fail upon encountering a checkstyle violation
* @ant-property-name checkstyle.failonviolation
*/
String failonviolation
/*
* A comma separated list of optional directories to run checkstyle over
* @ant-property-name checkstyle.optional.dirs
*/
String optionalDirs
/**
* Constructor sets ant and buildContext instances in super class
* @param ant AntBuilder instance
* @param buildContext BuildContext instance
*/
CheckstyleDelegate( AntBuilder ant, BuildContext buildContext ){
super( ant, buildContext)
}
/**
* Setter method for member variable enabled
* @param enabled String representation of a flag to enable checkstyle reporting
*/
void setEnabled( String enabled ){
this.enabled = enabled
}
/**
* Setter method for member variable enabled
* @param printHtmlReport String representation of if html report should be generated
*/
void setPrintHtmlReport( String printHtmlReport ){
this.printHtmlReport = printHtmlReport
}
/**
* Setter method for member variable reportdir
* @param xmlreportdir String representation of the directory path to write xml reports to
*/
void setXmlreportdir( String xmlreportdir ){
this.xmlreportdir = xmlreportdir
}
/**
* Setter method for member variable reportdir
* @param htmlreportdir String representation of the directory path to write html reports to
*/
void setHtmlreportdir( String htmlreportdir ){
this.htmlreportdir = htmlreportdir
}
/**
* Setter method for member variable reportname
* @param xmlreportname String representation of xml report name
*/
void setXmlreportname( String xmlreportname ){
this.xmlreportname = xmlreportname
}
/**
* Setter method for member variable reportname
* @param htmlreportname String representation of html report name
*/
void setHtmlreportname( String htmlreportname ){
this.htmlreportname = htmlreportname
}
/**
* Setter method for member variable enabled
* @param config String representation of the checkstyle_config.xml file location
*/
void setConfig( String config ){
this.config = config
}
/**
* Setter method for member variable enabled
* @param stylesheet String representation of the xsl file location
*/
void setStylesheet( String stylesheet ){
this.stylesheet = stylesheet
}
/**
* Setter method for member variable reportdir
* @param allowTestdir String representation ....
*/
void setAllowTestdir( String allowTestdir ){
this.allowTestdir = allowTestdir
}
/**
* Setter method for member variable failonviolation
* @param failonviolation String representation ....
*/
void setFailonviolation( String failonviolation ){
this.failonviolation = failonviolation
}
/**
* Setter method for member variable optionalDirs
* @param optionalDirs Comma separated list of optional directories to run checkstyle on
*/
void setOptionalDirs( String optionalDirs ){
this.optionalDirs = optionalDirs
}
/**
* Private method for executing the checkstyle report
*/
private void checkstyle() {
// retrieve project plugins
Map properties = buildContext.get( "project.properties" )
List srcProjectProperties = properties.get( "src" )
List testProjectProperties = properties.get( "test" )
ant.taskdef(resource:"checkstyletask.properties", classpathref:"kundo-checkstyle-plugin.dependency.path")
ant.mkdir(dir:xmlreportdir)
ant.checkstyle(config:config, failOnViolation:failonviolation){
srcProjectProperties.each{srcProjectProperty ->
ant.fileset( dir:srcProjectProperty.dir, includes:srcProjectProperty.include )
}
//check if test clases need reporting on
if (allowTestdir.equalsIgnoreCase("true"))
testProjectProperties.each{testProjectProperty ->
ant.fileset( dir:testProjectProperty.dir, includes:testProjectProperty.include )
}
ant.formatter(type:"xml", toFile:xmlreportdir + xmlreportname)
}
if( optionalDirs != null && optionalDirs != "" ){
optionalDirs.split( "," ).each{ dir ->
ant.checkstyle(config:config, failOnViolation:failonviolation){
ant.fileset( dir:dir, includes:"**/*.*" )
ant.formatter(type:"xml", toFile:xmlreportdir + xmlreportname)
}
}
}
if( printHtmlReport != "false" ){
File xslFile = new File( "${ant.project.getProperty( 'basedir' )}${File.separator}${stylesheet}" )
if( xslFile.exists() ){
ant.mkdir(dir:htmlreportdir)
ant.xslt( in : xmlreportdir + xmlreportname, out : htmlreportdir + htmlreportname, style : stylesheet )
}else{
log.info( "Specified xsl file: ${stylesheet} does not exist" )
}
}
}
/**
* Implementation of abstract method from AbstractPluginTargetDelegate
* to hold assertions that all global and local properties are available to the plugin.
* This method is automatically called prior to execution.
*/
void doCheck(){
assert enabled != null
assert printHtmlReport != null
assert xmlreportdir != null
assert htmlreportdir != null
assert config != null
assert allowTestdir != null
assert buildContext.get( "project.properties" ) != null
assert (enabled.equalsIgnoreCase("true") || enabled.equalsIgnoreCase("false"))
assert (allowTestdir.equalsIgnoreCase("true") || allowTestdir.equalsIgnoreCase("false"))
}
/**
* Implementation of abstract method from AbstractPluginTargetDelegate
* this method is automatically called.
*/
void doExecute(){
// check if plugin is enabled
if ( log.isDebugEnabled() )
log.debug( "Begin Checkstyle plugin")
if (enabled.equalsIgnoreCase("true"))
checkstyle()
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy