mailwolt/app/Console/Commands/CheckUpdates.php

124 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CheckUpdates extends Command
{
protected $signature = 'mailwolt:check-updates';
protected $description = 'Check for newer MailWolt releases via git tags';
public function handle(): int
{
$currentNorm = $this->readInstalledVersionNorm();
$currentRaw = $this->readInstalledVersionRaw() ?? ($currentNorm ? 'v'.$currentNorm : null);
// Neuesten Tag vom Remote holen (semver-freundlich sortiert)
$appPath = base_path();
$cmd = <<<BASH
set -e
cd {$appPath}
git fetch --tags --force --quiet origin +refs/tags/*:refs/tags/*
# Beste Wahl zuerst: v*-Tags semver-sortiert, sonst alle Tags
(git tag -l 'v*' --sort=-v:refname | head -n1) || true
BASH;
$latestTagRaw = trim((string) shell_exec($cmd));
if ($latestTagRaw === '') {
// Fallback auf alle Tags (ohne Filter), falls keine v*-Tags existieren
$latestTagRaw = trim((string) shell_exec("cd {$appPath} && git tag -l --sort=-v:refname | head -n1"));
}
// Normieren (v weg, Suffixe wie -3-gabcd/-dirty entfernen)
$latestNorm = $this->normalizeVersion($latestTagRaw);
// Nichts gefunden -> Caches leeren
if (!$latestNorm) {
cache()->forget('updates:latest');
cache()->forget('updates:latest_raw');
cache()->forget('mailwolt.update_available'); // Legacy
$this->warn('Keine Release-Tags gefunden.');
return self::SUCCESS;
}
// Cache schreiben
cache()->put('updates:latest', $latestNorm, now()->addMinutes(10));
cache()->put('updates:latest_raw', $latestTagRaw, now()->addMinutes(10));
cache()->forever('mailwolt.update_available', $latestNorm); // Legacy-Kompat
// Vergleich & Ausgabe
if ($currentNorm && version_compare($latestNorm, $currentNorm, '>')) {
$this->info("Update verfügbar: {$latestTagRaw} (installiert: ".($currentRaw ?? $currentNorm).")");
} else {
$this->info("Aktuell (installiert: ".($currentRaw ?? $currentNorm ?? 'unbekannt').").");
// Kein Update Legacy-Key aufräumen (UI liest die neuen Keys)
cache()->forget('mailwolt.update_available');
}
cache()->put('updates:last_checked_at', now(), now()->addMinutes(10));
return self::SUCCESS;
}
/* ===== Helpers ===== */
private function readInstalledVersionNorm(): ?string
{
$paths = [
'/var/lib/mailwolt/version', // vom Wrapper (normiert)
base_path('VERSION'), // App-Fallback
];
foreach ($paths as $p) {
$raw = @trim(@file_get_contents($p) ?: '');
if ($raw !== '') return $this->normalizeVersion($raw);
}
// Noch ein Fallback aus RAW-Datei
$raw = $this->readInstalledVersionRaw();
return $raw ? $this->normalizeVersion($raw) : null;
}
private function readInstalledVersionRaw(): ?string
{
$p = '/var/lib/mailwolt/version_raw'; // vom Wrapper (z.B. "v1.0.25" oder "v1.0.25-3-gabcd")
$raw = @trim(@file_get_contents($p) ?: '');
return $raw !== '' ? $raw : null;
}
private function normalizeVersion(?string $v): ?string
{
if (!$v) return null;
$v = trim($v);
if ($v === '') return null;
$v = ltrim($v, "vV \t\n\r\0\x0B"); // führendes v entfernen
$v = preg_replace('/-.*$/', '', $v); // Build-/dirty-Suffix abschneiden
return $v !== '' ? $v : null;
}
// public function handle(): int
// {
// $appPath = base_path();
// $current = trim(@file_get_contents(base_path('VERSION'))) ?: '0.0.0';
// // newest tag from origin (sorted semver-friendly)
// $latest = trim(shell_exec(
// "cd {$appPath} && git fetch --tags --quiet origin && git tag --list | sort -V | tail -n1"
// ) ?? '');
//
// // Tags haben usually ein 'v' Prefix entfernen
// $latest = ltrim($latest, 'v');
//
// if (!$latest) {
// $this->warn('Keine Release-Tags gefunden.');
// cache()->forget('mailwolt.update_available');
// return 0;
// }
//
// if (version_compare($latest, $current, '>')) {
// cache()->forever('mailwolt.update_available', $latest);
// $this->info("Update verfügbar: {$latest} (installiert: {$current})");
// } else {
// cache()->forget('mailwolt.update_available');
// $this->info("Aktuell (installiert: {$current}).");
// }
// return 0;
// }
}