Skip to content

Commit 6681845

Browse files
committed
Simplify class interception
1 parent e6ca1a8 commit 6681845

14 files changed

+79
-62
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/InterceptingGCL.groovy

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class InterceptingGCL extends GroovyClassLoader {
1212
metaClazz.static.invokeMethod = helper.getMethodInterceptor()
1313
metaClazz.methodMissing = helper.getMethodMissingInterceptor()
1414
metaClazz.propertyMissing = helper.getPropertyMissingInterceptor()
15-
metaClazz.getEnv = {return binding.env}
15+
metaClazz.getEnv = { return binding.env }
1616
// find and replace script method closure with any matching allowed method closure
1717
metaClazz.methods.forEach { scriptMethod ->
1818
def signature = method(scriptMethod.name, scriptMethod.nativeParameterTypes)
@@ -67,10 +67,7 @@ class InterceptingGCL extends GroovyClassLoader {
6767
return super.loadClass(name)
6868
}
6969

70-
// Copy from this.parseClass(GroovyCodeSource, boolean)
71-
cls.metaClass.invokeMethod = helper.getMethodInterceptor()
72-
cls.metaClass.static.invokeMethod = helper.getMethodInterceptor()
73-
cls.metaClass.methodMissing = helper.getMethodMissingInterceptor()
70+
interceptClassMethods(cls.metaClass, helper, binding)
7471

7572
return cls;
7673
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.lesfurets.jenkins.unit.declarative.agent.DockerAgentDeclaration
44
import com.lesfurets.jenkins.unit.declarative.agent.KubernetesAgentDeclaration
55
import groovy.transform.ToString
66

7+
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
8+
79
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.createComponent
810
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
911
import static groovy.lang.Closure.DELEGATE_FIRST
@@ -63,7 +65,7 @@ class AgentDeclaration {
6365
this.dockerfileDir = dir
6466
}
6567

66-
def execute(Object delegate) {
68+
def execute(Script script) {
6769
def agentDesc = null
6870

6971
if (label) {

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AnyOfDeclaration.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ class AnyOfDeclaration extends WhenDeclaration {
2626
return false
2727
}
2828

29-
Boolean execute(Object delegate) {
29+
Boolean execute(Script script) {
3030
boolean br = false;
3131
boolean exp = false;
3232

3333
if (this.branches.size() > 0) {
34-
br = this.branches.contains(delegate.env.BRANCH_NAME)
34+
br = this.branches.contains(script.env.BRANCH_NAME)
3535
}
3636

3737
if (this.expressions.size() > 0) {
38-
exp = expressions(delegate)
38+
exp = expressions(script)
3939
}
4040

4141
return exp || br

src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipeline.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ class DeclarativePipeline extends GenericPipelineDeclaration {
4040
this.parameters = createComponent(ParametersDeclaration, closure)
4141
}
4242

43-
def execute(Object delegate) {
44-
super.execute(delegate)
43+
def execute(Script script) {
44+
super.execute(script)
4545
this.options.forEach {
4646
executeWith(delegate, it)
4747
}
4848
this.agent?.execute(delegate)
4949
executeWith(delegate, this.triggers)
5050
this.stages.entrySet().forEach { e ->
51-
e.value.execute(delegate)
51+
e.value.execute(script)
5252
}
53-
this.post?.execute(delegate)
53+
this.post?.execute(script)
5454
}
5555

5656
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/GenericPipelineDeclaration.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ abstract class GenericPipelineDeclaration {
6262
this.stages.put(stageName, createComponent(StageDeclaration, closure).with { it.name = stageName; it })
6363
}
6464

65-
def execute(Object delegate) {
65+
def execute(Script script) {
6666
Map envValuestoRestore = [:]
6767

6868
// set environment
6969
if (this.environment) {
70-
envValuestoRestore = initEnvironment(this.environment, delegate)
70+
envValuestoRestore = initEnvironment(this.environment, script)
7171
}
72-
resetEnvironment(envValuestoRestore, delegate)
72+
resetEnvironment(envValuestoRestore, script)
7373
}
7474

7575
public static Map initEnvironment(Closure environment, Object delegate) {

src/main/groovy/com/lesfurets/jenkins/unit/declarative/NotDeclaration.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.lesfurets.jenkins.unit.declarative
22

33
class NotDeclaration extends WhenDeclaration {
44

5-
Boolean execute(Object delegate) {
6-
return !super.execute(delegate)
5+
Boolean execute(Script script) {
6+
return !super.execute(script)
77
}
88
}
99

src/main/groovy/com/lesfurets/jenkins/unit/declarative/ParallelDeclaration.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class ParallelDeclaration {
2121
this.stages.put(name, createComponent(StageDeclaration, closure).with{it.name = name;it} )
2222
}
2323

24-
def execute(Object delegate) {
24+
def execute(Script script) {
2525
this.stages.entrySet().forEach { e ->
26-
e.value.execute(delegate)
26+
e.value.execute(script)
2727
}
2828
}
2929

src/main/groovy/com/lesfurets/jenkins/unit/declarative/PostDeclaration.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lesfurets.jenkins.unit.declarative
22

3+
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
4+
35
import static com.lesfurets.jenkins.unit.declarative.DeclarativePipeline.executeWith
46

57
class PostDeclaration {
@@ -55,9 +57,9 @@ class PostDeclaration {
5557
this.regression = closure
5658
}
5759

58-
def execute(Object delegate) {
59-
def currentBuild = delegate.currentBuild.result
60-
def previousBuild = delegate.currentBuild?.previousBuild?.result
60+
def execute(Script script) {
61+
def currentBuild = script.currentBuild.result
62+
def previousBuild = script.currentBuild?.previousBuild?.result
6163
if (this.always) {
6264
executeWith(delegate, this.always)
6365
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/StageDeclaration.groovy

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StageDeclaration extends GenericPipelineDeclaration {
3737
options.add(closure)
3838
}
3939

40-
def execute(Object delegate) {
40+
def execute(Script script) {
4141
String name = this.name
4242
this.options.each {
4343
executeWith(delegate, it)
@@ -51,27 +51,33 @@ class StageDeclaration extends GenericPipelineDeclaration {
5151
return
5252
}
5353

54-
if (!when || when.execute(delegate)) {
54+
if (!when || when.execute(script)) {
5555
Map envValuestoRestore = [:]
5656

5757
// set environment
5858
if (this.environment) {
59-
envValuestoRestore = initEnvironment(this.environment, delegate)
59+
envValuestoRestore = initEnvironment(this.environment, script)
6060
}
6161

6262
// TODO handle credentials
6363
this.stages.entrySet().forEach { stageEntry ->
64-
stageEntry.value.execute(delegate)
64+
stageEntry.value.execute(script)
6565
}
66-
if(steps) {
67-
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, this)
68-
Closure cl = { stage("$name", stageBody) }
69-
executeWith(delegate, cl)
66+
if (steps) {
67+
Closure rehydratedSteps = steps.rehydrate(script, this, steps);
68+
rehydratedSteps.setResolveStrategy(Closure.DELEGATE_FIRST)
69+
Closure stageBody = {
70+
agent?.execute(script)
71+
} >> rehydratedSteps
72+
Closure cl = {
73+
stage("$name", stageBody)
74+
}
75+
executeWith(script, cl, Closure.DELEGATE_ONLY)
7076
}
7177
if (post) {
72-
this.post.execute(delegate)
78+
this.post.execute(script)
7379
}
74-
resetEnvironment(envValuestoRestore, delegate)
80+
resetEnvironment(envValuestoRestore, script)
7581
} else {
7682
executeWith(delegate, { echo "Skipping stage $name" })
7783
}

src/main/groovy/com/lesfurets/jenkins/unit/declarative/WhenDeclaration.groovy

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.lesfurets.jenkins.unit.declarative
22

33
import java.util.regex.Pattern
44

5+
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
6+
57
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.createComponent
68
import static groovy.lang.Closure.DELEGATE_FIRST
79

@@ -48,7 +50,7 @@ class WhenDeclaration {
4850
this.expressionCondition = closure
4951
}
5052

51-
Boolean execute(Object delegate) {
53+
Boolean execute(Script script) {
5254
boolean expressionCheck = true
5355
boolean branchCheck = true
5456
boolean tagCheck = true
@@ -57,26 +59,26 @@ class WhenDeclaration {
5759
boolean notCheck = true
5860

5961
if (anyOfCondition) {
60-
anyOfCheck = anyOfCondition.execute(delegate)
62+
anyOfCheck = anyOfCondition.execute(script)
6163
}
6264
if (notCondition) {
63-
notCheck = notCondition.execute(delegate)
65+
notCheck = notCondition.execute(script)
6466
}
6567
if (expressionCondition) {
6668
expressionCheck = executeWith(delegate, expressionCondition)
6769
}
6870
if (branchCondition) {
69-
branchCheck = this.branchCondition == delegate.env.BRANCH_NAME
71+
branchCheck = this.branchCondition == script.env.BRANCH_NAME
7072
}
7173
if (buildingTagCondition) {
72-
tagCheck = delegate.env.containsKey(TAG_NAME)
74+
tagCheck = script.env.containsKey(TAG_NAME)
7375
}
7476
if (tagCondition) {
75-
tagCheck = delegate.env.TAG_NAME =~ tagCondition
77+
tagCheck = script.env.TAG_NAME =~ tagCondition
7678
}
7779
if (environmentCondition && !(environmentCondition as Map)?.isEmpty()) {
7880
environmentCondition.entrySet().forEach { e ->
79-
envCheck = envCheck && (delegate.env."${e.key}" == e.value)
81+
envCheck = envCheck && (script.env."${e.key}" == e.value)
8082
}
8183
}
8284

0 commit comments

Comments
 (0)