Release / release (push) Successful in 14s
A Sibling projects menu item, shown when any are installed, says which have a newer version (from `sibling check --porcelain`, run with each update check). It opens a dialog listing each sibling's version and state, with buttons to run `sibling update` for one or all of them in a terminal. A newer sibling also counts as an available update for the Update pill, the header and the top bar dot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
253 lines
9.6 KiB
JavaScript
253 lines
9.6 KiB
JavaScript
// Talking to CoreDesktop's command line tools: running them in the background
|
|
// to read their output, or in a terminal window for the user to follow.
|
|
|
|
import Gio from 'gi://Gio';
|
|
import GLib from 'gi://GLib';
|
|
|
|
Gio._promisify(Gio.Subprocess.prototype, 'communicate_utf8_async');
|
|
Gio._promisify(Gio.File.prototype, 'load_contents_async');
|
|
Gio._promisify(Gio.File.prototype, 'enumerate_children_async');
|
|
Gio._promisify(Gio.FileEnumerator.prototype, 'next_files_async');
|
|
|
|
const HOME = GLib.get_home_dir();
|
|
export const COREDESKTOP = GLib.build_filenamev([HOME, '.local', 'share', 'coredesktop']);
|
|
export const BIN = GLib.build_filenamev([HOME, '.local', 'bin']);
|
|
export const STATE = GLib.build_filenamev([HOME, '.local', 'state', 'coredesktop']);
|
|
export const LOGS = GLib.build_filenamev([STATE, 'logs']);
|
|
export const REBOOT_NEEDED = GLib.build_filenamev([STATE, 'reboot-needed']);
|
|
export const PACMAN_UPDATE_LOG = GLib.build_filenamev([STATE, 'pacman-update.log']);
|
|
export const SIBLINGS = GLib.build_filenamev([HOME, '.local', 'share', 'siblings']);
|
|
export const STATUS_DIR = GLib.build_filenamev([GLib.get_user_runtime_dir(), 'coredesktop-extension']);
|
|
|
|
// Terminals and how each runs a command, in the order CoreDesktop's
|
|
// system/run-in-terminal tries them
|
|
export const TERMINALS = {
|
|
'xdg-terminal-exec': [],
|
|
'kgx': ['--'],
|
|
'ghostty': ['-e'],
|
|
'kitty': [],
|
|
'ptyxis': ['--new-window', '--'],
|
|
'gnome-terminal': ['--'],
|
|
'alacritty': ['-e'],
|
|
};
|
|
|
|
export function stripAnsi(text) {
|
|
// eslint-disable-next-line no-control-regex
|
|
return text.replace(/\x1b\[[0-9;?]*[ -/]*[@-~]/g, '').replace(/\r/g, '');
|
|
}
|
|
|
|
export function bin(name) {
|
|
return GLib.build_filenamev([BIN, name]);
|
|
}
|
|
|
|
export function isInstalled() {
|
|
return GLib.file_test(GLib.build_filenamev([COREDESKTOP, '.git']), GLib.FileTest.EXISTS);
|
|
}
|
|
|
|
// Run argv in the background. Resolves to {status, stdout, stderr}.
|
|
export async function run(argv, cancellable = null) {
|
|
const launcher = new Gio.SubprocessLauncher({
|
|
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
|
|
});
|
|
launcher.setenv('PATH', `${BIN}:${GLib.getenv('PATH') ?? '/usr/bin'}`, true);
|
|
// Commands such as version show a spinner and ask questions only on a
|
|
// terminal; make sure nothing thinks there's one
|
|
launcher.unsetenv('COREDESKTOP_TTY');
|
|
const proc = launcher.spawnv(argv);
|
|
const [stdout, stderr] = await proc.communicate_utf8_async(null, cancellable);
|
|
return {status: proc.get_exit_status(), stdout: stdout ?? '', stderr: stderr ?? ''};
|
|
}
|
|
|
|
// The version this machine is on, e.g. 2026.11.26 (01d2432)
|
|
export async function localVersion() {
|
|
try {
|
|
const {status, stdout} = await run(['git', '-C', COREDESKTOP, 'log', '-1',
|
|
'--format=%cd (%h)', '--date=format:%Y.%m.%d']);
|
|
return status === 0 ? stdout.trim() : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// version --check: {state: 'available', version} | {state: 'current'} | {state: 'unknown'}
|
|
export async function checkForUpdate(cancellable) {
|
|
try {
|
|
const {status, stdout} = await run([bin('version'), '--check'], cancellable);
|
|
if (status === 0 && stdout.trim())
|
|
return {state: 'available', version: stdout.trim()};
|
|
if (status === 1)
|
|
return {state: 'current'};
|
|
} catch (e) {
|
|
if (e.matches?.(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
|
|
throw e;
|
|
}
|
|
return {state: 'unknown'};
|
|
}
|
|
|
|
// Names of the sibling projects installed with `sibling`, alphabetically. Read
|
|
// from disk, so it's quick and needs no network.
|
|
export async function installedSiblings() {
|
|
const names = [];
|
|
for (const file of await listFiles(Gio.File.new_for_path(SIBLINGS))) {
|
|
if (GLib.file_test(GLib.build_filenamev([file.get_path(), '.git']), GLib.FileTest.IS_DIR))
|
|
names.push(file.get_basename());
|
|
}
|
|
return names.sort();
|
|
}
|
|
|
|
// sibling check --porcelain, which fetches each sibling's remote:
|
|
// [{name, installed, modified, state: 'available' | 'current' | 'unknown', available}]
|
|
// Throws if sibling itself couldn't run.
|
|
export async function checkSiblings(cancellable) {
|
|
const {status, stdout, stderr} = await run([bin('sibling'), 'check', '--porcelain'], cancellable);
|
|
if (status !== 0)
|
|
throw new Error(stripAnsi(stderr || stdout).trim() || `sibling exited with status ${status}`);
|
|
const siblings = [];
|
|
for (const line of stdout.split('\n')) {
|
|
const [name, installedField, available = ''] = line.split('\t');
|
|
if (!name || installedField === undefined)
|
|
continue;
|
|
const modified = installedField.endsWith(', modified');
|
|
let state = 'available';
|
|
if (available === '')
|
|
state = 'current';
|
|
else if (available === '?')
|
|
state = 'unknown';
|
|
siblings.push({
|
|
name,
|
|
installed: modified ? installedField.slice(0, -', modified'.length) : installedField,
|
|
modified,
|
|
state,
|
|
available: state === 'available' ? available : null,
|
|
});
|
|
}
|
|
return siblings;
|
|
}
|
|
|
|
// configure --list, with each item's description from its configure.d file:
|
|
// [{name, title, description, done}]
|
|
export async function configureItems() {
|
|
const {status, stdout} = await run([bin('configure'), '--list']);
|
|
if (status !== 0)
|
|
return [];
|
|
const descriptions = await configureDescriptions();
|
|
const items = [];
|
|
for (const line of stdout.split('\n')) {
|
|
const m = line.match(/^(done|to do)\s+(\S+)\s+(.*)$/);
|
|
if (!m)
|
|
continue;
|
|
items.push({
|
|
done: m[1] === 'done',
|
|
name: m[2],
|
|
title: m[3].trim(),
|
|
description: descriptions.get(m[2]) ?? '',
|
|
});
|
|
}
|
|
return items;
|
|
}
|
|
|
|
async function configureDescriptions() {
|
|
const result = new Map();
|
|
const dir = Gio.File.new_for_path(GLib.build_filenamev([COREDESKTOP, 'configure.d']));
|
|
for (const file of await listFiles(dir)) {
|
|
const base = file.get_basename();
|
|
if (!base.endsWith('.sh'))
|
|
continue;
|
|
const name = base.replace(/\.sh$/, '').replace(/^\d\d-/, '');
|
|
try {
|
|
const [bytes] = await file.load_contents_async(null);
|
|
const text = new TextDecoder().decode(bytes);
|
|
const m = text.match(/^DESCRIPTION=(["'])(.*)\1\s*$/m);
|
|
if (m)
|
|
result.set(name, m[2]);
|
|
} catch {
|
|
// An unreadable item just goes without a description
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export async function listFiles(dir) {
|
|
const files = [];
|
|
try {
|
|
const enumerator = await dir.enumerate_children_async('standard::name,time::modified',
|
|
Gio.FileQueryInfoFlags.NONE, GLib.PRIORITY_DEFAULT, null);
|
|
for (;;) {
|
|
const infos = await enumerator.next_files_async(50, GLib.PRIORITY_DEFAULT, null);
|
|
if (infos.length === 0)
|
|
break;
|
|
for (const info of infos) {
|
|
const file = dir.get_child(info.get_name());
|
|
file._mtime = info.get_modification_date_time()?.to_unix() ?? 0;
|
|
files.push(file);
|
|
}
|
|
}
|
|
} catch {
|
|
// A missing directory has no files
|
|
}
|
|
return files;
|
|
}
|
|
|
|
// The newest update or install log, or null
|
|
export async function newestLog() {
|
|
const files = (await listFiles(Gio.File.new_for_path(LOGS)))
|
|
.filter(f => f.get_basename().endsWith('.log'));
|
|
files.sort((a, b) => b._mtime - a._mtime);
|
|
return files[0] ?? null;
|
|
}
|
|
|
|
// Seconds since the epoch the file was last modified, or null
|
|
export function mtime(path) {
|
|
try {
|
|
const info = Gio.File.new_for_path(path).query_info('time::modified',
|
|
Gio.FileQueryInfoFlags.NONE, null);
|
|
return info.get_modification_date_time()?.to_unix() ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function findTerminal(preference) {
|
|
if (preference && preference !== 'auto') {
|
|
if (GLib.find_program_in_path(preference))
|
|
return [preference, ...(TERMINALS[preference] ?? ['-e'])];
|
|
}
|
|
for (const [name, args] of Object.entries(TERMINALS)) {
|
|
if (GLib.find_program_in_path(name))
|
|
return [name, ...args];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Open a terminal window running `argv` under run-tracked, which records its
|
|
// progress as <name>.status in STATUS_DIR. The terminal runs as its own systemd
|
|
// unit so it outlives a GNOME Shell restart. Returns false if there's no terminal.
|
|
export function runInTerminal(extensionPath, preference, name, argv) {
|
|
const terminal = findTerminal(preference);
|
|
if (!terminal)
|
|
return false;
|
|
const tracked = GLib.build_filenamev([extensionPath, 'bin', 'run-tracked']);
|
|
const command = ['systemd-run', '--user', '--quiet', '--collect',
|
|
// A sibling's name can hold characters a unit name can't
|
|
`--unit=coredesktop-${name.replace(/[^A-Za-z0-9:_.-]/g, '_')}-${Date.now()}`, '--',
|
|
...terminal, 'bash', tracked, name, ...argv];
|
|
GLib.mkdir_with_parents(STATUS_DIR, 0o700);
|
|
Gio.Subprocess.new(command, Gio.SubprocessFlags.NONE);
|
|
return true;
|
|
}
|
|
|
|
// "just now", "5 minutes ago", "3 hours ago", "2 days ago"
|
|
export function timeAgo(unixSeconds) {
|
|
if (!unixSeconds)
|
|
return null;
|
|
const secs = Math.max(0, GLib.get_real_time() / 1e6 - unixSeconds);
|
|
const plural = (n, unit) => `${n} ${unit}${n === 1 ? '' : 's'} ago`;
|
|
if (secs < 60)
|
|
return 'just now';
|
|
if (secs < 3600)
|
|
return plural(Math.floor(secs / 60), 'minute');
|
|
if (secs < 86400)
|
|
return plural(Math.floor(secs / 3600), 'hour');
|
|
return plural(Math.floor(secs / 86400), 'day');
|
|
}
|