diff --git a/bin/generate-tests.php b/bin/generate-tests.php index b17f47a6..41dea9f4 100755 --- a/bin/generate-tests.php +++ b/bin/generate-tests.php @@ -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"; diff --git a/doc/getting-started/basic-usage.md b/doc/getting-started/basic-usage.md index b3f39ba5..f3a6ea1f 100644 --- a/doc/getting-started/basic-usage.md +++ b/doc/getting-started/basic-usage.md @@ -106,7 +106,7 @@ 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 @@ -114,9 +114,13 @@ 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 castor to display all available commands.'); + io()->comment('Run castor about to display this project help.'); + io()->comment('Run castor help [command] to display Castor help.'); } ``` diff --git a/src/Function/FunctionLoader.php b/src/Function/FunctionLoader.php index 833bea00..26a9adad 100644 --- a/src/Function/FunctionLoader.php +++ b/src/Function/FunctionLoader.php @@ -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; @@ -24,7 +24,6 @@ public function __construct( #[Autowire(lazy: true)] private readonly Application $application, private readonly TaskCommandFactory $taskCommandFactory, - private readonly LoggerInterface $logger, ) { } @@ -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; diff --git a/tests/Generated/DefaultTaskConflictTest.php b/tests/Generated/DefaultTaskConflictTest.php new file mode 100644 index 00000000..a5a8e2db --- /dev/null +++ b/tests/Generated/DefaultTaskConflictTest.php @@ -0,0 +1,22 @@ +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()); + } +} diff --git a/tests/Generated/DefaultTaskConflictTest.php.err.txt b/tests/Generated/DefaultTaskConflictTest.php.err.txt new file mode 100644 index 00000000..0e561c5e --- /dev/null +++ b/tests/Generated/DefaultTaskConflictTest.php.err.txt @@ -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. + + diff --git a/tests/Generated/DefaultTaskConflictTest.php.output.txt b/tests/Generated/DefaultTaskConflictTest.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Generated/DefaultTaskTest.php b/tests/Generated/DefaultTaskTest.php new file mode 100644 index 00000000..cc670735 --- /dev/null +++ b/tests/Generated/DefaultTaskTest.php @@ -0,0 +1,22 @@ +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()); + } +} diff --git a/tests/Generated/DefaultTaskTest.php.output.txt b/tests/Generated/DefaultTaskTest.php.output.txt new file mode 100644 index 00000000..09e16f36 --- /dev/null +++ b/tests/Generated/DefaultTaskTest.php.output.txt @@ -0,0 +1 @@ +about diff --git a/tests/fixtures/broken/default-task-conflict/castor.php b/tests/fixtures/broken/default-task-conflict/castor.php new file mode 100644 index 00000000..82673994 --- /dev/null +++ b/tests/fixtures/broken/default-task-conflict/castor.php @@ -0,0 +1,13 @@ +writeln('about'); +}