1
+
2
+ imports :
3
+
4
+ - classpath:/org/restflow/groovy/types.yaml
5
+
6
+ types :
7
+
8
+ - id : PerlActor
9
+ type : GroovyActor
10
+ properties :
11
+ wrapperScript : |
12
+ //////////////////////////////////////////////////////////////////
13
+ // Groovy code for running a perl script, passing the script input
14
+ // variables by prepending the step script with set commands and
15
+ // retrieving script outputs by appending print commands that
16
+ // render output variables in YAML.
17
+
18
+ // create a delimiter for separating script output from YAML block
19
+ String OUTPUT_DELIMITER='__END_PERL_SCRIPT_OUTPUT__'
20
+
21
+ // prepare script fragment to copy input values to shell variables
22
+ String inputVars = "";
23
+ for (key in _states.keySet()) {
24
+ inputVars += "\$" + key + "=\'" + _states.get(key) + "\';\n";
25
+ }
26
+ for (key in _inputs.keySet()) {
27
+ inputVars += "\$" + key + "=\'" + _inputs.get(key) + "\';\n";
28
+ }
29
+ if (_status.getStepDirectory() != null) {
30
+ inputVars += "chdir " + _status.getStepDirectory() + ";\n";
31
+ }
32
+
33
+ // prepare script fragment to render output variables in YAML
34
+ String outputVars = "\nprint \"${OUTPUT_DELIMITER}\\n\";\n";
35
+ for (name in _outputs) {
36
+ outputVars += "print " + "\"" + name + ": \\\"\$" + name + "\\\"\\n\";\n";
37
+ }
38
+
39
+ for (name in _states.keySet()) {
40
+ outputVars += "print " + "\"" + name + ": \\\"\$" + name + "\\\"\\n\";\n";
41
+ }
42
+
43
+ for (name in ["enabledInputs", "disabledInputs", "enabledInputs", "disabledOutputs"]) {
44
+ outputVars += "print " + "\"" + name + ": \\\"\$" + name + "\\\"\\n\";\n";
45
+ }
46
+
47
+ String functions =
48
+ "sub disableOutput { \$disabledOutputs=\"\$disabledOutputs \$_[0]\";}\n" +
49
+ "sub enableInput { \$enabledInputs=\"\$enabledInputs \$_[0]\";}\n" +
50
+ "sub disableInput { \$disabledInputs=\"\$disabledInputs \$_[0]\";}\n" +
51
+ "sub enableOutput { \$enabledOutputs=\"\$enabledOutputs \$_[0]\";}\n";
52
+
53
+ // bracket step script with data binding script fragments
54
+ script = functions + inputVars + _script + "\n" + outputVars;
55
+
56
+ //File file = new File("script.pl");
57
+ //file << "**************************************\n";
58
+ //file << script;
59
+
60
+ // run perl and feed the script to it through standard input
61
+ command = 'perl -';
62
+ def process;
63
+ if ( binding.variables.containsKey("env") ) {
64
+ process = command.execute(env,_status.getStepDirectory());
65
+ } else {
66
+ process = command.execute();
67
+ }
68
+ process << script;
69
+ process << "__END__";
70
+ process.out.close()
71
+
72
+ // capture the standard output from the run of the script
73
+ completeOutput = process.text;
74
+
75
+ // find the beginning of the script output delimiter
76
+ int delimiterStart = completeOutput.lastIndexOf(OUTPUT_DELIMITER);
77
+
78
+ // extract script output preceding delimiter and send to terminal
79
+ stdout = completeOutput.substring(0, delimiterStart);
80
+ stdoutWithCorrectLineSeparators = stdout.replace("\n", System.getProperty("line.separator"));
81
+ System.out.print(stdoutWithCorrectLineSeparators);
82
+
83
+ // extract yaml output following delimiter
84
+ int yamlStart = delimiterStart + OUTPUT_DELIMITER.length();
85
+ yamlOutput = completeOutput.substring(yamlStart);
86
+
87
+ // parse the yaml block and save output variable values in map
88
+ import org.yaml.snakeyaml.Yaml;
89
+ Yaml yaml = new Yaml();
90
+ def outputMap = (Map) yaml.load(yamlOutput);
91
+ outputMap.each {
92
+ if (it.value == "null") {
93
+ setProperty(it.key, null);
94
+ } else if (_type.get(it.key) == "File") {
95
+ setProperty(it.key, new File(_status.getStepDirectory(), it.value));
96
+ } else {
97
+ setProperty(it.key, it.value)
98
+ }
99
+ }
100
+ String enabledInputs = outputMap.get("enabledInputs");
101
+ if (enabledInputs != null) {
102
+ for (name in enabledInputs.split(" ")) {
103
+ _status.enableInput(name);
104
+ }
105
+ }
106
+ String enabledOutputs = outputMap.get("enabledOutputs");
107
+ if (enabledOutputs != null) {
108
+ for (name in enabledOutputs.split(" ")) {
109
+ _status.enableOutput(name);
110
+ }
111
+ }
112
+ String disabledInputs = outputMap.get("disabledInputs");
113
+ if (disabledInputs != null) {
114
+ for (name in disabledInputs.split(" ")) {
115
+ _status.disableInput(name);
116
+ }
117
+ }
118
+ String disabledOutputs = outputMap.get("disabledOutputs");
119
+ if (disabledOutputs != null) {
120
+ for (name in disabledOutputs.split(" ")) {
121
+ _status.disableOutput(name);
122
+ }
123
+ }
124
+
125
+ //////////////////////////////////////////////////////////////////
126
+
127
+ - id : PerlActorNode
128
+ type : Node
129
+ properties :
130
+ actor : !inline
131
+ type : PerlActor
132
+
0 commit comments