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

SourceControlDiscovery.ts

171 lines5.5 KB

1import {
2 type SourceControlDiscoveryResult,
3 type VcsDiscoveryItem,
4 type VcsDriverKind,
5} from "@t3tools/contracts";
6import * as Context from "effect/Context";
7import * as Duration from "effect/Duration";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
12​
13import * as ServerConfig from "../config.ts";
14import { refreshPathFromLoginShell } from "../os-jank.ts";
15import * as VcsProcess from "../vcs/VcsProcess.ts";
16import {
17 detailFromCause,
18 failedProbeHint,
19 firstNonEmptyLine,
20 isExecutableOnPath,
21} from "@t3tools/source-control-core/server/discovery";
22import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
23​
24interface DiscoveryProbe {
25 readonly label: string;
26 readonly executable?: string;
27 readonly versionArgs?: ReadonlyArray<string>;
28 readonly implemented: boolean;
29 readonly installHint: string;
31​
32type VcsProbe = DiscoveryProbe & {
33 readonly kind: VcsDriverKind;
34 readonly executable: string;
35 readonly versionArgs: ReadonlyArray<string>;
36};
37​
38interface DiscoveryProbeResult<Kind extends string> {
39 readonly kind: Kind;
40 readonly label: string;
41 readonly executable?: string;
42 readonly implemented: boolean;
43 readonly status: "available" | "missing";
44 readonly version: Option.Option<string>;
45 readonly installHint: string;
46 readonly detail: Option.Option<string>;
48​
49const VCS_PROBES: ReadonlyArray<VcsProbe> = [
50 {
51 kind: "git",
52 label: "Git",
53 executable: "git",
54 versionArgs: ["--version"],
55 implemented: true,
56 installHint: "Install Git from https://git-scm.com/downloads or with your package manager.",
57 },
58 {
59 kind: "jj",
60 label: "Jujutsu",
61 executable: "jj",
62 versionArgs: ["--version"],
63 implemented: false,
64 installHint: "Install Jujutsu with `brew install jj` or from https://github.com/jj-vcs/jj.",
65 },
66];
67​
68export class SourceControlDiscovery extends Context.Service<
69 SourceControlDiscovery,
70 {
71 readonly discover: Effect.Effect<SourceControlDiscoveryResult>;
72 }
73>()("t3/sourceControl/SourceControlDiscovery") {}
74​
75/** @public */
76export const make = Effect.gen(function* () {
77 const config = yield* ServerConfig.ServerConfig;
78 const process = yield* VcsProcess.VcsProcess;
79 const sourceControlProviders = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
80 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
81 // Discovery re-reads the login shell so a tool installed since launch
82 // resolves here and in every later spawn. Thread views, the palette and
83 // mobile also run discovery, so one interactive shell serves a short window.
84 const refreshPath = yield* refreshPathFromLoginShell().pipe(
85 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
86 Effect.cachedWithTTL(Duration.seconds(10)),
87 );
88​
89 const probe = <Kind extends VcsDriverKind>(
90 input: VcsProbe & { readonly kind: Kind },
91 ): Effect.Effect<DiscoveryProbeResult<Kind>> => {
92 const executable = input.executable;
93 const versionArgs = input.versionArgs;
94​
95 return process
96 .run({
97 operation: "source-control.discovery.probe",
98 command: executable,
99 args: versionArgs,
100 cwd: config.cwd,
101 timeoutMs: 5_000,
102 maxOutputBytes: 8_000,
103 appendTruncationMarker: true,
104 })
105 .pipe(
106 Effect.map(
107 (result) =>
108 ({
109 kind: input.kind,
110 label: input.label,
111 executable,
112 implemented: input.implemented,
113 status: "available" as const,
114 version: Option.orElse(firstNonEmptyLine(result.stdout), () =>
115 firstNonEmptyLine(result.stderr),
116 ),
117 installHint: input.installHint,
118 detail: Option.none<string>(),
119 }) satisfies DiscoveryProbeResult<Kind>,
120 ),
121 Effect.catch((cause) =>
122 Effect.gen(function* () {
123 const installHint = yield* failedProbeHint(input, cause);
124 return {
125 kind: input.kind,
126 label: input.label,
127 executable,
128 implemented: input.implemented,
129 status: "missing" as const,
130 version: Option.none<string>(),
131 installHint,
132 detail: detailFromCause(cause),
133 } satisfies DiscoveryProbeResult<Kind>;
134 }),
135 ),
136 );
137 };
138​
139 return SourceControlDiscovery.of({
140 discover: Effect.andThen(
141 refreshPath,
142 Effect.all(
143 {
144 versionControlSystems: Effect.all(
145 VCS_PROBES.map((entry) =>
146 Effect.flatMap(isExecutableOnPath(entry.executable), (found) =>
147 found
148 ? probe(entry)
149 : Effect.succeed({
150 kind: entry.kind,
151 label: entry.label,
152 executable: entry.executable,
153 implemented: entry.implemented,
154 status: "missing" as const,
155 version: Option.none<string>(),
156 installHint: entry.installHint,
157 detail: Option.none<string>(),
158 }),
159 ),
160 ) as ReadonlyArray<Effect.Effect<VcsDiscoveryItem>>,
161 { concurrency: "unbounded" },
162 ),
163 sourceControlProviders: sourceControlProviders.discover,
164 },
165 { concurrency: "unbounded" },
166 ),
167 ),
168 });
169});
170​
171export const layer = Layer.effect(SourceControlDiscovery, make);