Skip to content

Commit 824bcc8

Browse files
Merge pull request #3 from ArchMaster2024/dev
Fusionar dev con main
2 parents f049ba2 + 23a8685 commit 824bcc8

18 files changed

+581
-13
lines changed

.php-cs-fixer.cache

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

app/Commands/ProjectStart.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use App\Core\Services\DependencyInstaller;
6+
use App\Core\Services\LaravelInstaller;
7+
use Illuminate\Console\Scheduling\Schedule;
8+
use LaravelZero\Framework\Commands\Command;
9+
10+
use function Laravel\Prompts\error;
11+
12+
class ProjectStart extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'create-proyect';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Start a new Laravel Project';
27+
28+
public function __construct(
29+
private laravelInstaller $laravelInstaller,
30+
private DependencyInstaller $dependencies
31+
){
32+
parent::__construct();
33+
}
34+
35+
36+
/**
37+
* Execute the console command.
38+
*/
39+
public function handle()
40+
{
41+
info('Welcome to laravel starter');
42+
43+
$this->task('Instaling Laravel', function () use(&$result): bool {
44+
$result = $this->laravelInstaller->install();
45+
return $result->isSuccess();
46+
});
47+
48+
if($result->isFailure()){
49+
error('An error has occurred');
50+
error('Here is more information about the failure:');
51+
error($result->getContent());
52+
53+
return self::FAILURE;
54+
}
55+
56+
$data = $result->getContent();
57+
58+
$dependencies = $data->options;
59+
$directory = $data->projectName;
60+
61+
$this->task('Installing aditional libraries', function () use($dependencies, $directory) : bool {
62+
$this->dependencies->choose($dependencies);
63+
$this->dependencies->onDirectory($directory)->install();
64+
return false;
65+
});
66+
67+
68+
}
69+
70+
/**
71+
* Define the command's schedule.
72+
*/
73+
public function schedule(Schedule $schedule): void
74+
{
75+
// $schedule->command(static::class)->everyMinute();
76+
}
77+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Core\Handlers;
4+
5+
class DependenciesContainer {
6+
public function __construct(
7+
private readonly array $pendingDependencies,
8+
private array $selectedDependencies = []
9+
){}
10+
11+
public function __invoke(){
12+
13+
14+
$this->handle($this->pendingDependencies);
15+
16+
return $this->selectedDependencies;
17+
}
18+
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;
27+
}
28+
29+
30+
if(array_key_exists('commands', $this->pendingDependencies)){
31+
$this->addDependencies($this->pendingDependencies['commands']);
32+
return;
33+
}
34+
35+
foreach($this->pendingDependencies as $container){
36+
$this->createNewContainer($container);
37+
}
38+
39+
}
40+
41+
private function createNewContainer(array $dependencies): void
42+
{
43+
$dependenciesContainer = new DependenciesContainer($dependencies);
44+
45+
$selectedDependencies = $dependenciesContainer();
46+
$this->addDependencies($selectedDependencies);
47+
}
48+
49+
private function addDependencies(array $dependencies): void
50+
{
51+
$this->selectedDependencies = array_merge(
52+
$this->selectedDependencies,
53+
$dependencies
54+
);
55+
}
56+
}

app/Core/Handlers/Input.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Core\Handlers;
4+
5+
use function Laravel\Prompts\confirm;
6+
use function Laravel\Prompts\select;
7+
use function Laravel\Prompts\multiselect;
8+
9+
class Input {
10+
11+
public function select(string $label, array $options){
12+
$keys = array_keys($options);
13+
$result = select(label:$label, options: $keys, required: true);
14+
15+
return (object) [
16+
'key' => $result,
17+
'option' => $options[$result]
18+
];
19+
}
20+
21+
public function choice(string $label, array $options)
22+
{
23+
$keys = array_keys($options);
24+
multiselect(label:$label, options: $keys, required : false );
25+
}
26+
27+
public function confirm(string $label){
28+
$result = confirm(label: $label, required : true);
29+
return $result;
30+
}
31+
}

app/Core/Handlers/Result.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Core\Handlers;
4+
5+
use Exception;
6+
7+
readonly class Result {
8+
public function __construct(
9+
private bool $_isSuccess,
10+
private mixed $content = null
11+
){}
12+
13+
public function isSuccess() : bool
14+
{
15+
return $this->_isSuccess === true;
16+
}
17+
18+
public function isFailure() : bool
19+
{
20+
return $this->_isSuccess === false;
21+
}
22+
23+
public function isException() : bool
24+
{
25+
return $this->content instanceof Exception;
26+
}
27+
28+
public function getContent() : mixed
29+
{
30+
return $this->content;
31+
}
32+
33+
public static function Success(mixed $content = null) : Result
34+
{
35+
return new Result(true, $content);
36+
}
37+
38+
public static function Failure(mixed $content = null) : Result
39+
{
40+
return new Result(false, $content);
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Core\Services;
4+
5+
use App\Core\Handlers\DependenciesContainer;
6+
7+
class DependencyInstaller {
8+
private array $dependencies;
9+
private string | null $directory = null;
10+
public function __construc(
11+
12+
){}
13+
public function choose(array $dependencies){
14+
$container = new DependenciesContainer($dependencies);
15+
$this->dependencies = $container();
16+
}
17+
public function install(): void{
18+
print_r($this->dependencies);
19+
}
20+
21+
public function onDirectory(string $directory): self
22+
{
23+
$this->directory = $directory;
24+
return $this;
25+
}
26+
27+
28+
29+
}
30+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Core\Services;
4+
5+
use App\Core\Handlers\Result;
6+
use Illuminate\Contracts\Process\ProcessResult;
7+
use Illuminate\Process\Exceptions\ProcessTimedOutException;
8+
9+
use function Laravel\Prompts\select;
10+
use function Laravel\Prompts\info;
11+
use function Laravel\Prompts\text;
12+
use function Laravel\Prompts\spin;
13+
14+
use Illuminate\Support\Facades\Process;
15+
use RuntimeException;
16+
17+
class LaravelInstaller {
18+
public function install() : Result
19+
{
20+
21+
$version = select(label: 'Laravel version', options: [10, 11], required: true);
22+
$options = config("laravel_$version");
23+
24+
if(empty($options)) {
25+
return Result::Failure('Version not configure');
26+
}
27+
28+
$projectName = text(label : 'Project namme', placeholder: 'awesome_project', required : true);
29+
30+
$data = (object) [
31+
'projectName' => $projectName,
32+
'options' => $options,
33+
'version' => $version
34+
];
35+
36+
$result = spin(function() use ($data): Result{
37+
try {
38+
Process::timeout(3000)->run("composer create-project laravel/laravel:^$data->version $data->projectName")->throw();
39+
} catch(ProcessTimedOutException | RuntimeException $error ){
40+
return Result::Failure($error->getMessage());
41+
}
42+
43+
return Result::Success($data);
44+
}, 'Instaling Laravel');
45+
46+
return $result;
47+
}
48+
}

app/Utils/MenuBuilder.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Utils;
4+
5+
use Illuminate\Support\Arr;
6+
use function Laravel\Prompts\select;
7+
8+
class MenuBuilder
9+
{
10+
/**
11+
* Method to extract the options of the array
12+
*
13+
* @param array $options
14+
* @return array
15+
*/
16+
private static function extractMenuOptions (array $options): array
17+
{
18+
return Arr::map($options, function ($value, string $key) {
19+
return $key;
20+
});
21+
}
22+
23+
/**
24+
* Method to extract options of array without the selected part
25+
*
26+
* @param array $options
27+
* @param array $keys
28+
* @return array
29+
*/
30+
private static function excludeOptions(array $options, array $keys): array
31+
{
32+
return Arr::except($options, $keys);
33+
}
34+
35+
/**
36+
* Method to generate options of menu
37+
*
38+
* @param array $options
39+
* @param array $optionsToExclude
40+
* @return array
41+
*/
42+
public static function createMenu(array $options, string $label, array $optionsToExclude = [])
43+
{
44+
$optionsProcessed = self::extractMenuOptions($options);
45+
if (isset($optionsToExclude) && count($optionsToExclude) > 0) {
46+
$optionsProcessed = self::excludeOptions($optionsProcessed, $optionsToExclude);
47+
}
48+
return select(
49+
label: $label,
50+
options: $optionsProcessed,
51+
);
52+
// return $optionsProcessed;
53+
}
54+
}

bootstrap/providers.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<?php
22

3-
return [
4-
App\Providers\AppServiceProvider::class,
5-
];
3+
return [App\Providers\AppServiceProvider::class, App\Providers\LinuxProcessManagerServiceProvider::class, App\Providers\TaskManagerServiceProvider::class, App\Providers\LaravelInstallerServiceProvider::class, App\Providers\ProjectMakerContextServiceProvider::class];

composer.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
{
2-
"name": "laravel-zero/laravel-zero",
3-
"description": "The Laravel Zero Framework.",
4-
"keywords": ["framework", "laravel", "laravel zero", "console", "cli"],
2+
"name": "archmaster/laravel_starter",
3+
"description": "The Laravel Starter command",
4+
"keywords": ["tool", "project initializer", "laravel starter", "console", "cli"],
55
"homepage": "https://laravel-zero.com",
66
"type": "project",
7-
"license": "MIT",
7+
"license": "GPL3.0",
88
"support": {
99
"issues": "https://github.com/laravel-zero/laravel-zero/issues",
1010
"source": "https://github.com/laravel-zero/laravel-zero"
1111
},
1212
"authors": [
1313
{
14-
"name": "Nuno Maduro",
15-
"email": "enunomaduro@gmail.com"
14+
"name": "Manuel Gutierrez",
15+
"email": "gutierrezgonzalezmanuel360@gmail.com"
1616
}
1717
],
1818
"require": {
1919
"php": "^8.2.0",
20-
"laravel-zero/framework": "^11.0.2"
20+
"laravel-zero/framework": "^11.0.2",
21+
"laravel/prompts": "^0.3.1",
22+
"nunomaduro/laravel-console-task": "^1.9"
2123
},
2224
"require-dev": {
2325
"laravel/pint": "^1.18.1",

0 commit comments

Comments
 (0)