|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\FrameworkComposer; |
|
6
|
|
|
use Formularium\Model; |
|
7
|
|
|
use HaydenPierce\ClassFinder\ClassFinder; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
use Modelarium\Frontend\FrontendGenerator; |
|
10
|
|
|
|
|
11
|
|
|
class ModelariumFrontendCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
use WriterTrait; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name and signature of the console command. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signature = 'modelarium:frontend |
|
21
|
|
|
{name : The model name. Use "*" or "all" for all models} |
|
22
|
|
|
{--framework=* : The frameworks to use} |
|
23
|
|
|
{--overwrite : overwrite files if they exist} |
|
24
|
|
|
{--prettier : run prettier on files} |
|
25
|
|
|
'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The console command description. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $description = 'Creates frontend using Modelarium'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var FrameworkComposer |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $composer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new command instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Execute the console command. |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function handle() |
|
55
|
|
|
{ |
|
56
|
|
|
$name = $this->argument('name'); |
|
57
|
|
|
|
|
58
|
|
|
// setup stuff |
|
59
|
|
|
$frameworks = $this->option('framework'); |
|
60
|
|
|
if (empty($frameworks)) { |
|
61
|
|
|
$this->error('If you are generating frontend you need to specify frameworks. Example: `--framework=HTML --framework=Bootstrap --framework=Vue`'); |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
if (!is_array($frameworks)) { |
|
|
|
|
|
|
65
|
|
|
$frameworks = [$frameworks]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->composer = FrameworkComposer::create($frameworks); |
|
69
|
|
|
|
|
70
|
|
|
if ($name === '*' || $name === 'all') { |
|
71
|
|
|
// TODO: all classes |
|
72
|
|
|
$classesInNamespace = ClassFinder::getClassesInNamespace('App\\Models'); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($classesInNamespace as $class) { |
|
75
|
|
|
$reflection = new \ReflectionClass($class); |
|
76
|
|
|
if (!$reflection->isInstantiable()) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
$this->generateFromModel($class); |
|
80
|
|
|
} |
|
81
|
|
|
return; |
|
82
|
|
|
} elseif (is_array($name)) { |
|
83
|
|
|
// TODO |
|
84
|
|
|
} else { |
|
85
|
|
|
$this->generateFromModel($name); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
$this->info('Finished frontend.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function generateFromModel(string $name) |
|
91
|
|
|
{ |
|
92
|
|
|
$model = $name::getFormularium(); |
|
93
|
|
|
$generator = new FrontendGenerator($this->composer, $model); |
|
94
|
|
|
$collection = $generator->generate(); |
|
95
|
|
|
|
|
96
|
|
|
if (!$collection) { |
|
|
|
|
|
|
97
|
|
|
$this->info('Nothing generated.'); |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$basepath = base_path('resources/js/components/'); |
|
|
|
|
|
|
102
|
|
|
$writtenFiles = $this->writeFiles( |
|
103
|
|
|
$collection, |
|
104
|
|
|
$basepath, |
|
105
|
|
|
(bool)$this->option('overwrite') |
|
106
|
|
|
); |
|
107
|
|
|
$this->info('Files generated.'); |
|
108
|
|
|
|
|
109
|
|
|
if ($this->option('prettier')) { |
|
110
|
|
|
$this->info('Running prettier on generated files.'); |
|
111
|
|
|
$useYarn = file_exists(base_path('yarn.lock')); |
|
112
|
|
|
if ($useYarn) { |
|
113
|
|
|
$command = "cd $basepath && npx prettier --write "; |
|
114
|
|
|
} else { |
|
115
|
|
|
$command = "cd $basepath && yarn prettier --write "; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// this runs all prettier commands in parallel. |
|
119
|
|
|
$run = array_reduce( |
|
120
|
|
|
$writtenFiles, |
|
121
|
|
|
function ($carry, $f) use ($command) { |
|
122
|
|
|
return $carry . '(' . $command . $f . ') & '; |
|
123
|
|
|
} |
|
124
|
|
|
); |
|
125
|
|
|
shell_exec($run . ' wait'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|