Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test:
@echo "Running test..."
docker exec -it whitecat-82 composer test

php-cs-fixer:
style:
@echo "Launching php-cs-fixer"
docker exec -it whitecat-82 composer php-cs-fixer

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ composer require --dev trusted97/whitecat

### Usage

This command list all possible command available in whitecat
List the commands available in Whitecat:

``` sh
vendor/bin/whitecat list
```

Every generator command writes to the current working directory by default. To target another project without changing directories, pass `--working-dir`:

``` sh
vendor/bin/whitecat phpunit:init --working-dir=/path/to/project
```

Commands that depend on Composer tooling validate the target project's `composer.json` first and print the exact `composer require --dev ...` command when a dependency is missing.

#### Docker setup

This command setup basic docker environment for your library
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"sort-packages": true
},
"scripts": {
"test": "vendor/bin/phpunit -c phpunit.xml --no-coverage",
"test-coverage-text": "XDEBUG_MODE=coverage vendor/bin/phpunit -c phpunit.xml --colors=never --coverage-text",
"test-coverage-html": "XDEBUG_MODE=coverage vendor/bin/phpunit -c phpunit.xml --coverage-html coverage",
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --verbose",
"php-cs-fixer-dry-run": "vendor/bin/php-cs-fixer fix --verbose --diff --dry-run",
"test": "php -d error_reporting=8191 vendor/bin/phpunit -c phpunit.xml --no-coverage",
"test-coverage-text": "XDEBUG_MODE=coverage php -d error_reporting=8191 vendor/bin/phpunit -c phpunit.xml --colors=never --coverage-text",
"test-coverage-html": "XDEBUG_MODE=coverage php -d error_reporting=8191 vendor/bin/phpunit -c phpunit.xml --coverage-html coverage",
"php-cs-fixer": "PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --verbose --sequential",
"php-cs-fixer-dry-run": "PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --verbose --diff --dry-run --sequential",
"phpstan" : "vendor/bin/phpstan analyse src -l 10",
"phpstan-test" : "vendor/bin/phpstan analyse tests -l 10"
}
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
paths:
- src
- tests
parallel:
maximumNumberOfProcesses: 1
57 changes: 57 additions & 0 deletions src/Command/AbstractWhitecatCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Whitecat\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

abstract class AbstractWhitecatCommand extends Command
{
protected function configure(): void
{
$this->addOption(
name: 'working-dir',
mode: InputOption::VALUE_REQUIRED,
description: 'Run the command as if it was started in the given project directory.'
);

$this->setHelp($this->commandHelp());
}

final protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

try {
return $this->runCommand($io, new Filesystem(), $this->resolveWorkingDirectory($input));
} catch (\Throwable $throwable) {
$io->error($throwable->getMessage());
$io->note('Run the command again with -vvv for a stack trace, or check that the target project is writable.');

return self::FAILURE;
}
}

abstract protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int;

protected function commandHelp(): string
{
return 'Generates project files in the current directory. Use <comment>--working-dir</comment> to target another project.';
}

private function resolveWorkingDirectory(InputInterface $input): string
{
$workingDirectory = $input->getOption('working-dir');

if (!\is_string($workingDirectory) || 1 !== \preg_match('/\S/u', $workingDirectory)) {
$workingDirectory = \getcwd() ?: '.';
}

return Path::canonicalize($workingDirectory);
}
}
12 changes: 3 additions & 9 deletions src/Command/DockerSetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\DockerSetupService;
Expand All @@ -15,13 +12,10 @@
description: 'Generate basic docker setup for PHP library',
hidden: false
)]
class DockerSetupCommand extends Command
class DockerSetupCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new DockerSetupService($io, $fs))->run();
return (new DockerSetupService($io, $filesystem, $workingDirectory))->run();
}
}
14 changes: 4 additions & 10 deletions src/Command/GithubInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\GithubInitService;

#[AsCommand(
name: 'github:init',
description: 'Generate basic Github directory with starter files',
description: 'Generate basic GitHub directory with starter files',
hidden: false
)]
class GithubInitCommand extends Command
class GithubInitCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new GithubInitService($io, $fs))->run();
return (new GithubInitService($io, $filesystem, $workingDirectory))->run();
}
}
14 changes: 4 additions & 10 deletions src/Command/GithubIssueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\GithubIssueService;

#[AsCommand(
name: 'github:issue',
description: 'Generate basic template for Github issue',
description: 'Generate basic template for GitHub issue',
hidden: false
)]
class GithubIssueCommand extends Command
class GithubIssueCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new GithubIssueService($io, $fs))->run();
return (new GithubIssueService($io, $filesystem, $workingDirectory))->run();
}
}
14 changes: 4 additions & 10 deletions src/Command/GithubPullCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\GithubPullService;

#[AsCommand(
name: 'github:pull',
description: 'Generate basic template for Github pull request',
description: 'Generate basic template for GitHub pull request',
hidden: false
)]
class GithubPullCommand extends Command
class GithubPullCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new GithubPullService($io, $fs))->run();
return (new GithubPullService($io, $filesystem, $workingDirectory))->run();
}
}
12 changes: 3 additions & 9 deletions src/Command/GithubWorkflowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\GithubWorkflowService;
Expand All @@ -15,13 +12,10 @@
description: 'Generate basic workflow for PHP library',
hidden: false
)]
class GithubWorkflowCommand extends Command
class GithubWorkflowCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new GithubWorkflowService($io, $fs))->run();
return (new GithubWorkflowService($io, $filesystem, $workingDirectory))->run();
}
}
12 changes: 3 additions & 9 deletions src/Command/PhpCsFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\PhpCsFixerService;
Expand All @@ -15,13 +12,10 @@
description: 'Setup basic php-cs-fixer for PHP library',
hidden: false
)]
class PhpCsFixerCommand extends Command
class PhpCsFixerCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new PhpCsFixerService($io, $fs))->run();
return (new PhpCsFixerService($io, $filesystem, $workingDirectory))->run();
}
}
12 changes: 3 additions & 9 deletions src/Command/PhpStanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\PhpStanService;
Expand All @@ -15,13 +12,10 @@
description: 'Setup basic phpstan for PHP library',
hidden: false
)]
class PhpStanCommand extends Command
class PhpStanCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new PhpStanService($io, $fs))->run();
return (new PhpStanService($io, $filesystem, $workingDirectory))->run();
}
}
12 changes: 3 additions & 9 deletions src/Command/PhpUnitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Whitecat\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Whitecat\Service\PhpUnitService;
Expand All @@ -15,13 +12,10 @@
description: 'Setup basic phpunit for PHP library',
hidden: false
)]
class PhpUnitCommand extends Command
class PhpUnitCommand extends AbstractWhitecatCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
protected function runCommand(SymfonyStyle $io, Filesystem $filesystem, string $workingDirectory): int
{
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem();

return (new PhpUnitService($io, $fs))->run();
return (new PhpUnitService($io, $filesystem, $workingDirectory))->run();
}
}
18 changes: 18 additions & 0 deletions src/Console/CommandMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Whitecat\Console;

final class CommandMessages
{
public const SUCCESS = 'Done. Your project files are ready.';

public static function skipped(string $target): string
{
return \sprintf('Skipped %s.', $target);
}

public static function adding(string $target): string
{
return \sprintf('Adding %s...', $target);
}
}
38 changes: 38 additions & 0 deletions src/Console/WhitecatApplicationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Whitecat\Console;

use Symfony\Component\Console\Application;
use Whitecat\Command\DockerSetupCommand;
use Whitecat\Command\GithubInitCommand;
use Whitecat\Command\GithubIssueCommand;
use Whitecat\Command\GithubPullCommand;
use Whitecat\Command\GithubWorkflowCommand;
use Whitecat\Command\PhpCsFixerCommand;
use Whitecat\Command\PhpStanCommand;
use Whitecat\Command\PhpUnitCommand;

final class WhitecatApplicationFactory
{
public static function create(): Application
{
$application = new Application('whitecat', self::version());
$application->addCommands([
new DockerSetupCommand(),
new GithubInitCommand(),
new GithubIssueCommand(),
new GithubPullCommand(),
new GithubWorkflowCommand(),
new PhpCsFixerCommand(),
new PhpStanCommand(),
new PhpUnitCommand(),
]);

return $application;
}

private static function version(): string
{
return '1.x-dev';
}
}
Loading
Loading