Skip to content

Commit a624ba6

Browse files
Merge pull request #11 from HichemTab-tech/add-remove-template-command
Add command for removing saved items and update documentation
2 parents 9338245 + 13d080d commit a624ba6

File tree

5 files changed

+154
-10
lines changed

5 files changed

+154
-10
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LaravelFS functions similarly to the Laravel Installer but with **extra capabili
1515
✅ Support for **Breeze and Jetstream**, even if they are abandoned.
1616
✅ Install **custom starter kits** from Packagist.
1717
✅ Save and reuse project setups with **Templates**.
18+
✅ Easily **remove saved templates** when no longer needed.
1819
✅ Ensure that provided starter kits are **Composer packages of type `project`**.
1920
✅ CLI command to fetch additional details about a starter kit package.
2021

@@ -93,6 +94,27 @@ This runs the exact same command as if you typed everything manually!
9394

9495
---
9596

97+
## **🗑️ Removing Templates**
98+
Need to clean up your templates? You can easily remove them.
99+
100+
### **Remove a Specific Template**
101+
To delete a single template:
102+
103+
```sh
104+
laravelfs template:remove my-template
105+
```
106+
107+
### **Remove All Templates**
108+
To remove **all saved templates** at once:
109+
110+
```sh
111+
laravelfs template:remove --all
112+
```
113+
114+
> ⚠️ **This action is irreversible!** Make sure you want to delete all templates before running this command.
115+
116+
---
117+
96118
## **Installing Custom Starter Kits**
97119
LaravelFS allows you to install **custom Laravel starter kits** from Packagist by providing the package name:
98120

bin/laravelfs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
99
}
1010

1111
// Define our own version and the Laravel Installer version we aim to match.
12-
$laravelFSVersion = '1.1.0';
12+
$laravelFSVersion = '1.2.0';
1313
$laravelInstallerVersion = '5.13.0';
1414

1515
// Compose the displayed version string.
@@ -20,6 +20,7 @@ $app->add(new HichemTabTech\LaravelFS\Console\NewCommand);
2020
$app->add(new HichemTabTech\LaravelFS\Console\NewTemplateCommand);
2121
$app->add(new HichemTabTech\LaravelFS\Console\ShowTemplatesCommand);
2222
$app->add(new HichemTabTech\LaravelFS\Console\UseTemplateCommand);
23+
$app->add(new HichemTabTech\LaravelFS\Console\RemoveTemplateCommand);
2324

2425
/** @noinspection PhpUnhandledExceptionInspection */
2526
$app->run();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace HichemTabTech\LaravelFS\Console\Concerns;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use function Laravel\Prompts\text;
7+
8+
trait CommonTemplateUtils
9+
{
10+
protected function ensureTemplateNameArgument(InputInterface $input): void
11+
{
12+
$templatesData = $this->getSavedTemplates(true);
13+
$templates = $templatesData['templates'];
14+
$input->setArgument('template-name', text(
15+
label: 'What is the name this template',
16+
placeholder: count($templates) == 0 ? 'E.g. template1, or-any-name-u-want' : ('E.g. '.implode(', ', array_slice(array_keys($templates), 0, 3)).(count($templates) > 3 ? ', ...' : '')),
17+
required: 'The template name is required.',
18+
hint: 'This name is the key of the template you are searching for.',
19+
));
20+
}
21+
}

src/RemoveTemplateCommand.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace HichemTabTech\LaravelFS\Console;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
use function Laravel\Prompts\confirm;
12+
use function Laravel\Prompts\error;
13+
use function Laravel\Prompts\intro;
14+
15+
class RemoveTemplateCommand extends Command
16+
{
17+
use Concerns\CommandsUtils, Concerns\ConfiguresPrompts, Concerns\CommonTemplateUtils;
18+
19+
protected function configure(): void
20+
{
21+
$this->setName('template:remove')
22+
->setDescription('remove a saved template')
23+
->setHelp('This command removes a saved template that you no longer need.')
24+
->addOption('all', 'a', InputOption::VALUE_NONE, 'Remove all saved templates')
25+
->addArgument('template-name', InputArgument::REQUIRED, 'The name of the template to remove');
26+
}
27+
28+
protected function interact(InputInterface $input, OutputInterface $output): void
29+
{
30+
parent::interact($input, $output);
31+
32+
$this->configurePrompts($input, $output);
33+
34+
$output->write(PHP_EOL . ' <fg=blue> _ _
35+
| | | |
36+
| | __ _ _ __ __ ___ _____| |
37+
| | / _` | __/ _` \ \ / / _ \ |
38+
| |___| (_| | | | (_| |\ V / __/ |
39+
|______\__,_|_| \__,_| \_/ \___|_|</>' . PHP_EOL . PHP_EOL);
40+
41+
if (!$input->getArgument('template-name')) {
42+
if (!$input->getOption('all')) {
43+
$this->ensureTemplateNameArgument($input);
44+
} else {
45+
$input->setArgument('template-name', confirm(
46+
label: 'Are you sure you want to remove all saved templates?',
47+
default: false,
48+
hint: 'This action is irreversible.',
49+
) ? '/all/' : null);
50+
}
51+
}
52+
}
53+
54+
55+
protected function execute(InputInterface $input, OutputInterface $output): int
56+
{
57+
58+
$templatesData = $this->getSavedTemplates();
59+
$templates = $templatesData['templates'];
60+
if (empty($templates)) {
61+
$this->info('No saved templates found.');
62+
return Command::SUCCESS;
63+
}
64+
65+
if ($input->getArgument('template-name') == '/all/') {
66+
$templateToRemove = null;
67+
} elseif (!$input->getArgument('template-name')) {
68+
intro('Operation cancelled.');
69+
return Command::SUCCESS;
70+
} else {
71+
$templateName = $input->getArgument('template-name');
72+
73+
if (!isset($templates[$templateName])) {
74+
error("Template '$templateName' not found.");
75+
return Command::INVALID;
76+
}
77+
78+
$templateToRemove = $templateName;
79+
}
80+
intro("Removing a saved template...");
81+
82+
if ($templateToRemove) {
83+
$done = $this->removeTemplates($templatesData['path'], $templatesData, $templateToRemove);
84+
} else {
85+
$done = $this->removeTemplates($templatesData['path'], $templatesData);
86+
}
87+
88+
if ($done) {
89+
$this->info($templateToRemove ? "Template '$templateToRemove' removed successfully." : 'All saved templates removed successfully.');
90+
return Command::SUCCESS;
91+
}
92+
93+
error($templateToRemove ? "Failed to remove template '$templateToRemove'." : 'Failed to remove all saved templates.');
94+
return Command::FAILURE;
95+
}
96+
97+
private function removeTemplates(string $path, array $templatesData, string|null $templateToRemove = null): bool
98+
{
99+
if (!$templateToRemove) {
100+
$templatesConfig = ['templates' => []];
101+
} else {
102+
unset($templatesData['templates'][$templateToRemove]);
103+
$templatesConfig = ['templates' => $templatesData['templates']];
104+
}
105+
return file_put_contents($path, json_encode($templatesConfig, JSON_PRETTY_PRINT)) !== false;
106+
}
107+
}

src/UseTemplateCommand.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class UseTemplateCommand extends Command
1616
{
17-
use Concerns\CommandsUtils, Concerns\ConfiguresPrompts;
17+
use Concerns\CommandsUtils, Concerns\ConfiguresPrompts, Concerns\CommonTemplateUtils;
1818
protected function configure(): void
1919
{
2020
$this->setName('use')
@@ -39,14 +39,7 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
3939
|______\__,_|_| \__,_| \_/ \___|_|</>'.PHP_EOL.PHP_EOL);
4040

4141
if (!$input->getArgument('template-name')) {
42-
$templatesData = $this->getSavedTemplates(true);
43-
$templates = $templatesData['templates'];
44-
$input->setArgument('template-name', text(
45-
label: 'What is the name this template',
46-
placeholder: count($templates) == 0 ? 'E.g. template1, or-any-name-u-want' : ('E.g. '.implode(', ', array_slice(array_keys($templates), 0, 3)).(count($templates) > 3 ? ', ...' : '')),
47-
required: 'The template name is required.',
48-
hint: 'This name is the key of the template you are searching for.',
49-
));
42+
$this->ensureTemplateNameArgument($input);
5043
}
5144

5245
if (!$input->getArgument('project-name')) {

0 commit comments

Comments
 (0)