Files
FWS-PacManage/include/update-manager.h
T
BlackAngelTV 54bff16369 feat(update): self-update from a cloned repo wherever the app is installed
Self-update assumed a conventional install prefix (silently reinstalling into
~/.local when the binary lived elsewhere) and needed a release tarball to do
anything. Pick the strategy from what the machine actually has -- a local
checkout, git+cmake+compiler for a clone, the prebuilt archive, or the release
sources -- and always write the fresh binary next to the running one, building
in a temp directory so sudo is only needed for the final copy.

Bundles the UI work that depends on it (tray, shortcuts, log filter, CSV
exports, cached package details), the structured log categories, and the new
test targets (update-manager, AUR, plugins) wired into CTest.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-11 20:15:41 +02:00

147 lines
6.8 KiB
C++

#pragma once
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDateTime>
#include <QString>
#include <QProcess>
// Checks the Gitea repository for new releases and tracks the last check
// state in a JSON file so the app only queries the API once per day.
// Self-update is location-independent: the fresh binary is always written to
// the directory that holds the running executable, wherever that is
// (~/.local/bin, /usr/bin, an arbitrary extracted folder, a build tree...).
// The strategy is picked by updateMethod():
// - Checkout: a source checkout around the binary is pulled + rebuilt;
// - Clone: installed without a checkout, the repo is cloned (then pulled)
// into ~/.local/share/PacManager/src, built, and installed in place;
// - Prebuilt: the release's prebuilt archive is unpacked (no compiler);
// - Tarball: the release sources are built and installed in place.
// The running binary stays untouched until the next launch.
class UpdateManager : public QObject {
Q_OBJECT
public:
explicit UpdateManager(QObject* parent = nullptr);
// Current app version, read from the version file at build time.
static QString currentVersion();
// Path of the state file storing the last check date and the latest
// known remote version (e.g. ~/.local/share/PacManager/update-state.json).
static QString stateFilePath();
// True when the last check happened less than 24h ago, so we can skip
// the network call at launch.
bool shouldCheck() const;
// Latest remote version recorded in the state file (empty if never checked).
QString lastKnownRemoteVersion() const;
// Fetches the latest release from the Gitea API. Emits checkFinished.
void checkForUpdates();
// True when the remote version is newer than the local one.
static bool isNewerVersion(const QString& remote, const QString& local);
// Path of the source repository, or empty when the app is installed
// without the repo (e.g. ~/.local/bin).
QString repoPath() const;
// Download URL of the source tarball for the pending update (empty until
// a check completes), plus the human-readable release page.
QString tarballUrl() const;
QString releasePageUrl() const;
// Prebuilt binary archive attached to the release
// (PacManager-<version>-x86_64.tar.gz), when the release workflow
// uploaded one. Used as a fallback when no build toolchain is installed.
QString prebuiltUrl() const;
// True when the pending update can be applied by unpacking a prebuilt
// archive instead of recompiling from source.
bool hasPrebuiltUpdate() const;
// Directory holding the running executable. The update always installs the
// new binary here (in place), so it works whatever the layout on disk.
QString binDir() const;
// Persistent source clone used to update an installed app that has no
// local checkout (~/.local/share/PacManager/src). The first update clones,
// later ones fast-forward to the latest main.
QString sourceClonePath() const;
// How the pending update will be applied. Exposed so the UI can describe
// the operation (and offer the releases page when there is no way to
// update locally).
enum class UpdateMethod { None, Checkout, Clone, Prebuilt, Tarball };
UpdateMethod updateMethod() const;
// Inputs of the strategy decision, split out so the choice is a pure
// function that can be unit-tested without a network, a filesystem or an
// installed toolchain.
struct UpdateCapabilities {
bool checkoutUsable = false; // a FWS source checkout surrounds the binary
bool hasGit = false;
bool hasCmake = false;
bool hasCompiler = false;
bool hasPrebuilt = false; // the release ships a prebuilt binary archive
bool hasTarball = false; // the release ships a source tarball
};
static UpdateMethod chooseUpdateMethod(const UpdateCapabilities& capabilities);
// Human-readable description of the given method and install target, for
// the "update available" dialog.
static QString methodDescription(UpdateMethod method, const QString& targetDir);
// Whether a self-update is possible with the current state.
bool canSelfUpdate() const;
// Rebuilds + reinstalls the app in the background. Pass the sudo password
// when the target directory is not user-writable (empty = no sudo pipe,
// `sudo` will prompt on its own if needed). Emits selfUpdateFinished.
void performSelfUpdate(const QString& sudoPassword = QString());
signals:
// Emitted after a check completes. hasUpdate is true when a newer
// release exists; errorMessage is non-empty when the request failed.
void checkFinished(bool hasUpdate, const QString& latestVersion,
const QString& errorMessage);
// Emitted when the background self-update completes. success is true
// if the pull + build produced a fresh executable.
void selfUpdateFinished(bool success, const QString& message);
private:
void saveState(const QString& remoteVersion);
void loadState();
void runShell(const QString& script, const QString& workingDir);
void performCheckoutUpdate();
void performCloneUpdate(const QString& sudoPassword);
void performPrebuiltUpdate(const QString& sudoPassword);
void performTarballUpdate(const QString& sudoPassword);
// Shared script header for the installed (no checkout) paths: temp work
// dir, cleanup trap, and the export of the paths the install step needs.
QString installedScriptPrologue() const;
// Copy command that installs a staged tree ($STAGE/bin) into the running
// binary's directory, wrapped in sudo when the target is not writable.
QString installCommand(const QString& quotedPassword,
const QString& action) const;
// Directory holding the installed binary, quoted for the shell.
QString quotedBinDir() const;
static QString quoteShell(const QString& s);
static QString quotedPassword(const QString& sudoPassword);
// Tool lookup cached per process, so the update dialog does not spawn a
// fresh `command -v` for every check.
static bool toolAvailable(const char* tool);
static bool compilerAvailable();
QNetworkAccessManager m_network;
QProcess m_process; // runs the git pull + build script
QString m_lastCheckDate; // ISO date of the last successful check
QString m_lastKnownRemoteVersion; // latest version seen on the server
QString m_tarballUrl; // source tarball for the pending update
QString m_prebuiltUrl; // prebuilt binary archive, when attached
QString m_releasePageUrl; // human-readable release page
QString m_repoPath; // source repo dir when found
};