Skip to content

Commit

Permalink
start daemon from gui
Browse files Browse the repository at this point in the history
  • Loading branch information
arvid220u committed Apr 10, 2022
1 parent 2a66de3 commit 6909c1f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"clean-build": "ts-node ./helpers/scripts/clean.js dist && rm -rf ./release/build",
"package-prepare-mac-arm64": "npm run build-binaries-mac-arm64 && npm run clean-build && npm run build",
"package-prepare-mac-x86_64": "npm run build-binaries-mac-x86_64 && npm run clean-build && npm run build",
"general-prepackage": "sed -i -e \"s/RELEASE_COMMIT_HASH.*/RELEASE_COMMIT_HASH = \\\"$(git rev-parse HEAD)\\\";/g\" ../daemon/constants.hpp",
"general-prepackage": "sed -i -e \"s/RELEASE_COMMIT_HASH.*/RELEASE_COMMIT_HASH = \\\"$(git rev-parse HEAD)\\\";/g\" ../daemon/constants.hpp src/main/constants.ts",
"package-mac-arm64": "npm run general-prepackage && npm run package-prepare-mac-arm64 && MAC_ARCH=arm64 ts-node ./helpers/scripts/package-mac.ts",
"package-mac-x86_64": "npm run general-prepackage && npm run package-prepare-mac-x86_64 && MAC_ARCH=x64 ts-node ./helpers/scripts/package-mac.ts",
"postinstall": "ts-node helpers/scripts/check-native-dep.js && electron-builder install-app-deps && cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./helpers/configs/webpack.config.renderer.dev.dll.ts",
Expand Down
41 changes: 41 additions & 0 deletions gui/src/main/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { systemPreferences } from "electron";
import { exit } from "process";
import path from "path";

// this commit hash will be automatically updated by gui/package.json.
export const RELEASE_COMMIT_HASH = "a942a89de170f21f554c2910fafc7b1b4895ab2b";

export const PLIST_PATH = () => {
if (process.platform === "darwin" && process.env.HOME) {
return path.join(
process.env.HOME,
"Library",
"LaunchAgents",
"co.anysphere.anysphered.plist"
);
} else {
process.stderr.write("Platform not supported");
exit(1);
}
};

export const PLIST_CONTENTS = (pkgPath: string, logPath: string) => {
return `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>co.anysphere.anysphered</string>
<key>Program</key>
<string>${pkgPath}/Resources/anysphered</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>${logPath}/anysphered.log</string>
<key>StandardErrorPath</key>
<string>${logPath}/anysphered.err</string>
</dict>
</plist>`;
};
63 changes: 60 additions & 3 deletions gui/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,79 @@

import path from "path";
import { app, BrowserWindow, session, shell, Notification } from "electron";
import { promisify } from "util";
import MenuBuilder from "./menu";
import { resolveHtmlPath } from "./util";
import { exec as execNonPromisified } from "child_process";
const exec = promisify(execNonPromisified);

import {
getDaemonClient,
convertProtobufIncomingMessageToTypedMessage,
truncate,
} from "./daemon";
import daemonM from "../daemon/schema/daemon_pb";
import { PLIST_CONTENTS, PLIST_PATH, RELEASE_COMMIT_HASH } from "./constants";
import { exit } from "process";
import fs from "fs";
const daemonClient = getDaemonClient();

const isDevelopment =
process.env.NODE_ENV === "development" || process.env.DEBUG_PROD === "true";

if (process.env.NODE_ENV === "production") {
const sourceMapSupport = require("source-map-support");
sourceMapSupport.install();
}

const isDevelopment =
process.env.NODE_ENV === "development" || process.env.DEBUG_PROD === "true";
const startDaemonIfNeeded = async (pkgPath: string) => {
const request = new daemonM.GetStatusRequest();
const getStatus = promisify(daemonClient.getStatus).bind(daemonClient);
try {
const response = (await getStatus(request)) as daemonM.GetStatusResponse;
// if release hash is wrong, we need to restart!
if (response.getReleaseHash() !== RELEASE_COMMIT_HASH) {
throw new Error("incorrect release hash");
}
// daemon is running, correct version, nothing to do
return;
} catch (e) {
// if development, we don't want to start the daemon (want to do it manually)
if (isDevelopment) {
process.stdout.write(
`Daemon is either not running or running the wrong version. Please start it; we're not doing anything because we're in DEV mode. Error: ${e}.`
);
return;
}

// TODO(arvid): handle windows and linux too
// possible problem, so let's start the daemon!
// unload the plist if it exists.
const plist_path = PLIST_PATH();
// 1: unload plist
await exec("launchctl unload " + plist_path); // we don't care if it fails or not!
let logPath = "";
if (process.env.XDG_CACHE_HOME) {
logPath = path.join(process.env.XDG_CACHE_HOME, "anysphere", "logs");
} else if (process.env.HOME) {
logPath = path.join(process.env.HOME, ".anysphere", "cache", "logs");
} else {
process.stderr.write(
"$HOME or $XDG_CACHE_HOME not set! Cannot create daemon, aborting :("
);
exit(1);
}
const contents = PLIST_CONTENTS(pkgPath, logPath);
// 2: write plist
await fs.promises.writeFile(plist_path, contents);
// 3: load plist
const response = await exec("launchctl load " + plist_path);
if (response.stderr) {
process.stderr.write(response.stderr);
exit(1);
}
}
};

const installExtensions = async () => {
const installer = require("electron-devtools-installer");
Expand Down Expand Up @@ -110,7 +166,6 @@ app.on("window-all-closed", () => {
});

function registerForNotifications() {
const daemonClient = getDaemonClient();
const request = new daemonM.GetMessagesRequest();
request.setFilter(daemonM.GetMessagesRequest.Filter.NEW);
var call = daemonClient.getMessagesStreamed(request);
Expand Down Expand Up @@ -161,6 +216,8 @@ function registerForNotifications() {
app
.whenReady()
.then(() => {
startDaemonIfNeeded(app.getAppPath());

createWindow();
app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
Expand Down

0 comments on commit 6909c1f

Please sign in to comment.