Skip to content

Commit 1356983

Browse files
committed
Add fail hook support to MAML recipes
1 parent 313ecdc commit 1356983

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/Import/MamlRecipe.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use function Deployer\before;
2929
use function Deployer\cd;
3030
use function Deployer\download;
31+
use function Deployer\fail;
3132
use function Deployer\host;
3233
use function Deployer\localhost;
3334
use function Deployer\run;
@@ -147,6 +148,9 @@ private function schema(): SchemaType
147148
),
148149
),
149150
),
151+
'fail' => S::optional(
152+
S::map(S::string()),
153+
),
150154
]);
151155
}
152156

@@ -200,6 +204,10 @@ public function run(): void
200204
$this->after($property);
201205
break;
202206

207+
case 'fail':
208+
$this->fail($property);
209+
break;
210+
203211
default:
204212
$this->throwException("Unknown property $key", $property->key->span);
205213
}
@@ -443,6 +451,19 @@ protected function after(Property $property): void
443451
}
444452
}
445453

454+
protected function fail(Property $property): void
455+
{
456+
$object = $property->value;
457+
if (!$object instanceof ObjectNode) {
458+
$this->throwException('Invalid fail format', $object->span);
459+
}
460+
foreach ($object->properties as $property) {
461+
$key = $property->key->value;
462+
$value = Maml::toValue($property->value);
463+
fail($key, $value);
464+
}
465+
}
466+
446467
private function throwException(string $string, Span $span): never
447468
{
448469
throw new Exception(Maml::errorSnippet(

tests/src/Import/MamlRecipeTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,4 +724,50 @@ public function testTaskWithMultipleConfigProperties(): void
724724

725725
self::assertInstanceOf(\Closure::class, $callbackProp->getValue($task));
726726
}
727+
728+
public function testFailHook(): void
729+
{
730+
$this->runMaml('{
731+
tasks: {
732+
deploy: [
733+
{ run: "echo deploy" }
734+
]
735+
rollback: [
736+
{ run: "echo rollback" }
737+
]
738+
}
739+
fail: {
740+
deploy: "rollback"
741+
}
742+
}');
743+
744+
self::assertEquals('rollback', $this->deployer->fail->get('deploy'));
745+
}
746+
747+
public function testFailHookMultipleTasks(): void
748+
{
749+
$this->runMaml('{
750+
tasks: {
751+
deploy: [
752+
{ run: "echo deploy" }
753+
]
754+
migrate: [
755+
{ run: "echo migrate" }
756+
]
757+
rollback: [
758+
{ run: "echo rollback" }
759+
]
760+
unlock: [
761+
{ run: "echo unlock" }
762+
]
763+
}
764+
fail: {
765+
deploy: "rollback"
766+
migrate: "unlock"
767+
}
768+
}');
769+
770+
self::assertEquals('rollback', $this->deployer->fail->get('deploy'));
771+
self::assertEquals('unlock', $this->deployer->fail->get('migrate'));
772+
}
727773
}

0 commit comments

Comments
 (0)