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