Skip to content

Instantly share code, notes, and snippets.

@timyates
Created August 16, 2011 12:11
Show Gist options
  • Save timyates/1148940 to your computer and use it in GitHub Desktop.
Save timyates/1148940 to your computer and use it in GitHub Desktop.
Run CodeNarc over the code blocks on the groovy user-guide webpages
@Grab(group='org.codenarc', module='CodeNarc', version='0.15')
@Grab(group='net.sourceforge.nekohtml', module='nekohtml', version='1.9.15')
import org.codenarc.AnalysisContext
import org.codenarc.CodeNarc
import org.codenarc.CodeNarcRunner
import org.codenarc.analyzer.SourceAnalyzer
import org.codenarc.analyzer.StringSourceAnalyzer
import org.codenarc.report.TextReportWriter
import org.codenarc.ruleset.PropertiesFileRuleSetConfigurer
import org.codenarc.results.Results
import org.codenarc.ruleregistry.RuleRegistryInitializer
// URL of the groovy site that you want to analyse
def url = 'http://groovy.codehaus.org/Convert+SQL+Result+To+XML'
// Load it in via XmlSlurper
def page = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parse( url )
// And fetch out all the <PRE> blocks with class='code-java'
def data = page.depthFirst().grep{ it.name() == 'PRE' && it.@class.toString() == 'code-java' }*.text().flatten()
// Join all the code blocks into a single String
String pageCode = new StringWriter().with { w -> data.each { w << "$it\n" } ; w }.toString()
// Then, run the analysis
new Narc().execute( pageCode )
///////////////////////////////////////////////////////////
// Utility classes that I required to get CodeNarc running
// for code in Strings instead of Files. There is probably
// an easier way than this that I missed, but this works of
// a fashion
class NarcWriter extends TextReportWriter {
void writeReport(Writer writer, AnalysisContext analysisContext, Results results) {
initializeResourceBundle()
def printWriter = new PrintWriter( writer )
results.violations.each {
println """$it.rule
\tLine : $it.lineNumber
\tSrc : $it.sourceLine
\tMessage: $it.message"""
}
println "Got ${results.violations.size()} violations"
}
}
class NarcRunner extends CodeNarcRunner {
Results execute() {
new RuleRegistryInitializer().initializeRuleRegistry()
def ruleSet = createRuleSet()
// Hack to remove rules that don't make sense in this context
def disabledRules = [
'Println', // println is fine in script examples
'JavaIoPackageAccess', // use of File is fine
]
ruleSet.rules = ruleSet.rules.findAll { !( it.name in disabledRules ) }
def results = sourceAnalyzer.analyze(ruleSet)
def analysisContext = new AnalysisContext(ruleSet:ruleSet, sourceDirectories:sourceAnalyzer.sourceDirectories)
reportWriters.each { reportWriter ->
reportWriter.writeReport(analysisContext, results)
}
results
}
}
class Narc extends CodeNarc {
protected void execute( src ) {
// Load the latest rules from sourceforge
setDefaultsIfNecessary()
new NarcRunner().with { nr ->
nr.ruleSetFiles = 'http://codenarc.svn.sourceforge.net/viewvc/codenarc/trunk/src/site/resources/StarterRuleSet-AllRules.groovy.txt?revision=813'
nr.reportWriters = [ new NarcWriter() ]
nr.sourceAnalyzer = new StringSourceAnalyzer( src )
nr.execute()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment