diff --git a/src/Commands/Generators/Code.php b/src/Commands/Generators/Code.php new file mode 100644 index 0000000..33f12d4 --- /dev/null +++ b/src/Commands/Generators/Code.php @@ -0,0 +1,31 @@ +getStubFileName(); + } + + /** + * Get the stub file name for the generator. + */ + public function getStubFileName(): string + { + return __DIR__.'/../../../storage/canvas/code.stub'; + } +} diff --git a/src/Commands/Generators/ConsoleGenerator.php b/src/Commands/Generators/ConsoleGenerator.php new file mode 100644 index 0000000..de76c1a --- /dev/null +++ b/src/Commands/Generators/ConsoleGenerator.php @@ -0,0 +1,87 @@ + + */ + protected string $processor = GeneratesCommandCode::class; + + /** + * Get the stub file for the generator. + */ + public function getPublishedStubFileName(): ?string + { + return null; + } + + /** + * Get the stub file for the generator. + */ + public function getStubFile(): string + { + return __DIR__.'/../../../storage/canvas/generator.stub'; + } + + /** + * Get the default namespace for the class. + */ + public function getDefaultNamespace(string $rootNamespace): string + { + return $this->preset->config('console.namespace', $rootNamespace.'\Console\Commands'); + } + + /** + * Generator options. + * + * @return array + */ + public function generatorOptions(): array + { + /** @var string $command */ + $command = $this->option('command'); + + if (! Str::startsWith($command, 'make:')) { + $command = "make:{$command}"; + } + + return [ + 'command' => $command, + 'force' => $this->option('force'), + ]; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the generator already exists'], + ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned', 'make:name'], + ]; + } +} diff --git a/storage/canvas/code.stub b/storage/canvas/code.stub new file mode 100644 index 0000000..daae325 --- /dev/null +++ b/storage/canvas/code.stub @@ -0,0 +1,8 @@ + + */ + protected string $processor = GeneratesCode::class; + + /** + * Get the stub file name for the generator. + */ + public function getStubFileName(): string + { + // Implement path to stub file. + } + + /** + * Get the default namespace for the class. + */ + public function getDefaultNamespace(string $rootNamespace): string + { + return $rootNamespace; + } + + /** + * Generator options. + */ + public function generatorOptions(): array + { + return [ + 'name' => $this->generatorName(), + ]; + } +} diff --git a/tests/Feature/Commands/Generators/CodeTest.php b/tests/Feature/Commands/Generators/CodeTest.php new file mode 100644 index 0000000..eb1c192 --- /dev/null +++ b/tests/Feature/Commands/Generators/CodeTest.php @@ -0,0 +1,53 @@ + 'App', 'generators' => [Generators\Code::class]], $this->app->basePath(), $this->filesystem + ); + + $this->instance('orchestra.canvas', $preset); + + Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); + + $this->artisan('make:class', ['name' => 'Value/Foo']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Value;', + 'class Foo', + ], 'app/Value/Foo.php'); + } + + /** @test */ + public function it_cant_generate_class_file() + { + $this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException'); + $this->expectExceptionMessage('The command "make:class" does not exist.'); + + $this->artisan('make:class', ['name' => 'Foo']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Value;', + 'class Foo', + ], 'app/Value/Foo.php'); + } +} diff --git a/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php b/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php new file mode 100644 index 0000000..c7a48f6 --- /dev/null +++ b/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php @@ -0,0 +1,87 @@ + 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem + ); + + $this->instance('orchestra.canvas', $preset); + + Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); + + $this->artisan('make:generator', ['name' => 'FooCommand']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Console\Commands;', + 'use Orchestra\Canvas\Commands\Generator;', + 'use Symfony\Component\Console\Attribute\AsCommand;', + '#[AsCommand(name: \'make:name\', description: \'Create a new class\')]', + 'class FooCommand extends Generator', + ], 'app/Console/Commands/FooCommand.php'); + } + + /** @test */ + public function it_can_generate_command_file_with_command_name() + { + $preset = new Laravel( + ['namespace' => 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem + ); + + $this->instance('orchestra.canvas', $preset); + + Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); + + $this->artisan('make:generator', ['name' => 'FooCommand', '--command' => 'make:foobar']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Console\Commands;', + 'use Orchestra\Canvas\Commands\Generator;', + 'use Symfony\Component\Console\Attribute\AsCommand;', + '#[AsCommand(name: \'make:foobar\', description: \'Create a new class\')]', + 'class FooCommand extends Generator', + ], 'app/Console/Commands/FooCommand.php'); + } + + /** @test */ + public function it_can_generate_command_file_with_command_name_without_make_prefix() + { + $preset = new Laravel( + ['namespace' => 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem + ); + + $this->instance('orchestra.canvas', $preset); + + Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); + + $this->artisan('make:generator', ['name' => 'FooCommand', '--command' => 'foobar']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Console\Commands;', + 'use Orchestra\Canvas\Commands\Generator;', + 'use Symfony\Component\Console\Attribute\AsCommand;', + '#[AsCommand(name: \'make:foobar\', description: \'Create a new class\')]', + 'class FooCommand extends Generator', + ], 'app/Console/Commands/FooCommand.php'); + } +}