Skip to content

Commit ff90fe6

Browse files
authored
Merge branch 'main' into main
2 parents 85546ff + 30c9622 commit ff90fe6

14 files changed

Lines changed: 450 additions & 11 deletions

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as DesktopWindow from "../window/DesktopWindow.ts";
1717
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
1818
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
1919
import * as DesktopLifecycle from "./DesktopLifecycle.ts";
20+
import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
2021
import * as DesktopObservability from "./DesktopObservability.ts";
2122
import * as DesktopShutdown from "./DesktopShutdown.ts";
2223
import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts";
@@ -220,6 +221,7 @@ const startup = Effect.gen(function* () {
220221
const applicationMenu = yield* DesktopApplicationMenu.DesktopApplicationMenu;
221222
const electronApp = yield* ElectronApp.ElectronApp;
222223
const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
224+
const linuxUrlHandler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
223225
const clerk = yield* DesktopClerk.DesktopClerk;
224226
const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
225227
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
@@ -248,6 +250,7 @@ const startup = Effect.gen(function* () {
248250
yield* appIdentity.configure;
249251
yield* applicationMenu.configure;
250252
yield* updates.configure;
253+
yield* linuxUrlHandler.register;
251254
yield* bootstrap.pipe(Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)));
252255
}).pipe(Effect.withSpan("desktop.startup"));
253256

‎apps/desktop/src/app/DesktopConfig.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const compactEnv = (env: Readonly<Record<string, string | undefined>>): Record<s
3535
export const DesktopConfig = Config.all({
3636
appDataDirectory: trimmedString("APPDATA"),
3737
xdgConfigHome: trimmedString("XDG_CONFIG_HOME"),
38+
xdgDataHome: trimmedString("XDG_DATA_HOME"),
3839
t3Home: trimmedString("T3CODE_HOME"),
3940
devServerUrl: Config.url("VITE_DEV_SERVER_URL").pipe(Config.option),
4041
appUserModelIdOverride: trimmedString("T3CODE_DESKTOP_APP_USER_MODEL_ID"),

‎apps/desktop/src/app/DesktopEnvironment.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class DesktopEnvironment extends Context.Service<
6666
readonly appUserModelId: string;
6767
readonly linuxDesktopEntryName: string;
6868
readonly linuxWmClass: string;
69+
readonly linuxApplicationsDir: string;
70+
readonly appImagePath: Option.Option<string>;
6971
readonly userDataDirName: string;
7072
readonly legacyUserDataDirName: string;
7173
readonly defaultDesktopSettings: DesktopAppSettings.DesktopSettings;
@@ -162,6 +164,10 @@ const make = Effect.fn("desktop.environment.make")(function* (
162164
);
163165
const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
164166
const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
167+
const linuxApplicationsDir = path.join(
168+
Option.getOrElse(config.xdgDataHome, () => path.join(homeDirectory, ".local", "share")),
169+
"applications",
170+
);
165171
const resourcesPath = input.resourcesPath;
166172

167173
return DesktopEnvironment.of({
@@ -205,6 +211,8 @@ const make = Effect.fn("desktop.environment.make")(function* (
205211
),
206212
linuxDesktopEntryName: isDevelopment ? "t3code-dev.desktop" : "t3code.desktop",
207213
linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
214+
linuxApplicationsDir,
215+
appImagePath: config.appImagePath,
208216
userDataDirName,
209217
legacyUserDataDirName,
210218
defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Layer from "effect/Layer";
5+
import * as Option from "effect/Option";
6+
import * as PlatformError from "effect/PlatformError";
7+
import * as Sink from "effect/Sink";
8+
import * as Stream from "effect/Stream";
9+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
10+
11+
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12+
import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
13+
14+
interface RecordedRegistration {
15+
readonly directories: string[];
16+
readonly files: Array<{ readonly path: string; readonly content: string }>;
17+
readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
18+
}
19+
20+
const makeEnvironment = (overrides: Record<string, unknown> = {}) =>
21+
DesktopEnvironment.DesktopEnvironment.of({
22+
platform: "linux",
23+
isPackaged: true,
24+
isDevelopment: false,
25+
displayName: "T3 Code (Alpha)",
26+
linuxWmClass: "t3code",
27+
linuxApplicationsDir: "/home/alice/.local/share/applications",
28+
appImagePath: Option.some("/home/alice/Applications/T3-Code.AppImage"),
29+
path: { join: (...parts: ReadonlyArray<string>) => parts.join("/") },
30+
...overrides,
31+
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
32+
33+
const mockProcess = (exitCode: number) =>
34+
ChildProcessSpawner.makeHandle({
35+
pid: ChildProcessSpawner.ProcessId(1),
36+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
37+
isRunning: Effect.succeed(false),
38+
kill: () => Effect.void,
39+
unref: Effect.succeed(Effect.void),
40+
stdin: Sink.drain,
41+
stdout: Stream.empty,
42+
stderr: Stream.empty,
43+
all: Stream.empty,
44+
getInputFd: () => Sink.drain,
45+
getOutputFd: () => Stream.empty,
46+
});
47+
48+
const makeHandlerLayer = (
49+
recorded: RecordedRegistration,
50+
input: {
51+
readonly environment?: Record<string, unknown>;
52+
readonly xdgMimeExitCode?: number;
53+
readonly writeError?: PlatformError.PlatformError;
54+
} = {},
55+
) =>
56+
DesktopLinuxUrlHandler.layer.pipe(
57+
Layer.provide(
58+
Layer.mergeAll(
59+
Layer.succeed(DesktopEnvironment.DesktopEnvironment, makeEnvironment(input.environment)),
60+
FileSystem.layerNoop({
61+
makeDirectory: (path) =>
62+
Effect.sync(() => {
63+
recorded.directories.push(path);
64+
}),
65+
writeFileString: (path, content) =>
66+
input.writeError
67+
? Effect.fail(input.writeError)
68+
: Effect.sync(() => {
69+
recorded.files.push({ path, content });
70+
}),
71+
}),
72+
Layer.succeed(
73+
ChildProcessSpawner.ChildProcessSpawner,
74+
ChildProcessSpawner.make((command) => {
75+
const childProcess = command as unknown as {
76+
readonly command: string;
77+
readonly args: ReadonlyArray<string>;
78+
};
79+
recorded.commands.push({
80+
command: childProcess.command,
81+
args: childProcess.args,
82+
});
83+
return Effect.succeed(mockProcess(input.xdgMimeExitCode ?? 0));
84+
}),
85+
),
86+
),
87+
),
88+
);
89+
90+
const runRegister = (
91+
recorded: RecordedRegistration,
92+
input: Parameters<typeof makeHandlerLayer>[1] = {},
93+
) =>
94+
Effect.gen(function* () {
95+
const handler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
96+
yield* handler.register;
97+
}).pipe(Effect.provide(makeHandlerLayer(recorded, input)));
98+
99+
const emptyRecording = (): RecordedRegistration => ({
100+
directories: [],
101+
files: [],
102+
commands: [],
103+
});
104+
105+
describe("DesktopLinuxUrlHandler", () => {
106+
it("renders a scheme-handler desktop entry with freedesktop Exec quoting", () => {
107+
const entry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
108+
displayName: "T3 Code (Nightly)",
109+
execTarget: '/home/al ice/Apps/T3 "100%" $HOME\\x.AppImage',
110+
scheme: "t3code",
111+
});
112+
113+
assert.include(entry, "[Desktop Entry]");
114+
assert.include(entry, "Name=T3 Code (Nightly)");
115+
// Exec composes both escaping layers: a literal backslash becomes four
116+
// backslashes in the file, a quote three characters, a dollar sign two
117+
// backslashes plus the sign.
118+
assert.include(
119+
entry,
120+
'Exec="/home/al ice/Apps/T3 \\\\"100%%\\\\" \\\\$HOME\\\\\\\\x.AppImage" %U',
121+
);
122+
assert.include(entry, "NoDisplay=true");
123+
assert.notInclude(entry, "StartupWMClass=");
124+
assert.include(entry, "MimeType=x-scheme-handler/t3code;");
125+
});
126+
127+
it("carries structured context on registration errors", () => {
128+
const writeError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
129+
step: "write-desktop-entry",
130+
scheme: "t3code",
131+
desktopEntryPath: "/home/alice/.local/share/applications/t3code-url-handler.desktop",
132+
cause: new Error("boom"),
133+
});
134+
assert.equal(
135+
writeError.message,
136+
"Failed to register the t3code:// URL handler (step: write-desktop-entry).",
137+
);
138+
assert.equal(
139+
writeError.desktopEntryPath,
140+
"/home/alice/.local/share/applications/t3code-url-handler.desktop",
141+
);
142+
143+
const exitError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
144+
step: "set-default-handler",
145+
scheme: "t3code",
146+
exitCode: 4,
147+
});
148+
assert.equal(
149+
exitError.message,
150+
"Failed to register the t3code:// URL handler (step: set-default-handler, xdg-mime exit code 4).",
151+
);
152+
});
153+
154+
it.effect("writes the handler entry and claims the scheme default via xdg-mime", () => {
155+
const recorded = emptyRecording();
156+
157+
return Effect.gen(function* () {
158+
yield* runRegister(recorded);
159+
160+
assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
161+
assert.equal(recorded.files.length, 1);
162+
assert.equal(
163+
recorded.files[0]?.path,
164+
"/home/alice/.local/share/applications/t3code-url-handler.desktop",
165+
);
166+
assert.include(
167+
recorded.files[0]?.content,
168+
'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
169+
);
170+
assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
171+
assert.deepEqual(recorded.commands, [
172+
{
173+
command: "xdg-mime",
174+
args: ["default", "t3code-url-handler.desktop", "x-scheme-handler/t3code"],
175+
},
176+
]);
177+
});
178+
});
179+
180+
it.effect("falls back to the process executable outside an AppImage", () => {
181+
const recorded = emptyRecording();
182+
183+
return Effect.gen(function* () {
184+
yield* runRegister(recorded, { environment: { appImagePath: Option.none() } });
185+
186+
assert.include(
187+
recorded.files[0]?.content,
188+
`Exec=${DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument(process.execPath)} %U`,
189+
);
190+
});
191+
});
192+
193+
it.effect("does nothing on other platforms or unpackaged builds", () => {
194+
const nonLinux = emptyRecording();
195+
const unpackaged = emptyRecording();
196+
197+
return Effect.gen(function* () {
198+
yield* runRegister(nonLinux, { environment: { platform: "darwin" } });
199+
yield* runRegister(unpackaged, { environment: { isPackaged: false } });
200+
201+
for (const recorded of [nonLinux, unpackaged]) {
202+
assert.deepEqual(recorded.directories, []);
203+
assert.deepEqual(recorded.files, []);
204+
assert.deepEqual(recorded.commands, []);
205+
}
206+
});
207+
});
208+
209+
it.effect("never fails startup when registration cannot complete", () => {
210+
const xdgMimeFailed = emptyRecording();
211+
const writeFailed = emptyRecording();
212+
213+
return Effect.gen(function* () {
214+
yield* runRegister(xdgMimeFailed, { xdgMimeExitCode: 1 });
215+
yield* runRegister(writeFailed, {
216+
writeError: PlatformError.systemError({
217+
_tag: "PermissionDenied",
218+
module: "FileSystem",
219+
method: "writeFileString",
220+
description: "read-only filesystem",
221+
pathOrDescriptor: "/home/alice/.local/share/applications/t3code-url-handler.desktop",
222+
}),
223+
});
224+
225+
assert.equal(xdgMimeFailed.files.length, 1);
226+
assert.deepEqual(writeFailed.commands, []);
227+
});
228+
});
229+
});

0 commit comments

Comments
 (0)