| ... | ... | @@ -3,6 +3,7 @@ import * as Duration from "effect/Duration"; |
| 3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | 4 | import * as Layer from "effect/Layer"; |
| 5 | 5 | import * as Option from "effect/Option"; |
| 6 | import * as Schema from "effect/Schema"; |
| 6 | 7 | import * as ChildProcess from "effect/unstable/process/ChildProcess"; |
| 7 | 8 | import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; |
| 8 | 9 | |
| ... | ... | @@ -20,6 +21,44 @@ interface WindowsProbeOptions { |
| 20 | 21 | readonly loadProfile: boolean; |
| 21 | 22 | } |
| 22 | 23 | |
| 24 | const DesktopShellEnvironmentProbe = Schema.Literals([ |
| 25 | "login-shell", |
| 26 | "launchctl-path", |
| 27 | "powershell-profile", |
| 28 | "powershell-no-profile", |
| 29 | ]); |
| 30 | type DesktopShellEnvironmentProbe = typeof DesktopShellEnvironmentProbe.Type; |
| 31 | |
| 32 | const desktopShellEnvironmentCommandFields = { |
| 33 | probe: DesktopShellEnvironmentProbe, |
| 34 | executable: Schema.String, |
| 35 | argumentCount: Schema.Number, |
| 36 | }; |
| 37 | |
| 38 | export class DesktopShellEnvironmentCommandError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandError>()( |
| 39 | "DesktopShellEnvironmentCommandError", |
| 40 | { |
| 41 | ...desktopShellEnvironmentCommandFields, |
| 42 | cause: Schema.Defect(), |
| 43 | }, |
| 44 | ) { |
| 45 | override get message(): string { |
| 46 | return `Desktop shell environment ${this.probe} probe (${this.executable}) failed.`; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandTimeoutError>()( |
| 51 | "DesktopShellEnvironmentCommandTimeoutError", |
| 52 | { |
| 53 | ...desktopShellEnvironmentCommandFields, |
| 54 | timeoutMs: Schema.Number, |
| 55 | }, |
| 56 | ) { |
| 57 | override get message(): string { |
| 58 | return `Desktop shell environment ${this.probe} probe (${this.executable}) timed out after ${this.timeoutMs}ms.`; |
| 59 | } |
| 60 | } |
| 61 | |
| 23 | 62 | export class DesktopShellEnvironment extends Context.Service< |
| 24 | 63 | DesktopShellEnvironment, |
| 25 | 64 | { |
| ... | ... | @@ -127,6 +166,18 @@ const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray<string> => [ |
| 127 | 166 | const startMarker = (name: string) => `__T3CODE_ENV_${name}_START__`; |
| 128 | 167 | const endMarker = (name: string) => `__T3CODE_ENV_${name}_END__`; |
| 129 | 168 | |
| 169 | const executableName = (command: string): string => command.split(/[\\/]/u).at(-1) ?? command; |
| 170 | |
| 171 | const logShellEnvironmentCommandError = ( |
| 172 | error: DesktopShellEnvironmentCommandError | DesktopShellEnvironmentCommandTimeoutError, |
| 173 | ) => |
| 174 | Effect.logWarning(error).pipe( |
| 175 | Effect.annotateLogs({ |
| 176 | component: "desktop-shell-environment", |
| 177 | error, |
| 178 | }), |
| 179 | ); |
| 180 | |
| 130 | 181 | const capturePosixEnvironmentCommand = (names: ReadonlyArray<string>) => |
| 131 | 182 | names |
| 132 | 183 | .map((name) => { |
| ... | ... | @@ -175,13 +226,14 @@ const extractEnvironment = (output: string, names: ReadonlyArray<string>): Envir |
| 175 | 226 | }; |
| 176 | 227 | |
| 177 | 228 | const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(function* (input: { |
| 229 | readonly probe: DesktopShellEnvironmentProbe; |
| 178 | 230 | readonly command: string; |
| 179 | 231 | readonly args: ReadonlyArray<string>; |
| 180 | 232 | readonly timeout: Duration.Duration; |
| 181 | 233 | readonly shell?: boolean; |
| 182 | 234 | }): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> { |
| 183 | 235 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 184 | | return yield* spawner |
| 236 | const output = yield* spawner |
| 185 | 237 | .string( |
| 186 | 238 | ChildProcess.make(input.command, input.args, { |
| 187 | 239 | shell: input.shell ?? false, |
| ... | ... | @@ -193,10 +245,33 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")( |
| 193 | 245 | }), |
| 194 | 246 | ) |
| 195 | 247 | .pipe( |
| 248 | Effect.mapError( |
| 249 | (cause) => |
| 250 | new DesktopShellEnvironmentCommandError({ |
| 251 | probe: input.probe, |
| 252 | executable: executableName(input.command), |
| 253 | argumentCount: input.args.length, |
| 254 | cause, |
| 255 | }), |
| 256 | ), |
| 257 | Effect.catchTags({ |
| 258 | DesktopShellEnvironmentCommandError: (error) => |
| 259 | logShellEnvironmentCommandError(error).pipe(Effect.as("")), |
| 260 | }), |
| 196 | 261 | Effect.timeoutOption(input.timeout), |
| 197 | | Effect.map(Option.getOrElse(() => "")), |
| 198 | | Effect.orElseSucceed(() => ""), |
| 199 | 262 | ); |
| 263 | if (Option.isSome(output)) { |
| 264 | return output.value; |
| 265 | } |
| 266 | |
| 267 | const error = new DesktopShellEnvironmentCommandTimeoutError({ |
| 268 | probe: input.probe, |
| 269 | executable: executableName(input.command), |
| 270 | argumentCount: input.args.length, |
| 271 | timeoutMs: Duration.toMillis(input.timeout), |
| 272 | }); |
| 273 | yield* logShellEnvironmentCommandError(error); |
| 274 | return ""; |
| 200 | 275 | }); |
| 201 | 276 | |
| 202 | 277 | const readLoginShellEnvironment = ( |
| ... | ... | @@ -206,12 +281,14 @@ const readLoginShellEnvironment = ( |
| 206 | 281 | names.length === 0 |
| 207 | 282 | ? Effect.succeed({}) |
| 208 | 283 | : runCommandOutput({ |
| 284 | probe: "login-shell", |
| 209 | 285 | command: shell, |
| 210 | 286 | args: ["-ilc", capturePosixEnvironmentCommand(names)], |
| 211 | 287 | timeout: LOGIN_SHELL_TIMEOUT, |
| 212 | 288 | }).pipe(Effect.map((output) => extractEnvironment(output, names))); |
| 213 | 289 | |
| 214 | 290 | const readLaunchctlPath = runCommandOutput({ |
| 291 | probe: "launchctl-path", |
| 215 | 292 | command: "/bin/launchctl", |
| 216 | 293 | args: ["getenv", "PATH"], |
| 217 | 294 | timeout: LAUNCHCTL_TIMEOUT, |
| ... | ... | @@ -234,6 +311,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn |
| 234 | 311 | |
| 235 | 312 | for (const command of WINDOWS_SHELL_CANDIDATES) { |
| 236 | 313 | const output = yield* runCommandOutput({ |
| 314 | probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile", |
| 237 | 315 | command, |
| 238 | 316 | args, |
| 239 | 317 | timeout: LOGIN_SHELL_TIMEOUT, |