Skip to content

Commit

Permalink
Merge pull request #455 from jolicode/remote
Browse files Browse the repository at this point in the history
Fixed import of remote packages when a file is specified
  • Loading branch information
lyrixx committed May 7, 2024
2 parents 400e671 + bf52a8b commit 505a4c0
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 15 deletions.
8 changes: 4 additions & 4 deletions castor.composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"sort-packages": true
},
"replace": {
"castor\/castor": "v0.15.0"
"castor/castor": "v0.15.0"
},
"require": {
"doctrine/collections": "^2.2",
"pyrech/castor-example": "^1.0",
"pyrech/castor-example-package-not-published": "*",
"doctrine/collections": "^2.2.2",
"pyrech/castor-example": "^1.2",
"pyrech/castor-example-package-not-published": "^1",
"pyrech/foobar": "v1"
},
"repositories": [
Expand Down
16 changes: 8 additions & 8 deletions castor.composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/remote-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// Importing tasks from a Composer package
import('composer://pyrech/castor-example');
import('composer://pyrech/castor-example', file: 'foobar.php');
// Importing tasks from a Composer package not published on packagist (but still having a composer.json)
import('composer://pyrech/castor-example-package-not-published');
// Importing tasks from a repository not using Composer
Expand Down
1 change: 1 addition & 0 deletions src/Import/Mount.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(
public readonly bool $allowEmptyEntrypoint = false,
public readonly ?string $namespacePrefix = null,
public readonly bool $allowRemotePackage = true,
public readonly ?string $file = null,
) {
}
}
7 changes: 6 additions & 1 deletion src/Import/Remote/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ public function importFromPackage(string $scheme, string $package, ?string $file
throw new ImportError(sprintf('The package "%s" is not installed, make sure you required it in your castor.composer.json file.', $package));
}

if ($file && !file_exists($packageDirectory . '/' . $file)) {
throw new ImportError(sprintf('The file "%s" does not exist in the package "%s".', $file, $package));
}

$this->kernel->addMount(new Mount(
PathHelper::getCastorVendorDir() . '/' . $package . '/' . ($file ?? ''),
PathHelper::getCastorVendorDir() . '/' . $package,
allowEmptyEntrypoint: true,
allowRemotePackage: false,
file: $file,
));

return;
Expand Down
13 changes: 11 additions & 2 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function load(
}

try {
$this->requireEntrypoint($mount->path);
$this->requireEntrypoint($mount);
} catch (CouldNotFindEntrypointException $e) {
if (!$mount->allowEmptyEntrypoint) {
throw $e;
Expand Down Expand Up @@ -152,8 +152,17 @@ private function load(
);
}

private function requireEntrypoint(string $path): void
private function requireEntrypoint(Mount $mount): void
{
$path = $mount->path;

// It's an import, via a remote package, with a file specified
if ($mount->file) {
$this->importer->importFile($mount->path . '/' . $mount->file);

return;
}

if (file_exists($file = $path . '/castor.php')) {
$this->importer->importFile($file);
} elseif (file_exists($file = $path . '/.castor/castor.php')) {
Expand Down
1 change: 1 addition & 0 deletions tests/Generated/ListTest.php.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ parallel:exception Sleep and thro
parallel:sleep Sleeps for 5, 7, and 10 seconds in parallel
pyrech:foobar Hello from foobar!
pyrech:hello-example Hello from example!
pyrech:not-imported Not imported task
qa:cs:cs Fix CS
qa:cs:install install dependencies
qa:cs:update Update dependencies
Expand Down
22 changes: 22 additions & 0 deletions tests/Generated/PyrechNotImportedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class PyrechNotImportedTest extends TaskTestCase
{
// pyrech:not-imported
public function test(): void
{
$process = $this->runTask(['pyrech:not-imported']);

if (1 !== $process->getExitCode()) {
throw new ProcessFailedException($process);
}

$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
}
}
4 changes: 4 additions & 0 deletions tests/Generated/PyrechNotImportedTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

There are no commands defined in the "pyrech" namespace.


Empty file.

0 comments on commit 505a4c0

Please sign in to comment.