Skip to content

Commit

Permalink
Merge pull request #106 from php-etl/fix/clear-vendors
Browse files Browse the repository at this point in the history
clear vendors when composer.json is not valid,
  • Loading branch information
gplanchat authored Jun 8, 2023
2 parents 27733fb + 2e87672 commit 57b0c17
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/Adapter/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function execute(Process $process): void
});

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

Expand Down Expand Up @@ -86,14 +86,76 @@ public function minimumStability(string $stability): void
);
}

private function clearVendor(): void
{
$iterator = new \AppendIterator();

try {
$iterator->append(new \GlobIterator($this->workdir.'/composer.*', \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME));
$iterator->append(new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$this->workdir.'/vendor',
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::CHILD_FIRST,
));
} catch (\UnexpectedValueException $e) {
$this->logger->warning($e->getMessage());
}

foreach ($iterator as $file) {
'dir' === $file->getType() ? rmdir($file->getPathname()) : unlink($file->getPathname());
}
}

public function init(string $name): void
{
if (file_exists($this->workdir.'/composer.json')) {
if (filesize($this->workdir.'/composer.json') <= 2) {
$this->clearVendor();
} else {
try {
$this->allowPlugins('php-http/discovery');

return;
} catch (ComposerFailureException) {
$this->clearVendor();
}
}
}

$this->command(
'composer',
'init',
'--no-interaction',
sprintf('--name=%s', $name),
);

$this->allowPlugins('php-http/discovery');
}

public function allowPlugins(string ...$plugins): void
{
foreach ($plugins as $packageName) {
$this->command(
'composer',
'config',
sprintf('allow-plugins.%s', $packageName),
'true',
);
}
}

public function denyPlugins(string ...$plugins): void
{
foreach ($plugins as $packageName) {
$this->command(
'composer',
'config',
sprintf('allow-plugins.%s', $packageName),
'false',
);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Adapter/ComposerFailureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Adapter;

final class ComposerFailureException extends \RuntimeException
{
public function __construct(private readonly string $command = '', string $message = '', int $code = 0, null|\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

public function getCommand(): string
{
return $this->command;
}
}
5 changes: 5 additions & 0 deletions src/Console/Command/PipelineRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
),
);

if (!file_exists('pipeline.php')) {
$style->error('The provided path does not contain one single pipeline, did you mean to run "run:workflow"?');

return \Symfony\Component\Console\Command\Command::FAILURE;
}
/** @var callable(runtime: PipelineRuntimeInterface): \Runtime $pipeline */
$pipeline = include 'pipeline.php';

Expand Down
5 changes: 5 additions & 0 deletions src/Console/Command/WorkflowRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
new \Kiboko\Component\Pipeline\PipelineRunner(new \Psr\Log\NullLogger()),
);

if (!file_exists('workflow.php')) {
$style->error('The provided path does not contain a workflow, did you mean to run "run:pipeline"?');

return \Symfony\Component\Console\Command\Command::FAILURE;
}
/** @var callable(runtime: WorkflowRuntimeInterface): \Runtime $workflow */
$workflow = include 'workflow.php';

Expand Down

0 comments on commit 57b0c17

Please sign in to comment.