Skip to content

Commit a94306c

Browse files
authored
Merge branch 'main' into fix/clear-workflow-pipelines
2 parents 7641d57 + 57b0c17 commit a94306c

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

src/Adapter/Composer.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private function execute(Process $process): void
3232
});
3333

3434
if (0 !== $process->getExitCode()) {
35-
throw new \RuntimeException(sprintf('Process exited unexpectedly. %s', $process->getCommandLine()));
35+
throw new ComposerFailureException($process->getCommandLine(), sprintf('Process exited unexpectedly with output: %s', $process->getErrorOutput()), $process->getExitCode());
3636
}
3737
}
3838

@@ -86,14 +86,76 @@ public function minimumStability(string $stability): void
8686
);
8787
}
8888

89+
private function clearVendor(): void
90+
{
91+
$iterator = new \AppendIterator();
92+
93+
try {
94+
$iterator->append(new \GlobIterator($this->workdir.'/composer.*', \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME));
95+
$iterator->append(new \RecursiveIteratorIterator(
96+
new \RecursiveDirectoryIterator(
97+
$this->workdir.'/vendor',
98+
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS
99+
),
100+
\RecursiveIteratorIterator::CHILD_FIRST,
101+
));
102+
} catch (\UnexpectedValueException $e) {
103+
$this->logger->warning($e->getMessage());
104+
}
105+
106+
foreach ($iterator as $file) {
107+
'dir' === $file->getType() ? rmdir($file->getPathname()) : unlink($file->getPathname());
108+
}
109+
}
110+
89111
public function init(string $name): void
90112
{
113+
if (file_exists($this->workdir.'/composer.json')) {
114+
if (filesize($this->workdir.'/composer.json') <= 2) {
115+
$this->clearVendor();
116+
} else {
117+
try {
118+
$this->allowPlugins('php-http/discovery');
119+
120+
return;
121+
} catch (ComposerFailureException) {
122+
$this->clearVendor();
123+
}
124+
}
125+
}
126+
91127
$this->command(
92128
'composer',
93129
'init',
94130
'--no-interaction',
95131
sprintf('--name=%s', $name),
96132
);
133+
134+
$this->allowPlugins('php-http/discovery');
135+
}
136+
137+
public function allowPlugins(string ...$plugins): void
138+
{
139+
foreach ($plugins as $packageName) {
140+
$this->command(
141+
'composer',
142+
'config',
143+
sprintf('allow-plugins.%s', $packageName),
144+
'true',
145+
);
146+
}
147+
}
148+
149+
public function denyPlugins(string ...$plugins): void
150+
{
151+
foreach ($plugins as $packageName) {
152+
$this->command(
153+
'composer',
154+
'config',
155+
sprintf('allow-plugins.%s', $packageName),
156+
'false',
157+
);
158+
}
97159
}
98160

99161
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Satellite\Adapter;
6+
7+
final class ComposerFailureException extends \RuntimeException
8+
{
9+
public function __construct(private readonly string $command = '', string $message = '', int $code = 0, null|\Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
14+
public function getCommand(): string
15+
{
16+
return $this->command;
17+
}
18+
}

src/Console/Command/PipelineRunCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6565
),
6666
);
6767

68+
if (!file_exists('pipeline.php')) {
69+
$style->error('The provided path does not contain one single pipeline, did you mean to run "run:workflow"?');
70+
71+
return \Symfony\Component\Console\Command\Command::FAILURE;
72+
}
6873
/** @var callable(runtime: PipelineRuntimeInterface): \Runtime $pipeline */
6974
$pipeline = include 'pipeline.php';
7075

src/Console/Command/WorkflowRunCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6161
new \Kiboko\Component\Pipeline\PipelineRunner(new \Psr\Log\NullLogger()),
6262
);
6363

64+
if (!file_exists('workflow.php')) {
65+
$style->error('The provided path does not contain a workflow, did you mean to run "run:pipeline"?');
66+
67+
return \Symfony\Component\Console\Command\Command::FAILURE;
68+
}
6469
/** @var callable(runtime: WorkflowRuntimeInterface): \Runtime $workflow */
6570
$workflow = include 'workflow.php';
6671

0 commit comments

Comments
 (0)