coolify/app/Console/Commands/ServicesGenerate.php

88 lines
2.7 KiB
PHP
Raw Normal View History

2023-10-19 09:28:25 +00:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
2023-10-19 09:28:25 +00:00
use Symfony\Component\Yaml\Yaml;
2023-10-24 10:33:49 +00:00
class ServicesGenerate extends Command
2023-10-19 09:28:25 +00:00
{
/**
* {@inheritdoc}
2023-10-19 09:28:25 +00:00
*/
protected $signature = 'services:generate';
/**
* {@inheritdoc}
2023-10-19 09:28:25 +00:00
*/
protected $description = 'Generate service-templates.yaml based on /templates/compose directory';
public function handle(): int
2023-10-19 09:28:25 +00:00
{
$serviceTemplatesJson = collect(glob(base_path('templates/compose/*.yaml')))
->mapWithKeys(function ($file): array {
$file = basename($file);
$parsed = $this->processFile($file);
return $parsed === false ? [] : [
Arr::pull($parsed, 'name') => $parsed,
];
})->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
file_put_contents(base_path('templates/service-templates.json'), $serviceTemplatesJson.PHP_EOL);
return self::SUCCESS;
2023-10-19 09:28:25 +00:00
}
private function processFile(string $file): false|array
2023-10-19 09:28:25 +00:00
{
$content = file_get_contents(base_path("templates/compose/$file"));
preg_match_all(
'/#\s*(documentation|env_file|ignore|logo|minversion|port|slogan|tags)\s*:\s*(.+)\s*/',
$content, $matches
);
$data = array_combine($matches[1], $matches[2]);
if (str($data['ignore'] ?? false)->toBoolean()) {
2023-10-19 09:28:25 +00:00
$this->info("Ignoring $file");
2024-06-10 20:43:34 +00:00
return false;
2023-10-19 09:28:25 +00:00
}
2023-10-19 09:28:25 +00:00
$this->info("Processing $file");
$documentation = $data['documentation'] ?? null;
$documentation = $documentation ? $documentation.'?utm_source=coolify.io' : 'https://coolify.io/docs';
2023-10-19 09:28:25 +00:00
$json = Yaml::parse($content);
$compose = base64_encode(Yaml::dump($json, 10, 2));
$tags = str($data['tags'] ?? '')->lower()->explode(',')->map(fn ($tag) => trim($tag))->filter();
$tags = $tags->isEmpty() ? null : $tags->all();
2023-10-19 09:28:25 +00:00
$payload = [
'name' => pathinfo($file, PATHINFO_FILENAME),
2023-10-19 09:28:25 +00:00
'documentation' => $documentation,
'slogan' => $data['slogan'] ?? str($file)->headline(),
'compose' => $compose,
2023-10-24 10:33:49 +00:00
'tags' => $tags,
'logo' => $data['logo'] ?? 'svgs/coolify.png',
'minversion' => $data['minversion'] ?? '0.0.0',
2023-10-19 09:28:25 +00:00
];
if ($port = $data['port'] ?? null) {
$payload['port'] = $port;
}
if ($envFile = $data['env_file'] ?? null) {
$envFileContent = file_get_contents(base_path("templates/compose/$envFile"));
$payload['envs'] = base64_encode($envFileContent);
2023-10-19 09:28:25 +00:00
}
2024-06-10 20:43:34 +00:00
2023-10-19 09:28:25 +00:00
return $payload;
}
}