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


##########
grails-core/src/test/groovy/org/apache/grails/core/cli/compiler/CommandFactoriesTransformationSpec.groovy:
##########
@@ -0,0 +1,45 @@
+/*
+ *  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.core.cli.compiler
+
+import spock.lang.Specification
+import spock.lang.Unroll
+
+/**
+ * Commands shipped in a companion cli artifact compile from the {@code cli} 
source set
+ * ({@code src/cli/groovy}), which is not a standard project-source location — 
the transformation
+ * must still register them in {@code META-INF/grails-cli.factories}.
+ */
+class CommandFactoriesTransformationSpec extends Specification {

Review Comment:
   Added FactoriesFileWriterSpec, which drives the registration mechanism 
end-to-end: a concrete subtype is registered in the factories file, an abstract 
base class is excluded, a non-subtype is ignored, and two distinct commands 
whose fully-qualified names share a prefix are both kept. The stale legacy 
grails.factories clean-break scenario is left as-is per the thread.



##########
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 ->
+                props.store(writer, 'Grails Factories File')
+            }
+
+            PropertyFileUtils.makePropertiesFileReproducible(factoriesFile)
+
+            return true
+        }
+        return false
+    }
+
+    private static void loadFromFile(Properties props, File factoriesFile) {
+        if (factoriesFile.exists()) {
+            Properties fileProps = new Properties()
+            factoriesFile.withInputStream { InputStream input ->
+                fileProps.load(input)
+                fileProps.each { Map.Entry prop ->
+                    addToProps(props, (String) prop.key, (String) prop.value)
+                }
+            }
+        }
+    }
+
+    private static Properties addToProps(Properties props, String 
superTypeName, String classNodeNames) {
+        final List<String> classNodesNameList = classNodeNames.tokenize(',')
+        classNodesNameList.forEach(classNodeName -> {
+            String existing = props.getProperty(superTypeName)
+            if (!existing) {
+                props.put(superTypeName, classNodeName)
+            } else if (existing && !existing.contains(classNodeName)) {

Review Comment:
   Fixed. addToProps now dedups on exact membership (a LinkedHashSet of trimmed 
names) rather than a substring contains() test, so distinct commands whose 
fully-qualified names are prefixes of one another (Foo vs FooCommand) are both 
registered. FactoriesFileWriterSpec includes a regression case for the prefix 
collision.



##########
grails-bom/base/build.gradle:
##########
@@ -97,6 +97,23 @@ dependencies {
     }
 }
 
+// Companion cli artifacts (published by the cli-artifact convention plugin) 
are additional
+// publications of existing projects, so the subproject enumeration above 
cannot see them. Each
+// applying project exports its companion coordinate via the `cliArtifactId` 
extra property, which
+// only exists once that project has been evaluated — so the constraints are 
computed lazily, in
+// the mutation window Gradle provides right before the configuration is first 
observed.
+configurations.named('api').configure { apiConfiguration ->

Review Comment:
   Fixed. The companion-constraint enumeration is extracted into 
gradle/cli-companion-bom-constraints.gradle and applied to the base and every 
derived BOM, so the -cli constraints are declared directly on each platform 
rather than only inherited via platform(), and enforcedPlatform() consumers 
receive forced companion versions. Verified from the generated POMs that 
grails-bom, both hibernate BOMs, and all three micronaut BOMs now carry all 
nine companions.



-- 
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