Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopAppIdentity.ts

155 lines5.4 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Schema from "effect/Schema";
8​
9import * as ElectronApp from "../electron/ElectronApp.ts";
10import * as DesktopAssets from "./DesktopAssets.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12​
13const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,40}$/i;
14const COMMIT_HASH_DISPLAY_LENGTH = 12;
15​
16const AppPackageMetadata = Schema.Struct({
17 t3codeCommitHash: Schema.optional(Schema.String),
18});
19const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata));
20​
21export class DesktopUserDataPathResolutionError extends Schema.TaggedErrorClass<DesktopUserDataPathResolutionError>()(
22 "DesktopUserDataPathResolutionError",
23 {
24 legacyPath: Schema.String,
25 cause: Schema.Defect(),
26 },
27) {
28 override get message(): string {
29 return `Failed to inspect legacy desktop user-data path at "${this.legacyPath}".`;
30 }
32​
33export class DesktopAppIdentity extends Context.Service<
34 DesktopAppIdentity,
35 {
36 readonly resolveUserDataPath: Effect.Effect<string, DesktopUserDataPathResolutionError>;
37 readonly configure: Effect.Effect<void>;
38 }
39>()("@t3tools/desktop/app/DesktopAppIdentity") {}
40​
41const normalizeCommitHash = (value: string): Option.Option<string> => {
42 const trimmed = value.trim();
43 return COMMIT_HASH_PATTERN.test(trimmed)
44 ? Option.some(trimmed.slice(0, COMMIT_HASH_DISPLAY_LENGTH).toLowerCase())
45 : Option.none();
46};
47​
48export const resolveUserDataPath = Effect.gen(function* () {
49 const environment = yield* DesktopEnvironment.DesktopEnvironment;
50 const fileSystem = yield* FileSystem.FileSystem;
51 const legacyPath = environment.path.join(
52 environment.appDataDirectory,
53 environment.legacyUserDataDirName,
54 );
55 const legacyPathExists = yield* fileSystem.exists(legacyPath).pipe(
56 Effect.mapError(
57 (cause) =>
58 new DesktopUserDataPathResolutionError({
59 legacyPath,
60 cause,
61 }),
62 ),
63 );
64 return legacyPathExists
65 ? legacyPath
66 : environment.path.join(environment.appDataDirectory, environment.userDataDirName);
67}).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath"));
68​
69export const make = Effect.gen(function* () {
70 const assets = yield* DesktopAssets.DesktopAssets;
71 const electronApp = yield* ElectronApp.ElectronApp;
72 const environment = yield* DesktopEnvironment.DesktopEnvironment;
73 const fileSystem = yield* FileSystem.FileSystem;
74 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
75​
76 const resolveEmbeddedCommitHash = Effect.gen(function* () {
77 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
78 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
79 return yield* Option.match(raw, {
80 onNone: () => Effect.succeed(Option.none<string>()),
81 onSome: (value) =>
82 decodeAppPackageMetadata(value).pipe(
83 Effect.map((parsed) =>
84 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
85 ),
86 Effect.orElseSucceed(() => Option.none<string>()),
87 ),
88 });
89 });
90​
91 const resolveAboutCommitHash = Effect.gen(function* () {
92 const cached = yield* Ref.get(commitHashCache);
93 if (Option.isSome(cached)) {
94 return cached.value;
95 }
96​
97 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
98 if (Option.isSome(override)) {
99 yield* Ref.set(commitHashCache, Option.some(override));
100 return override;
101 }
102​
103 if (!environment.isPackaged) {
104 const empty = Option.none<string>();
105 yield* Ref.set(commitHashCache, Option.some(empty));
106 return empty;
107 }
108​
109 const commitHash = yield* resolveEmbeddedCommitHash;
110 yield* Ref.set(commitHashCache, Option.some(commitHash));
111 return commitHash;
112 });
113​
114 const userDataPath = resolveUserDataPath.pipe(
115 Effect.provide(
116 yield* Effect.context<DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem>(),
117 ),
118 );
119​
120 const configure = Effect.gen(function* () {
121 const commitHash = yield* resolveAboutCommitHash;
122 yield* electronApp.setName(environment.displayName);
123 yield* electronApp.setAboutPanelOptions({
124 applicationName: environment.displayName,
125 applicationVersion: environment.appVersion,
126 version: Option.getOrElse(commitHash, () => "unknown"),
127 });
128​
129 if (environment.platform === "win32") {
130 yield* electronApp.setAppUserModelId(environment.appUserModelId);
131 }
132​
133 if (environment.platform === "linux") {
134 yield* electronApp.setDesktopName(environment.linuxDesktopEntryName);
135 }
136​
137 // Unpackaged runs only. A packaged bundle already carries its icon in
138 // Info.plist, so setting the dock tile again changes nothing except to
139 // overwrite a custom icon the user attached to the app themselves.
140 if (environment.platform === "darwin" && !environment.isPackaged) {
141 const iconPaths = yield* assets.iconPaths;
142 yield* Option.match(iconPaths.png, {
143 onNone: () => Effect.void,
144 onSome: electronApp.setDockIcon,
145 });
146 }
147 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
148​
149 return DesktopAppIdentity.of({
150 resolveUserDataPath: userDataPath,
151 configure,
152 });
153});
154​
155export const layer = Layer.effect(DesktopAppIdentity, make);