-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge non-breaking changes from development/code-cleanup
- Loading branch information
1 parent
9685cfb
commit 6600693
Showing
28 changed files
with
1,088 additions
and
1,027 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
id 'groovy' | ||
} | ||
|
||
group 'com.github.breadmoirai' | ||
version '2.3.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.codehaus.groovy:groovy-all:2.5.4' | ||
implementation gradleApi() | ||
} |
14 changes: 14 additions & 0 deletions
14
ast/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ast/ExtensionClass.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.breadmoirai.githubreleaseplugin.ast | ||
|
||
import org.codehaus.groovy.transform.GroovyASTTransformationClass | ||
|
||
import java.lang.annotation.ElementType | ||
import java.lang.annotation.Retention | ||
import java.lang.annotation.RetentionPolicy | ||
import java.lang.annotation.Target | ||
|
||
@Retention(RetentionPolicy.SOURCE) | ||
@Target([ElementType.TYPE]) | ||
@GroovyASTTransformationClass(["com.github.breadmoirai.githubreleaseplugin.ast.ExtensionClassASTTransformation"]) | ||
@interface ExtensionClass { | ||
} |
38 changes: 38 additions & 0 deletions
38
...ovy/com/github/breadmoirai/githubreleaseplugin/ast/ExtensionClassASTTransformation.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.breadmoirai.githubreleaseplugin.ast | ||
|
||
import groovy.transform.CompileStatic | ||
import org.codehaus.groovy.ast.* | ||
import org.codehaus.groovy.ast.expr.* | ||
import org.codehaus.groovy.ast.stmt.BlockStatement | ||
import org.codehaus.groovy.ast.stmt.ExpressionStatement | ||
import org.codehaus.groovy.ast.tools.GeneralUtils | ||
import org.codehaus.groovy.ast.tools.GenericsUtils | ||
import org.codehaus.groovy.control.CompilePhase | ||
import org.codehaus.groovy.control.SourceUnit | ||
import org.codehaus.groovy.macro.methods.MacroGroovyMethods | ||
import org.codehaus.groovy.syntax.Token | ||
import org.codehaus.groovy.syntax.Types | ||
import org.codehaus.groovy.transform.AbstractASTTransformation | ||
import org.codehaus.groovy.transform.GroovyASTTransformation | ||
import org.gradle.api.Project | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.internal.impldep.org.mozilla.javascript.ast.AstNode | ||
|
||
import java.util.concurrent.Callable | ||
|
||
@CompileStatic | ||
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS) | ||
class ExtensionClassASTTransformation extends AbstractASTTransformation { | ||
|
||
@Override | ||
void visit(ASTNode[] astNodes, SourceUnit sourceUnit) { | ||
def transformation = new ExtensionPropertyASTTransformation() | ||
ClassNode node = astNodes[1] as ClassNode | ||
node.fields.each { | ||
if (it.type.typeClass.name == Property.name) { | ||
transformation.visit([null, it] as ASTNode[], null) | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
ast/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ast/ExtensionProperty.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.breadmoirai.githubreleaseplugin.ast | ||
|
||
import org.codehaus.groovy.transform.GroovyASTTransformationClass | ||
|
||
import java.lang.annotation.ElementType | ||
import java.lang.annotation.Retention | ||
import java.lang.annotation.RetentionPolicy | ||
import java.lang.annotation.Target | ||
|
||
@Retention(RetentionPolicy.SOURCE) | ||
@Target([ElementType.FIELD]) | ||
@GroovyASTTransformationClass(["com.github.breadmoirai.githubreleaseplugin.ast.ExtensionPropertyASTTransformation"]) | ||
@interface ExtensionProperty { | ||
} |
164 changes: 164 additions & 0 deletions
164
.../com/github/breadmoirai/githubreleaseplugin/ast/ExtensionPropertyASTTransformation.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.github.breadmoirai.githubreleaseplugin.ast | ||
|
||
import groovy.transform.CompileStatic | ||
import org.codehaus.groovy.ast.* | ||
import org.codehaus.groovy.ast.expr.ArgumentListExpression | ||
import org.codehaus.groovy.ast.expr.FieldExpression | ||
import org.codehaus.groovy.ast.expr.MethodCallExpression | ||
import org.codehaus.groovy.ast.expr.VariableExpression | ||
import org.codehaus.groovy.ast.stmt.ExpressionStatement | ||
import org.codehaus.groovy.ast.stmt.ReturnStatement | ||
import org.codehaus.groovy.ast.tools.GeneralUtils | ||
import org.codehaus.groovy.ast.tools.GenericsUtils | ||
import org.codehaus.groovy.control.CompilePhase | ||
import org.codehaus.groovy.control.SourceUnit | ||
import org.codehaus.groovy.transform.AbstractASTTransformation | ||
import org.codehaus.groovy.transform.GroovyASTTransformation | ||
import org.gradle.api.Project | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.Input | ||
|
||
import java.util.concurrent.Callable | ||
|
||
@CompileStatic | ||
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS) | ||
class ExtensionPropertyASTTransformation extends AbstractASTTransformation { | ||
|
||
@Override | ||
void visit(ASTNode[] astNodes, SourceUnit _) { | ||
FieldNode fieldNode = astNodes[1] as FieldNode | ||
ClassNode classNode = fieldNode.declaringClass | ||
// assertions to make sure it is applied correctly | ||
/** | ||
* Requirements | ||
* is applied to type: Property | ||
* has accessible field: Project project | ||
*/ | ||
if (fieldNode.type.typeClass.name != 'org.gradle.api.provider.Property') { | ||
throw new ExtensionPropertyException("The ExtensionProperty annotation can only be applied to fields of the type ${Property.name}. This annotation has been applied to the field '${fieldNode.name}' of type '${fieldNode.type.typeClass.name}'") | ||
} | ||
FieldNode projectField = classNode.getField('project') | ||
if (projectField == null || projectField.type.typeClass.name != 'org.gradle.api.Project') { | ||
throw new ExtensionPropertyException("The ExtensionProperty annotation can only be applied to fields with an accompanying field named `project` of the type ${Project.name}") | ||
} | ||
|
||
String fieldName = fieldNode.name | ||
String fieldNameCap = fieldName.capitalize() | ||
GenericsType genericType = fieldNode.type.getGenericsTypes()[0] | ||
|
||
|
||
FieldExpression fieldVar = GeneralUtils.fieldX(fieldNode) | ||
|
||
// create getter method | ||
// getPropertyProvider | ||
def providerClassNode = createParameterizedNode(Provider, genericType) | ||
classNode.addMethod new MethodNode( | ||
"get${fieldNameCap}Provider", | ||
ACC_PROTECTED, | ||
providerClassNode, | ||
[] as Parameter[], | ||
[] as ClassNode[], | ||
new ReturnStatement(fieldVar) | ||
).tap { | ||
it.addAnnotation new AnnotationNode(new ClassNode(Input)) | ||
} | ||
|
||
//create setter methods | ||
// setProperty(T value) | ||
def paramValue = new Parameter(genericType.type, fieldName) | ||
classNode.addMethod(new MethodNode( | ||
"set${fieldNameCap}", | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramValue] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramValue))) | ||
)) | ||
|
||
// prop(T value) | ||
classNode.addMethod(new MethodNode( | ||
fieldName, | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramValue] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramValue))) | ||
)) | ||
|
||
// setProp(Provider<? extends T> value) | ||
def providerWildClassNode = createParameterizedNode(Provider, GenericsUtils.buildWildcardType(genericType.type)) | ||
def paramProvider = new Parameter(providerWildClassNode, fieldName) | ||
classNode.addMethod(new MethodNode( | ||
"set${fieldNameCap}", | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramProvider] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramProvider))) | ||
)) | ||
|
||
// prop(Provider<? extends T> value) | ||
classNode.addMethod(new MethodNode( | ||
fieldName, | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramProvider] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramProvider))) | ||
)) | ||
|
||
// setProp(Callable<? extends T> callable) | ||
def paramCallable = new Parameter(createParameterizedNode(Callable, GenericsUtils.buildWildcardType(genericType.type)), fieldName) | ||
classNode.addMethod(new MethodNode( | ||
"set${fieldNameCap}", | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramCallable] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement( | ||
new MethodCallExpression( | ||
new FieldExpression(fieldNode), | ||
"set", | ||
new MethodCallExpression( | ||
new FieldExpression(projectField), | ||
"provider", | ||
new VariableExpression(paramCallable) | ||
) | ||
) | ||
) | ||
)) | ||
// prop(Callable<? extends T> callable) | ||
classNode.addMethod(new MethodNode( | ||
fieldName, | ||
ACC_PUBLIC, | ||
ClassHelper.VOID_TYPE, | ||
[paramCallable] as Parameter[], | ||
[] as ClassNode[], | ||
new ExpressionStatement( | ||
new MethodCallExpression( | ||
new FieldExpression(fieldNode), | ||
"set", | ||
new MethodCallExpression( | ||
new FieldExpression(projectField), | ||
"provider", | ||
new VariableExpression(paramCallable) | ||
) | ||
) | ||
) | ||
)) | ||
} | ||
|
||
private static ClassNode createParameterizedNode(Class returnType, GenericsType genericType) { | ||
GenericsType[] generics = [genericType] | ||
ClassNode redirect = new ClassNode(returnType) | ||
redirect.usingGenerics = true | ||
redirect.genericsTypes = generics | ||
ClassNode returnNode = new ClassNode(returnType) | ||
returnNode.setRedirect(redirect) | ||
returnNode.usingGenerics = true | ||
returnNode.genericsTypes = generics | ||
return returnNode | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...n/groovy/com/github/breadmoirai/githubreleaseplugin/ast/ExtensionPropertyException.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.breadmoirai.githubreleaseplugin.ast | ||
|
||
class ExtensionPropertyException extends RuntimeException { | ||
ExtensionPropertyException() { | ||
super() | ||
} | ||
|
||
ExtensionPropertyException(String message) { | ||
super(message) | ||
} | ||
|
||
ExtensionPropertyException(String message, Throwable cause) { | ||
super(message, cause) | ||
} | ||
|
||
ExtensionPropertyException(Throwable cause) { | ||
super(cause) | ||
} | ||
|
||
protected ExtensionPropertyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
ast/src/test/groovy/com/github/breadmoirai/githubreleaseplugin/ASTTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.breadmoirai.githubreleaseplugin | ||
|
||
class ASTTest extends GroovyTestCase { | ||
|
||
void testTransformation() { | ||
assertScript ''' | ||
import com.github.breadmoirai.githubreleaseplugin.ast.ExtensionProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.Project | ||
class TestClass { | ||
@ExtensionProperty | ||
Property<java.lang.String> testP | ||
Project project | ||
public TestClass() { | ||
println 'test do it' | ||
} | ||
} | ||
for (def method in TestClass.class.declaredMethods) { | ||
if (method.name.toLowerCase().contains('testp')) { | ||
println method | ||
println method.parameters | ||
} | ||
} | ||
''' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
rootProject.name = 'github-release' | ||
include 'ast' | ||
|
Oops, something went wrong.