|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Exec; |
|
8
|
|
|
use Ktomk\Pipelines\File\BbplMatch; |
|
9
|
|
|
|
|
10
|
|
|
class ArtifactSource |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Exec |
|
14
|
|
|
*/ |
|
15
|
|
|
private $exec; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string container id |
|
19
|
|
|
*/ |
|
20
|
|
|
private $id; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string in-container build directory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dir; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var null|array all in-container build directory file paths |
|
29
|
|
|
*/ |
|
30
|
|
|
private $allFiles; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ArtifactSource constructor. |
|
34
|
|
|
* @param Exec $exec |
|
35
|
|
|
* @param string $id container id |
|
36
|
|
|
* @param string $dir in-container build directory ($BITBUCKET_CLONE_DIR) |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function __construct(Exec $exec, $id, $dir) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->exec = $exec; |
|
41
|
4 |
|
$this->id = $id; |
|
42
|
4 |
|
$this->dir = $dir; |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws \RuntimeException |
|
47
|
|
|
* @return array|string[] |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function getAllFiles() |
|
50
|
|
|
{ |
|
51
|
3 |
|
if (null === $this->allFiles) { |
|
52
|
3 |
|
$this->allFiles = $this->getFindPaths(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $this->allFiles; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $pattern |
|
60
|
|
|
* @throws \RuntimeException |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function findByPattern($pattern) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$matcher = function ($subject) use ($pattern) { |
|
66
|
1 |
|
return BbplMatch::match($pattern, $subject); |
|
67
|
1 |
|
}; |
|
68
|
|
|
|
|
69
|
1 |
|
$paths = $this->getAllFiles(); |
|
70
|
|
|
|
|
71
|
1 |
|
$found = array_filter($paths, $matcher); |
|
72
|
|
|
|
|
73
|
1 |
|
return array_values($found); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string container id |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getId() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->id; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* get an array of paths obtained via docker exec & find |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \RuntimeException |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
3 |
|
private function getFindPaths() |
|
91
|
|
|
{ |
|
92
|
3 |
|
$buffer = $this->getFindBuffer(); |
|
93
|
|
|
|
|
94
|
3 |
|
$lines = explode("\n", trim($buffer, "\n")); |
|
95
|
3 |
|
$pattern = '~^\\./~'; |
|
96
|
3 |
|
$existing = preg_grep($pattern, $lines); |
|
97
|
3 |
|
$paths = preg_replace($pattern, '', $existing); |
|
98
|
|
|
|
|
99
|
3 |
|
return array_values($paths); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @throws \RuntimeException |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
3 |
|
private function getFindBuffer() |
|
107
|
|
|
{ |
|
108
|
|
|
$command = array( |
|
109
|
3 |
|
'find', '(', '-name', '.git', '-o', '-name', '.idea', ')', |
|
110
|
|
|
'-prune', '-type', 'f', '-o', '-type', 'f', |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
3 |
|
$status = $this->exec->capture('docker', array( |
|
114
|
3 |
|
'exec', '-w', '/app', $this->id, $command |
|
115
|
3 |
|
), $out); |
|
116
|
|
|
|
|
117
|
3 |
|
if (0 === $status) { |
|
118
|
2 |
|
return $out; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return ''; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|