Skip to content

Commit

Permalink
Merge pull request #859 from daspilker/deprecation-ast
Browse files Browse the repository at this point in the history
use AST transformation to log deprecation warnings
  • Loading branch information
daspilker committed May 23, 2016
2 parents ed8d4f4 + 36d3d7d commit 5d7050b
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 82 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ project(':job-dsl-core') {
}
}
main.compileClasspath += sourceSets.ast.output
test.compileClasspath += sourceSets.ast.output
}

description = 'Generates Jenkins jobs via a DSL'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package javaposse.jobdsl.dsl.transform

import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.syntax.CSTNode

class ASTTransformationHelper {
static VariableExpression getJobManagementVariable(SourceUnit sourceUnit, ClassNode clazz, CSTNode context) {
if (!clazz.getField('jobManagement') && !clazz.getField('jm')) {
sourceUnit.errorCollector.addError("no jobManagement field in $clazz", context, sourceUnit)
}
new VariableExpression(clazz.getField('jobManagement') ? 'jobManagement' : 'jm')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,18 @@ class CoreVersionASTTransformation implements ASTTransformation {
@Override
void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
sourceUnit.AST?.classes*.methods.flatten().each { MethodNode method ->
method.getAnnotations(REQUIRES_CORE_ANNOTATION).each { AnnotationNode requiresCoreAnnotation ->
if (!method.declaringClass.getField('jobManagement')) {
sourceUnit.errorCollector.addError(
"no jobManagement field in $method.declaringClass",
Token.newString(
requiresCoreAnnotation.text,
requiresCoreAnnotation.lineNumber,
requiresCoreAnnotation.columnNumber
),
sourceUnit
)
}
method.getAnnotations(REQUIRES_CORE_ANNOTATION).each { AnnotationNode annotationNode ->
VariableExpression jobManagementVariable = ASTTransformationHelper.getJobManagementVariable(
sourceUnit,
method.declaringClass,
Token.newString(annotationNode.text, annotationNode.lineNumber, annotationNode.columnNumber)
)

MethodCallExpression pluginCheckStatement = new MethodCallExpression(
new VariableExpression('jobManagement'),
jobManagementVariable,
new ConstantExpression('requireMinimumCoreVersion'),
new ArgumentListExpression(
requiresCoreAnnotation.members.minimumVersion,
annotationNode.members.minimumVersion,
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package javaposse.jobdsl.dsl.transform

import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.AnnotationNode
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.expr.ArgumentListExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.syntax.Token
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation

/**
* Global AST transformation for logging deprecation warnings.
*
* Each method in a class implementing <code>javaposse.jobdsl.dsl.Context</code> annotated by {@link Deprecated} is
* supplemented with a method call to <code>jobManagement.logDeprecationWarning()</code>. The method must have access to
* a <code>jobManagement</code> field.
*/
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
class DeprecationWarningASTTransformation implements ASTTransformation {
private static final ClassNode DEPRECATED_ANNOTATION = ClassHelper.make('java.lang.Deprecated')
private static final ClassNode CONTEXT_CLASS = ClassHelper.make('javaposse.jobdsl.dsl.Context')

@Override
void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
List<ClassNode> classes = sourceUnit.AST.classes.findAll {
!it.interface && it.implementsInterface(CONTEXT_CLASS)
}
classes.methods.flatten().each { MethodNode method ->
List<AnnotationNode> annotations = method.getAnnotations(DEPRECATED_ANNOTATION)
if (annotations) {
VariableExpression jobManagementVariable = ASTTransformationHelper.getJobManagementVariable(
sourceUnit,
method.declaringClass,
Token.newString(annotations[0].text, annotations[0].lineNumber, annotations[0].columnNumber)
)

((BlockStatement) method.code).statements.add(0, new ExpressionStatement(new MethodCallExpression(
jobManagementVariable,
new ConstantExpression('logDeprecationWarning'),
new ArgumentListExpression()
)))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,27 @@ class PluginASTTransformation implements ASTTransformation {
}
}

annotations.each { AnnotationNode requiresPluginAnnotation ->
if (!method.declaringClass.getField('jobManagement') && !method.declaringClass.getField('jm')) {
sourceUnit.errorCollector.addError(
"no jobManagement field in $method.declaringClass",
Token.newString(
requiresPluginAnnotation.text,
requiresPluginAnnotation.lineNumber,
requiresPluginAnnotation.columnNumber
),
sourceUnit
)
}
String jobManagementVariable = method.declaringClass.getField('jobManagement') ? 'jobManagement' : 'jm'
annotations.each { AnnotationNode annotationNode ->
VariableExpression jobManagementVariable = ASTTransformationHelper.getJobManagementVariable(
sourceUnit,
method.declaringClass,
Token.newString(annotationNode.text, annotationNode.lineNumber, annotationNode.columnNumber)
)

ArgumentListExpression argumentList = new ArgumentListExpression(requiresPluginAnnotation.members.id)
ArgumentListExpression argumentList = new ArgumentListExpression(annotationNode.members.id)
Expression methodExpression
if (requiresPluginAnnotation.members.minimumVersion) {
argumentList.addExpression(requiresPluginAnnotation.members.minimumVersion)
if (annotationNode.members.minimumVersion) {
argumentList.addExpression(annotationNode.members.minimumVersion)
methodExpression = new ConstantExpression('requireMinimumPluginVersion')
} else {
methodExpression = new ConstantExpression('requirePlugin')
}
if (requiresPluginAnnotation.members.failIfMissing) {
argumentList.addExpression(requiresPluginAnnotation.members.failIfMissing)
if (annotationNode.members.failIfMissing) {
argumentList.addExpression(annotationNode.members.failIfMissing)
}

MethodCallExpression pluginCheckStatement = new MethodCallExpression(
new VariableExpression(jobManagementVariable),
jobManagementVariable,
methodExpression,
argumentList
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
javaposse.jobdsl.dsl.transform.ContextASTTransformation
javaposse.jobdsl.dsl.transform.PluginASTTransformation
javaposse.jobdsl.dsl.transform.CoreVersionASTTransformation
javaposse.jobdsl.dsl.transform.DeprecationWarningASTTransformation
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class Folder extends Item {

@Deprecated
protected void execute(Closure rootClosure) {
jobManagement.logDeprecationWarning()
configure(rootClosure)
}
}
2 changes: 0 additions & 2 deletions job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/Job.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,6 @@ abstract class Job extends Item {
@RequiresPlugin(id = 'PrioritySorter')
@Deprecated
void priority(int value) {
jobManagement.logDeprecationWarning()

configure { Node project ->
Node node = new Node(project / 'properties', 'hudson.queueSorter.PrioritySorterJobProperty')
node.appendNode('priority', value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ abstract class JobParent extends Script implements DslFactory {
@Deprecated
@Override
WorkflowJob workflowJob(String name, @DslContext(WorkflowJob) Closure closure = null) {
jm.logDeprecationWarning()

pipelineJob(name, closure)
}

Expand All @@ -114,8 +112,6 @@ abstract class JobParent extends Script implements DslFactory {
@Override
MultibranchWorkflowJob multibranchWorkflowJob(String name,
@DslContext(MultibranchWorkflowJob) Closure closure = null) {
jm.logDeprecationWarning()

multibranchPipelineJob(name, closure)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ abstract class View extends AbstractContext {

@Deprecated
protected void execute(Closure rootClosure) {
jobManagement.logDeprecationWarning()
configure(rootClosure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ class ScmContext extends AbstractExtensibleContext {
@RequiresPlugin(id = 'perforce')
@Deprecated
void p4(String viewspec, Closure configure = null) {
jobManagement.logDeprecationWarning()
p4(viewspec, 'rolem', '', configure)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class FlexiblePublisherContext extends AbstractContext {
*/
@Deprecated
void condition(@DslContext(RunConditionContext) Closure closure) {
jobManagement.logDeprecationWarning()

RunConditionContext context = new RunConditionContext(jobManagement, item)
ContextHelper.executeInContext(closure, context)
condition = context.condition
Expand All @@ -53,8 +51,6 @@ class FlexiblePublisherContext extends AbstractContext {
@RequiresPlugin(id = 'any-buildstep')
@Deprecated
void step(@DslContext(StepContext) Closure closure) {
jobManagement.logDeprecationWarning()

StepContext stepContext = new StepContext(jobManagement, item)
ContextHelper.executeInContext(closure, stepContext)
actions.addAll(stepContext.stepNodes)
Expand All @@ -65,8 +61,6 @@ class FlexiblePublisherContext extends AbstractContext {
*/
@Deprecated
void publisher(@DslContext(PublisherContext) Closure closure) {
jobManagement.logDeprecationWarning()

PublisherContext publisherContext = new PublisherContext(jobManagement, item)
ContextHelper.executeInContext(closure, publisherContext)
actions.addAll(publisherContext.publisherNodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ class PublisherContext extends AbstractExtensibleContext {
@Deprecated
void extendedEmail(String recipients, String subjectTemplate, String contentTemplate,
@DslContext(EmailContext) Closure emailClosure = null) {
jobManagement.logDeprecationWarning()

EmailContext emailContext = new EmailContext()
ContextHelper.executeInContext(emailClosure, emailContext)

Expand Down Expand Up @@ -1485,8 +1483,6 @@ class PublisherContext extends AbstractExtensibleContext {
@Deprecated
@RequiresPlugin(id = 'ghprb', minimumVersion = '1.26')
void mergePullRequest(@DslContext(PullRequestPublisherContext) Closure contextClosure = null) {
jobManagement.logDeprecationWarning()

PullRequestPublisherContext pullRequestPublisherContext = new PullRequestPublisherContext(jobManagement)
ContextHelper.executeInContext(contextClosure, pullRequestPublisherContext)

Expand Down Expand Up @@ -1532,8 +1528,6 @@ class PublisherContext extends AbstractExtensibleContext {
@RequiresPlugin(id = 'hipchat', minimumVersion = '0.1.9')
@Deprecated
void hipChat(@DslContext(HipChatPublisherContext) Closure hipChatClosure = null) {
jobManagement.logDeprecationWarning()

HipChatPublisherContext hipChatContext = new HipChatPublisherContext()
ContextHelper.executeInContext(hipChatClosure, hipChatContext)

Expand Down Expand Up @@ -1690,8 +1684,6 @@ class PublisherContext extends AbstractExtensibleContext {
@RequiresPlugin(id = 'slack', minimumVersion = '1.8')
@Deprecated
void slackNotifications(@DslContext(SlackNotificationsContext) Closure slackNotificationsClosure) {
jobManagement.logDeprecationWarning()

SlackNotificationsContext context = new SlackNotificationsContext()
ContextHelper.executeInContext(slackNotificationsClosure, context)

Expand Down

0 comments on commit 5d7050b

Please sign in to comment.