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

DesktopAppIdentity.ts

152 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.TaggedError<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​
69/** @public Service construction is part of the canonical Effect module API. */
70export const make = Effect.gen(function* () {
71 const assets = yield* DesktopAssets.DesktopAssets;
72 const electronApp = yield* ElectronApp.ElectronApp;
73 const environment = yield* DesktopEnvironment.DesktopEnvironment;
74 const fileSystem = yield* FileSystem.FileSystem;
75 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
76​
77 const resolveEmbeddedCommitHash = Effect.gen(function* () {
78 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
79 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
80 return yield* Option.match(raw, {
81 onNone: () => Effect.succeed(Option.none<string>()),
82 onSome: (value) =>
83 decodeAppPackageMetadata(value).pipe(
84 Effect.map((parsed) =>
85 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
86 ),
87 Effect.orElseSucceed(() => Option.none<string>()),
88 ),
89 });
90 });
91​
92 const resolveAboutCommitHash = Effect.gen(function* () {
93 const cached = yield* Ref.get(commitHashCache);
94 if (Option.isSome(cached)) {
95 return cached.value;
96 }
97​
98 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
99 if (Option.isSome(override)) {
100 yield* Ref.set(commitHashCache, Option.some(override));
101 return override;
102 }
103​
104 if (!environment.isPackaged) {
105 const empty = Option.none<string>();
106 yield* Ref.set(commitHashCache, Option.some(empty));
107 return empty;
108 }
109​
110 const commitHash = yield* resolveEmbeddedCommitHash;
111 yield* Ref.set(commitHashCache, Option.some(commitHash));
112 return commitHash;
113 });
114​
115 const userDataPath = resolveUserDataPath.pipe(
116 Effect.provide(
117 yield* Effect.context<DesktopEnvironment.DesktopEnvironment | FileSystem.FileSystem>(),
118 ),
119 );
120​
121 const configure = Effect.gen(function* () {
122 const commitHash = yield* resolveAboutCommitHash;
123 yield* electronApp.setName(environment.displayName);
124 yield* electronApp.setAboutPanelOptions({
125 applicationName: environment.displayName,
126 applicationVersion: environment.appVersion,
127 version: Option.getOrElse(commitHash, () => "unknown"),
128 });
129​
130 if (environment.platform === "win32") {
131 yield* electronApp.setAppUserModelId(environment.appUserModelId);
132 }
133​
134 // Unpackaged runs only. A packaged bundle already carries its icon in
135 // Info.plist, so setting the dock tile again changes nothing except to
136 // overwrite a custom icon the user attached to the app themselves.
137 if (environment.platform === "darwin" && !environment.isPackaged) {
138 const iconPaths = yield* assets.iconPaths;
139 yield* Option.match(iconPaths.png, {
140 onNone: () => Effect.void,
141 onSome: electronApp.setDockIcon,
142 });
143 }
144 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
145​
146 return DesktopAppIdentity.of({
147 resolveUserDataPath: userDataPath,
148 configure,
149 });
150});
151​
152export const layer = Layer.effect(DesktopAppIdentity, make);