Skip to content

Commit 1d53778

Browse files
Add template management commands to LaravelFS
Introduced commands for creating, using, and displaying saved templates. Enhanced flexibility for project scaffolding with customizable starter templates. Updated the tool version to 2.0.0 to reflect these significant new features.
1 parent 838e007 commit 1d53778

File tree

9 files changed

+592
-141
lines changed

9 files changed

+592
-141
lines changed

bin/laravelfs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ 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.0.0';
12+
$laravelFSVersion = '2.0.0';
1313
$laravelInstallerVersion = '5.13.0';
1414

1515
// Compose the displayed version string.
1616
$displayVersion = sprintf('%s (advanced from Laravel Installer %s)', $laravelFSVersion, $laravelInstallerVersion);
1717

1818
$app = new Symfony\Component\Console\Application('LaravelFS Installer', $displayVersion);
1919
$app->add(new HichemTabTech\LaravelFS\Console\NewCommand);
20+
$app->add(new HichemTabTech\LaravelFS\Console\NewTemplateCommand);
21+
$app->add(new HichemTabTech\LaravelFS\Console\ShowTemplatesCommand);
22+
$app->add(new HichemTabTech\LaravelFS\Console\UseTemplateCommand);
2023

2124
/** @noinspection PhpUnhandledExceptionInspection */
2225
$app->run();

src/Concerns/CommandsUtils.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace HichemTabTech\LaravelFS\Console\Concerns;
4+
5+
use Illuminate\Support\ProcessUtils;
6+
use Illuminate\Support\Str;
7+
use RuntimeException;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Process\PhpExecutableFinder;
11+
use Symfony\Component\Process\Process;
12+
use function Illuminate\Support\php_binary;
13+
use function Laravel\Prompts\error;
14+
15+
trait CommandsUtils
16+
{
17+
18+
private function getGlobalTemplatesPath(): string
19+
{
20+
// Determine OS-specific config directory
21+
if (windows_os()) {
22+
$configDir = (getenv('APPDATA') ?: $_SERVER['APPDATA']) . '\laravelfs';
23+
} else {
24+
$configDir = (getenv('XDG_CONFIG_HOME') ?: $_SERVER['HOME']) . '\.config\laravelfs';
25+
}
26+
27+
return $configDir . '\templates.json';
28+
}
29+
30+
private function getSavedTemplates(bool $noInteract = false): array
31+
{
32+
// Get the global templates path
33+
$configPath = $this->getGlobalTemplatesPath();
34+
$noTemplates = false;
35+
36+
// Check if the template file exists
37+
if (!file_exists($configPath)) {
38+
$noTemplates = true;
39+
}
40+
41+
if (!$noTemplates) {
42+
// Load the templates
43+
$templatesConfig = json_decode(file_get_contents($configPath), true);
44+
$templates = [];
45+
46+
if (empty($templatesConfig)) {
47+
$noTemplates = true;
48+
}
49+
elseif (empty($templatesConfig['templates'])) {
50+
$noTemplates = true;
51+
}
52+
else {
53+
$templates = $templatesConfig['templates'];
54+
}
55+
56+
}
57+
58+
if ($noTemplates) {
59+
if (!$noInteract) {
60+
error('No templates found. Create one using `laravelfs template:new <template-name>`');
61+
}
62+
return [
63+
'templates' => [],
64+
'path' => $configPath,
65+
];
66+
}
67+
68+
return [
69+
'templates' => $templates,
70+
'path' => $configPath,
71+
];
72+
}
73+
74+
/**
75+
* Get the path to the appropriate PHP binary.
76+
*
77+
* @return string
78+
*/
79+
protected function phpBinary(): string
80+
{
81+
$phpBinary = function_exists('Illuminate\Support\php_binary')
82+
? php_binary()
83+
: (new PhpExecutableFinder)->find(false);
84+
85+
return $phpBinary !== false
86+
? ProcessUtils::escapeArgument($phpBinary)
87+
: 'php';
88+
}
89+
90+
/**
91+
* Run the given commands.
92+
*
93+
* @param array $commands
94+
* @param InputInterface $input
95+
* @param OutputInterface $output
96+
* @param string|null $workingPath
97+
* @param array $env
98+
* @return Process
99+
*/
100+
protected function runCommands(array $commands, InputInterface $input, OutputInterface $output, ?string $workingPath = null, array $env = []): Process
101+
{
102+
if (!$output->isDecorated()) {
103+
$commands = array_map(function ($value) {
104+
if (Str::startsWith($value, ['chmod', 'git', $this->phpBinary().' ./vendor/bin/pest'])) {
105+
return $value;
106+
}
107+
108+
return $value.' --no-ansi';
109+
}, $commands);
110+
}
111+
112+
if ($input->getOption('quiet')) {
113+
$commands = array_map(function ($value) {
114+
if (Str::startsWith($value, ['chmod', 'git', $this->phpBinary().' ./vendor/bin/pest'])) {
115+
return $value;
116+
}
117+
118+
return $value.' --quiet';
119+
}, $commands);
120+
}
121+
122+
$process = Process::fromShellCommandline(implode(' && ', $commands), $workingPath, $env, null, null);
123+
124+
if ('\\' !== DIRECTORY_SEPARATOR AND file_exists('/dev/tty') AND is_readable('/dev/tty')) {
125+
try {
126+
$process->setTty(true);
127+
} catch (RuntimeException $e) {
128+
$output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
129+
}
130+
}
131+
132+
$process->run(function ($type, $line) use ($output) {
133+
$output->write(' '.$line);
134+
});
135+
136+
return $process;
137+
}
138+
139+
/**
140+
* Get the installation directory.
141+
*
142+
* @param string $name
143+
* @return string
144+
*/
145+
protected function getInstallationDirectory(string $name): string
146+
{
147+
return $name !== '.' ? getcwd().'/'.$name : '.';
148+
}
149+
150+
/**
151+
* Verify that the application does not already exist.
152+
*
153+
* @param string $directory
154+
* @return void
155+
*/
156+
protected function verifyApplicationDoesntExist(string $directory): void
157+
{
158+
if ((is_dir($directory) || is_file($directory)) AND $directory != getcwd()) {
159+
throw new RuntimeException('Application already exists!');
160+
}
161+
}
162+
}

src/Concerns/ConfiguresPrompts.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace HichemTabTech\LaravelFS\Console\Concerns;
44

5+
use Closure;
56
use Laravel\Prompts\ConfirmPrompt;
67
use Laravel\Prompts\MultiSelectPrompt;
78
use Laravel\Prompts\PasswordPrompt;
@@ -19,11 +20,11 @@ trait ConfiguresPrompts
1920
/**
2021
* Configure the prompt fallbacks.
2122
*
22-
* @param \Symfony\Component\Console\Input\InputInterface $input
23-
* @param \Symfony\Component\Console\Output\OutputInterface $output
23+
* @param InputInterface $input
24+
* @param OutputInterface $output
2425
* @return void
2526
*/
26-
protected function configurePrompts(InputInterface $input, OutputInterface $output)
27+
protected function configurePrompts(InputInterface $input, OutputInterface $output): void
2728
{
2829
Prompt::fallbackWhen(! $input->isInteractive() || PHP_OS_FAMILY === 'Windows');
2930

@@ -99,18 +100,18 @@ function () use ($prompt, $input, $output) {
99100
/**
100101
* Prompt the user until the given validation callback passes.
101102
*
102-
* @param \Closure $prompt
103-
* @param bool|string $required
104-
* @param \Closure|null $validate
105-
* @param \Symfony\Component\Console\Output\OutputInterface $output
103+
* @param Closure $prompt
104+
* @param bool|string $required
105+
* @param Closure|null $validate
106+
* @param OutputInterface $output
106107
* @return mixed
107108
*/
108-
protected function promptUntilValid($prompt, $required, $validate, $output)
109+
protected function promptUntilValid(Closure $prompt, bool|string $required, ?Closure $validate, OutputInterface $output): mixed
109110
{
110111
while (true) {
111112
$result = $prompt();
112113

113-
if ($required && ($result === '' || $result === [] || $result === false)) {
114+
if ($required AND ($result === '' || $result === [] || $result === false)) {
114115
$output->writeln('<error>'.(is_string($required) ? $required : 'Required.').'</error>');
115116

116117
continue;
@@ -119,8 +120,8 @@ protected function promptUntilValid($prompt, $required, $validate, $output)
119120
if ($validate) {
120121
$error = $validate($result);
121122

122-
if (is_string($error) && strlen($error) > 0) {
123-
$output->writeln("<error>{$error}</error>");
123+
if (is_string($error) AND strlen($error) > 0) {
124+
$output->writeln("<error>$error</error>");
124125

125126
continue;
126127
}

src/Concerns/InteractsWithHerdOrValet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function isParkedOnHerdOrValet(string $directory): bool
1919

2020
$decodedOutput = json_decode($output);
2121

22-
return is_array($decodedOutput) && in_array(dirname($directory), $decodedOutput);
22+
return is_array($decodedOutput) AND in_array(dirname($directory), $decodedOutput);
2323
}
2424

2525
/**

0 commit comments

Comments
 (0)