Add update check and auto-update functionality

This commit is contained in:
2026-03-20 08:46:55 +01:00
parent 674a01b90a
commit bc3532ce2c
4 changed files with 284 additions and 3 deletions

View File

@@ -2,14 +2,23 @@ const { app, BrowserWindow, Menu, dialog, ipcMain, shell } = require('electron')
const path = require('path')
const { is } = require('@electron-toolkit/utils')
const fs = require('fs')
const { execFile } = require('child_process')
const { promisify } = require('util')
let mainWindow
let settingsWindow = null
const execFileAsync = promisify(execFile)
const DEFAULT_BACKEND_API_URL = 'http://127.0.0.1:8000'
const DEFAULT_OLLAMA_API_URL = 'http://127.0.0.1:11434'
const REPO_ROOT = path.resolve(__dirname, '..')
const UPDATE_REMOTE_URL = 'https://giers10.uber.space/giers10/Heimgeist.git'
const UPDATE_BRANCH = 'master'
const GIT_ENV = { ...process.env, GIT_TERMINAL_PROMPT: '0' }
const settingsFilePath = process.env.HEIMGEIST_SETTINGS_FILE || path.join(app.getPath('userData'), 'settings.json')
let appSettings = {}
let lastUpdateCheckResult = null
let activeUpdateCheck = null
const DEFAULT_UI_SCALE = 1
const MIN_UI_SCALE = 0.7
const MAX_UI_SCALE = 1.3
@@ -111,6 +120,156 @@ function saveSettings() {
}
}
function setUpdateStatus(status) {
lastUpdateCheckResult = {
state: 'idle',
message: '',
checkedAt: new Date().toISOString(),
localCommit: null,
remoteCommit: null,
branch: null,
restartScheduled: false,
...status,
}
return lastUpdateCheckResult
}
async function runGitCommand(args, options = {}) {
return execFileAsync('git', ['-C', REPO_ROOT, ...args], {
env: GIT_ENV,
maxBuffer: 1024 * 1024,
timeout: options.timeout ?? 15000,
})
}
function scheduleAppRestart() {
setTimeout(() => {
app.relaunch()
app.exit(0)
}, 300)
}
async function performUpdateCheck(trigger = 'manual') {
if (!fs.existsSync(path.join(REPO_ROOT, '.git'))) {
return setUpdateStatus({
state: 'unavailable',
trigger,
message: 'Update check unavailable: no Git checkout found.',
})
}
setUpdateStatus({
state: 'checking',
trigger,
message: 'Checking remote repository for updates...',
})
try {
const [
{ stdout: localStdout },
{ stdout: branchStdout },
{ stdout: remoteStdout },
{ stdout: worktreeStdout },
] = await Promise.all([
runGitCommand(['rev-parse', 'HEAD']),
runGitCommand(['branch', '--show-current']),
runGitCommand(['ls-remote', UPDATE_REMOTE_URL, `refs/heads/${UPDATE_BRANCH}`]),
runGitCommand(['status', '--porcelain']),
])
const localCommit = localStdout.trim()
const branch = branchStdout.trim() || null
const remoteCommit = remoteStdout.trim().split(/\s+/)[0] || null
const worktreeDirty = Boolean(worktreeStdout.trim())
if (!localCommit) {
throw new Error('Could not resolve the current local commit hash.')
}
if (!remoteCommit) {
throw new Error(`Could not resolve the remote ${UPDATE_BRANCH} commit hash.`)
}
if (localCommit === remoteCommit) {
return setUpdateStatus({
state: 'up-to-date',
trigger,
branch,
localCommit,
remoteCommit,
message: 'Heimgeist is already up to date.',
})
}
if (branch && branch !== UPDATE_BRANCH) {
return setUpdateStatus({
state: 'skipped',
trigger,
branch,
localCommit,
remoteCommit,
message: `Update skipped: current branch is "${branch}", expected "${UPDATE_BRANCH}".`,
})
}
if (worktreeDirty) {
return setUpdateStatus({
state: 'skipped',
trigger,
branch: branch || UPDATE_BRANCH,
localCommit,
remoteCommit,
message: 'Update skipped: uncommitted local changes detected.',
})
}
setUpdateStatus({
state: 'updating',
trigger,
branch: branch || UPDATE_BRANCH,
localCommit,
remoteCommit,
message: 'Update found. Pulling latest changes and restarting Heimgeist...',
})
await runGitCommand(['pull', '--ff-only', UPDATE_REMOTE_URL, UPDATE_BRANCH], { timeout: 120000 })
const { stdout: updatedStdout } = await runGitCommand(['rev-parse', 'HEAD'])
const updatedLocalCommit = updatedStdout.trim()
const result = setUpdateStatus({
state: 'updated',
trigger,
branch: branch || UPDATE_BRANCH,
localCommit: updatedLocalCommit || localCommit,
remoteCommit,
message: 'Update installed. Heimgeist restarts now.',
restartScheduled: true,
})
scheduleAppRestart()
return result
} catch (error) {
console.error('Failed to check for updates:', error)
return setUpdateStatus({
state: 'error',
trigger,
message: `Update check failed: ${error.message || String(error)}`,
})
}
}
function checkForUpdates(trigger = 'manual') {
if (!activeUpdateCheck) {
activeUpdateCheck = performUpdateCheck(trigger).finally(() => {
activeUpdateCheck = null
})
}
return activeUpdateCheck
}
async function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1000,
@@ -194,9 +353,14 @@ async function createSettingsWindow() {
}
}
app.whenReady().then(() => {
app.whenReady().then(async () => {
loadSettings()
createMainWindow()
const startupUpdateResult = await checkForUpdates('startup')
if (startupUpdateResult?.restartScheduled) {
return
}
await createMainWindow()
const menuTemplate = [
{
@@ -250,6 +414,8 @@ app.whenReady().then(() => {
})
ipcMain.handle('get-settings', () => appSettings)
ipcMain.handle('get-update-status', () => lastUpdateCheckResult)
ipcMain.handle('check-for-updates', () => checkForUpdates('manual'))
ipcMain.handle('set-setting', (event, key, value) => {
appSettings[key] = key === 'uiScale' ? normalizeUiScale(value) : value

View File

@@ -4,6 +4,8 @@ const { contextBridge, ipcRenderer } = require('electron')
// Expose a secure API to the renderer process
contextBridge.exposeInMainWorld('electronAPI', {
getSettings: () => ipcRenderer.invoke('get-settings'),
getUpdateStatus: () => ipcRenderer.invoke('get-update-status'),
checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
setSetting: (key, value) => ipcRenderer.invoke('set-setting', key, value),
updateSettings: (settings) => ipcRenderer.invoke('update-settings', settings),
pickPaths: () => ipcRenderer.invoke('pick-paths'),