Skip to content

Commit

Permalink
added various further parameter to task and extension (#11)
Browse files Browse the repository at this point in the history
Namely:
* ignoreAnnotations
* ignoreIdentifiers
* ignoreLiterals
* language
* skipBlocks
* skipBlocksPattern
  • Loading branch information
aaschmid committed Apr 19, 2015
1 parent a450cba commit 7936bbc
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ e.g. using ```cpdCheck { }```:
| Attribute | Default | Applies for ```language``` |
| ----------------- |:--------------------:|:--------------------------:|
| encoding | System default | |
| ignoreAnnotations | ```false``` | ```'java'``` |
| ignoreFailures | ```false``` | |
| ignoreIdentifiers | ```false``` | ```'java'``` |
| ignoreLiterals | ```false``` | ```'java'``` |
| language | ```'java'``` | |
| minimumTokenCount | ```50``` | |
| skipBlocks | ```true``` | ```'cpp'``` |
| skipBlocksPattern | ```'#if 0|#endif'``` | ```'cpp'``` |

For more information about options and their descriptions, see [here](http://pmd.sourceforge.net/usage/cpd-usage.html#Options).
For more information about options and their descriptions, see [here](http://pmd.sourceforge.net/usage/cpd-usage.html#Options),
and for the available programming languages have a look on [CPD documentation](http://pmd.sourceforge.net/usage/cpd-usage.html#Supported_Languages).
To request more options, please file an issue [here](/../../issues).

Additionally, one can configure the following reports for every task analogous to
Expand Down
59 changes: 59 additions & 0 deletions src/main/groovy/de/aaschmid/gradle/plugins/cpd/Cpd.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import javax.inject.Inject
*
* task cpd(type: Cpd, description: 'Copy/Paste detection for all Ruby scripts') {
*
* // change language of cpd to Ruby
* language = 'ruby'
*
* // set minimum token count causing a duplication warning
* minimumTokenCount = 10
*
Expand Down Expand Up @@ -71,6 +74,15 @@ class Cpd extends SourceTask implements VerificationTask, Reporting<CpdReports>
@Input
String encoding

/**
* Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very
* redundant and causes false positives.
* <p>
* Example: {@code ignoreAnnotations = true}
*/
@Input
boolean ignoreAnnotations

/**
* Whether or not to allow the build to continue if there are warnings.
* <p>
Expand All @@ -79,6 +91,32 @@ class Cpd extends SourceTask implements VerificationTask, Reporting<CpdReports>
@Input
boolean ignoreFailures

/**
* Option if CPD should ignore identifiers differences, i.e. variable names, methods names, and so forth,
* when evaluating a duplicate block.
* <p>
* Example: {@code ignoreIdentifiers = true}
*/
@Input
boolean ignoreIdentifiers

/**
* Option if CPD should ignore literal value differences when evaluating a duplicate block. This means e.g. that
* {@code foo=42;} and {@code foo=43;} will be seen as equivalent.
* <p>
* Example: {@code ignoreLiterals = true}
*/
@Input
boolean ignoreLiterals

/**
* Flag to select the appropriate language.
* <p>
* Example: {@code language = 'java'}
*/
@Input
String language

/**
* A positive integer indicating the minimum token count to trigger a CPD match; defaults to
* {@link CpdExtension#getMinimumTokenCount()}.
Expand All @@ -94,6 +132,27 @@ class Cpd extends SourceTask implements VerificationTask, Reporting<CpdReports>
@InputFiles
FileCollection pmdClasspath

/**
* Enables or disables skipping of blocks configured by {@link #skipBlocksPattern}.
* <p>
* Example: {@code skipBlocks = false}
* @see #skipBlocksPattern
*/
@Input
boolean skipBlocks

/**
* Configures the pattern, to find the blocks to skip if enabled using {@link #skipBlocks}. It is a {@link String}
* property and contains of two parts, separated by {@code '|'}. The first part is the start pattern, the second part
* is the ending pattern.
* <p>
* Example: {@code skipBlocksPattern = '#include <|>'}
* @see #skipBlocks
*/
@Input
String skipBlocksPattern


@Nested
private final CpdReportsImpl reports

Expand Down
59 changes: 59 additions & 0 deletions src/main/groovy/de/aaschmid/gradle/plugins/cpd/CpdExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.gradle.api.plugins.quality.CodeQualityExtension
*
* cpd {
* encoding = 'UTF-8'
* ignoreAnnotations = true
* language = 'java'
* minimumTokenCount = 20
* }
* </pre>
Expand All @@ -36,6 +38,26 @@ class CpdExtension extends CodeQualityExtension {
*/
String encoding = System.getProperty('file.encoding')

/**
* Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very
* redundant and causes false positives; defaults to {@code false}.
* <p>
* Note: if this option is recognized depends on the used {@link #language} and its tokenizer.
* <p>
* Example: {@code ignoreAnnotations = true}
*/
boolean ignoreAnnotations = false

/**
* Option if CPD should ignore identifiers differences, i.e. variable names, methods names, and so forth,
* when evaluating a duplicate block; defaults to {@code false}.
* <p>
* Note: if this option is recognized depends on the used {@link #language} and its tokenizer.
* <p>
* Example: {@code ignoreIdentifiers = true}
*/
boolean ignoreIdentifiers = false

/**
* Whether or not to allow the build to continue if there are warnings; defaults to {@code false}, as for any other
* static code analysis tool.
Expand All @@ -44,10 +66,47 @@ class CpdExtension extends CodeQualityExtension {
*/
boolean ignoreFailures = false

/**
* Option if CPD should ignore literal value differences when evaluating a duplicate block; defaults to
* {@code false}. This means e.g. that {@code foo=42;} and {@code foo=43;} will be seen as equivalent.
* <p>
* Note: if this option is recognized depends on the used {@link #language} and its tokenizer.
* <p>
* Example: {@code ignoreLiterals = true}
*/
boolean ignoreLiterals = false

/**
* Flag to select the appropriate language; defaults to {@code 'java'}.
* <p>
* Example: {@code language = 'java'}
*/
String language = 'java'

/**
* A positive integer indicating the minimum token count to trigger a CPD match; defaults to {@code 50}.
* <p>
* Example: {@code minimumTokenCount = 25}
*/
int minimumTokenCount = 50

/**
* Enables or disables skipping of blocks configured by {@link #skipBlocksPattern} like a pre-processor;
* defaults to {@code true}.
* <p>
* Example: {@code skipBlocks = false}
* @see #skipBlocksPattern
*/
boolean skipBlocks = true

/**
* CConfigures the pattern, to find the blocks to skip if enabled using {@link #skipBlocks}. It is a {@link String}
* property and contains of two parts, separated by {@code '|'}. The first part is the start pattern, the second part
* is the ending pattern. defaults to {@code '#if 0|#endif'} (which should be the same as
* {@link net.sourceforge.pmd.cpd.Tokenizer#DEFAULT_SKIP_BLOCKS_PATTERN}).
* <p>
* Example: {@code skipBlocksPattern = '#include <|>'}
* @see #skipBlocks
*/
String skipBlocksPattern = '#if 0|#endif'
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ class CpdPlugin implements Plugin<Project> {
project.tasks.withType(Cpd){ Cpd task ->
task.conventionMapping.with{
encoding = { extension.encoding }
ignoreAnnotations = { extension.ignoreAnnotations }
ignoreFailures = { extension.ignoreFailures }
ignoreIdentifiers = { extension.ignoreIdentifiers }
ignoreLiterals = { extension.ignoreLiterals }
language = { extension.language }
minimumTokenCount = { extension.minimumTokenCount }
pmdClasspath = { project.configurations.findByName('cpd') }
skipBlocks = { extension.skipBlocks }
skipBlocksPattern = { extension.skipBlocksPattern }
}
task.reports.all{ report ->
report.conventionMapping.with{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package de.aaschmid.gradle.plugins.cpd.internal
import de.aaschmid.gradle.plugins.cpd.Cpd
import net.sourceforge.pmd.cpd.CPD
import net.sourceforge.pmd.cpd.CPDConfiguration
import net.sourceforge.pmd.cpd.Language
import net.sourceforge.pmd.cpd.LanguageFactory
import net.sourceforge.pmd.cpd.Match
import net.sourceforge.pmd.cpd.ReportException
import net.sourceforge.pmd.cpd.Tokenizer
import org.gradle.api.GradleException
import org.gradle.api.InvalidUserDataException
import org.gradle.api.file.FileTree
Expand All @@ -17,6 +19,7 @@ public class CpdExecutor {
private static final Logger logger = Logging.getLogger(CpdExecutor.class);

private final String encoding;
private final Language language;
private final int minimumTokenCount;
private final FileTree source;

Expand All @@ -30,19 +33,17 @@ public class CpdExecutor {
}

this.encoding = task.getEncoding();
this.language = createLanguage(task);
this.minimumTokenCount = task.getMinimumTokenCount();
this.source = task.getSource();
}

public List<Match> run() {
Properties p = new Properties();
try {
if (logger.isInfoEnabled()) {
logger.info("Starting CPD, minimumTokenCount is {}", minimumTokenCount);
}
def language = new LanguageFactory().createLanguage("java", p);
def config = new CPDConfiguration(minimumTokenCount, language, encoding);
def cpd = new CPD(config);
def cpd = new CPD(new CPDConfiguration(minimumTokenCount, language, encoding));

if (logger.isInfoEnabled()) {
logger.info("Tokenizing files");
Expand Down Expand Up @@ -81,4 +82,20 @@ public class CpdExecutor {
}
return [];
}

private Language createLanguage(Cpd task) {
Properties p = new Properties();
if (task.getIgnoreLiterals()) {
p.setProperty(Tokenizer.IGNORE_LITERALS, "true");
}
if (task.getIgnoreIdentifiers()) {
p.setProperty(Tokenizer.IGNORE_IDENTIFIERS, "true");
}
if (task.getIgnoreAnnotations()) {
p.setProperty(Tokenizer.IGNORE_ANNOTATIONS, "true");
}
p.setProperty(Tokenizer.OPTION_SKIP_BLOCKS, Boolean.toString(task.getSkipBlocks()));
p.setProperty(Tokenizer.OPTION_SKIP_BLOCKS_PATTERN, task.getSkipBlocksPattern());
return LanguageFactory.createLanguage(task.getLanguage(), p);
}
}
Loading

0 comments on commit 7936bbc

Please sign in to comment.