Skip to content

Commit f449bcd

Browse files
committed
[Toolkit] Update version handling in dependencies to use string format, improve command output messages, improve InstallComponentCommand (interactive ask), remove LintKitCommand
1 parent d3b3fc0 commit f449bcd

19 files changed

+76
-245
lines changed

src/Toolkit/bin/ux-toolkit-kit-debug

+2-8
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,8 @@ if (!class_exists(Application::class)) {
4747

4848
$filesystem = new Filesystem();
4949
$kitFactory = new KitFactory($filesystem, new KitSynchronizer($filesystem));
50-
$registryFactory = new RegistryFactory(new class([
51-
Type::Local->value => fn () => new LocalRegistry($kitFactory, $filesystem, getcwd() ?? throw new \RuntimeException('The current working directory could not be determined.')),
52-
Type::GitHub->value => fn () => new GitHubRegistry($kitFactory, $filesystem, class_exists(HttpClient::class) ? HttpClient::create() : null),
53-
]) implements ServiceProviderInterface {
54-
use ServiceLocatorTrait;
55-
});
56-
57-
(new Application())->add($command = new DebugKitCommand($registryFactory))
50+
51+
(new Application())->add($command = new DebugKitCommand($kitFactory))
5852
->getApplication()
5953
->setDefaultCommand($command->getName(), true)
6054
->run()

src/Toolkit/bin/ux-toolkit-kit-lint

-61
This file was deleted.

src/Toolkit/composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
},
5050
"bin": [
5151
"bin/ux-toolkit-kit-create",
52-
"bin/ux-toolkit-kit-lint",
5352
"bin/ux-toolkit-kit-debug"
5453
],
5554
"autoload": {

src/Toolkit/config/services.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\UX\Toolkit\Command\DebugKitCommand;
1515
use Symfony\UX\Toolkit\Command\InstallComponentCommand;
16-
use Symfony\UX\Toolkit\Command\LintKitCommand;
1716
use Symfony\UX\Toolkit\Kit\KitFactory;
1817
use Symfony\UX\Toolkit\Kit\KitSynchronizer;
1918
use Symfony\UX\Toolkit\Registry\GitHubRegistry;
@@ -30,27 +29,21 @@
3029

3130
->set('.ux_toolkit.command.debug_kit', DebugKitCommand::class)
3231
->args([
33-
service('.ux_toolkit.registry.factory'),
32+
service('.ux_toolkit.kit.kit_factory'),
3433
])
3534
->tag('console.command')
3635

3736
->set('.ux_toolkit.command.install', InstallComponentCommand::class)
3837
->args([
3938
param('ux_toolkit.kit'),
40-
service('.ux_toolkit.registry.factory'),
39+
service('.ux_toolkit.registry.registry_factory'),
4140
service('filesystem'),
4241
])
4342
->tag('console.command')
4443

45-
->set('.ux_toolkit.command.lint_kit', LintKitCommand::class)
46-
->args([
47-
service('.ux_toolkit.registry.factory'),
48-
])
49-
->tag('console.command')
50-
5144
// Registry
5245

53-
->set('.ux_toolkit.registry.factory', RegistryFactory::class)
46+
->set('.ux_toolkit.registry.registry_factory', RegistryFactory::class)
5447
->args([
5548
service_locator([
5649
Type::Local->value => service('.ux_toolkit.registry.local'),

src/Toolkit/src/Command/DebugKitCommand.php

+13-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use Symfony\Component\Console\Input\InputInterface;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
22-
use Symfony\UX\Toolkit\Registry\RegistryFactory;
22+
use Symfony\Component\Filesystem\Path;
23+
use Symfony\UX\Toolkit\Kit\KitFactory;
2324

2425
/**
2526
* @author Jean-François Lépine
@@ -29,33 +30,28 @@
2930
*/
3031
#[AsCommand(
3132
name: 'ux:toolkit:debug-kit',
32-
description: 'Debug a kit, dump the dependencies.',
33+
description: 'Debug a local Kit.',
3334
hidden: true,
3435
)]
3536
class DebugKitCommand extends Command
3637
{
3738
public function __construct(
38-
private readonly RegistryFactory $registryFactory,
39+
private readonly KitFactory $kitFactory,
3940
) {
4041
parent::__construct();
4142
}
4243

4344
protected function configure(): void
4445
{
4546
$this
46-
->addArgument('kit', InputArgument::REQUIRED, 'The kit name, can be a local kit (e.g.: "shadcn") or a GitHub kit (e.g.: "https://github.com/user/repository@kit-name").')
47+
->addArgument('kit-path', InputArgument::OPTIONAL, 'The path to the kit to debug', '.')
4748
->setHelp(<<<'EOF'
48-
The kit name can be a local kit (e.g.: "shadcn") or a GitHub kit (e.g.: "https://github.com/user/repository@kit-name").
49-
50-
To debug a local kit:
51-
52-
<info>php %command.full_name% shadcn</info>
53-
54-
To debug a GitHub kit:
55-
56-
<info>php %command.full_name% https://github.com/user/repository@kit-name</info>
57-
<info>php %command.full_name% https://github.com/user/repository@[email protected]</info>
49+
To debug a Kit in the current directory:
50+
<info>php %command.full_name%</info>
5851
52+
Or in another directory:
53+
<info>php %command.full_name% ./kits/shadcn</info>
54+
<info>php %command.full_name% /path/to/my-kit</info>
5955
EOF
6056
);
6157
}
@@ -64,9 +60,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6460
{
6561
$io = new SymfonyStyle($input, $output);
6662

67-
$kitName = $input->getArgument('kit');
68-
$registry = $this->registryFactory->getForKit($kitName);
69-
$kit = $registry->getKit($kitName);
63+
$kitPath = $input->getArgument('kit-path');
64+
$kitPath = Path::makeAbsolute($kitPath, getcwd());
65+
$kit = $this->kitFactory->createKitFromAbsolutePath($kitPath);
7066

7167
$io->title(\sprintf('Kit "%s"', $kit->name));
7268

src/Toolkit/src/Command/InstallComponentCommand.php

+30-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
#[AsCommand(
3636
name: 'ux:toolkit:install-component',
37-
description: 'This command will install a new UX Component in your project',
37+
description: 'Install a new UX Component (e.g. Alert) in your project',
3838
)]
3939
class InstallComponentCommand extends Command
4040
{
@@ -52,7 +52,7 @@ public function __construct(
5252
protected function configure(): void
5353
{
5454
$this
55-
->addArgument('component', InputArgument::REQUIRED, 'The component name (Ex: Button)')
55+
->addArgument('component', InputArgument::OPTIONAL, 'The component name (Ex: Button)')
5656
->addOption(
5757
'destination',
5858
'd',
@@ -76,8 +76,8 @@ protected function configure(): void
7676
7777
To install a component from an external GitHub kit, use the <info>--kit</info> option:
7878
79-
<info>php %command.full_name% Button --kit=https://github.com/user/repository@kit</info>
80-
<info>php %command.full_name% Button --kit=https://github.com/user/repository@kit:branch</info>
79+
<info>php %command.full_name% Button --kit=https://github.com/user/my-kit</info>
80+
<info>php %command.full_name% Button --kit=https://github.com/user/my-kit:branch</info>
8181
EOF
8282
);
8383
}
@@ -95,8 +95,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9595
$registry = $this->registryFactory->getForKit($kitName);
9696
$kit = $registry->getKit($kitName);
9797

98-
// Get the component name from the argument, or suggest alternatives if it doesn't exist
99-
if (null === $component = $kit->getComponent($componentName = $input->getArgument('component'))) {
98+
if (null === $componentName = $input->getArgument('component')) {
99+
// Ask for the component name if not provided
100+
$componentName = $io->choice('Which component do you want to install?', array_map(fn (Component $component) => $component->name, $this->getAvailableComponents($kit)));
101+
$component = $kit->getComponent($componentName);
102+
} elseif (null === $component = $kit->getComponent($componentName)) {
103+
// Suggest alternatives if component does not exist
100104
$message = \sprintf('The component "%s" does not exist.', $componentName);
101105

102106
$alternativeComponents = $this->getAlternativeComponents($kit, $componentName);
@@ -120,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
120124
}
121125
}
122126

123-
$this->io->text(\sprintf('Installing component <info>%s</>...', $component->name));
127+
$io->writeln(\sprintf('Installing component <info>%s</> from the <info>%s</> kit...', $component->name, $kitName));
124128

125129
$installer = new Installer($this->filesystem, fn (string $question) => $this->io->confirm($question, $input->isInteractive()));
126130
$installationReport = $installer->installComponent($kit, $component, $destinationPath = $input->getOption('destination'), $input->getOption('force'));
@@ -144,7 +148,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
144148
}
145149

146150
/**
147-
* Get alternative components that are similar to the given component name.
151+
* @return list<Component>
152+
*/
153+
private function getAvailableComponents(Kit $kit): array
154+
{
155+
$availableComponents = [];
156+
157+
foreach ($kit->getComponents() as $component) {
158+
if (str_contains($component->name, ':')) {
159+
continue;
160+
}
161+
162+
$availableComponents[] = $component;
163+
}
164+
165+
return $availableComponents;
166+
}
167+
168+
/**
169+
* @return list<Component>
148170
*/
149171
private function getAlternativeComponents(Kit $kit, string $componentName): array
150172
{

src/Toolkit/src/Command/LintKitCommand.php

-73
This file was deleted.

src/Toolkit/src/Dependency/Version.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\UX\Toolkit\Dependency;
1313

1414
/**
15-
* Represents a version number, following a simplified version of the SemVer specification.
15+
* Represents a version number, following the SemVer specification.
1616
*
1717
* @internal
1818
*
@@ -21,26 +21,20 @@
2121
final class Version implements \Stringable
2222
{
2323
/**
24-
* @param int<0, max> $major
25-
* @param int<0, max> $minor
26-
* @param int<0, max> $patch
24+
* @param non-empty-string
2725
*/
2826
public function __construct(
29-
public readonly int $major,
30-
public readonly int $minor,
31-
public readonly int $patch,
27+
public readonly string $value,
3228
) {
3329
}
3430

3531
public function isHigherThan(self $version): bool
3632
{
37-
return $this->major > $version->major
38-
|| ($this->major === $version->major && $this->minor > $version->minor)
39-
|| ($this->major === $version->major && $this->minor === $version->minor && $this->patch > $version->patch);
33+
return version_compare($this->value, $version->value, '>');
4034
}
4135

4236
public function __toString(): string
4337
{
44-
return \sprintf('%d.%d.%d', $this->major, $this->minor, $this->patch);
38+
return $this->value;
4539
}
4640
}

src/Toolkit/src/Kit/KitSynchronizer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function resolveComponentDependencies(Kit $kit, Component $component): v
106106
if (FileType::Twig === $file->type) {
107107
if (str_contains($fileContent, 'html_cva')) {
108108
$component->addDependency(new PhpPackageDependency('twig/extra-bundle'));
109-
$component->addDependency(new PhpPackageDependency('twig/html-extra', new Version(3, 12, 0)));
109+
$component->addDependency(new PhpPackageDependency('twig/html-extra', new Version('3.12.0')));
110110
}
111111

112112
if (str_contains($fileContent, 'tailwind_merge')) {

0 commit comments

Comments
 (0)