Open History to see the last commit
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 }
31}
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 make = Effect.gen(function* () {
49 const assets = yield* DesktopAssets.DesktopAssets;
50 const electronApp = yield* ElectronApp.ElectronApp;
51 const environment = yield* DesktopEnvironment.DesktopEnvironment;
52 const fileSystem = yield* FileSystem.FileSystem;
53 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
54
55 const resolveEmbeddedCommitHash = Effect.gen(function* () {
56 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
57 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
58 return yield* Option.match(raw, {
59 onNone: () => Effect.succeed(Option.none<string>()),
60 onSome: (value) =>
61 decodeAppPackageMetadata(value).pipe(
62 Effect.map((parsed) =>
63 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
64 ),
65 Effect.orElseSucceed(() => Option.none<string>()),
66 ),
67 });
68 });
69
70 const resolveAboutCommitHash = Effect.gen(function* () {
71 const cached = yield* Ref.get(commitHashCache);
72 if (Option.isSome(cached)) {
73 return cached.value;
74 }
75
76 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
77 if (Option.isSome(override)) {
78 yield* Ref.set(commitHashCache, Option.some(override));
79 return override;
80 }
81
82 if (!environment.isPackaged) {
83 const empty = Option.none<string>();
84 yield* Ref.set(commitHashCache, Option.some(empty));
85 return empty;
86 }
87
88 const commitHash = yield* resolveEmbeddedCommitHash;
89 yield* Ref.set(commitHashCache, Option.some(commitHash));
90 return commitHash;
91 });
92
93 const resolveUserDataPath = Effect.gen(function* () {
94 const legacyPath = environment.path.join(
95 environment.appDataDirectory,
96 environment.legacyUserDataDirName,
97 );
98 const legacyPathExists = yield* fileSystem.exists(legacyPath).pipe(
99 Effect.mapError(
100 (cause) =>
101 new DesktopUserDataPathResolutionError({
102 legacyPath,
103 cause,
104 }),
105 ),
106 );
107 return legacyPathExists
108 ? legacyPath
109 : environment.path.join(environment.appDataDirectory, environment.userDataDirName);
110 }).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath"));
111
112 const configure = Effect.gen(function* () {
113 const commitHash = yield* resolveAboutCommitHash;
114 yield* electronApp.setName(environment.displayName);
115 yield* electronApp.setAboutPanelOptions({
116 applicationName: environment.displayName,
117 applicationVersion: environment.appVersion,
118 version: Option.getOrElse(commitHash, () => "unknown"),
119 });
120
121 if (environment.platform === "win32") {
122 yield* electronApp.setAppUserModelId(environment.appUserModelId);
123 }
124
125 if (environment.platform === "linux") {
126 yield* electronApp.setDesktopName(environment.linuxDesktopEntryName);
127 }
128
129 if (environment.platform === "darwin") {
130 const iconPaths = yield* assets.iconPaths;
131 yield* Option.match(iconPaths.png, {
132 onNone: () => Effect.void,
133 onSome: electronApp.setDockIcon,
134 });
135 }
136 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
137
138 return DesktopAppIdentity.of({
139 resolveUserDataPath,
140 configure,
141 });
142});
143
144export const layer = Layer.effect(DesktopAppIdentity, make);