Skip to content

Commit

Permalink
Finish the default feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Nov 13, 2024
1 parent 447d9f9 commit 05966a2
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 14 deletions.
1 change: 1 addition & 0 deletions bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
add_test(['list'], 'LayoutWithOldFolder', '{{ base }}/tests/fixtures/valid/layout-with-old-folder');
add_test([], 'ImportSamePackageWithDefaultVersion', '{{ base }}/tests/fixtures/valid/import-same-package-with-default-version', needRemote: true, needResetVendor: true);
add_test(['fs-watch'], 'WatchWithForcedTimeout', '{{ base }}/tests/fixtures/valid/watch-with-forced-timeout');
add_test([], 'DefaultTask', '{{ base }}/tests/fixtures/valid/default-task');

echo "\nDone.\n";

Expand Down
12 changes: 8 additions & 4 deletions doc/getting-started/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,21 @@ function a_very_long_function_name_that_is_very_painful_to_write(): void
## Setting a default task

The `Castor\Attribute\AsTask` attribute allows you to set a default task when
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
#[AsTask(description: 'Displays some help and available urls for the current project', default: true)]
function about(): void
{
io()->writeln('Foo bar');
io()->title('About this project');

io()->comment('Run <comment>castor</comment> to display all available commands.');
io()->comment('Run <comment>castor about</comment> to display this project help.');
io()->comment('Run <comment>castor help [command]</comment> to display Castor help.');
}
```
13 changes: 3 additions & 10 deletions src/Function/FunctionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Castor\Descriptor\ListenerDescriptor;
use Castor\Descriptor\SymfonyTaskDescriptor;
use Castor\Descriptor\TaskDescriptor;
use Castor\Exception\FunctionConfigurationException;
use Castor\Factory\TaskCommandFactory;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

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

Expand Down Expand Up @@ -78,14 +77,8 @@ public function loadTasks(
$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
)
);
if ($previousDefault) {
throw new FunctionConfigurationException(\sprintf('The task is marked as default, but task "%s()" was already marked as default.', $previousDefault), $descriptor->function);
}

$previousDefault = $taskName;
Expand Down
22 changes: 22 additions & 0 deletions tests/Generated/DefaultTaskConflictTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class DefaultTaskConflictTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/fixtures/broken/default-task-conflict', needRemote: true);

if (1 !== $process->getExitCode()) {
throw new ProcessFailedException($process);
}

$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput());
}
}
7 changes: 7 additions & 0 deletions tests/Generated/DefaultTaskConflictTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
In FunctionLoader.php line XXXX:

Function "about2()" is not properly configured:
The task is marked as default, but task "about()" was already marked as default.
Defined in "castor.php" line 11.


Empty file.
22 changes: 22 additions & 0 deletions tests/Generated/DefaultTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class DefaultTaskTest extends TaskTestCase
{
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/fixtures/valid/default-task');

if (0 !== $process->getExitCode()) {
throw new ProcessFailedException($process);
}

$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
$this->assertSame('', $process->getErrorOutput());
}
}
1 change: 1 addition & 0 deletions tests/Generated/DefaultTaskTest.php.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
about
13 changes: 13 additions & 0 deletions tests/fixtures/broken/default-task-conflict/castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Castor\Attribute\AsTask;

#[AsTask(default: true)]
function about(): void
{
}

#[AsTask(default: true)]
function about2(): void
{
}
11 changes: 11 additions & 0 deletions tests/fixtures/valid/default-task/castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Castor\Attribute\AsTask;

use function Castor\io;

#[AsTask(default: true)]
function about(): void
{
io()->writeln('about');
}

0 comments on commit 05966a2

Please sign in to comment.