jdaugherty commented on code in PR #15948: URL: https://github.com/apache/grails-core/pull/15948#discussion_r3608658628
########## grails-core/src/main/groovy/org/grails/compiler/injection/FactoriesFileWriter.groovy: ########## @@ -0,0 +1,136 @@ +/* + * 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.grails.compiler.injection + +import java.lang.reflect.Modifier + +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ClassNode + +import org.springframework.core.CollectionFactory + +import org.apache.grails.gradle.common.PropertyFileUtils + +/** + * Writes factory registrations for compiled classes into a factories file in the compilation + * target directory, merging any existing entries from previous compilation runs and from + * hand-authored source registrations. The factories file location is supplied by the caller, + * so the writer is shared by transformations targeting different registration files + * (e.g. {@code META-INF/grails.factories} and {@code META-INF/grails-cli.factories}). + * + * @since 8.0 + */ +@CompileStatic +class FactoriesFileWriter { + + /** + * Registers the class as an implementation of the given super type in the factories file + * when the class is a non-abstract subtype. + * + * @param classNode the compiled class + * @param superType the factory type to register the class under + * @param compilationTargetDirectory the compilation output directory + * @param factoriesLocation the factories file path relative to the target directory + * @param sourceFactoriesLocations project-relative paths of hand-authored factories files to merge + * @return {@code true} when the class was a subtype of the factory type + */ + static boolean updateFactoriesWithType(ClassNode classNode, ClassNode superType, File compilationTargetDirectory, + String factoriesLocation, List<String> sourceFactoriesLocations) { + if (GrailsASTUtils.isSubclassOfOrImplementsInterface(classNode, superType)) { + if (Modifier.isAbstract(classNode.getModifiers())) { + return false + } + + def classNodeName = classNode.name + // Use SortedProperties to ensure a consistent order of entries for reproducible builds + def props = CollectionFactory.createSortedProperties(false) + def superTypeName = superType.getName() + + File factoriesFile = new File(compilationTargetDirectory, factoriesLocation) + if (!factoriesFile.parentFile.exists()) { + factoriesFile.parentFile.mkdirs() + } + loadFromFile(props, factoriesFile) Review Comment: This code was modeled off the existing factories writer. Yes, this is an issue but it's been a long standing issue that would require tracking the current class set. Incremental compile is considered an incubating feature of Gradle and not a default feature. This is something we should track separately from this change. I've opened https://github.com/apache/grails-core/issues/16007 for this. ########## grails-core/src/main/groovy/org/grails/compiler/injection/FactoriesFileWriter.groovy: ########## @@ -0,0 +1,136 @@ +/* + * 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.grails.compiler.injection + +import java.lang.reflect.Modifier + +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ClassNode + +import org.springframework.core.CollectionFactory + +import org.apache.grails.gradle.common.PropertyFileUtils + +/** + * Writes factory registrations for compiled classes into a factories file in the compilation + * target directory, merging any existing entries from previous compilation runs and from + * hand-authored source registrations. The factories file location is supplied by the caller, + * so the writer is shared by transformations targeting different registration files + * (e.g. {@code META-INF/grails.factories} and {@code META-INF/grails-cli.factories}). + * + * @since 8.0 + */ +@CompileStatic +class FactoriesFileWriter { + + /** + * Registers the class as an implementation of the given super type in the factories file + * when the class is a non-abstract subtype. + * + * @param classNode the compiled class + * @param superType the factory type to register the class under + * @param compilationTargetDirectory the compilation output directory + * @param factoriesLocation the factories file path relative to the target directory + * @param sourceFactoriesLocations project-relative paths of hand-authored factories files to merge + * @return {@code true} when the class was a subtype of the factory type + */ + static boolean updateFactoriesWithType(ClassNode classNode, ClassNode superType, File compilationTargetDirectory, + String factoriesLocation, List<String> sourceFactoriesLocations) { + if (GrailsASTUtils.isSubclassOfOrImplementsInterface(classNode, superType)) { + if (Modifier.isAbstract(classNode.getModifiers())) { + return false + } + + def classNodeName = classNode.name + // Use SortedProperties to ensure a consistent order of entries for reproducible builds + def props = CollectionFactory.createSortedProperties(false) + def superTypeName = superType.getName() + + File factoriesFile = new File(compilationTargetDirectory, factoriesLocation) + if (!factoriesFile.parentFile.exists()) { + factoriesFile.parentFile.mkdirs() + } + loadFromFile(props, factoriesFile) + + File sourceDirectory = findSourceDirectory(compilationTargetDirectory) + if (sourceDirectory != null) { + for (String sourceFactoriesLocation : sourceFactoriesLocations) { + File sourceFactoriesFile = new File(sourceDirectory, sourceFactoriesLocation) + loadFromFile(props, sourceFactoriesFile) + } + } + + addToProps(props, superTypeName, classNodeName) + + factoriesFile.withWriter { Writer writer -> Review Comment: This code was modeled off the existing factories writer. Yes, this is an issue but it's been a long standing issue that would require tracking the current class set. Incremental compile is considered an incubating feature of Gradle and not a default feature. This is something we should track separately from this change. I've opened https://github.com/apache/grails-core/issues/16007 for this. -- 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]
