forked from electric-cloud-community/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSL and Command Block Property Expansion.groovy
74 lines (69 loc) · 2.7 KB
/
DSL and Command Block Property Expansion.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
CloudBees Flow DSL: Workaround for lack of "expand" option to procedure command field
Currently (version 2002.1), there is not option to suppress expansion of property expansion ($[...]). So, if you are attempting to create objects with DSL that is to include runtime expansions, you need to work around this limitation. There are at least three options:
1. Break up the string '$[...]', e.g., '$' + '[...]'
2. Substitution:
def D = '$'
"${D}[...]"
3. Store the DSL in a step property and evaluate the property with expand=false. This example illustrates how to do that using both ec-perl and ec-groovy
RFE: https://cloudbees.atlassian.net/browse/CEV-24448
*/
project "DSL Property Expansion", {
procedure "Test DSL Expansion", {
// This example illustrates the problem
step "Property expansion in command block", shell: "ectool evalDsl --dslFile '{0}'",
command : '''\
project "Default",{
procedure "Rendered Procedure - evalDsl shell",{
step "Contains Expansion",
// This property will be expanded prematurely (in the context of the outer procedure, not the runtime of this procedure.
command: 'echo Hello From $[/myProcedure]'
def D = '$'
step "Workaround",
command: "echo Hello From ${D}[/myProcedure] and " + '$' + "[/myStep]"
}
}
'''.stripIndent()
step "DSL as property ec-groovy",{
property "DSL", value: '''\
project "Default",{
procedure "Rendered Procedure - ec-groovy shell",{
step "Contains Expansion",
command: 'echo Hello From $[/myProcedure]'
step "Value from evalDsl",
command: "echo Value from evalDsl: ${args.val}"
}
}
'''.stripIndent()
command = '''\
import com.electriccloud.client.groovy.ElectricFlow
import groovy.json.*
ElectricFlow ef = new ElectricFlow()
def DSL=ef.getProperty(propertyName: "/myStep/DSL", expand: false, jobStepId: System.getenv()["COMMANDER_JOBSTEPID"]).property.value
println DSL
def Params = JsonOutput.toJson([val: "Value passed in through evalDsl"])
ef.evalDsl(dsl: DSL, parameters: Params)
'''.stripIndent()
shell = "ec-groovy"
}
step "DSL as property ec-perl",{
property "DSL", value: '''\
project "Default",{
procedure "Rendered Procedure - ec-perl shell",{
step "Contains Expansion",
command: 'echo Hello From $[/myProcedure]'
}
}
'''.stripIndent()
command = '''\
use Data::Dumper;
use ElectricCommander;
$ef = ElectricCommander->new();
my $DSL = $ef->getProperty("/myStep/DSL", {expand=>"0"})->findvalue('//value');
print $DSL;
$ef->evalDsl({dsl=>$DSL});
'''.stripIndent()
shell = "ec-perl"
}
}
}