coolify/app/Console/Commands/Dev.php

81 lines
2.2 KiB
PHP
Raw Normal View History

2023-12-13 10:35:53 +00:00
<?php
namespace App\Console\Commands;
use App\Models\InstanceSettings;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Process;
use Symfony\Component\Yaml\Yaml;
2023-12-13 10:35:53 +00:00
class Dev extends Command
{
2024-07-06 12:33:59 +00:00
protected $signature = 'dev {--init} {--generate-openapi}';
2024-06-10 20:43:34 +00:00
2024-07-06 12:33:59 +00:00
protected $description = 'Helper commands for development.';
2023-12-13 10:35:53 +00:00
public function handle()
2024-07-06 12:33:59 +00:00
{
if ($this->option('init')) {
$this->init();
return;
}
if ($this->option('generate-openapi')) {
$this->generateOpenApi();
return;
}
}
public function generateOpenApi()
{
// Generate OpenAPI documentation
echo "Generating OpenAPI documentation.\n";
2024-11-11 13:28:24 +00:00
// https://github.com/OAI/OpenAPI-Specification/releases
2025-01-07 13:52:08 +00:00
$processResult = Process::run([
2024-11-11 13:28:24 +00:00
'/var/www/html/vendor/bin/openapi',
'app',
'-o',
'openapi.yaml',
'--version',
'3.1.0',
]);
2025-01-07 13:52:08 +00:00
$error = $processResult->errorOutput();
2024-07-06 12:33:59 +00:00
$error = preg_replace('/^.*an object literal,.*$/m', '', $error);
$error = preg_replace('/^\h*\v+/m', '', $error);
echo $error;
2025-01-07 13:52:08 +00:00
echo $processResult->output();
// Convert YAML to JSON
$yaml = file_get_contents('openapi.yaml');
$json = json_encode(Yaml::parse($yaml), JSON_PRETTY_PRINT);
file_put_contents('openapi.json', $json);
echo "Converted OpenAPI YAML to JSON.\n";
2024-07-06 12:33:59 +00:00
}
public function init()
2023-12-13 10:35:53 +00:00
{
// Generate APP_KEY if not exists
2024-07-06 12:33:59 +00:00
2024-11-12 14:53:05 +00:00
if (empty(config('app.key'))) {
2023-12-13 10:35:53 +00:00
echo "Generating APP_KEY.\n";
Artisan::call('key:generate');
}
2024-07-21 16:30:28 +00:00
// Generate STORAGE link if not exists
if (! file_exists(public_path('storage'))) {
echo "Generating STORAGE link.\n";
Artisan::call('storage:link');
}
2023-12-13 10:35:53 +00:00
// Seed database if it's empty
2025-01-07 13:52:08 +00:00
$settings = InstanceSettings::query()->find(0);
2024-06-10 20:43:34 +00:00
if (! $settings) {
2023-12-13 10:35:53 +00:00
echo "Initializing instance, seeding database.\n";
Artisan::call('migrate --seed');
} else {
echo "Instance already initialized.\n";
}
}
}