Skip to content

Commit

Permalink
merge non-breaking changes from development/code-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BreadMoirai committed Mar 11, 2019
1 parent 9685cfb commit 6600693
Show file tree
Hide file tree
Showing 28 changed files with 1,088 additions and 1,027 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ crashlytics.properties

crashlytics-build.properties

fabric.properties
fabric.properties
/ast/build/
/.idea/shelf/
15 changes: 15 additions & 0 deletions ast/build.gradle
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()
}
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 {
}
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)
}
}
}
}
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 {
}
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

}
}
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)
}
}
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
}
}
'''
}
}
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {
}

group = 'com.github.breadmoirai'
version = '2.2.4'
version = '2.3.0'

repositories {
jcenter()
Expand All @@ -32,6 +32,8 @@ repositories {
}

dependencies {
compileOnly project(':ast')

implementation 'org.codehaus.groovy:groovy-all:2.5.4'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.j256.simplemagic:simplemagic:1.10'
Expand All @@ -50,7 +52,7 @@ dependencies {
gradlePlugin {
plugins.create('github-release') {
id = 'com.github.breadmoirai.github-release'
implementationClass = 'com.github.breadmoirai.GithubReleasePlugin'
implementationClass = 'com.github.breadmoirai.githubreleaseplugin.GithubReleasePlugin'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rootProject.name = 'github-release'
include 'ast'

Loading

0 comments on commit 6600693

Please sign in to comment.