import org.codenarc.rule.generic.RequiredStringRule

final TEST_FILES = ".*(Test|Tests|TestCase)\\.groovy"

ruleset {
    description 'RuleSet for running CodeNarc against the project source code. See RunCodeNarcAgainstProjectSourceCodeTest'

    ruleset('rulesets/basic.xml')

    ruleset('rulesets/braces.xml')

    ruleset('rulesets/convention.xml') {
        exclude 'NoDef'
    }

    ruleset('rulesets/design.xml') {
        Instanceof(enabled:false)
        PublicInstanceField(doNotApplyToClassNames:'*Test') //Tests are guarded by JUnitPublicField
    }

    ruleset('rulesets/dry.xml') {
        include 'DuplicateMapLiteral'
    }
    ruleset('rulesets/exceptions.xml')

    ruleset('rulesets/groovyism.xml') {
        GStringExpressionWithinString(doNotApplyToClassNames:'*Test')
    }

    ruleset('rulesets/formatting.xml') {
        exclude 'MissingBlankLineAfterImports'
        exclude 'SpaceAfterCatch'
        exclude 'SpaceAfterFor'
        exclude 'SpaceAfterIf'
        exclude 'SpaceAfterSwitch'
        exclude 'SpaceAfterWhile'
        exclude 'SpaceAroundMapEntryColon'
        exclude 'TrailingWhitespace'
    }

    ruleset('rulesets/imports.xml') {
        exclude 'NoWildcardImports'
    }

    ruleset('rulesets/junit.xml') {
        exclude 'JUnitStyleAssertions'
    }
    ruleset('rulesets/logging.xml')

    ruleset('rulesets/naming.xml') {
        PackageNameMatchesFilePath(groupId:'org.codenarc')
    }

    ruleset('rulesets/size.xml') {
        exclude 'CrapMetric'
    }

    ruleset('rulesets/unnecessary.xml') {
        exclude 'UnnecessaryGetter'
        exclude 'UnnecessaryObjectReferences'
        exclude 'UnnecessaryReturnKeyword'
    }

    ruleset('rulesets/unused.xml')

    //------------------------------------------------------------------------------------
    // Custom rules
    //------------------------------------------------------------------------------------

    // Ensure that Rule and AstVisitor classes do not contain unnecessary public methods
    IllegalClassMember {
        name = 'UnnecessaryPublicMethodsInRuleClasses'
        applyToClassNames = 'Rule, *AstVisitor'
        doNotApplyToClassNames = 'AstVisitor, FieldReferenceAstVisitor'
        doNotApplyToFilesMatching = TEST_FILES
        illegalMethodModifiers = 'public'
        ignoreMethodsWithAnnotationNames = 'Override'
        ignoreMethodNames = 'visit*, handleClosure'
        description = 'Checks that Rule and AstVisitor classes do not contain unnecessary public methods'
    }

    // Ensure that each source file contains the Apache License header
    rule(RequiredStringRule) {
        name = 'ApacheLicenseRequired'
        string = 'Licensed under the Apache License, Version 2.0 (the "License");'
        violationMessage = 'The Apache License 2.0 comment is missing from the source file'
        description = 'Checks that all source files contain the Apache License 2.0 comment'
    }

}
