Skip to content

Commit

Permalink
feat: find composer.json correctly when run from PHAR (#29)
Browse files Browse the repository at this point in the history
Fixes #37
  • Loading branch information
hussainweb authored Jan 9, 2022
1 parent c7e7aa7 commit d0f858d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
44 changes: 28 additions & 16 deletions src/ConventionalCommits/Configuration/FinderTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\Validator;
use Phar;
use Ramsey\ConventionalCommits\Exception\ComposerNotFound;
use Ramsey\ConventionalCommits\Exception\InvalidArgument;
use Ramsey\ConventionalCommits\Exception\InvalidValue;
Expand All @@ -38,6 +39,7 @@

use function dirname;
use function file_get_contents;
use function getcwd;
use function gettype;
use function implode;
use function is_array;
Expand Down Expand Up @@ -168,7 +170,7 @@ private function validateConfig(object $config): bool
{
$validator = new Validator();
$schema = $validator->loader()->loadObjectSchema(
(object) json_decode((string) file_get_contents((string) realpath(__DIR__ . '/../../../schema.json'))),
(object) json_decode((string) file_get_contents(dirname(dirname(dirname(__DIR__))) . '/schema.json')),
);
$result = $validator->validate($config, $schema);

Expand Down Expand Up @@ -203,6 +205,30 @@ private function validateConfig(object $config): bool
* @throws ComposerNotFound when unable to find a suitable composer.json
*/
private function findComposerJson(Filesystem $filesystem): string
{
$path = Phar::running() ? getcwd() : $this->getAutoloaderPath($filesystem);
$composerJson = '';

do {
if ($filesystem->exists((string) $path . '/composer.json')) {
$composerJson = (string) realpath((string) $path . '/composer.json');
}

// We have reached the root directory.
if (dirname((string) $path) === $path) {
throw new ComposerNotFound('Could not find composer.json.');
}

$path = dirname((string) $path);
} while ($composerJson === '');

return $composerJson;
}

/**
* @throws ComposerNotFound when unable to find the autoload.php file
*/
private function getAutoloaderPath(Filesystem $filesystem): string
{
// We do this as a way to determine where the package is installed, so
// we can determine which composer.json to use. This isn't fool-proof,
Expand All @@ -219,7 +245,6 @@ private function findComposerJson(Filesystem $filesystem): string
];

$path = '';
$composerJson = '';

foreach ($composerAutoloadLocations as $file) {
if ($filesystem->exists($file)) {
Expand All @@ -235,19 +260,6 @@ private function findComposerJson(Filesystem $filesystem): string
);
}

do {
if ($filesystem->exists($path . '/composer.json')) {
$composerJson = (string) realpath($path . '/composer.json');
}

// We have reached the root directory.
if (dirname($path) === $path) {
throw new ComposerNotFound('Could not find composer.json.');
}

$path = dirname($path);
} while ($composerJson === '');

return $composerJson;
return $path;
}
}
2 changes: 1 addition & 1 deletion tests/expect/config_04.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spawn ../../bin/conventional-commits config --config ../configs/config-04.json -
match_max 100000

expect -exact "\r
\[33mIn FinderTool.php line 118:\[39m\r
\[33mIn FinderTool.php line 120:\[39m\r
\[37;41m \[39;49m\r
\[37;41m Expected a configuration array in ../configs/config-04.json; received string instead. \[39;49m\r
\[37;41m \[39;49m\r
Expand Down
2 changes: 1 addition & 1 deletion tests/expect/config_05.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spawn ../../bin/conventional-commits config --config ../configs/config-05.json -
match_max 100000

expect -exact "\r
\[33mIn FinderTool.php line 196:\[39m\r
\[33mIn FinderTool.php line 198:\[39m\r
\[37;41m \[39;49m\r
\[37;41m Invalid /types value found in configuration: The data (string) must match the type: array \[39;49m\r
\[37;41m \[39;49m\r
Expand Down

0 comments on commit d0f858d

Please sign in to comment.