Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ subprojects {
}
}

// Framework modules wire their cli tier with project dependencies; only the test-example
// apps exercise the external-coordinate auto-provisioning (resolved locally through the
// dependency substitution in gradle/functional-test-config.gradle) like real applications.
if (!name.startsWith('grails-test-examples')) {
ext.grailsCliAutoProvision = false
}

if (name.startsWith('grails-doc') || name.startsWith('grails-data-doc') || name.endsWith('-docs') || name.endsWith('-test-report')) {
rootProject.ext['docProjects'] << name
}
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ext {
'directory-watcher.version' : '0.19.1',
'gradle-groovy.version' : '4.0.32',
'gradle-spock.version' : '2.4-groovy-4.0',
'grails-publish-plugin.version' : '1.0.0-M1',
'grails-publish-plugin.version' : '1.0.0-SNAPSHOT',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking for release: this 1.0.0-SNAPSHOT flows into gradleBomDependencies and from there into the constraints { api ... } block of grails-bom/base, i.e. into the published <dependencyManagement> of org.apache.grails:grails-bom that every application consumes. A staged 8.0.x release would ship a BOM recommending a SNAPSHOT of org.apache.grails.gradle:grails-publish.

I understand this is pending apache/grails-gradle-publish#34 shipping — but the PR should be explicitly marked blocked on that release, with the version reverted to a GA coordinate before merge (or at latest before the first 8.0.x release candidate).

'jansi.version' : '2.4.2',
'javaparser-core.version' : '3.28.2',
'jline.version' : '3.30.6',
Expand Down
11 changes: 11 additions & 0 deletions gradle/functional-test-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ configurations.configureEach {
else {
substitute module(substitutedArtifact) using project(":$possibleProject.name")
}

// companion cli artifacts are additional publications of the same project,
// reachable locally through the project's cli capability
def cliArtifactId = possibleProject.findProperty('cliArtifactId')
if (cliArtifactId) {
def cliCoordinate = "$possibleProject.group:$cliArtifactId" as String
def cliReplacement = it.variant(project(":$possibleProject.name")) { v ->
v.capabilities { it.requireCapability(cliCoordinate) }
}
substitute module(cliCoordinate) using cliReplacement
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions grails-bom/base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
apiConfiguration.withDependencies {
for (Project subproject : rootProject.subprojects) {
def cliArtifactId = subproject.findProperty('cliArtifactId')
if (cliArtifactId) {
apiConfiguration.dependencyConstraints.add(
project.dependencies.constraints.create("${subproject.group}:${cliArtifactId}:${projectVersion}"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the grails-publish SNAPSHOT in dependencies.gradle: note that validateNoSnapshotDependencies (below in this file) only iterates config.allDependencies — constraints created here via dependencies.constraints.create(...) (and the constraints { api ... } block above) live in allDependencyConstraints, so the release guard has a blind spot for exactly this kind of leak and would let a SNAPSHOT constraint through a release build unflagged. Worth extending the guard to scan constraints as part of this PR, since the new lazy constraint mechanism widens that surface.

}
}
}
}

configurations.register('bomDependencies').configure {
it.canBeResolved = true
it.transitive = true
Expand All @@ -120,6 +137,11 @@ tasks.register('extractConstraints', ExtractDependenciesTask).configure { Extrac

rootProject.subprojects.each { p ->
artifactIdMappings[p.name] = p.findProperty('pomArtifactId') ?: p.name

String cliArtifactId = p.findProperty('cliArtifactId')
if (cliArtifactId) {
artifactIdMappings[cliArtifactId] = cliArtifactId
}
}

for (Map.Entry<String, String> dependency : project.ext.gradleBuildProjects.entrySet()) {
Expand All @@ -136,6 +158,11 @@ tasks.register('extractConstraints', ExtractDependenciesTask).configure { Extrac
String artifactId = p.findProperty('pomArtifactId') as String ?: p.name
String baseVersionName = artifactId.replaceAll('[.]', '-')
projectCoordinates["${p.group}:${ artifactId}:${p.version}" as String] = baseVersionName

String cliArtifactId = p.findProperty('cliArtifactId')
if (cliArtifactId) {
projectCoordinates["${p.group}:${cliArtifactId}:${p.version}" as String] = cliArtifactId
}
}

for (Map.Entry<String, String> dependency : project.ext.gradleBuildProjects.entrySet()) {
Expand Down Expand Up @@ -191,6 +218,12 @@ ext {
for (String gradleArtifactId : project.ext.gradleBuildProjects) {
propertyNameCalculator.addProject('org.apache.grails.gradle', gradleArtifactId, project.version as String, gradleArtifactId)
}
for (Project p : rootProject.subprojects) {
String cliArtifactId = p.findProperty('cliArtifactId')
if (cliArtifactId) {
propertyNameCalculator.addProject(p.group as String, cliArtifactId, p.version as String, cliArtifactId)
}
}

Map<String, String> pomProperties = [:]
deps.dependency.each { dep ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,11 @@ interface GroovyTransformOrder {
* Transforms a method to non-block IO
*/
static final int RX_SCHEDULER_ORDER = LINK_ORDER + DECREMENT_PRIORITY

/**
* Registers compiled ApplicationCommand implementations in META-INF/grails-cli.factories.
* Runs after the global Grails transform; the two write different files so there is no
* contention, but a deterministic order keeps compilation output reproducible.
*/
static final int COMMAND_FACTORIES_ORDER = RX_SCHEDULER_ORDER + DECREMENT_PRIORITY
}
10 changes: 10 additions & 0 deletions grails-console/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ plugins {
id 'org.apache.grails.buildsrc.sbom'
id 'org.apache.grails.buildsrc.vulnerability-scan'
id 'org.apache.grails.gradle.grails-code-style'
// the cli tier is consumed in the main variants (see the capability dependency below); the
// published pom and module metadata must record the companion coordinate (grails-core-cli)
// instead of the capability request, which external consumers cannot resolve
id 'org.apache.grails.gradle.grails-cli-library'
id 'org.apache.grails.gradle.grails-jacoco'
}

Expand All @@ -39,6 +43,12 @@ dependencies {
implementation platform(project(':grails-bom'))

api project(":grails-core")
// the command runner executes ApplicationCommands, which live in the cli companion artifact
api(project(':grails-core')) {
capabilities {
requireCapability('org.apache.grails:grails-core-cli')
}
}
api 'org.apache.groovy:groovy'
api 'org.apache.groovy:groovy-console'
api 'org.apache.groovy:groovy-swing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.context.ConfigurableApplicationContext

import grails.config.Settings
import grails.dev.commands.ApplicationContextCommandRegistry
import grails.dev.commands.ExecutionContext
import org.apache.grails.core.cli.ApplicationContextCommandRegistry
import org.apache.grails.core.cli.ExecutionContext
import grails.ui.support.DevelopmentGrailsApplication
import org.grails.build.parsing.CommandLine
import org.grails.build.parsing.CommandLineParser

/**
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
Expand Down
13 changes: 13 additions & 0 deletions grails-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ plugins {
id 'project-report'
id 'org.apache.grails.buildsrc.properties'
id 'org.apache.grails.buildsrc.dependency-validator'
id 'org.apache.grails.gradle.grails-plugin-cli'
id 'org.apache.grails.buildsrc.compile'
id 'org.apache.grails.buildsrc.publish'
id 'org.apache.grails.buildsrc.sbom'
Expand All @@ -34,6 +35,12 @@ plugins {
version = projectVersion
group = 'org.apache.grails'

cliArtifact {
automaticModuleName = 'org.apache.grails.core.cli'
// this framework module wires its cli dependencies with project dependencies below
defaultDependencies = false
}

dependencies {

implementation platform(project(':grails-bom'))
Expand Down Expand Up @@ -64,6 +71,12 @@ dependencies {

api project(':grails-datastore-core')

// the cli tier — published separately as org.apache.grails:grails-core-cli
cliImplementation platform(project(':grails-bom'))
cliApi project(project.path)
cliImplementation 'org.apache.groovy:groovy-json'
cliImplementation 'org.apache.groovy:groovy-templates'

testImplementation 'org.springframework:spring-jdbc'

testImplementation 'org.hamcrest:hamcrest'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package grails.dev.commands
package org.apache.grails.core.cli

import groovy.transform.CompileStatic

Expand All @@ -30,7 +30,6 @@ import grails.util.Named
* Represents a command that runs with access to the
* {@link org.springframework.context.ApplicationContext}.
*
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.dev.commands
package org.apache.grails.core.cli

import groovy.transform.CompileStatic

import org.grails.core.io.support.GrailsFactoriesLoader

/**
* A registry of {@grails.dev.commands.ApplicationContextCommand} instances
* A registry of {@link org.apache.grails.core.cli.ApplicationCommand} instances
*
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
Expand All @@ -33,14 +32,16 @@ class ApplicationContextCommandRegistry {
private final Map<String, ApplicationCommand> commands = [:]

ApplicationContextCommandRegistry() {
for (ApplicationCommand cmd : GrailsFactoriesLoader.loadFactories(ApplicationCommand)) {
for (ApplicationCommand cmd : GrailsFactoriesLoader.loadFactories(ApplicationCommand,
ApplicationContextCommandRegistry.classLoader, GrailsFactoriesLoader.CLI_FACTORIES_RESOURCE_LOCATION)) {
if (!commands.containsKey(cmd.name)) {
commands[cmd.name] = cmd
}
}

// If this is reflectively loaded from the delegating cli, we need to make sure the context class loader is also used to pull any commands that are loaded from the gradle classpath
for (ApplicationCommand cmd : GrailsFactoriesLoader.loadFactories(ApplicationCommand, Thread.currentThread().contextClassLoader)) {
for (ApplicationCommand cmd : GrailsFactoriesLoader.loadFactories(ApplicationCommand,
Thread.currentThread().contextClassLoader, GrailsFactoriesLoader.CLI_FACTORIES_RESOURCE_LOCATION)) {
if (!commands.containsKey(cmd.name)) {
commands[cmd.name] = cmd
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package grails.dev.commands
package org.apache.grails.core.cli

import groovy.json.JsonSlurper
import groovy.transform.CompileStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package grails.dev.commands
package org.apache.grails.core.cli

import groovy.transform.Canonical
import groovy.transform.CompileStatic
Expand All @@ -27,7 +27,6 @@ import org.grails.build.parsing.CommandLine
/**
* A context command to pass to {@link ApplicationCommand} instances
*
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* under the License.
*/

package grails.dev.commands
package org.apache.grails.core.cli

import grails.codegen.model.ModelBuilder
import grails.dev.commands.io.FileSystemInteraction
import grails.dev.commands.io.FileSystemInteractionImpl
import grails.dev.commands.template.TemplateRenderer
import grails.dev.commands.template.TemplateRendererImpl
import org.apache.grails.core.cli.io.FileSystemInteraction
import org.apache.grails.core.cli.io.FileSystemInteractionImpl
import org.apache.grails.core.cli.template.TemplateRenderer
import org.apache.grails.core.cli.template.TemplateRendererImpl

trait GrailsApplicationCommand implements ApplicationCommand, ModelBuilder {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 java.util.regex.Pattern

import groovy.transform.CompileStatic
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.ModuleNode
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation
import org.codehaus.groovy.transform.TransformWithPriority

import org.apache.grails.common.compiler.GroovyTransformOrder
import org.grails.compiler.injection.FactoriesFileWriter
import org.grails.compiler.injection.GlobalGrailsClassInjectorTransformation
import org.grails.compiler.injection.GrailsASTUtils
import org.grails.io.support.GrailsResourceUtils
import org.grails.io.support.UrlResource

/**
* A global transformation that registers compiled
* {@link org.apache.grails.core.cli.ApplicationCommand} implementations in
* {@code META-INF/grails-cli.factories}, keyed by the command contract's class name.
*
* The transform ships in the {@code grails-core-cli} artifact, so it is active exactly when a
* command can compile — a class implementing the contract only resolves when the cli artifact is
* on the compile classpath. Command registrations never touch {@code META-INF/grails.factories}.
*
* @since 8.0
*/
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
@CompileStatic
class CommandFactoriesTransformation implements ASTTransformation, TransformWithPriority {

public static final ClassNode APPLICATION_COMMAND_CLASS = ClassHelper.make('org.apache.grails.core.cli.ApplicationCommand')

/** The command registration file written into the compilation target directory */
public static final String CLI_FACTORIES_LOCATION = 'META-INF/grails-cli.factories'

/** Hand-authored registrations merged into the generated file */
protected static final List<String> SOURCE_CLI_FACTORIES_LOCATIONS = [
'src/main/resources/META-INF/grails-cli.factories',
'src/cli/resources/META-INF/grails-cli.factories',
].asImmutable()

/**
* Commands shipped in a companion cli artifact live in the {@code cli} source set
* ({@code src/cli/groovy} or {@code src/cli/java}), which is not one of the standard
* project-source locations recognised by {@link GrailsResourceUtils#isProjectSource}.
*/
protected static final Pattern CLI_SOURCE_PATTERN =
Pattern.compile('.+?[\\\\/]src[\\\\/]cli[\\\\/](groovy|java)[\\\\/].+?\\.(groovy|java)$')

@Override
int priority() {
return GroovyTransformOrder.COMMAND_FACTORIES_ORDER
}

@Override
void visit(ASTNode[] nodes, SourceUnit source) {
ModuleNode ast = source.getAST()
List<ClassNode> classes = new ArrayList<>(ast.getClasses())

URL url = GrailsASTUtils.getSourceUrl(source)

if (url == null) {
return
}
if (!GrailsResourceUtils.isProjectSource(new UrlResource(url)) && !isCliSource(url)) {
return
}

File compilationTargetDirectory = GlobalGrailsClassInjectorTransformation.resolveCompilationTargetDirectory(source)

for (ClassNode classNode : classes) {
FactoriesFileWriter.updateFactoriesWithType(classNode, APPLICATION_COMMAND_CLASS,
compilationTargetDirectory, CLI_FACTORIES_LOCATION, SOURCE_CLI_FACTORIES_LOCATIONS)
}
}

protected static boolean isCliSource(URL url) {
CLI_SOURCE_PATTERN.matcher(url.file).matches()
}
}
Loading
Loading