|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentGitLabCI\GitLabCI; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\FilesystemException; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
use TheAentMachine\AentGitLabCI\Exception\GitLabCIFileException; |
|
8
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractBuildJob; |
|
9
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractCleanupJob; |
|
10
|
|
|
use TheAentMachine\AentGitLabCI\GitLabCI\Job\AbstractDeployJob; |
|
11
|
|
|
use TheAentMachine\Aenthill\Pheromone; |
|
12
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
13
|
|
|
use TheAentMachine\YamlTools\YamlTools; |
|
14
|
|
|
use function Safe\chown; |
|
15
|
|
|
use function Safe\chgrp; |
|
16
|
|
|
use function Safe\file_put_contents; |
|
17
|
|
|
|
|
18
|
|
|
final class GitLabCIFile |
|
19
|
|
|
{ |
|
20
|
|
|
public const DEFAULT_FILENAME = '.gitlab-ci.yml'; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $path; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \SplFileInfo */ |
|
26
|
|
|
private $file; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* GitLabCIFile constructor. |
|
30
|
|
|
* @throws MissingEnvironmentVariableException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->path = Pheromone::getContainerProjectDirectory() . '/' . self::DEFAULT_FILENAME; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function exist(): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return \file_exists($this->path); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return self |
|
47
|
|
|
* @throws GitLabCIFileException |
|
48
|
|
|
* @throws FilesystemException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findOrCreate(): self |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->exist()) { |
|
53
|
|
|
return $this->create()->addStages(); |
|
54
|
|
|
} |
|
55
|
|
|
$this->file = new \SplFileInfo($this->path); |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return self |
|
61
|
|
|
* @throws FilesystemException |
|
62
|
|
|
*/ |
|
63
|
|
|
private function create(): self |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->exist()) { |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
$fileSystem = new Filesystem(); |
|
69
|
|
|
$fileSystem->dumpFile($this->path, ''); |
|
70
|
|
|
$containerProjectDirInfo = new \SplFileInfo(\dirname($this->path)); |
|
71
|
|
|
chown($this->path, $containerProjectDirInfo->getOwner()); |
|
72
|
|
|
chgrp($this->path, $containerProjectDirInfo->getGroup()); |
|
73
|
|
|
$this->file = new \SplFileInfo($this->path); |
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return self |
|
79
|
|
|
* @throws GitLabCIFileException |
|
80
|
|
|
* @throws FilesystemException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function addStages(): self |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->exist()) { |
|
85
|
|
|
throw GitLabCIFileException::missingFile(); |
|
86
|
|
|
} |
|
87
|
|
|
$stages = [ |
|
88
|
|
|
'stages' => [ |
|
89
|
|
|
'test', |
|
90
|
|
|
'build', |
|
91
|
|
|
'deploy', |
|
92
|
|
|
'cleanup', |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
$yaml = YamlTools::dump($stages); |
|
96
|
|
|
file_put_contents($this->path, $yaml); |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param AbstractBuildJob $job |
|
102
|
|
|
* @return self |
|
103
|
|
|
* @throws GitLabCIFileException |
|
104
|
|
|
* @throws FilesystemException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addBuild(AbstractBuildJob $job): self |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$this->exist()) { |
|
109
|
|
|
throw GitLabCIFileException::missingFile(); |
|
110
|
|
|
} |
|
111
|
|
|
$yaml = YamlTools::dump($job->dump()); |
|
112
|
|
|
YamlTools::mergeContentIntoFile($yaml, $this->path); |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param AbstractDeployJob $job |
|
118
|
|
|
* @return self |
|
119
|
|
|
* @throws GitLabCIFileException |
|
120
|
|
|
* @throws FilesystemException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function addDeploy(AbstractDeployJob $job): self |
|
123
|
|
|
{ |
|
124
|
|
|
if (!$this->exist()) { |
|
125
|
|
|
throw GitLabCIFileException::missingFile(); |
|
126
|
|
|
} |
|
127
|
|
|
$yaml = YamlTools::dump($job->dump()); |
|
128
|
|
|
YamlTools::mergeContentIntoFile($yaml, $this->path); |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param AbstractCleanupJob $job |
|
134
|
|
|
* @return self |
|
135
|
|
|
* @throws GitLabCIFileException |
|
136
|
|
|
* @throws FilesystemException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function addCleanUp(AbstractCleanupJob $job): self |
|
139
|
|
|
{ |
|
140
|
|
|
if (!$this->exist()) { |
|
141
|
|
|
throw GitLabCIFileException::missingFile(); |
|
142
|
|
|
} |
|
143
|
|
|
$yaml = YamlTools::dump($job->dump()); |
|
144
|
|
|
YamlTools::mergeContentIntoFile($yaml, $this->path); |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|