jdaugherty commented on code in PR #15568:
URL: https://github.com/apache/grails-core/pull/15568#discussion_r3227466357


##########
build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy:
##########
@@ -32,33 +37,405 @@ import org.gradle.api.plugins.quality.CheckstylePlugin
 import org.gradle.api.plugins.quality.CodeNarc
 import org.gradle.api.plugins.quality.CodeNarcExtension
 import org.gradle.api.plugins.quality.CodeNarcPlugin
-
-@CompileStatic
+import org.gradle.api.plugins.quality.Pmd
+import org.gradle.api.plugins.quality.PmdExtension
+import org.gradle.api.plugins.quality.PmdPlugin
+
+import com.github.spotbugs.snom.Confidence
+import com.github.spotbugs.snom.Effort
+import com.github.spotbugs.snom.SpotBugsExtension
+import com.github.spotbugs.snom.SpotBugsPlugin
+import com.github.spotbugs.snom.SpotBugsTask
+import org.gradle.api.tasks.testing.Test
+import org.gradle.testing.jacoco.plugins.JacocoPlugin
+import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
+import org.gradle.testing.jacoco.tasks.JacocoReport
+
+/**
+ * Convention plugin for Grails code style enforcement.
+ */
+@CompileDynamic
 class GrailsCodeStylePlugin implements Plugin<Project> {
 
     static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle'
+    static String CHECKSTYLE_ENABLED_PROPERTY = 
'grails.codestyle.enabled.checkstyle'
     static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml'
     static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 
'checkstyle-suppressions.xml'
 
+    static String PMD_DIR_PROPERTY = 'grails.codestyle.dir.pmd'
+    static String PMD_ENABLED_PROPERTY = 'grails.codestyle.enabled.pmd'
+    static String PMD_CONFIG_FILE_NAME = 'pmd.xml'
+
     static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc'
+    static String CODENARC_ENABLED_PROPERTY = 
'grails.codestyle.enabled.codenarc'
     static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy'
 
+    static String CODENARC_FIX_PROPERTY = 'grails.codestyle.codenarc.fix'
+
+    static String SPOTBUGS_ENABLED_PROPERTY = 
'grails.codestyle.enabled.spotbugs'
+
+    static String JACOCO_ENABLED_PROPERTY = 'grails.codestyle.enabled.jacoco'
+
+    static String IGNORE_FAILURES_PROPERTY = 'grails.codestyle.ignoreFailures'
+
+    static String TEST_STYLING_PROPERTY = 'grails.codestyle.enabled.tests'
+
     static String BASE_RESOURCE_PATH = 
'/META-INF/org.apache.grails.buildsrc.codestyle'
 
     @Override
     void apply(Project project) {
         initExtension(project)
         configureCodeStyle(project)
-        doNotApplyStylingToTests(project)
+        configureAggregation(project)
+        
+        boolean jacocoEnabled = GradleUtils.lookupProperty(project, 
JACOCO_ENABLED_PROPERTY, false)
+        if (jacocoEnabled) {
+            configureJacoco(project)
+            if (project == project.rootProject) {
+                project.logger.info("JaCoCo enabled globally, applying to 
subprojects")
+                project.subprojects.each { subproject ->
+                    subproject.pluginManager.withPlugin('java') {
+                        configureJacoco(subproject)
+                    }
+                    subproject.pluginManager.withPlugin('groovy') {
+                        configureJacoco(subproject)
+                    }
+                }
+            }
+        }
+    }
+
+    static void configureJacoco(Project project) {
+        project.logger.info("Configuring JaCoCo for project: ${project.name}")
+        project.pluginManager.apply(JacocoPlugin)
+
+        project.extensions.configure(JacocoPluginExtension) {
+            it.toolVersion = "0.8.14"
+        }
+
+        project.tasks.withType(Test).configureEach {
+            it.finalizedBy 'jacocoTestReport'
+        }
+
+        project.tasks.withType(JacocoReport).configureEach {
+            it.dependsOn project.tasks.withType(Test)
+            it.reports {
+                it.xml.required = true
+                it.html.required = true
+                it.csv.required = true
+            }
+        }
+    }
+
+    private static void configureAggregation(Project project) {
+        Project root = project.rootProject
+        if (root.tasks.findByName('aggregateStyleViolations')) {
+            return
+        }
+
+        root.tasks.register('aggregateStyleViolations') { task ->
+            task.group = 'verification'
+            task.description = 'Aggregates all code style violations into 
separate reports'
+
+            boolean checkTests = GradleUtils.lookupProperty(project, 
TEST_STYLING_PROPERTY, false)
+            boolean jacocoEnabled = GradleUtils.lookupProperty(project, 
JACOCO_ENABLED_PROPERTY, false)
+
+            // Dependencies: all check tasks in all subprojects
+            root.subprojects.each { subproject ->
+                // CodeNarc (prod only unless test styling is enabled)
+                task.dependsOn(subproject.tasks.withType(CodeNarc).matching { 
t ->
+                    checkTests || (!t.name.toLowerCase().contains('test') && 
!t.name.toLowerCase().contains('integrationtest'))
+                })
+
+                // Checkstyle (prod only unless test styling is enabled)
+                task.dependsOn(subproject.tasks.withType(Checkstyle).matching 
{ t ->
+                    checkTests || (!t.name.toLowerCase().contains('test') && 
!t.name.toLowerCase().contains('integrationtest'))
+                })
+
+                if (GradleUtils.lookupProperty(project, PMD_ENABLED_PROPERTY, 
false)) {
+                    task.dependsOn(subproject.tasks.withType(Pmd).matching { t 
->
+                        checkTests || (!t.name.toLowerCase().contains('test') 
&& !t.name.toLowerCase().contains('integrationtest'))
+                    })
+                }
+
+                if (GradleUtils.lookupProperty(project, 
SPOTBUGS_ENABLED_PROPERTY, false)) {
+                    
task.dependsOn(subproject.tasks.withType(SpotBugsTask).matching { t ->
+                        checkTests || (!t.name.toLowerCase().contains('test') 
&& !t.name.toLowerCase().contains('integrationtest'))
+                    })
+                }
+
+                if (jacocoEnabled) {
+                    task.dependsOn(subproject.tasks.withType(JacocoReport))
+                }
+            }
+
+            def reportsDir = 
project.extensions.getByType(GrailsCodeStyleExtension).reportsDirectory
+            task.inputs.dir(reportsDir).optional()
+            
task.outputs.file(root.layout.projectDirectory.file('CODENARC_VIOLATIONS.md'))

Review Comment:
   can we put them under a common folder then?  Putting lots of files at the 
root often leads to accidental check ins.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to