Skip to content

Commit

Permalink
Adds default command
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Dec 24, 2024
1 parent d827b94 commit 9e8baa3
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
41 changes: 41 additions & 0 deletions bin/peck
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Symfony\Component\Console\Application;

$vendorPath = dirname(__DIR__, 4).'/vendor/autoload.php';
$localPath = dirname(__DIR__).'/vendor/autoload.php';

if (file_exists($vendorPath)) {
include_once $vendorPath;
$autoloadPath = $vendorPath;
} else {
include_once $localPath;
$autoloadPath = $localPath;
}

$application = new Application(
'Peck',
'0.0.1',
);

$application->add(
new \Peck\Console\Commands\DefaultCommand(),
);

$application->setDefaultCommand('default');

$application->run();











14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
],
"require": {
"php": "^8.3.0",
"nunomaduro/termwind": "^2.3",
"symfony/console": "^7.2.1",
"symfony/finder": "^7.2",
"tigitz/php-spellchecker": "^0.7.0"
},
"require-dev": {
"laravel/pint": "^1.18.1",
"pestphp/pest": "^3.5.1",
"pestphp/pest-plugin-type-coverage": "^3.1",
"phpstan/phpstan": "^1.12.7",
"rector/rector": "^1.2.8",
"symfony/var-dumper": "^7.1.6"
"laravel/pint": "^1.18.3",
"pestphp/pest": "^3.7.1",
"pestphp/pest-plugin-type-coverage": "^3.2.1",
"phpstan/phpstan": "^1.12.13",
"rector/rector": "^1.2.10",
"symfony/var-dumper": "^7.2.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions src/Checkers/FileSystemChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function __construct(
public function check(array $parameters): array
{
$filesOrDirectories = Finder::create()
->ignoreDotFiles(true)
->ignoreVCS(true)
->ignoreUnreadableDirs()
->ignoreVCSIgnored(true)
->in($parameters['directory'])
->getIterator();

Expand Down
81 changes: 81 additions & 0 deletions src/Console/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Peck\Console\Commands;

use Composer\Autoload\ClassLoader;
use Peck\Kernel;
use Peck\ValueObjects\Issue;
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 function Termwind\render;
use function Termwind\renderUsing;

/**
* @codeCoverageIgnore
*
* @internal
*/
#[AsCommand(name: 'default')]
class DefaultCommand extends Command
{
/**
* Executes the command.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$kernel = Kernel::default();

$issues = $kernel->handle([
'directory' => $directory = self::inferProjectPath(),
]);

foreach ($issues as $issue) {
$this->renderIssue($output, $issue, $directory);
}

return $issues !== [] ? Command::FAILURE : Command::SUCCESS;
}

/**
* Configures the current command.
*/
protected function configure(): void
{
$this->setDescription('Checks for misspellings in the given directory.');
}

protected function renderIssue(OutputInterface $output, Issue $issue, string $currentDirectory): void
{
renderUsing($output);

$file = str_replace($currentDirectory, '.', $issue->file);

render(<<<HTML
<div class="text-red-700 pl-2">
<span>{$issue->misspelling->word} in {$file}.</span>
</div>
HTML);
}

/**
* Infer the project's base directory from the environment.
*/
protected static function inferProjectPath(): string
{
$basePath = dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]);

return match (true) {
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
default => match (true) {
is_dir($basePath.'/src') => ($basePath.'/src'),
is_dir($basePath.'/app') => ($basePath.'/app'),
default => $basePath,
},
};
}
}

0 comments on commit 9e8baa3

Please sign in to comment.