diff --git a/CHANGELOG.md b/CHANGELOG.md index d9d24730..68025ca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features * Add support for PHP 8.4 +* Add the ability to set a default task when calling `castor` without any arguments ## 0.19.1 (2024-11-04) diff --git a/doc/getting-started/basic-usage.md b/doc/getting-started/basic-usage.md index cc7fa300..b3f39ba5 100644 --- a/doc/getting-started/basic-usage.md +++ b/doc/getting-started/basic-usage.md @@ -103,3 +103,20 @@ function a_very_long_function_name_that_is_very_painful_to_write(): void > [!TIP] > Related example: [foo.php](https://github.com/jolicode/castor/blob/main/examples/foo.php) + +## Setting a default task + +The `Castor\Attribute\AsTask` attribute allows you to set a default task when +calling `castor` without any arguments: + +```php +use Castor\Attribute\AsTask; + +use function Castor\io; + +#[AsTask(name: 'bar', namespace: 'foo', default: true)] +function a_very_long_function_name_that_is_very_painful_to_write(): void +{ + io()->writeln('Foo bar'); +} +``` diff --git a/src/Attribute/AsTask.php b/src/Attribute/AsTask.php index f745fb75..91235ae8 100644 --- a/src/Attribute/AsTask.php +++ b/src/Attribute/AsTask.php @@ -17,6 +17,7 @@ public function __construct( public array $onSignals = [], public string|bool $enabled = true, public bool $ignoreValidationErrors = false, + public bool $default = false, ) { } } diff --git a/src/Function/FunctionLoader.php b/src/Function/FunctionLoader.php index 58036bdb..833bea00 100644 --- a/src/Function/FunctionLoader.php +++ b/src/Function/FunctionLoader.php @@ -11,6 +11,7 @@ use Castor\Descriptor\SymfonyTaskDescriptor; use Castor\Descriptor\TaskDescriptor; use Castor\Factory\TaskCommandFactory; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -23,6 +24,7 @@ public function __construct( #[Autowire(lazy: true)] private readonly Application $application, private readonly TaskCommandFactory $taskCommandFactory, + private readonly LoggerInterface $logger, ) { } @@ -64,8 +66,31 @@ public function loadTasks( array $taskDescriptors, array $symfonyTaskDescriptors, ): void { + $previousDefault = null; + foreach ($taskDescriptors as $descriptor) { $this->application->add($this->taskCommandFactory->createTask($descriptor)); + + if ($descriptor->taskAttribute->default) { + $taskName = $descriptor->taskAttribute->name; + + if ($descriptor->taskAttribute->namespace) { + $taskName = $descriptor->taskAttribute->namespace . ':' . $taskName; + } + + if (null !== $previousDefault) { + $this->logger->warning( + \sprintf( + 'Task "%s" is marked as default, but task "%s" is already marked as default', + $taskName, + $previousDefault + ) + ); + } + + $previousDefault = $taskName; + $this->application->setDefaultCommand($taskName); + } } foreach ($symfonyTaskDescriptors as $descriptor) { $this->application->add(SymfonyTaskCommand::createFromDescriptor($descriptor));