Skip to content

Commit

Permalink
feat!: Add GitHub output format (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankverhoeven authored Apr 14, 2022
1 parent c708a56 commit 3a03d2c
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 133 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: cs2pr
- uses: ramsey/composer-install@v2
- run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
- run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- run: bin/devtools ${{ matrix.tool }}
- run: bin/devtools ${{ matrix.tool }} --format=github
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"composer/semver": "^3.3",
"symfony/console": "^5.4 || ^6.0",
"symfony/process": "^5.4 || ^6.0"
Expand Down
9 changes: 3 additions & 6 deletions src/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ final class AnalyzeCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Run all enabled tools.';

private Configuration $configuration;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;

public function __construct(
private Configuration $configuration,
) {
parent::__construct();
}

Expand Down
21 changes: 14 additions & 7 deletions src/Command/CodesnifferCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class CodesnifferCommand extends DevToolsCommand
{
Expand All @@ -13,14 +15,19 @@ final class CodesnifferCommand extends DevToolsCommand
/** @var string|null */
protected static $defaultDescription = 'PHP_CodeSniffer';

/**
* @inheritDoc
*/
protected function getCommand(): array
protected function getProcess(InputInterface $input): Process
{
return [
$this->withVendorBinPath('phpcs'),
];
if ($this->isGitHubFormat($input)) {
return Process::fromShellCommandline(
$this->withVendorBinPath('phpcs') . ' -q --report=checkstyle | cs2pr',
timeout: null,
);
}

return new Process(
[$this->withVendorBinPath('phpcs')],
timeout: null,
);
}

public static function isAvailable(Configuration $configuration): bool
Expand Down
66 changes: 33 additions & 33 deletions src/Command/DevToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@
use MyOnlineStore\DevTools\Configuration;
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\Process\Process;

abstract class DevToolsCommand extends Command
{
protected Configuration $configuration;
public function __construct(
protected Configuration $configuration,
) {
parent::__construct();
}

public function __construct(Configuration $configuration)
protected function configure(): void
{
$this->configuration = $configuration;

parent::__construct();
$this->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'Output format to use (by supported commands).'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$commands = $this->getMultiCommand();
$processes = $this->getMultiProcess($input);

if (0 === \count($commands)) {
$commands[] = $this->getCommand();
if (0 === \count($processes)) {
$processes[] = $this->getProcess($input);
}

$exitCode = 0;

foreach ($commands as $command) {
$process = new Process(
\array_merge(
$command,
(array) ($input->getArguments()['args'] ?? [])
),
null,
null,
null,
null
);
foreach ($processes as $process) {
$process->start();

$exitCode |= $process->wait(
Expand All @@ -53,31 +51,33 @@ static function (string $_type, string $buffer) use ($output): void {
return $exitCode;
}

protected function withBinPath(string $command): string
protected function getProcess(InputInterface $input): Process
{
return $this->configuration->getRootDir() . 'bin/' . $command;
throw new \RuntimeException('Either implement getProcess() or getMultiProcess()');
}

protected function withVendorBinPath(string $command): string
/**
* @return list<Process>
*/
protected function getMultiProcess(InputInterface $input): array
{
return $this->configuration->getRootDir() . 'vendor/bin/' . $command;
return [];
}

abstract public static function isAvailable(Configuration $configuration): bool;

/**
* @return list<string>
*/
protected function getCommand(): array
protected function isGitHubFormat(InputInterface $input): bool
{
return [];
return 'github' === $input->getOption('format');
}

/**
* @return list<list<string>>
*/
protected function getMultiCommand(): array
protected function withBinPath(string $command): string
{
return [];
return $this->configuration->getRootDir() . 'bin/' . $command;
}

protected function withVendorBinPath(string $command): string
{
return $this->configuration->getRootDir() . 'vendor/bin/' . $command;
}
}
57 changes: 33 additions & 24 deletions src/Command/DoctrineMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,56 @@
namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class DoctrineMigrationsCommand extends DevToolsCommand
{
/** @var string|null */
protected static $defaultName = 'doctrine-migrations';

/** @var string|null */
protected static $defaultDescription = 'Doctrine Migrations, always runs in test environment';

/**
* @inheritDoc
*/
protected function getMultiCommand(): array
protected function getMultiProcess(InputInterface $input): array
{
return [
// Ensure we're up-to-date
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
new Process(
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
timeout: null,
),
// Test all down patches
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'first',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
new Process(
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'first',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
timeout: null,
),
// Test all migrations ánd if down patches did their job
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
new Process(
[
$this->withBinPath('console'),
'doctrine:migrations:migrate',
'--allow-no-migration',
'--no-interaction',
'--env=test',
],
timeout: null,
),
];
}

Expand Down
17 changes: 9 additions & 8 deletions src/Command/LintSymfonyContainerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class LintSymfonyContainerCommand extends DevToolsCommand
Expand All @@ -14,15 +15,15 @@ final class LintSymfonyContainerCommand extends DevToolsCommand
/** @var string|null */
protected static $defaultDescription = 'Lint Symfony container';

/**
* @inheritDoc
*/
protected function getCommand(): array
protected function getProcess(InputInterface $input): Process
{
return [
$this->withBinPath('console'),
'lint:container',
];
return new Process(
[
$this->withBinPath('console'),
'lint:container',
],
timeout: null,
);
}

public static function isAvailable(Configuration $configuration): bool
Expand Down
21 changes: 11 additions & 10 deletions src/Command/LintYamlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class LintYamlCommand extends DevToolsCommand
Expand All @@ -14,17 +15,17 @@ final class LintYamlCommand extends DevToolsCommand
/** @var string|null */
protected static $defaultDescription = 'Lint YAML';

/**
* @inheritDoc
*/
protected function getCommand(): array
protected function getProcess(InputInterface $input): Process
{
return [
$this->withBinPath('console'),
'lint:yaml',
'config',
'--parse-tags',
];
return new Process(
[
$this->withBinPath('console'),
'lint:yaml',
'config',
'--parse-tags',
],
timeout: null,
);
}

public static function isAvailable(Configuration $configuration): bool
Expand Down
9 changes: 3 additions & 6 deletions src/Command/ListPhpVersionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ final class ListPhpVersionsCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Lists PHP versions allowed by composer.json (in JSON format).';

private Configuration $configuration;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;

public function __construct(
private Configuration $configuration,
) {
parent::__construct();
}

Expand Down
9 changes: 3 additions & 6 deletions src/Command/ListToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ final class ListToolsCommand extends Command
/** @var string|null */
protected static $defaultDescription = 'Lists enabled tools (in JSON).';

private Configuration $configuration;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;

public function __construct(
private Configuration $configuration,
) {
parent::__construct();
}

Expand Down
14 changes: 7 additions & 7 deletions src/Command/PhpUnitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace MyOnlineStore\DevTools\Command;

use MyOnlineStore\DevTools\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;

final class PhpUnitCommand extends DevToolsCommand
{
Expand All @@ -13,14 +15,12 @@ final class PhpUnitCommand extends DevToolsCommand
/** @var string|null */
protected static $defaultDescription = 'PHP Unit';

/**
* @inheritDoc
*/
protected function getCommand(): array
protected function getProcess(InputInterface $input): Process
{
return [
$this->withVendorBinPath('phpunit'),
];
return new Process(
[$this->withVendorBinPath('phpunit')],
timeout: null,
);
}

public static function isAvailable(Configuration $configuration): bool
Expand Down
Loading

0 comments on commit 3a03d2c

Please sign in to comment.