|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\Pipeline\Step; |
|
8
|
|
|
use Ktomk\Pipelines\Lib; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class StepScriptWriter |
|
12
|
|
|
* |
|
13
|
|
|
* Writes a pipeline step script as string. In use for script and after-script. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Ktomk\Pipelines\Runner |
|
16
|
|
|
*/ |
|
17
|
|
|
class StepScriptWriter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param array|string[] $script |
|
21
|
|
|
* @param int $status exit status |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public static function writeAfterScript(array $script, $status = 0) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$buffer = "# this /bin/sh script is generated from a pipeline after-script\n"; |
|
26
|
1 |
|
$buffer .= "set -e\n"; |
|
27
|
1 |
|
$buffer .= sprintf("BITBUCKET_EXIT_CODE=%d\n", $status); |
|
28
|
1 |
|
$buffer .= "export BITBUCKET_EXIT_CODE\n"; |
|
29
|
1 |
|
$buffer .= self::generateScriptBody($script); |
|
30
|
|
|
|
|
31
|
1 |
|
return $buffer; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* generate step script from Step |
|
36
|
|
|
* |
|
37
|
|
|
* @param array|string[] $script |
|
38
|
|
|
* @param bool $scriptExitEarly the 'script.exit-early' configuration setting |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function writeStepScript(array $script, $scriptExitEarly = false) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$buffer = "# this /bin/sh script is generated from a pipeline script\n"; |
|
45
|
1 |
|
$buffer .= "set -e\n"; |
|
46
|
1 |
|
$buffer .= self::generateScriptBody( |
|
47
|
1 |
|
$script, |
|
48
|
1 |
|
self::getLinePostCommand($scriptExitEarly) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
1 |
|
return $buffer; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* after command |
|
56
|
|
|
* |
|
57
|
|
|
* optional command after each line in a step script to more strictly |
|
58
|
|
|
* check the last step script command exit status. |
|
59
|
|
|
* |
|
60
|
|
|
* for debugging purposes. |
|
61
|
|
|
* |
|
62
|
|
|
* @param bool $strict |
|
63
|
|
|
* |
|
64
|
|
|
* @return null|string |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public static function getLinePostCommand($strict) |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $strict ? '( r=$?; if [ $r -ne 0 ]; then exit $r; fi; ) || exit' . "\n" : null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array|string[] $script |
|
73
|
|
|
* @param null|string $afterCommand |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public static function generateScriptBody(array $script, $afterCommand = null) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$buffer = ''; |
|
80
|
2 |
|
foreach ($script as $line) { |
|
81
|
1 |
|
$command = self::generateCommand($line); |
|
82
|
1 |
|
$line && $buffer .= 'printf \'\\n\'' . "\n"; |
|
83
|
1 |
|
$buffer .= 'printf \'\\035+ %s\\n\' ' . Lib::quoteArg($command) . "\n"; |
|
84
|
1 |
|
$buffer .= $command . "\n"; |
|
85
|
1 |
|
null !== $afterCommand && $buffer .= $afterCommand; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $buffer; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param null|array|string $line |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public static function generateCommand($line) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$standard = is_scalar($line) || null === $line; |
|
99
|
2 |
|
$pipe = is_array($line) && isset($line['pipe']) && is_string($line['pipe']); |
|
100
|
|
|
|
|
101
|
2 |
|
if ($standard) { |
|
102
|
|
|
/** @var null|float|int|string $line */ |
|
103
|
1 |
|
return (string)$line; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
$line = (array)$line; |
|
107
|
1 |
|
$buffer = ''; |
|
108
|
|
|
|
|
109
|
1 |
|
if ($pipe) { |
|
110
|
1 |
|
$buffer .= "echo \"pipe: {$line['pipe']} (pending feature)\" # pipe feature is pending\n"; |
|
111
|
1 |
|
if (isset($line['variables']) && is_array($line['variables'])) { |
|
112
|
1 |
|
foreach ($line['variables'] as $name => $value) { |
|
113
|
1 |
|
$buffer .= sprintf( |
|
114
|
1 |
|
"printf %%s %s; printf '%%s ' %s; printf '\\n' \n", |
|
115
|
1 |
|
Lib::quoteArg(" ${name} (${value}): "), |
|
116
|
|
|
$value |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
return $buffer; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|