aboutsummaryrefslogtreecommitdiff
path: root/src/main/updater
diff options
context:
space:
mode:
authorV <vendicated@riseup.net>2023-04-04 01:16:29 +0200
committerGitHub <noreply@github.com>2023-04-04 01:16:29 +0200
commit6b26c12bfa1f28d40834478b50d2f7b09c9f54fb (patch)
tree48687349f9bf6803ee8993b3eafa3ea1e4f3dff2 /src/main/updater
parent5bb08bdb6465eebc9dcf80ba2971d894b804abb8 (diff)
downloadVencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.tar.gz
Vencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.tar.bz2
Vencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.zip
Add additional build flavours for Vencord Desktop (#765)
Diffstat (limited to 'src/main/updater')
-rw-r--r--src/main/updater/common.ts59
-rw-r--r--src/main/updater/git.ts83
-rw-r--r--src/main/updater/http.ts104
-rw-r--r--src/main/updater/index.ts19
4 files changed, 265 insertions, 0 deletions
diff --git a/src/main/updater/common.ts b/src/main/updater/common.ts
new file mode 100644
index 0000000..3729c6d
--- /dev/null
+++ b/src/main/updater/common.ts
@@ -0,0 +1,59 @@
+/*
+ * Vencord, a modification for Discord's desktop app
+ * Copyright (c) 2022 Vendicated and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+import { createHash } from "crypto";
+import { createReadStream } from "fs";
+import { join } from "path";
+
+export async function calculateHashes() {
+ const hashes = {} as Record<string, string>;
+
+ await Promise.all(
+ ["patcher.js", "preload.js", "renderer.js", "renderer.css"].map(file => new Promise<void>(r => {
+ const fis = createReadStream(join(__dirname, file));
+ const hash = createHash("sha1", { encoding: "hex" });
+ fis.once("end", () => {
+ hash.end();
+ hashes[file] = hash.read();
+ r();
+ });
+ fis.pipe(hash);
+ }))
+ );
+
+ return hashes;
+}
+
+export function serializeErrors(func: (...args: any[]) => any) {
+ return async function () {
+ try {
+ return {
+ ok: true,
+ value: await func(...arguments)
+ };
+ } catch (e: any) {
+ return {
+ ok: false,
+ error: e instanceof Error ? {
+ // prototypes get lost, so turn error into plain object
+ ...e
+ } : e
+ };
+ }
+ };
+}
diff --git a/src/main/updater/git.ts b/src/main/updater/git.ts
new file mode 100644
index 0000000..89c2d3c
--- /dev/null
+++ b/src/main/updater/git.ts
@@ -0,0 +1,83 @@
+/*
+ * Vencord, a modification for Discord's desktop app
+ * Copyright (c) 2022 Vendicated and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+import IpcEvents from "@utils/IpcEvents";
+import { execFile as cpExecFile } from "child_process";
+import { ipcMain } from "electron";
+import { join } from "path";
+import { promisify } from "util";
+
+import { calculateHashes, serializeErrors } from "./common";
+
+const VENCORD_SRC_DIR = join(__dirname, "..");
+
+const execFile = promisify(cpExecFile);
+
+const isFlatpak = process.platform === "linux" && Boolean(process.env.FLATPAK_ID?.includes("discordapp") || process.env.FLATPAK_ID?.includes("Discord"));
+
+if (process.platform === "darwin") process.env.PATH = `/usr/local/bin:${process.env.PATH}`;
+
+function git(...args: string[]) {
+ const opts = { cwd: VENCORD_SRC_DIR };
+
+ if (isFlatpak) return execFile("flatpak-spawn", ["--host", "git", ...args], opts);
+ else return execFile("git", args, opts);
+}
+
+async function getRepo() {
+ const res = await git("remote", "get-url", "origin");
+ return res.stdout.trim()
+ .replace(/git@(.+):/, "https://$1/")
+ .replace(/\.git$/, "");
+}
+
+async function calculateGitChanges() {
+ await git("fetch");
+
+ const res = await git("log", "HEAD...origin/main", "--pretty=format:%an/%h/%s");
+
+ const commits = res.stdout.trim();
+ return commits ? commits.split("\n").map(line => {
+ const [author, hash, ...rest] = line.split("/");
+ return {
+ hash, author, message: rest.join("/")
+ };
+ }) : [];
+}
+
+async function pull() {
+ const res = await git("pull");
+ return res.stdout.includes("Fast-forward");
+}
+
+async function build() {
+ const opts = { cwd: VENCORD_SRC_DIR };
+
+ const command = isFlatpak ? "flatpak-spawn" : "node";
+ const args = isFlatpak ? ["--host", "node", "scripts/build/build.mjs"] : ["scripts/build/build.mjs"];
+
+ const res = await execFile(command, args, opts);
+
+ return !res.stderr.includes("Build failed");
+}
+
+ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes));
+ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(getRepo));
+ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges));
+ipcMain.handle(IpcEvents.UPDATE, serializeErrors(pull));
+ipcMain.handle(IpcEvents.BUILD, serializeErrors(build));
diff --git a/src/main/updater/http.ts b/src/main/updater/http.ts
new file mode 100644
index 0000000..a2e2aad
--- /dev/null
+++ b/src/main/updater/http.ts
@@ -0,0 +1,104 @@
+/*
+ * Vencord, a modification for Discord's desktop app
+ * Copyright (c) 2022 Vendicated and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+import { VENCORD_USER_AGENT } from "@utils/constants";
+import IpcEvents from "@utils/IpcEvents";
+import { ipcMain } from "electron";
+import { writeFile } from "fs/promises";
+import { join } from "path";
+
+import gitHash from "~git-hash";
+import gitRemote from "~git-remote";
+
+import { get } from "../utils/simpleGet";
+import { calculateHashes, serializeErrors } from "./common";
+
+const API_BASE = `https://api.github.com/repos/${gitRemote}`;
+let PendingUpdates = [] as [string, string][];
+
+async function githubGet(endpoint: string) {
+ return get(API_BASE + endpoint, {
+ headers: {
+ Accept: "application/vnd.github+json",
+ // "All API requests MUST include a valid User-Agent header.
+ // Requests with no User-Agent header will be rejected."
+ "User-Agent": VENCORD_USER_AGENT
+ }
+ });
+}
+
+async function calculateGitChanges() {
+ const isOutdated = await fetchUpdates();
+ if (!isOutdated) return [];
+
+ const res = await githubGet(`/compare/${gitHash}...HEAD`);
+
+ const data = JSON.parse(res.toString("utf-8"));
+ return data.commits.map((c: any) => ({
+ // github api only sends the long sha
+ hash: c.sha.slice(0, 7),
+ author: c.author.login,
+ message: c.commit.message
+ }));
+}
+
+const FILES_TO_DOWNLOAD = [
+ IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js",
+ "preload.js",
+ IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js",
+ "renderer.css"
+];
+
+async function fetchUpdates() {
+ const release = await githubGet("/releases/latest");
+
+ const data = JSON.parse(release.toString());
+ const hash = data.name.slice(data.name.lastIndexOf(" ") + 1);
+ if (hash === gitHash)
+ return false;
+
+ data.assets.forEach(({ name, browser_download_url }) => {
+ if (FILES_TO_DOWNLOAD.some(s => name.startsWith(s))) {
+ PendingUpdates.push([name, browser_download_url]);
+ }
+ });
+ return true;
+}
+
+async function applyUpdates() {
+ await Promise.all(PendingUpdates.map(
+ async ([name, data]) => writeFile(
+ join(
+ __dirname,
+ IS_VENCORD_DESKTOP
+ // vencordDesktopRenderer.js -> renderer.js
+ ? name.replace(/vencordDesktop(\w)/, (_, c) => c.toLowerCase())
+ : name
+ ),
+ await get(data)
+ )
+ ));
+ PendingUpdates = [];
+ return true;
+}
+
+ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes));
+ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(() => `https://github.com/${gitRemote}`));
+ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges));
+ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates));
+ipcMain.handle(IpcEvents.BUILD, serializeErrors(applyUpdates));
diff --git a/src/main/updater/index.ts b/src/main/updater/index.ts
new file mode 100644
index 0000000..7036112
--- /dev/null
+++ b/src/main/updater/index.ts
@@ -0,0 +1,19 @@
+/*
+ * Vencord, a modification for Discord's desktop app
+ * Copyright (c) 2022 Vendicated and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+import(IS_STANDALONE ? "./http" : "./git");