The checkout path installed with the build's configured prefix instead of installing into the directory holding the running copy, so the binary in use and its version marker both stayed at the old release: the app kept reporting that version and offered the same update again at every launch. Every method now stages and installs into the running binary's directory, the install step writes the release it just installed next to the binary, and currentVersion() reads the version compiled into the running build first, falling back to the marker file. The repo-root `version` file stays the single source of truth: CMake compiles it in. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
170 lines
8.2 KiB
C++
170 lines
8.2 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, then
|
|
// installed into the running binary's directory like the other methods;
|
|
// - 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 (it keeps the inode that was replaced on
|
|
// disk); the UI hands over to the freshly installed build by relaunching it.
|
|
class UpdateManager : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
explicit UpdateManager(QObject* parent = nullptr);
|
|
|
|
// Current app version, compiled in from the repo-root `version` file (see
|
|
// CMakeLists.txt), falling back to the `version` file installed next to the
|
|
// executable when the build bypassed CMake.
|
|
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;
|
|
|
|
// Path of the freshly installed binary to hand over to after a successful
|
|
// update, or empty when `binDir` holds no runnable binary (the caller then
|
|
// falls back to asking for a manual restart). The running process still
|
|
// maps the replaced inode, so starting this file is the only way to pick up
|
|
// the new build without a manual restart. Static and parameterised so it
|
|
// can be unit-tested without performing an update.
|
|
static QString restartProgram(const QString& binDir);
|
|
|
|
// 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);
|
|
|
|
// Shell script that installs a staged tree (`$STAGE/bin`, plugins under
|
|
// `$STAGE/bin/plugins` or `$STAGE/plugins`) into the running binary's
|
|
// directory (`$BIN_DIR`), both taken from the environment. Each file is
|
|
// copied to a sibling temp file and renamed into place, so the install
|
|
// also works when the target is the executable (or plugin) currently in
|
|
// use, where a plain `cp` fails with ETXTBSY ("Text file busy").
|
|
// Exposed so the unit test can drive it against a genuinely running
|
|
// binary.
|
|
static QString stagedInstallScript();
|
|
|
|
// 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(const QString& sudoPassword);
|
|
void performCloneUpdate(const QString& sudoPassword);
|
|
void performPrebuiltUpdate(const QString& sudoPassword);
|
|
void performTarballUpdate(const QString& sudoPassword);
|
|
// Shared script header for every update path: temp work dir, cleanup trap,
|
|
// the release being installed, and the export of the paths the install step
|
|
// needs.
|
|
QString updateScriptPrologue() 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
|
|
}; |