feat(update): install missing build dependencies before rebuilding
A minimal Arch system has no cmake, no compiler and no qt6-base, so the self-update used to fail or fall back to the releases page. The update now asks pacman what is missing, installs it through sudo with the password typed in the update dialog, then resumes the build. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
4 files changed
+212
-13
No files matched your search
+83
-7
@@ -119,6 +119,50 @@ QString UpdateManager::releasePageUrl() const {
|
||||
return m_releasePageUrl;
|
||||
}
|
||||
|
||||
// Packages required to rebuild the app. base-devel is the Arch metapackage
|
||||
// pulling the whole toolchain (gcc, make, sudo…), git feeds the Clone method
|
||||
// and qt6-base provides the Widgets + Network modules the build links against.
|
||||
// cmake ships in base-devel on current Arch but is listed explicitly so a
|
||||
// stripped-down system still gets it.
|
||||
QStringList UpdateManager::buildDependencies() {
|
||||
return {QStringLiteral("base-devel"), QStringLiteral("cmake"),
|
||||
QStringLiteral("gcc"), QStringLiteral("git"),
|
||||
QStringLiteral("qt6-base")};
|
||||
}
|
||||
|
||||
// `pacman -T` answers "which of these packages is NOT installed" (exit 127 with
|
||||
// the missing names on stdout), the exact question the update asks. The result
|
||||
// is cached for the process lifetime: a package does not vanish between the
|
||||
// dialog and the update, and the check spawns one pacman per call.
|
||||
QStringList UpdateManager::missingBuildDependencies() {
|
||||
static QStringList cached;
|
||||
static bool fetched = false;
|
||||
if (fetched) return cached;
|
||||
fetched = true;
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
return cached;
|
||||
#else
|
||||
if (QProcess::execute("/bin/sh", {"-c", "command -v pacman >/dev/null 2>&1"}) != 0) {
|
||||
return cached; // not an Arch system: nothing to install, and the
|
||||
// rebuild methods will fail on their own if attempted
|
||||
}
|
||||
|
||||
QProcess pacman;
|
||||
QStringList args{QStringLiteral("-T")};
|
||||
args += buildDependencies();
|
||||
pacman.start("pacman", args);
|
||||
if (!pacman.waitForFinished(15000)) return cached;
|
||||
// simplified() collapses newlines and runs of spaces into single spaces:
|
||||
// pacman -T prints the missing targets whitespace-separated, and splitting
|
||||
// the raw output would keep line breaks inside the tokens.
|
||||
cached = QString::fromUtf8(pacman.readAllStandardOutput())
|
||||
.simplified()
|
||||
.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
return cached;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Cached `command -v` lookup. The update dialog queries several tools on
|
||||
// every check; caching keeps that from spawning a shell per query.
|
||||
bool UpdateManager::toolAvailable(const char* tool) {
|
||||
@@ -186,25 +230,33 @@ UpdateManager::UpdateMethod UpdateManager::updateMethod() const {
|
||||
capabilities.hasCompiler = compilerAvailable();
|
||||
capabilities.hasPrebuilt = !m_prebuiltUrl.isEmpty();
|
||||
capabilities.hasTarball = !m_tarballUrl.isEmpty();
|
||||
// pacman present = every missing build tool can be installed first, which
|
||||
// makes the Clone/Tarball methods reachable on a minimal Arch system.
|
||||
capabilities.canInstallPackages = toolAvailable("pacman");
|
||||
return chooseUpdateMethod(capabilities);
|
||||
}
|
||||
|
||||
// Strategy, in order of preference:
|
||||
// 1. Checkout: a source checkout is the developer's tree, pull + rebuild it;
|
||||
// 2. Clone: rebuild from the real sources (git + cmake + compiler present),
|
||||
// so an installed app follows the repo whatever its location;
|
||||
// 3. Prebuilt: no toolchain, unpack the release's binary archive;
|
||||
// 4. Tarball: no git but a compiler, build the release sources.
|
||||
// 2. Clone: rebuild from the real sources (git + cmake + compiler present, or
|
||||
// installable through pacman first), so an installed app follows the repo
|
||||
// whatever its location;
|
||||
// 3. Prebuilt: no toolchain and pacman cannot fill the gap, unpack the
|
||||
// release's binary archive;
|
||||
// 4. Tarball: no git but a buildable toolchain, build the release sources.
|
||||
// Anything else cannot update locally and falls back to the releases page.
|
||||
UpdateManager::UpdateMethod UpdateManager::chooseUpdateMethod(
|
||||
const UpdateCapabilities& capabilities) {
|
||||
if (capabilities.checkoutUsable) return UpdateMethod::Checkout;
|
||||
if (capabilities.hasGit && capabilities.hasCmake && capabilities.hasCompiler) {
|
||||
const bool buildable = capabilities.hasCmake && capabilities.hasCompiler;
|
||||
// pacman present = every missing build tool can be installed first, which
|
||||
// makes the rebuilding methods reachable on a minimal Arch system.
|
||||
const bool buildableWithPacman = capabilities.canInstallPackages;
|
||||
if (capabilities.hasGit && (buildable || buildableWithPacman)) {
|
||||
return UpdateMethod::Clone;
|
||||
}
|
||||
if (capabilities.hasPrebuilt) return UpdateMethod::Prebuilt;
|
||||
if (capabilities.hasTarball && capabilities.hasCmake
|
||||
&& capabilities.hasCompiler) {
|
||||
if (capabilities.hasTarball && (buildable || buildableWithPacman)) {
|
||||
return UpdateMethod::Tarball;
|
||||
}
|
||||
return UpdateMethod::None;
|
||||
@@ -392,6 +444,27 @@ bool UpdateManager::isNewerVersion(const QString& remote, const QString& local)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Shell snippet shared by every rebuilding method: asks pacman which of the
|
||||
// build dependencies are missing (`pacman -T`, exit 127) and installs them
|
||||
// through `pacman -S --needed --noconfirm`. --needed skips what is already
|
||||
// installed, --noconfirm keeps the unattended update from blocking on a
|
||||
// pacman prompt. With a password it is piped to `sudo -S`; without one, `-n`
|
||||
// keeps a passwordless-sudo setup working while a password-requiring sudo
|
||||
// fails fast instead of hanging the background run on an unanswerable prompt.
|
||||
// On a fully equipped machine the whole block is skipped.
|
||||
QString UpdateManager::dependencyInstallScript(const QString& quotedPassword) {
|
||||
const QString sudoPrefix = quotedPassword.isEmpty()
|
||||
? QStringLiteral("sudo -n")
|
||||
: QStringLiteral("printf '%s\\n' %1 | sudo -S").arg(quotedPassword);
|
||||
return QStringLiteral(
|
||||
"MISSING=$(pacman -T %1 2>/dev/null || true)\n"
|
||||
"if [ -n \"$MISSING\" ]; then\n"
|
||||
" echo \"Installation des dependances de compilation : $MISSING\"\n"
|
||||
" %2 pacman -S --needed --noconfirm $MISSING 2>&1\n"
|
||||
"fi\n")
|
||||
.arg(buildDependencies().join(QLatin1Char(' ')), sudoPrefix);
|
||||
}
|
||||
|
||||
// Rebuilds + reinstalls the app in the background, using the strategy picked
|
||||
// by updateMethod(). The running process is untouched (it keeps the replaced
|
||||
// inode); the caller relaunches into the new build. Exits non-zero on failure
|
||||
@@ -427,6 +500,7 @@ void UpdateManager::performSelfUpdate(const QString& sudoPassword) {
|
||||
// leave the copy in use (and its version marker) untouched.
|
||||
void UpdateManager::performCheckoutUpdate(const QString& sudoPassword) {
|
||||
QString script = updateScriptPrologue();
|
||||
script += dependencyInstallScript(quotedPassword(sudoPassword));
|
||||
script += QStringLiteral(
|
||||
"git pull --ff-only origin main 2>&1 || git pull --ff-only 2>&1\n"
|
||||
"if [ -d build ]; then\n"
|
||||
@@ -542,6 +616,7 @@ QString UpdateManager::quotedPassword(const QString& sudoPassword) {
|
||||
// running binary lives.
|
||||
void UpdateManager::performCloneUpdate(const QString& sudoPassword) {
|
||||
QString script = updateScriptPrologue();
|
||||
script += dependencyInstallScript(quotedPassword(sudoPassword));
|
||||
script += QStringLiteral(
|
||||
"SRC=%1\n"
|
||||
"export SRC\n"
|
||||
@@ -593,6 +668,7 @@ void UpdateManager::performPrebuiltUpdate(const QString& sudoPassword) {
|
||||
// result where the running binary lives. Used only when git is unavailable.
|
||||
void UpdateManager::performTarballUpdate(const QString& sudoPassword) {
|
||||
QString script = updateScriptPrologue();
|
||||
script += dependencyInstallScript(quotedPassword(sudoPassword));
|
||||
script += QStringLiteral(
|
||||
"command -v cmake >/dev/null 2>&1 || { echo 'cmake est requis pour la mise a jour'; exit 1; }\n"
|
||||
"URL=%1\n"
|
||||
|
||||
Reference in new issue
Block a user