|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cpx\Commands; |
| 6 | + |
| 7 | +use Cpx\Packages\Package; |
| 8 | +use Cpx\Packages\UserAliases; |
| 9 | +use InvalidArgumentException; |
| 10 | +use Laravel\Prompts\Exceptions\NonInteractiveValidationException; |
| 11 | +use Laravel\Prompts\Prompt; |
| 12 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | + |
| 18 | +use function Laravel\Prompts\error; |
| 19 | +use function Laravel\Prompts\info; |
| 20 | +use function Laravel\Prompts\text; |
| 21 | + |
| 22 | +#[AsCommand( |
| 23 | + name: 'alias', |
| 24 | + description: 'Create a shortcut command for a Composer package', |
| 25 | +)] |
| 26 | +class AliasCommand extends Command |
| 27 | +{ |
| 28 | + protected function configure(): void |
| 29 | + { |
| 30 | + $this->addArgument('package', InputArgument::OPTIONAL, 'The package to alias, e.g. <vendor>/<package>[:version]'); |
| 31 | + $this->addArgument('name', InputArgument::OPTIONAL, 'The alias name to run the package as, e.g. "cpx <name>"'); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 35 | + { |
| 36 | + Prompt::setOutput($output); |
| 37 | + |
| 38 | + try { |
| 39 | + $package = $this->resolvePackage($input); |
| 40 | + $name = $this->resolveName($input, $package); |
| 41 | + } catch (InvalidArgumentException|NonInteractiveValidationException $e) { |
| 42 | + error($e->getMessage()); |
| 43 | + |
| 44 | + return self::FAILURE; |
| 45 | + } |
| 46 | + |
| 47 | + UserAliases::open()->put($name, $package)->save(); |
| 48 | + |
| 49 | + info("Alias created: cpx {$name} now runs {$package}."); |
| 50 | + |
| 51 | + return self::SUCCESS; |
| 52 | + } |
| 53 | + |
| 54 | + private function resolvePackage(InputInterface $input): Package |
| 55 | + { |
| 56 | + if ($package = $input->getArgument('package')) { |
| 57 | + return Package::parse($package); |
| 58 | + } |
| 59 | + |
| 60 | + return Package::parse(text( |
| 61 | + label: 'Which package would you like to alias?', |
| 62 | + placeholder: '<vendor>/<package>[:version]', |
| 63 | + required: 'A package name must be provided.', |
| 64 | + validate: $this->validatePackage(...), |
| 65 | + )); |
| 66 | + } |
| 67 | + |
| 68 | + private function validatePackage(string $value): ?string |
| 69 | + { |
| 70 | + try { |
| 71 | + Package::parse($value); |
| 72 | + |
| 73 | + return null; |
| 74 | + } catch (InvalidArgumentException $e) { |
| 75 | + return $e->getMessage(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private function resolveName(InputInterface $input, Package $package): string |
| 80 | + { |
| 81 | + if ($name = $input->getArgument('name')) { |
| 82 | + if ($error = $this->validateName($name)) { |
| 83 | + throw new InvalidArgumentException($error); |
| 84 | + } |
| 85 | + |
| 86 | + return $name; |
| 87 | + } |
| 88 | + |
| 89 | + return text( |
| 90 | + label: 'What should the alias be called?', |
| 91 | + default: $package->name, |
| 92 | + validate: $this->validateName(...), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private function validateName(string $name): ?string |
| 97 | + { |
| 98 | + if (preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/', $name) !== 1) { |
| 99 | + return 'An alias name may only contain letters, numbers, dots, dashes and underscores.'; |
| 100 | + } |
| 101 | + |
| 102 | + if ($this->getApplication()?->has($name) === true) { |
| 103 | + return "\"{$name}\" is already a cpx command and cannot be used as an alias name."; |
| 104 | + } |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | +} |
0 commit comments