mailwolt/app/Console/Commands/CheckUpdates.php

120 lines
4.4 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);
$appPath = base_path();
$cmd = <<<BASH
set -e
cd {$appPath}
git fetch --tags --force --quiet origin +refs/tags/*:refs/tags/*
(git tag -l 'v*' --sort=-v:refname | head -n1) || true
BASH;
$latestTagRaw = trim((string) shell_exec($cmd));
if ($latestTagRaw === '') {
$latestTagRaw = trim((string) shell_exec("cd {$appPath} && git tag -l --sort=-v:refname | head -n1"));
}
$latestNorm = $this->normalizeVersion($latestTagRaw);
// Nichts gefunden -> alles 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;
}
// Nur wenn wirklich neuer als installiert -> Keys setzen
if ($currentNorm && version_compare($latestNorm, $currentNorm, '>')) {
cache()->forever('updates:latest', $latestNorm);
cache()->forever('updates:latest_raw', $latestTagRaw ?: ('v'.$latestNorm));
cache()->forever('mailwolt.update_available', $latestNorm); // legacy-kompat
$this->info("Update verfügbar: {$latestTagRaw} (installiert: ".($currentRaw ?? $currentNorm).")");
} else {
// Kein Update -> Keys löschen
cache()->forget('updates:latest');
cache()->forget('updates:latest_raw');
cache()->forget('mailwolt.update_available'); // legacy
$this->info("Aktuell (installiert: ".($currentRaw ?? $currentNorm ?? 'unbekannt').").");
}
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;
// }
}