Skip to content

Commit

Permalink
feat(task): allow to define a default task
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz authored and lyrixx committed Nov 13, 2024
1 parent 6da60e6 commit 0a99346
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions doc/getting-started/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
```
1 change: 1 addition & 0 deletions src/Attribute/AsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public array $onSignals = [],
public string|bool $enabled = true,
public bool $ignoreValidationErrors = false,
public bool $default = false,
) {
}
}
25 changes: 25 additions & 0 deletions src/Function/FunctionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +24,7 @@ public function __construct(
#[Autowire(lazy: true)]
private readonly Application $application,
private readonly TaskCommandFactory $taskCommandFactory,
private readonly LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 0a99346

Please sign in to comment.