matrei commented on code in PR #15316: URL: https://github.com/apache/grails-core/pull/15316#discussion_r2693832879
########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + createOrLoad(toCreate.resolve(CODENARC_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/codenarc/${CODENARC_CONFIG_FILE_NAME}" as String) + + directory + }) + } + + private static void createOrLoad(Path expectedPath, String defaultResource) { + if (!Files.exists(expectedPath) || expectedPath.size() == 0) { + InputStream defaultValue = GrailsCodeStylePlugin.getResourceAsStream(defaultResource) + if (!defaultValue) { + throw new IllegalStateException("Could not locate default configuration file: ${defaultResource}") + } + expectedPath.text = defaultValue.text + } + } + + private static void doNotApplyStylingToTests(Project project) { + project.tasks.named('checkstyleTest') { + it.enabled = false // Do not check test sources at this time + } + + project.afterEvaluate { + // Do not check test sources at this time + ['codenarcIntegrationTest', 'codenarcTest'].each { String testTaskName -> Review Comment: (cleanup) Type is inferred ```suggestion ['codenarcIntegrationTest', 'codenarcTest'].each { testTaskName -> ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) Review Comment: - (cleanup) Type is inferred ```suggestion def gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" Review Comment: - (codestyle) Single quotes for Strings? ```suggestion static String BASE_RESOURCE_PATH = '/META-INF/org.apache.grails.buildsrc.codestyle' ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStyleExtension.groovy: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import javax.inject.Inject + +import groovy.transform.CompileStatic + +import org.gradle.api.Project +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.model.ObjectFactory + +@CompileStatic +class GrailsCodeStyleExtension { + + /** + * Defaults to project.buildDir/checkstyle. Default checkstyle files will be written here and used from this location. + */ + final DirectoryProperty checkstyleDirectory + + /** + * Defaults to project.buildDir/codenarc. Default codenarc files will be written here and used from this location. Review Comment: - (readability) Break line ```suggestion * Defaults to project.buildDir/codenarc. * Default codenarc files will be written here and used from this location. ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') Review Comment: - (cleanup) Type is inferred - (readability) Break lines - (cleanup) Use `project.property(String)` as we know property exists ```suggestion def directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.property(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') Review Comment: - (cleanup) Type is inferred - (readability) Break line - (cleanup) Use `project.property(String)` as we know property exists ```suggestion def directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.property(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) Review Comment: - (readability) Break line - (cleanup) Unnecessary cast ```suggestion createOrLoad( toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" ) ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + createOrLoad(toCreate.resolve(CODENARC_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/codenarc/${CODENARC_CONFIG_FILE_NAME}" as String) + + directory + }) + } + + private static void createOrLoad(Path expectedPath, String defaultResource) { + if (!Files.exists(expectedPath) || expectedPath.size() == 0) { + InputStream defaultValue = GrailsCodeStylePlugin.getResourceAsStream(defaultResource) Review Comment: - (cleanup) Type is inferred ```suggestion def defaultValue = GrailsCodeStylePlugin.getResourceAsStream(defaultResource) ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStyleExtension.groovy: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import javax.inject.Inject + +import groovy.transform.CompileStatic + +import org.gradle.api.Project +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.model.ObjectFactory + +@CompileStatic +class GrailsCodeStyleExtension { + + /** + * Defaults to project.buildDir/checkstyle. Default checkstyle files will be written here and used from this location. + */ + final DirectoryProperty checkstyleDirectory + + /** + * Defaults to project.buildDir/codenarc. Default codenarc files will be written here and used from this location. + */ + final DirectoryProperty codenarcDirectory + + @Inject + GrailsCodeStyleExtension(ObjectFactory objects, Project project) { + checkstyleDirectory = objects.directoryProperty().convention(project.rootProject.layout.buildDirectory.dir('checkstyle')) + codenarcDirectory = objects.directoryProperty().convention(project.rootProject.layout.buildDirectory.dir('codenarc')) Review Comment: - (readability) Break lines ```suggestion checkstyleDirectory = objects.directoryProperty().convention( project.rootProject.layout.buildDirectory.dir('checkstyle') ) codenarcDirectory = objects.directoryProperty().convention( project.rootProject.layout.buildDirectory.dir('codenarc') ) ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() Review Comment: - (cleanup) Type is inferred - (codestyle) Use Groovy property syntax ```suggestion def toCreate = directory.asFile.toPath() ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) Review Comment: (readability) Break line (cleanup) Unnecessary cast ```suggestion createOrLoad( toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" ) ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStyleExtension.groovy: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import javax.inject.Inject + +import groovy.transform.CompileStatic + +import org.gradle.api.Project +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.model.ObjectFactory + +@CompileStatic +class GrailsCodeStyleExtension { + + /** + * Defaults to project.buildDir/checkstyle. Default checkstyle files will be written here and used from this location. Review Comment: - (readability) Break line ```suggestion * Defaults to project.buildDir/checkstyle. * Default checkstyle files will be written here and used from this location. ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') + + Path toCreate = directory.getAsFile().toPath() Review Comment: (cleanup) Type is inferred (codestyle) Use Groovy property syntax ```suggestion def toCreate = directory.asFile.toPath() ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + createOrLoad(toCreate.resolve(CODENARC_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/codenarc/${CODENARC_CONFIG_FILE_NAME}" as String) + + directory + }) + } + + private static void createOrLoad(Path expectedPath, String defaultResource) { + if (!Files.exists(expectedPath) || expectedPath.size() == 0) { + InputStream defaultValue = GrailsCodeStylePlugin.getResourceAsStream(defaultResource) + if (!defaultValue) { + throw new IllegalStateException("Could not locate default configuration file: ${defaultResource}") + } + expectedPath.text = defaultValue.text + } + } + + private static void doNotApplyStylingToTests(Project project) { + project.tasks.named('checkstyleTest') { + it.enabled = false // Do not check test sources at this time + } + + project.afterEvaluate { + // Do not check test sources at this time + ['codenarcIntegrationTest', 'codenarcTest'].each { String testTaskName -> + if (project.tasks.names.contains(testTaskName)) { + project.tasks.named(testTaskName) { + it.enabled = false + } + } + } + } + } + + private static void configureCodeStyle(Project project) { + configureCheckstyle(project) + configureCodenarc(project) + + project.tasks.register('codeStyle') { + it.group = 'verification' + it.description = 'Runs code style checks' + it.dependsOn(project.tasks.withType(Checkstyle)) + it.dependsOn(project.tasks.withType(CodeNarc)) + } + } + + static void configureCheckstyle(Project project) { + project.pluginManager.apply(CheckstylePlugin) + + project.extensions.configure(CheckstyleExtension) { + // Explicit `it` is required in extension configuration + it.getConfigDirectory().set(project.extensions.getByType(GrailsCodeStyleExtension).checkstyleDirectory) + it.maxWarnings = 0 + it.showViolations = true + it.ignoreFailures = false + it.toolVersion = project.findProperty('checkstyleVersion') + } + + project.tasks.withType(Checkstyle).configureEach { + it.group = 'verification' + it.onlyIf { !project.hasProperty('skipCodeStyle') } + } + } + + static void configureCodenarc(Project project) { + project.pluginManager.apply(CodeNarcPlugin) + + project.extensions.configure(CodeNarcExtension) { + it.configFile = project.extensions.getByType(GrailsCodeStyleExtension).codenarcDirectory.get().file(CODENARC_CONFIG_FILE_NAME).getAsFile() Review Comment: - (readability) Break line - (codestyle) Use Groovy property ```suggestion it.configFile = project.extensions.getByType(GrailsCodeStyleExtension) .codenarcDirectory.get().file(CODENARC_CONFIG_FILE_NAME).asFile ``` ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + Review Comment: - (codestyle) No more than one blank line ########## build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/GrailsCodeStylePlugin.groovy: ########## @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * https://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. + */ +package org.apache.grails.buildsrc + +import java.nio.file.Files +import java.nio.file.Path + +import groovy.transform.CompileStatic + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.Directory +import org.gradle.api.plugins.quality.Checkstyle +import org.gradle.api.plugins.quality.CheckstyleExtension +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 +class GrailsCodeStylePlugin implements Plugin<Project> { + + static String CHECKSTYLE_DIR_PROPERTY = 'grails.codestyle.dir.checkstyle' + static String CHECKSTYLE_CONFIG_FILE_NAME = 'checkstyle.xml' + static String CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME = 'checkstyle-suppressions.xml' + + static String CODENARC_DIR_PROPERTY = 'grails.codestyle.dir.codenarc' + static String CODENARC_CONFIG_FILE_NAME = 'codenarc.groovy' + + static String BASE_RESOURCE_PATH = "/META-INF/org.apache.grails.buildsrc.codestyle" + + @Override + void apply(Project project) { + initExtension(project) + configureCodeStyle(project) + doNotApplyStylingToTests(project) + } + + private static void initExtension(Project project) { + GrailsCodeStyleExtension gce = project.extensions.create('grailsCodeStyle', GrailsCodeStyleExtension) + + // Unfortunately, the codenarc plugin is still using a non-lazy property. + // Rather than rewrite the plugin to use afterEvaluate, + // this plugin uses properties to override the configuration location by default + + + gce.checkstyleDirectory.set(project.provider { + Directory directory = project.hasProperty(CHECKSTYLE_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CHECKSTYLE_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('checkstyle') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + + createOrLoad(toCreate.resolve(CHECKSTYLE_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_CONFIG_FILE_NAME}" as String) + createOrLoad(toCreate.resolve(CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/checkstyle/${CHECKSTYLE_SUPPRESSION_CONFIG_FILE_NAME}" as String) + + directory + }) + + gce.codenarcDirectory.set(project.provider { + Directory directory = project.hasProperty(CODENARC_DIR_PROPERTY) ? project.rootProject.layout.projectDirectory.dir(project.findProperty(CODENARC_DIR_PROPERTY) as String) : project.rootProject.layout.buildDirectory.get().dir('codestyle').dir('codenarc') + + Path toCreate = directory.getAsFile().toPath() + Files.createDirectories(toCreate) + + createOrLoad(toCreate.resolve(CODENARC_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/codenarc/${CODENARC_CONFIG_FILE_NAME}" as String) Review Comment: (readability) Break line (cleanup) Unnecessary cast ```suggestion createOrLoad( toCreate.resolve(CODENARC_CONFIG_FILE_NAME), "${BASE_RESOURCE_PATH}/codenarc/${CODENARC_CONFIG_FILE_NAME}" ) ``` -- 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]
