Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added verbose output of failures for requiring the scaffold as dev dep. #1

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,37 @@ cd consumer
composer create-project --prefer-dist drevops/scaffold="@dev" --repository '{"type": "path", "url": "../scaffold", "options": {"symlink": false}}' t1
```

## Maintenance

## Maintenance

composer install
composer lint
composer test


### Debugging

Run once:
```bash
cd scaffold
composer install

export COMPOSER_ALLOW_XDEBUG=1
export COMPOSER_DEBUG_EVENTS=1
export DREVOPS_SCAFFOLD_VERSION=@dev

cd ../consumer
```

Run as needed (from `consumer`):
```bash
# No-install
rm -Rf t1 >/dev/null && php ../scaffold/vendor/composer/composer/bin/composer create-project --prefer-dist drevops/scaffold="@dev" --repository '{"type": "path", "url": "../scaffold", "options": {"symlink": false}}' --no-install t1
```

or

```bash
# Full
rm -Rf t1 >/dev/null && php ../scaffold/vendor/composer/composer/bin/composer create-project --prefer-dist drevops/scaffold="@dev" --repository '{"type": "path", "url": "../scaffold", "options": {"symlink": false}}' t1
```
29 changes: 17 additions & 12 deletions scaffold/scripts/composer/ScaffoldGeneralizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public static function generalizeAndRemoveItselfAfterProjectCreate(Event $event)
// Remove references to this script.
// Remove event script used to invoke this script during the project
// creation.
unset($json['scripts']['post-root-package-install'][array_search(__METHOD__, $json['scripts']['post-root-package-install'], true)]);
unset($json['scripts']['post-root-package-install'][array_search(__METHOD__, $json['scripts']['post-root-package-install'], TRUE)]);
if (empty($json['scripts']['post-root-package-install'])) {
unset($json['scripts']['post-root-package-install']);
}
// Remove the classmap for this script from the autoload section.
unset($json['autoload']['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $json['autoload']['classmap'], true)]);
unset($json['autoload']['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $json['autoload']['classmap'], TRUE)]);
$json['autoload']['classmap'] = array_values($json['autoload']['classmap']);
// Remove the script file.
if (!$is_install) {
Expand All @@ -182,27 +182,32 @@ public static function generalizeAndRemoveItselfAfterProjectCreate(Event $event)
* Require the DrevOps Scaffold package.
*/
public static function postCreateProjectCmd(Event $event): void {
$version = static::getVersion();
$event->getIO()->write(sprintf('<info>Adding the DrevOps Scaffold at version "%s" as a development dependency.</info>', $version));

$ansi = $event->getIO()->isDecorated() ? '--ansi' : '--no-ansi';
$quiet = $event->getIO()->isVerbose() ? '' : '--quiet';
$cmd = 'composer require --no-interaction --dev ' . $ansi . ' ' . $quiet . ' ' . static::DREVOPS_SCAFFOLD_NAME . ':' . static::getVersion();
passthru($cmd, $status);
if ($status != 0) {
throw new \Exception('Command failed with exit code ' . $status);
$cmd = 'composer require --no-interaction --dev ' . $ansi . ' ' . static::DREVOPS_SCAFFOLD_NAME . ':' . $version . ' 2>&1';
exec($cmd, $output, $result_code);
if ($result_code != 0) {
throw new \Exception(sprintf('Command failed with exit code %s and the following output: %s', $result_code, PHP_EOL . implode(PHP_EOL, $output)));
}

// Remove the script file.
$fs = new Filesystem();
$fs->unlink(getcwd() . '/scripts/composer/ScaffoldGeneralizer.php');
}

/**
* Check if dependencies are being installed.
*/
protected static function isInstall(Event $event): bool {
$io = $event->getIO();

$reflectionIo = new \ReflectionObject($io);
$inputProperty = $reflectionIo->getProperty('input');
$inputProperty->setAccessible(TRUE);
$reflection = new \ReflectionObject($io);
$property = $reflection->getProperty('input');
$property->setAccessible(TRUE);
/** @var \Symfony\Component\Console\Input\StringInput $input */
$input = $inputProperty->getValue($io);
$input = $property->getValue($io);

return !$input->getOption('no-install');
}
Expand All @@ -225,7 +230,7 @@ protected static function getVersion(): string {
*/
protected static function arrayUpsert(&$array, $after, $key, $value): void {
if (array_key_exists($after, $array)) {
$position = array_search($after, array_keys($array), true) + 1;
$position = array_search($after, array_keys($array), TRUE) + 1;
$array = array_slice($array, 0, $position, TRUE)
+ [$key => $value]
+ array_slice($array, $position, NULL, TRUE);
Expand Down
18 changes: 14 additions & 4 deletions scaffold/tests/phpunit/Dirs.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function printInfo(): void {
fwrite(STDERR, PHP_EOL . implode(PHP_EOL, $lines) . PHP_EOL);
}

protected function prepareLocalRepo() {
protected function prepareLocalRepo(): void {
$root = $this->fileFindDir('composer.json');

$this->fs->copy($root . '/composer.json', $this->repo . '/composer.json');
Expand All @@ -80,15 +80,25 @@ protected function prepareLocalRepo() {
$this->fs->copy($root . '/myfile1.txt', $this->repo . '/myfile1.txt');

// Add the local repository to the composer.json file.
$dstJson = json_decode(file_get_contents($this->repo . '/composer.json'), TRUE);
$dstJson['repositories'][] = [
$composerjson = file_get_contents($this->repo . '/composer.json');
if ($composerjson === FALSE) {
throw new \Exception('Failed to read the local composer.json file.');
}

/** @var array $dst_json */
$dst_json = json_decode($composerjson, TRUE);
if (!$dst_json) {
throw new \Exception('Failed to decode the local composer.json file.');
}

$dst_json['repositories'][] = [
'type' => 'path',
'url' => $this->repo,
'options' => [
'symlink' => FALSE,
],
];
file_put_contents($this->repo . '/composer.json', json_encode($dstJson, JSON_PRETTY_PRINT));
file_put_contents($this->repo . '/composer.json', json_encode($dst_json, JSON_PRETTY_PRINT));
}

}
18 changes: 12 additions & 6 deletions scaffold/tests/phpunit/Traits/ComposerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function composerCreateProject(array|string $args = NULL): string {
*
* @param string $cmd
* The Composer command to execute (escaped as required)
* @param null $cwd
* @param string|null $cwd
* The current working directory to run the command from.
* @param array $env
* Environment variables to define for the subprocess.
Expand All @@ -35,8 +35,8 @@ protected function composerCreateProject(array|string $args = NULL): string {
*
* @throws \Exception
*/
public function composerRun($cmd, $cwd = NULL, $env = []): string {
$cwd = $cwd ?? $this->dirs->build;
public function composerRun(string $cmd, ?string $cwd = NULL, array $env = []): string {
$cwd = $cwd ?: $this->dirs->build;

$env += [
'DREVOPS_SCAFFOLD_VERSION' => '@dev',
Expand Down Expand Up @@ -64,11 +64,17 @@ public function composerRun($cmd, $cwd = NULL, $env = []): string {
return $output;
}

protected function composerReadJson($path = NULL) {
$path = $path ?? $this->dirs->sut . '/composer.json';
protected function composerReadJson(?string $path = NULL): array {
$path = $path ?: $this->dirs->sut . '/composer.json';
$this->assertFileExists($path);

return json_decode(file_get_contents($path), TRUE);
$composerjson = file_get_contents($path);
$this->assertIsString($composerjson);

$data = json_decode($composerjson, TRUE);
$this->assertIsArray($data);

return $data;
}

}
2 changes: 1 addition & 1 deletion scaffold/tests/phpunit/Traits/EnvTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function envIsSet(string $name): bool {
/**
* Check if an environment variable is not set.
*/
public static function envIsUnset($name): bool {
public static function envIsUnset(string $name): bool {
return getenv($name) === FALSE;
}

Expand Down
4 changes: 2 additions & 2 deletions scaffold/tests/phpunit/Traits/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
trait FileTrait {

public function fileFindDir(string $file, $start = NULL) {
public function fileFindDir(string $file, ?string $start = NULL): string {
if (empty($start)) {
$start = dirname(__FILE__);
}
Expand All @@ -23,7 +23,7 @@ public function fileFindDir(string $file, $start = NULL) {
if ($fs->exists($path)) {
return $current;
}
$current = dirname((string) $current);
$current = dirname($current);
}

throw new \RuntimeException('File not found: ' . $file);
Expand Down
8 changes: 5 additions & 3 deletions scaffold/tests/phpunit/Traits/JsonAssertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
use Helmich\JsonAssert\JsonAssertions;

/**
* Trait JsonAssertTrait.
*
* This trait provides a method to assert JSON data.
*/
trait JsonAssertTrait {

use JsonAssertions;

public function assertJsonHasNoKey($jsonDocument, string $jsonPath, $message = NULL): void {
$result = (new JSONPath($jsonDocument))->find($jsonPath);
public function assertJsonHasNoKey(array $json_data, string $path, ?string $message = NULL): void {
$result = (new JSONPath($json_data))->find($path);

if (isset($result[0])) {
$this->fail($message ?: sprintf("The JSON path '%s' exists, but it was expected not to.", $jsonPath));
$this->fail($message ?: sprintf("The JSON path '%s' exists, but it was expected not to.", $path));
}

$this->addToAssertionCount(1);
Expand Down