Skip to content

Commit

Permalink
Merge branch '2022.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rainboyan committed Jun 7, 2024
2 parents d43cf5f + e699075 commit 4ed646d
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 136 deletions.
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ ext {
// directories created during the build which are related
// to turning the workspace root into a GRAILS_HOME
distInstallDir = file("$buildDir/dist-tmp")
homeDistDir = file("dist")
homeBinDir = file("bin")
homeConfDir = file("conf")
homeDistDir = file("dist")
homeLibDir = file("lib")
homeRepoDir = file("repo")
homeSrcDir = file("src")
}

Expand Down Expand Up @@ -101,6 +102,14 @@ allprojects {
}
}
}
resolutionStrategy.dependencySubstitution.all { DependencySubstitution dependency ->
if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == "org.graceframework") {
def targetProject = findProject(":${dependency.requested.module}")
if (targetProject != null) {
dependency.useTarget targetProject
}
}
}
}
}

Expand Down Expand Up @@ -384,7 +393,6 @@ subprojects { project ->
}

if (!isTestSuite) {

task installToHomeDist(type: org.grails.gradle.Upload) {
configuration = configurations.archives
repositories {
Expand All @@ -409,6 +417,7 @@ task clean(type: Delete, group: 'build') {
homeConfDir,
homeDistDir,
homeLibDir,
homeRepoDir,
homeSrcDir
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class BootInitializerClassInjector extends GlobalClassInjectorAdapter {
return
}
// don't generate for plugins
if (classNode.getNodeMetaData('isPlugin')) {
if (Boolean.parseBoolean((String) classNode.getNodeMetaData('isPlugin'))) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class BuildSettings {
GRAILS_APP_PATH = GRAILS_APP_DIR.absolutePath.substring(GRAILS_APP_DIR.absolutePath.lastIndexOf(File.separator) + 1)
}
else {
GRAILS_APP_PATH = ''
GRAILS_APP_PATH = 'grails-app'
}

String projectTargetDir = System.getProperty(PROJECT_TARGET_DIR)
Expand Down
36 changes: 33 additions & 3 deletions grace-core/src/main/groovy/grails/compiler/ast/ClassInjector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2005 the original author or authors.
* Copyright 2004-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,19 +15,22 @@
*/
package grails.compiler.ast;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.URL;

import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.control.SourceUnit;
import org.grails.io.support.FileSystemResource;
import org.grails.io.support.Resource;

/**
* When implemented allows additional properties to be injected into Grails
* classes at compile time (ie when they are loaded by the GroovyClassLoader).
*
* @author Graeme Rocher
*
* @author Michael Yan
* @since 0.2
*/
public interface ClassInjector {
Expand Down Expand Up @@ -64,7 +67,34 @@ public interface ClassInjector {
*
* @param url The URL of the source file
* @return true if injection should occur
* @deprecated since 2022.3.0, in favor of {@link #shouldInject(ClassNode)}
*/
@Deprecated(forRemoval = true, since = "2023.0.0")
default boolean shouldInject(URL url) {
return true;
}

/**
* Returns whether this injector should inject
*
* @param classNode The classNode of the Groovy source
* @return true if injection should occur
*/
boolean shouldInject(URL url);
default boolean shouldInject(ClassNode classNode) {
String filename = classNode.getModule().getContext().getName();
if (filename == null) {
return false;
}
Resource resource = new FileSystemResource(filename);
if (resource.exists()) {
try {
URL url = resource.getURL();
return shouldInject(url);
}
catch (IOException ignored) {
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.InnerClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
Expand Down Expand Up @@ -103,10 +104,21 @@
* Helper methods for working with Groovy AST trees.
*
* @author Graeme Rocher
* @author Michael Yan
* @since 0.3
*/
public final class GrailsASTUtils {

public static final String META_DATA_KEY_GRAILS_APP_DIR = "GRAILS_APP_DIR";

public static final String META_DATA_KEY_PROJECT_DIR = "PROJECT_DIR";

public static final String META_DATA_KEY_PROJECT_NAME = "PROJECT_NAME";

public static final String META_DATA_KEY_PROJECT_TYPE = "PROJECT_TYPE";

public static final String META_DATA_KEY_PROJECT_VERSION = "PROJECT_VERSION";

public static final String DOMAIN_DIR = "domain";

public static final String GRAILS_APP_DIR = "grails-app";
Expand Down Expand Up @@ -1717,6 +1729,111 @@ public static URL getSourceUrl(ClassNode classNode) {
return getSourceUrl(classNode.getModule().getContext());
}

/**
* Checks whether the specified source is a Grails project source.
* A Grails project source is a Groovy or Java class under the project directory
*
* @param source The SourceUnit
* @return true if it is a project source
* @since 2022.2.5
*/
public static boolean isProjectSource(SourceUnit source) {
String filename = source.getName();
ModuleNode ast = source.getAST();
String projectDir = ast.getNodeMetaData(META_DATA_KEY_PROJECT_DIR);
return filename != null && projectDir != null && filename.startsWith(projectDir);
}

/**
* Checks whether the specified class node is a Grails project source.
* A Grails project source is a Groovy or Java class under the project directory
*
* @param classNode The ClassNode instance
* @return true if it is a Grails source
* @since 2022.2.5
*/
public static boolean isProjectSource(ClassNode classNode) {
return isProjectSource(classNode.getModule().getContext());
}

/**
* Checks whether the specific source is a Grails source.
* A Grails source is a Groovy plugin class or Groovy source under the grails-app directory
*
* @param source The SourceUnit to check
* @param artefactPath The artefact path
* @return True if it is a Grails source
* @since 2022.2.5
*/
public static boolean isGrailsSource(SourceUnit source, String artefactPath) {
String filename = source.getName();
ModuleNode ast = source.getAST();
String projectDir = ast.getNodeMetaData(META_DATA_KEY_PROJECT_DIR);
String grailsAppDir = ast.getNodeMetaData(META_DATA_KEY_GRAILS_APP_DIR);
if (filename == null || projectDir == null || grailsAppDir == null) {
return false;
}
return filename.startsWith(grailsAppDir + File.separatorChar + artefactPath) ||
(filename.startsWith(projectDir + File.separatorChar + "src") && filename.endsWith("GrailsPlugin.groovy"));
}

/**
* Checks whether the specific class node is a Grails source.
* A Grails source is a Groovy plugin class or Groovy source under the grails-app directory
*
* @param classNode The classNode to check
* @return True if it is a Grails source
* @since 2022.2.5
*/
public static boolean isGrailsSource(ClassNode classNode) {
return isGrailsSource(classNode, "");
}

/**
* Checks whether the specific class node is a Grails source.
* A Grails source is a Groovy plugin class or Groovy source under the grails-app directory
*
* @param classNode The classNode to check
* @param artefactPath The artefact path
* @return True if it is a Grails source
* @since 2022.2.5
*/
public static boolean isGrailsSource(ClassNode classNode, String artefactPath) {
return isGrailsSource(classNode, artefactPath, null);
}

/**
* Checks whether the specific class node is a Grails source.
* A Grails source is a Groovy plugin class or Groovy source under the grails-app directory
*
* @param classNode The classNode to check
* @param artefactPath The artefact path
* @param artefactSuffix The artefact suffix
* @return True if it is a Grails source
* @since 2022.2.5
*/
public static boolean isGrailsSource(ClassNode classNode, String artefactPath, String artefactSuffix) {
if (classNode.isEnum() || classNode.isInterface()
|| Modifier.isAbstract(classNode.getModifiers())
|| (classNode instanceof InnerClassNode)) {
return false;
}

if (!isGrailsSource(classNode.getModule().getContext(), artefactPath)) {
return false;
}

String name = classNode.getName();
if (name == null) {
return false;
}

if (artefactSuffix != null) {
return name.endsWith(artefactSuffix);
}
return true;
}

public static boolean hasParameters(MethodNode methodNode) {
return methodNode.getParameters().length > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class GrailsGradlePlugin extends GroovyPlugin {
configureRunCommand(project)

configurePathingJar(project)

configureGroovyASTMetadata(project)
}

protected void configureProfile(Project project) {
Expand Down Expand Up @@ -659,6 +661,48 @@ class GrailsGradlePlugin extends GroovyPlugin {
}
}

@CompileDynamic
protected void configureGroovyASTMetadata(Project project) {
def configScriptTask = project.tasks.create('configScript')

def configFile = project.file("$project.buildDir/config.groovy")
configScriptTask.outputs.file(configFile)

def projectName = project.name
def projectVersion = project.version
def projectDir = project.projectDir.absolutePath
def projectType = getGrailsProjectType()
def isPlugin = projectType == GrailsProjectType.PLUGIN
def grailsAppDir = new File(project.projectDir, grailsAppDir).absolutePath
configScriptTask.inputs.property('name', projectName)
configScriptTask.inputs.property('version', projectVersion)
configScriptTask.doLast {
configFile.parentFile.mkdirs()
configFile.text = """
withConfig(configuration) {
inline(phase: 'CONVERSION') { source, context, classNode ->
source.ast.putNodeMetaData('GRAILS_APP_DIR', '$grailsAppDir')
source.ast.putNodeMetaData('PROJECT_DIR', '$projectDir')
source.ast.putNodeMetaData('PROJECT_NAME', '$projectName')
source.ast.putNodeMetaData('PROJECT_TYPE', '$projectType')
source.ast.putNodeMetaData('PROJECT_VERSION', '$projectVersion')
classNode.putNodeMetaData('projectVersion', '$projectVersion')
classNode.putNodeMetaData('projectName', '$projectName')
classNode.putNodeMetaData('isPlugin', '$isPlugin')
}
}
"""
}
project.tasks.getByName('compileGroovy').dependsOn(configScriptTask)
project.compileGroovy {
groovyOptions.configurationScript = configFile
}
}

protected GrailsProjectType getGrailsProjectType() {
GrailsProjectType.NONE
}

protected FileCollection buildClasspath(Project project, Configuration... configurations) {
SourceSet mainSourceSet = SourceSets.findMainSourceSet(project)
SourceSetOutput output = mainSourceSet?.output
Expand All @@ -671,4 +715,14 @@ class GrailsGradlePlugin extends GroovyPlugin {
fileCollection
}

enum GrailsProjectType {
NONE,

WEB_APP,

PLUGIN,

PROFILE
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class GrailsPluginGradlePlugin extends GrailsGradlePlugin {

configureAstSources(project)

configureProjectNameAndVersionASTMetadata(project)

configurePluginResources(project)

configurePluginJarTask(project)
Expand Down Expand Up @@ -220,33 +218,9 @@ class GrailsPluginGradlePlugin extends GrailsGradlePlugin {
}
}

@CompileDynamic
protected void configureProjectNameAndVersionASTMetadata(Project project) {
def configScriptTask = project.tasks.create('configScript')

def configFile = project.file("$project.buildDir/config.groovy")
configScriptTask.outputs.file(configFile)

def projectName = project.name
def projectVersion = project.version
configScriptTask.inputs.property('name', projectName)
configScriptTask.inputs.property('version', projectVersion)
configScriptTask.doLast {
configFile.parentFile.mkdirs()
configFile.text = """
withConfig(configuration) {
inline(phase: 'CONVERSION') { source, context, classNode ->
classNode.putNodeMetaData('projectVersion', '$projectVersion')
classNode.putNodeMetaData('projectName', '$projectName')
classNode.putNodeMetaData('isPlugin', 'true')
}
}
"""
}
project.tasks.getByName('compileGroovy').dependsOn(configScriptTask)
project.compileGroovy {
groovyOptions.configurationScript = configFile
}
@Override
protected GrailsProjectType getGrailsProjectType() {
GrailsProjectType.PLUGIN
}

protected void checkForConfigurationClash(Project project) {
Expand Down
Loading

0 comments on commit 4ed646d

Please sign in to comment.