-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d827b94
commit 9e8baa3
Showing
4 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} | ||
} |