-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInstallComponentCommand.php
More file actions
104 lines (83 loc) · 3.18 KB
/
InstallComponentCommand.php
File metadata and controls
104 lines (83 loc) · 3.18 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
<?php
namespace Sheaf\Cli\Commands;
use Sheaf\Cli\Services\ComponentInstaller;
use Sheaf\Cli\Support\InstallationConfig;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use function Laravel\Prompts\text;
class InstallComponentCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sheaf:install
{name?* : the name of the component.}
{--force : override the component file if it exist.}
{--skip-deps : Skip Dependency Installation.}
{--only-deps : Install Only Dependency.}
{--internal-deps : installing required internal Dependencies.}
{--external-deps : installing required external Dependencies.}
{--dry-run : Preview what will be installed}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Installing a sheaf Component';
/**
* Execute the console command.
*/
public function handle()
{
$componentNames = $this->getComponentName();
$installationConfig = new InstallationConfig(
force: $this->option("force"),
skipDeps: $this->option("skip-deps"),
onlyDeps: $this->option("only-deps"),
internalDeps: $this->option("internal-deps"),
externalDeps: $this->option("external-deps"),
isDryRun: $this->option("dry-run")
);
foreach ($componentNames as $name) {
$title = $this->getBannerTitle($name);
$this->banner($title);
$installationConfig->setComponentName($name);
$result = (new ComponentInstaller($this, $this->components, $installationConfig))->install($name);
if ($result === Command::SUCCESS) {
$this->info(" ✓ Updated sheaf.json and sheaf-lock.json");
$this->components->info("Full documentation: https://sheafui.dev/docs/components/{$name}");
}
}
}
private function getComponentName()
{
$componentName = $this->argument('name');
if (!$componentName) {
$componentName = text(label: 'What are the component(s) you would like to install?', placeholder: 'button', required: true);
}
return Arr::wrap($componentName);
}
public function banner(string $title): void
{
$length = strlen(" {$title}") + 4;
$this->newLine();
$this->line(str_repeat("═", $length));
$this->line(" {$title}");
$this->line(str_repeat("═", $length));
$this->newLine();
}
public function getBannerTitle(string $title)
{
$formattedTitle = Str::of($title)->headline();
return match (true) {
$this->option('only-deps') => "Installing {$formattedTitle} Dependencies Only",
$this->option('dry-run') => "Preview: Installing {$formattedTitle} (Dry Run)",
$this->option('skip-deps') => "Installing {$formattedTitle} Files Only",
$this->option("force") => "Installing {$formattedTitle} (Force Mode)",
default => "Installing {$formattedTitle}"
};
}
}