Skip to content

Commit ea2b484

Browse files
authored
Corregir: Instalador de dependencias (#5)
Se corrigieron los errores en la clase que impedían la instalación de las dependencias.
1 parent 09a9a0c commit ea2b484

File tree

7 files changed

+265
-33
lines changed

7 files changed

+265
-33
lines changed

app/Commands/ProjectStart.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,19 @@ public function handle()
5858
$dependencies = $data->options;
5959
$directory = $data->projectName;
6060

61-
$this->task('Installing aditional libraries', function () use($dependencies, $directory) : bool {
61+
$this->task('Installing aditional libraries', function () use($dependencies, $directory, &$result) : bool {
6262
$this->dependencies->choose($dependencies);
63-
$this->dependencies->onDirectory($directory)->install();
64-
return false;
63+
$result = $this->dependencies->onDirectory($directory)->install();
64+
return $result->isSuccess();
6565
});
6666

67+
if($result->isFailure()){
68+
error('An error has occurred');
69+
error('Here is more information about the failure:');
70+
error($result->getContent());
6771

72+
return self::FAILURE;
73+
}
6874
}
6975

7076
/**
@@ -74,4 +80,4 @@ public function schedule(Schedule $schedule): void
7480
{
7581
// $schedule->command(static::class)->everyMinute();
7682
}
77-
}
83+
}

app/Core/Handlers/DependenciesContainer.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,42 @@
22

33
namespace App\Core\Handlers;
44

5-
class DependenciesContainer {
5+
class DependenciesContainer
6+
{
67
public function __construct(
78
private readonly array $pendingDependencies,
89
private array $selectedDependencies = []
9-
){}
10+
) {}
1011

11-
public function __invoke(){
12+
public function __invoke()
13+
{
1214

1315

1416
$this->handle($this->pendingDependencies);
1517

1618
return $this->selectedDependencies;
1719
}
1820

19-
private function handle($dependencies){
20-
if(array_key_exists('type', $dependencies)){
21-
if($dependencies['type'] === 'select'){
22-
$input = new Input();
23-
$result = $input->select($dependencies['label'], $dependencies['options']);
24-
$this->createNewContainer($result->option);
25-
}
26-
return;
21+
private function handle($dependencies)
22+
{
23+
if (array_key_exists('commands', $this->pendingDependencies)) {
24+
$this->addDependencies($this->pendingDependencies['commands']);
2725
}
2826

29-
30-
if(array_key_exists('commands', $this->pendingDependencies)){
31-
$this->addDependencies($this->pendingDependencies['commands']);
32-
return;
27+
if (array_key_exists('type', $dependencies)) {
28+
$this->handleInput($dependencies);
3329
}
3430

35-
foreach($this->pendingDependencies as $container){
31+
32+
foreach ($this->pendingDependencies as $key => $container) {
33+
if(!is_int($key)) continue;
3634
$this->createNewContainer($container);
3735
}
38-
3936
}
4037

41-
private function createNewContainer(array $dependencies): void
38+
private function createNewContainer(array | null $dependencies): void
4239
{
40+
if(empty($dependencies)) return;
4341
$dependenciesContainer = new DependenciesContainer($dependencies);
4442

4543
$selectedDependencies = $dependenciesContainer();
@@ -53,4 +51,23 @@ private function addDependencies(array $dependencies): void
5351
$dependencies
5452
);
5553
}
54+
55+
private function handleInput(array $dependencies)
56+
{
57+
$input = new Input();
58+
$type = $dependencies['type'];
59+
60+
if($type === 'choice' || $type === 'select') {
61+
$result = $input->$type($dependencies['label'], $dependencies['options']);
62+
$this->createNewContainer($result->option);
63+
return;
64+
}
65+
66+
$isConfirmed = $input->confirm($dependencies['label']);
67+
if($isConfirmed){
68+
$this->createNewContainer($dependencies['options']['yes']);
69+
} else {
70+
$this->createNewContainer($dependencies['options']['no']);
71+
}
72+
}
5673
}

app/Core/Handlers/Input.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace App\Core\Handlers;
44

5+
use Illuminate\Support\Arr;
6+
57
use function Laravel\Prompts\confirm;
68
use function Laravel\Prompts\select;
79
use function Laravel\Prompts\multiselect;
10+
use function Laravel\Prompts\progress;
811

912
class Input {
1013

@@ -21,11 +24,21 @@ public function select(string $label, array $options){
2124
public function choice(string $label, array $options)
2225
{
2326
$keys = array_keys($options);
24-
multiselect(label:$label, options: $keys, required : false );
27+
$result = multiselect(label:$label, options: $keys, required : false );
28+
29+
$selectedOptions = Arr::where(
30+
$options,
31+
fn ($value, $key): bool => in_array($key, $result)
32+
);
33+
34+
return (object) [
35+
'key' => $result,
36+
'option' => $selectedOptions
37+
];
2538
}
2639

27-
public function confirm(string $label){
40+
public function confirm(string $label): bool{
2841
$result = confirm(label: $label, required : true);
2942
return $result;
3043
}
31-
}
44+
}

app/Core/Handlers/Process.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Core\Handlers;
4+
5+
use Illuminate\Process\Exceptions\ProcessTimedOutException;
6+
use Illuminate\Process\PendingProcess;
7+
use Illuminate\Support\Facades\Process as ProcessFacade;
8+
use RuntimeException;
9+
10+
class Process {
11+
private string | null $directory = null;
12+
13+
public function setPath(string $path): self
14+
{
15+
$this->directory = $path;
16+
return $this;
17+
}
18+
19+
public function execute(string $command): Result
20+
{
21+
try {
22+
$this->process()->run($command)->throw();
23+
} catch (ProcessTimedOutException | RuntimeException $error) {
24+
return Result::Failure( $error->getMessage() );
25+
}
26+
27+
return Result::Success();
28+
}
29+
30+
private function process(): PendingProcess
31+
{
32+
$process = ProcessFacade::timeout(3000);
33+
34+
if( empty( $this->directory ) === false ) {
35+
$process = $process->path( $this->directory );
36+
}
37+
38+
return $process;
39+
}
40+
}

app/Core/Services/DependencyInstaller.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,47 @@
33
namespace App\Core\Services;
44

55
use App\Core\Handlers\DependenciesContainer;
6+
use App\Core\Handlers\Process;
7+
use App\Core\Handlers\Result;
8+
use function Laravel\Prompts\progress;
9+
610

711
class DependencyInstaller {
812
private array $dependencies;
913
private string | null $directory = null;
10-
public function __construc(
11-
14+
public function __construct(
15+
private Process $process
1216
){}
17+
1318
public function choose(array $dependencies){
1419
$container = new DependenciesContainer($dependencies);
1520
$this->dependencies = $container();
1621
}
17-
public function install(): void{
18-
print_r($this->dependencies);
22+
public function install(): Result
23+
{
24+
$progress = progress(
25+
label: 'Installing Dependencies',
26+
steps: count( $this->dependencies )
27+
);
28+
29+
foreach($this->dependencies as $dependency){
30+
$result = $this->process->execute($dependency);
31+
if($result->isFailure()){
32+
$progress->finish();
33+
return $result;
34+
}
35+
$progress->advance();
36+
}
37+
$progress->finish();
38+
return Result::Success();
1939
}
2040

2141
public function onDirectory(string $directory): self
2242
{
23-
$this->directory = $directory;
43+
$this->process->setPath($directory);
2444
return $this;
2545
}
2646

2747

2848

2949
}
30-

config/laravel_10.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
return [
4+
// scaffolding
5+
[
6+
'type' => 'select',
7+
'label' => 'Select your scaffolding',
8+
'options' => [
9+
'API' => null,
10+
'Monolit' => [
11+
'type' => 'select',
12+
'label' => 'Select your starter kit',
13+
'options' => [
14+
'Breeze' => [
15+
'commands' => [
16+
'composer require laravel/breeze --dev',
17+
'php artisan breeze:install',
18+
'php artisan migrate',
19+
'npm install',
20+
],
21+
],
22+
'Jetstream' => [
23+
'commands' => [
24+
'composer require laravel/jetstream'
25+
],
26+
'type' => 'select',
27+
'label' => 'What frontend framework do you prefer?',
28+
'options' => [
29+
'Livewire' => [
30+
'type' => 'select',
31+
'label' => 'What version do you prefer?',
32+
'options' => [
33+
'Alone' => [
34+
'commands' => ['php artisan jetstream:install livewire']
35+
],
36+
'Teams' => [
37+
'commands' => ['php artisan jetstream:install livewire --teams']
38+
]
39+
]
40+
],
41+
'Inertia' => [
42+
'type' => 'select',
43+
'label' => 'What version do you prefer?',
44+
'options' => [
45+
'Alone' => [
46+
'commands' => ['php artisan jetstream:install inertia']
47+
],
48+
'Teams' => [
49+
'commands' => ['php artisan jetstream:install inertia --teams']
50+
],
51+
'SSR' => [
52+
'commands' => ['php artisan jetstream:install inertia --ssr']
53+
],
54+
]
55+
],
56+
]
57+
],
58+
'Filament' => [
59+
'commands' => [
60+
'composer require livewire/livewire',
61+
'composer require filament/filament:"^3.2" -W',
62+
'php artisan filament:install --panels',
63+
],
64+
],
65+
'none' => null
66+
]
67+
]
68+
]
69+
],
70+
// Libraries
71+
[
72+
[
73+
'type' => 'choice',
74+
'label' => 'Which of these libraries would you like to install?',
75+
'options' => [
76+
'spatie permission' => [
77+
'commands' => [
78+
'composer require spatie/laravel-permission',
79+
'php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"'
80+
]
81+
],
82+
'spatie laravel-pdf' => [
83+
'commands' => [
84+
'composer require spatie/laravel-pdf'
85+
]
86+
],
87+
'telescope' => [
88+
'commands' => [
89+
'composer require laravel/telescope --dev',
90+
'php artisan telescope:install',
91+
'php artisan migrate'
92+
]
93+
],
94+
'laravel excel' => [
95+
'commands' => [
96+
'composer require "maatwebsite/excel:^3.1"',
97+
'php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config'
98+
]
99+
]
100+
]
101+
]
102+
],
103+
// Testing
104+
[
105+
'type' => 'confirm',
106+
'label' => 'Would you like to use PEST as a testing framework for your application?',
107+
'options' => [
108+
'yes' => [
109+
'commands' => [
110+
'composer remove --dev phpunit/phpunit',
111+
'composer require pestphp/pest --dev --with-all-dependencies',
112+
'./vendor/bin/pest --init'
113+
]
114+
],
115+
'no' => null,
116+
]
117+
]
118+
];

0 commit comments

Comments
 (0)