Skip to content

Commit 89b84e9

Browse files
weaverryanfabpot
authored andcommitted
Adding linting for the new add-lines configurator
1 parent a50caac commit 89b84e9

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/LintManifestsCommand.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LintManifestsCommand extends Command
3131
'conflict' => 0,
3232
'dockerfile' => 0,
3333
'docker-compose' => 0,
34+
'add-lines' => 0,
3435
];
3536

3637
private const SPECIAL_FILES = ['.', '..', 'manifest.json', 'post-install.txt', 'Makefile'];
@@ -84,6 +85,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8485
}
8586
}
8687

88+
if (isset($data['add-lines'])) {
89+
if (!$this->isAddLinesValid($data['add-lines'], $manifest, $output)) {
90+
$exit = 1;
91+
}
92+
}
93+
8794
if ($extraKeys = array_diff_key($data, self::ALLOWED_KEYS)) {
8895
$extraKeys = array_keys($extraKeys);
8996
$lastKey = array_pop($extraKeys);
@@ -122,4 +129,54 @@ protected function execute(InputInterface $input, OutputInterface $output): int
122129

123130
return $exit;
124131
}
132+
133+
private function isAddLinesValid(mixed $data, string $manifest, OutputInterface $output)
134+
{
135+
if (!is_array($data)) {
136+
$output->writeln(sprintf('::error file=%s::"add-lines" must be an array', $manifest));
137+
138+
return false;
139+
}
140+
141+
$isValid = true;
142+
foreach ($data as $index => $addLine) {
143+
foreach (['file', 'content', 'position'] as $key) {
144+
if (!isset($addLine[$key])) {
145+
$output->writeln(sprintf('::error file=%s::"add-lines" (index %d) must have a "%s" key', $manifest, $index, $key));
146+
$isValid = false;
147+
148+
continue;
149+
}
150+
151+
if (!is_string($addLine[$key])) {
152+
$output->writeln(sprintf('::error file=%s::"add-lines" (index %d) has a "%s" key but it must be a string value', $manifest, $index, $key));
153+
154+
$isValid = false;
155+
}
156+
}
157+
158+
if (isset($addLine['position'])) {
159+
$validPositions = ['top', 'bottom', 'after_target'];
160+
if (!\in_array($addLine['position'], $validPositions, true)) {
161+
$output->writeln(sprintf('::error file=%s::"add-lines" (index %d) must have a "position" key with one of the following values: "%s"', $manifest, $index, implode('", "', $validPositions)));
162+
163+
$isValid = false;
164+
}
165+
166+
if ('after_target' === $addLine['position']) {
167+
if (!isset($addLine['target'])) {
168+
$output->writeln(sprintf('::error file=%s::"add-lines" (index %d) must have a "target" key when "position" is "after_target"', $manifest, $index));
169+
170+
$isValid = false;
171+
} elseif (!is_string($addLine['target'])) {
172+
$output->writeln(sprintf('::error file=%s::"add-lines" (index %d) has a "target" key but it must be a string value', $manifest, $index));
173+
174+
$isValid = false;
175+
}
176+
}
177+
}
178+
}
179+
180+
return $isValid;
181+
}
125182
}

0 commit comments

Comments
 (0)