1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\AentDockerCompose\YamlTools; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
|
8
|
|
|
class YamlTools |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Merge the content of $sourceFile into $destinationFile's one (overwritten) |
13
|
|
|
* @param string $destinationFile |
14
|
|
|
* @param string $sourceFile |
15
|
|
|
*/ |
16
|
|
|
public static function mergeTwoFiles(string $destinationFile, string $sourceFile): void |
17
|
|
|
{ |
18
|
|
|
$files = [$destinationFile, $sourceFile]; |
19
|
|
|
self::mergeSuccessive($files, $destinationFile); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Given an array of yaml file pathnames, merge them from the last to the first |
24
|
|
|
* @param mixed[] $yamlFilePathnames |
25
|
|
|
* @param null|string $outputFile if null, dump the result to stdout |
26
|
|
|
*/ |
27
|
|
|
public static function mergeSuccessive(array $yamlFilePathnames, ?string $outputFile = null): void |
28
|
|
|
{ |
29
|
|
|
$command = array('yaml-tools', 'merge', '-i'); |
30
|
|
|
$command = array_merge($command, $yamlFilePathnames); |
31
|
|
|
if (null !== $outputFile) { |
32
|
|
|
$command[] = '-o'; |
33
|
|
|
$command[] = $outputFile; |
34
|
|
|
} |
35
|
|
|
$process = new Process($command); |
36
|
|
|
$process->enableOutput(); |
37
|
|
|
$process->setTty(true); |
38
|
|
|
$process->mustRun(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Merge yaml content into one file |
43
|
|
|
* @param string $content |
44
|
|
|
* @param string $file |
45
|
|
|
*/ |
46
|
|
|
public static function mergeContentIntoFile(string $content, string $file): void |
47
|
|
|
{ |
48
|
|
|
$fileSystem = new Filesystem(); |
49
|
|
|
$tmpFile = $fileSystem->tempnam(sys_get_temp_dir(), 'yaml-tools-merge-'); |
50
|
|
|
$fileSystem->dumpFile($tmpFile, $content); |
51
|
|
|
self::mergeTwoFiles($file, $tmpFile); |
52
|
|
|
$fileSystem->remove($tmpFile); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Delete one yaml item given its path (e.g. key1 key2 0 key3) in the $inputFile, then write it into $outputFile (or stdout if empty) |
57
|
|
|
* Caution : this also deletes its preceding comments |
58
|
|
|
* @param string[] $pathToItem e.g. key1 key2 0 key3 |
59
|
|
|
* @param string $file |
60
|
|
|
*/ |
61
|
|
|
public static function deleteYamlItem(array $pathToItem, string $file): void |
62
|
|
|
{ |
63
|
|
|
$command = array('yaml-tools', 'delete'); |
64
|
|
|
$command = array_merge($command, $pathToItem, [ |
65
|
|
|
'-i', $file, |
66
|
|
|
'-o', $file, |
67
|
|
|
]); |
68
|
|
|
$process = new Process($command); |
69
|
|
|
$process->enableOutput(); |
70
|
|
|
$process->setTty(true); |
71
|
|
|
$process->mustRun(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* See https://github.com/thecodingmachine/yaml-tools#normalize-docker-compose |
76
|
|
|
* @param string $inputFile |
77
|
|
|
* @param string|null $outputFile |
78
|
|
|
*/ |
79
|
|
|
public static function normalizeDockerCompose(string $inputFile, ?string $outputFile = null): void |
80
|
|
|
{ |
81
|
|
|
$command = array('yaml-tools', 'normalize-docker-compose', '-i', $inputFile); |
82
|
|
|
if (null !== $outputFile) { |
83
|
|
|
$command[] = '-o'; |
84
|
|
|
$command[] = $outputFile; |
85
|
|
|
} |
86
|
|
|
$process = new Process($command); |
87
|
|
|
$process->enableOutput(); |
88
|
|
|
$process->setTty(true); |
89
|
|
|
$process->mustRun(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|