@huaqiu/hardware-artifacts 0.7.18 → 0.7.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.mts +61 -8
- package/lib/index.mjs +2610 -6
- package/lib/routes-D78ML_ef.d.mts +438 -0
- package/lib/routes.d.mts +2 -18
- package/lib/routes.mjs +156 -41
- package/lib/{service-CtVn7z8b.d.mts → service-BGecln85.d.mts} +119 -28
- package/lib/{service-Yrc2C_eo.mjs → service-Bo58tzXv.mjs} +570 -167
- package/lib/service.d.mts +1 -1
- package/lib/service.mjs +1 -1
- package/package.json +2 -1
- package/src/index.ts +124 -5
- package/src/order.ts +22 -0
- package/src/package.ts +282 -0
- package/src/routes.ts +127 -48
- package/src/service.ts +1635 -1651
- package/src/toolchain/dialog.ts +240 -0
- package/src/toolchain/discovery.ts +266 -0
- package/src/toolchain/index.ts +36 -0
- package/src/toolchain/install.ts +430 -0
- package/src/toolchain/lzma.ts +384 -0
- package/src/toolchain/manager.ts +596 -0
- package/src/toolchain/manifest.ts +234 -0
- package/src/toolchain/resolve.ts +325 -0
- package/src/toolchain/zip.ts +495 -0
- package/src/tools.ts +470 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toolchain capability: status, validation, discovery, download and
|
|
3
|
+
* configuration for KiCad and SolidCraft.
|
|
4
|
+
*
|
|
5
|
+
* One object owns the whole lifecycle so the wizard, the Settings panel, the
|
|
6
|
+
* installer and the agent prompt read the same facts (§10.5). It never writes
|
|
7
|
+
* UI state and never invents a download: everything it reports comes from a
|
|
8
|
+
* resolution of the configured path, a validated manifest entry, or a job it is
|
|
9
|
+
* running right now.
|
|
10
|
+
*
|
|
11
|
+
* @module @huaqiu/hardware-artifacts/toolchain/manager
|
|
12
|
+
*/
|
|
13
|
+
import { execFile } from 'node:child_process'
|
|
14
|
+
import { existsSync } from 'node:fs'
|
|
15
|
+
import { homedir } from 'node:os'
|
|
16
|
+
import { join, resolve } from 'node:path'
|
|
17
|
+
import { pickFile, TOOLCHAIN_FILTERS, type PickFileResult } from './dialog.js'
|
|
18
|
+
import {
|
|
19
|
+
candidateRoots, probeInstallation, validateCandidates,
|
|
20
|
+
type DiscoveryInternals, type ToolDiscovery,
|
|
21
|
+
} from './discovery.js'
|
|
22
|
+
import { ToolchainInstaller, type InstallJobSnapshot, type InstallOutcome } from './install.js'
|
|
23
|
+
import { fetchManifest, MANIFEST_URL, type ManifestEntry, type ManifestFailure, type ToolchainManifest } from './manifest.js'
|
|
24
|
+
import {
|
|
25
|
+
canonicalPath, installationRootOf, label, normalizeVersion, resolveToolchain,
|
|
26
|
+
type ToolchainId, type ToolchainResolution, type ToolchainSource,
|
|
27
|
+
} from './resolve.js'
|
|
28
|
+
|
|
29
|
+
/** The fallback download pages offered when the HuaQiu manifest is unusable (§9.2). */
|
|
30
|
+
export const OFFICIAL_DOWNLOAD_PAGES: Readonly<Record<ToolchainId, string>> = Object.freeze({
|
|
31
|
+
kicad: 'https://www.kicad.org/download/',
|
|
32
|
+
solidcraft: 'https://github.com/tangency-labs/solidcraft/releases/',
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
/** Persisted configuration the manager reads; every field is user-owned. */
|
|
36
|
+
export interface ToolchainSettings {
|
|
37
|
+
readonly kicadRoot?: string
|
|
38
|
+
readonly solidcraftRoot?: string
|
|
39
|
+
readonly kicadVersion?: string
|
|
40
|
+
readonly kicadSource?: string
|
|
41
|
+
readonly kicadValidatedAt?: number
|
|
42
|
+
readonly solidcraftVersion?: string
|
|
43
|
+
readonly solidcraftSource?: string
|
|
44
|
+
readonly solidcraftValidatedAt?: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The patch written back after a successful install or an in-app validation. */
|
|
48
|
+
export type ToolchainConfigPatch = Partial<Record<
|
|
49
|
+
| 'kicadRoot' | 'solidcraftRoot'
|
|
50
|
+
| 'kicadVersion' | 'kicadSource' | 'kicadValidatedAt'
|
|
51
|
+
| 'solidcraftVersion' | 'solidcraftSource' | 'solidcraftValidatedAt',
|
|
52
|
+
string | number
|
|
53
|
+
>>
|
|
54
|
+
|
|
55
|
+
/** One tool's status, as the UI renders it. */
|
|
56
|
+
export interface ToolStatus {
|
|
57
|
+
readonly tool: ToolchainId
|
|
58
|
+
/** `validating` covers the window before the first probe answers. */
|
|
59
|
+
readonly state: 'unconfigured' | 'validating' | 'configured' | 'invalid'
|
|
60
|
+
readonly installPath?: string
|
|
61
|
+
readonly cliPath?: string
|
|
62
|
+
readonly appPath?: string
|
|
63
|
+
readonly version?: string
|
|
64
|
+
readonly source?: ToolchainSource
|
|
65
|
+
readonly validatedAt?: number
|
|
66
|
+
readonly error?: string
|
|
67
|
+
readonly discovery: ToolDiscovery
|
|
68
|
+
/** Manifest entry this host could install, when one is available. */
|
|
69
|
+
readonly download?: { readonly version: string; readonly url: string }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Everything the settings/wizard surface needs, in one poll. */
|
|
73
|
+
export interface ToolchainStatus {
|
|
74
|
+
readonly kicad: ToolStatus
|
|
75
|
+
readonly solidcraft: ToolStatus
|
|
76
|
+
readonly manifest:
|
|
77
|
+
| { readonly state: 'unknown' | 'loading' }
|
|
78
|
+
| { readonly state: 'ready'; readonly schemaVersion: number; readonly url: string }
|
|
79
|
+
| { readonly state: 'unavailable'; readonly code: string; readonly message: string }
|
|
80
|
+
/** Platform/architecture the download flow targets (§7.3). */
|
|
81
|
+
readonly target: { readonly platform: string; readonly arch: string; readonly downloadsSupported: boolean }
|
|
82
|
+
/** Official pages offered when the download flow cannot be used (§9.3). */
|
|
83
|
+
readonly official: Readonly<Record<ToolchainId, string>>
|
|
84
|
+
/** The running or last finished jobs the caller asked about — one per tool. */
|
|
85
|
+
readonly jobs?: readonly InstallJobSnapshot[]
|
|
86
|
+
/** First of `jobs`, kept for callers that still ask about a single run. */
|
|
87
|
+
readonly job?: InstallJobSnapshot
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Result of "validate and save" for one path. */
|
|
91
|
+
export interface ValidationOutcome {
|
|
92
|
+
readonly ok: boolean
|
|
93
|
+
readonly installPath?: string
|
|
94
|
+
readonly cliPath?: string
|
|
95
|
+
readonly appPath?: string
|
|
96
|
+
readonly version?: string
|
|
97
|
+
readonly error?: string
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Result of a native browse request. */
|
|
101
|
+
export interface BrowseOutcome {
|
|
102
|
+
readonly kind: 'picked' | 'cancelled' | 'unsupported'
|
|
103
|
+
readonly path?: string
|
|
104
|
+
/** Which chooser answered: the plugin's file dialog or DSH's own seam. */
|
|
105
|
+
readonly mechanism?: string
|
|
106
|
+
readonly message?: string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Injectable boundaries of the capability. */
|
|
110
|
+
export interface ToolchainManagerOptions {
|
|
111
|
+
/** Live read of the persisted configuration. */
|
|
112
|
+
readonly settings: () => ToolchainSettings
|
|
113
|
+
/** Persist a patch through the Host's own settings service. */
|
|
114
|
+
readonly persist?: (patch: ToolchainConfigPatch) => Promise<boolean>
|
|
115
|
+
/** Per-user toolchain root (`<DSH home>/toolchains`). */
|
|
116
|
+
readonly managedRoot?: string
|
|
117
|
+
readonly home?: string
|
|
118
|
+
readonly platform?: NodeJS.Platform
|
|
119
|
+
readonly arch?: string
|
|
120
|
+
readonly env?: Readonly<Record<string, string | undefined>>
|
|
121
|
+
readonly fetchImpl?: typeof fetch
|
|
122
|
+
readonly manifestUrl?: string
|
|
123
|
+
/** Version probe for one executable, answering the tool's raw report. */
|
|
124
|
+
readonly versionProbe?: (executable: string, tool: ToolchainId) => Promise<string | undefined>
|
|
125
|
+
/** DSH's own native directory chooser, used when the file dialog is unavailable. */
|
|
126
|
+
readonly pickFolder?: (title: string) => Promise<string | null>
|
|
127
|
+
/** Plugin-level native file dialog (overridable for tests). */
|
|
128
|
+
readonly pickFile?: (request: Parameters<typeof pickFile>[0], internals: { signal?: AbortSignal }) => Promise<PickFileResult>
|
|
129
|
+
readonly discoveryInternals?: DiscoveryInternals
|
|
130
|
+
readonly now?: () => number
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Version probes cost a process; cache them briefly per executable. */
|
|
134
|
+
const PROBE_CACHE_MS = 30_000
|
|
135
|
+
|
|
136
|
+
/** Dispatch one version command per tool — the interfaces are not identical (§5.3). */
|
|
137
|
+
function versionCommand(tool: ToolchainId, executable: string): { command: string; args: string[] } {
|
|
138
|
+
return tool === 'kicad' ? { command: executable, args: ['version'] } : { command: executable, args: ['--version'] }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Default probe: one short-lived process, no shell. */
|
|
142
|
+
async function defaultVersionProbe(executable: string, tool: ToolchainId): Promise<string | undefined> {
|
|
143
|
+
const { command, args } = versionCommand(tool, executable)
|
|
144
|
+
return new Promise((resolvePromise) => {
|
|
145
|
+
execFile(command, args, {
|
|
146
|
+
timeout: 15_000,
|
|
147
|
+
encoding: 'utf8',
|
|
148
|
+
windowsHide: true,
|
|
149
|
+
shell: false,
|
|
150
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
151
|
+
}, (error, stdout) => {
|
|
152
|
+
if (error !== null) {
|
|
153
|
+
resolvePromise(undefined)
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
resolvePromise(normalizeVersion(String(stdout)) ?? String(stdout).trim())
|
|
157
|
+
})
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The per-user toolchain root, following the harness-home convention (§8.1). */
|
|
162
|
+
export function defaultManagedRoot(env: Readonly<Record<string, string | undefined>> = process.env): string {
|
|
163
|
+
// `$DSH_HOME` wins; the fallback is this product's own root (`~/.huaqiu-eda`),
|
|
164
|
+
// not the upstream `~/.dsh` — a machine that runs both must not mix them.
|
|
165
|
+
const home = env.DSH_HOME !== undefined && env.DSH_HOME !== '' ? resolve(env.DSH_HOME) : join(homedir(), '.huaqiu-eda')
|
|
166
|
+
return join(home, 'toolchains')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Discovery is asynchronous by construction: `state: 'running'` is answered
|
|
171
|
+
* immediately, and the verdicts arrive through {@link ToolchainManager.status}.
|
|
172
|
+
*/
|
|
173
|
+
export class ToolchainManager {
|
|
174
|
+
private readonly installer: ToolchainInstaller
|
|
175
|
+
private readonly managedRoot: string
|
|
176
|
+
private readonly platform: NodeJS.Platform
|
|
177
|
+
private readonly arch: string
|
|
178
|
+
private readonly env: Readonly<Record<string, string | undefined>>
|
|
179
|
+
private readonly home: string
|
|
180
|
+
private readonly now: () => number
|
|
181
|
+
private readonly probeCache = new Map<string, { readonly at: number; readonly version?: string }>()
|
|
182
|
+
private readonly discovery: Record<ToolchainId, ToolDiscovery>
|
|
183
|
+
private readonly discovering = new Set<ToolchainId>()
|
|
184
|
+
private manifestCache?: { readonly at: number; readonly result: Awaited<ReturnType<typeof fetchManifest>> }
|
|
185
|
+
private manifestPending?: Promise<Awaited<ReturnType<typeof fetchManifest>>>
|
|
186
|
+
|
|
187
|
+
constructor(private readonly options: ToolchainManagerOptions) {
|
|
188
|
+
this.managedRoot = options.managedRoot ?? defaultManagedRoot(options.env ?? process.env)
|
|
189
|
+
this.platform = options.platform ?? process.platform
|
|
190
|
+
this.arch = options.arch ?? process.arch
|
|
191
|
+
this.env = options.env ?? process.env
|
|
192
|
+
this.home = options.home ?? homedir()
|
|
193
|
+
this.now = options.now ?? (() => Date.now())
|
|
194
|
+
this.installer = new ToolchainInstaller({
|
|
195
|
+
root: this.managedRoot,
|
|
196
|
+
...(options.fetchImpl === undefined ? {} : { fetchImpl: options.fetchImpl }),
|
|
197
|
+
now: this.now,
|
|
198
|
+
})
|
|
199
|
+
this.discovery = {
|
|
200
|
+
kicad: { tool: 'kicad', state: 'idle', candidates: [] },
|
|
201
|
+
solidcraft: { tool: 'solidcraft', state: 'idle', candidates: [] },
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The per-user root this capability installs into, for diagnostics. */
|
|
206
|
+
get toolchainRoot(): string {
|
|
207
|
+
return this.managedRoot
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Probe one executable, memoised briefly; an install invalidates it. */
|
|
211
|
+
private async versionOf(tool: ToolchainId, executable: string): Promise<string | undefined> {
|
|
212
|
+
const key = `${tool}:${executable}`
|
|
213
|
+
const cached = this.probeCache.get(key)
|
|
214
|
+
if (cached !== undefined && this.now() - cached.at < PROBE_CACHE_MS) return cached.version
|
|
215
|
+
const report = await (this.options.versionProbe ?? defaultVersionProbe)(executable, tool)
|
|
216
|
+
const version = report === undefined ? undefined : normalizeVersion(report) ?? report
|
|
217
|
+
this.probeCache.set(key, { at: this.now(), ...(version === undefined ? {} : { version }) })
|
|
218
|
+
return version
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Resolve one configured path into its executables and validated state. */
|
|
222
|
+
private async resolve(tool: ToolchainId, root: string | undefined): Promise<ToolchainResolution> {
|
|
223
|
+
if (root === undefined || root.trim() === '') {
|
|
224
|
+
return resolveToolchain(tool, root)
|
|
225
|
+
}
|
|
226
|
+
// Probe only when the primary executable actually exists, then hand the
|
|
227
|
+
// answer to the one shared resolver: the manager decides nothing itself.
|
|
228
|
+
const layout = resolveToolchain(tool, root, { platform: this.platform })
|
|
229
|
+
const executable = layout.cli ?? layout.app
|
|
230
|
+
const version = executable === undefined ? undefined : await this.versionOf(tool, executable)
|
|
231
|
+
return resolveToolchain(tool, root, {
|
|
232
|
+
platform: this.platform,
|
|
233
|
+
probeVersion: () => version,
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The persisted path for one tool, as the settings service holds it. */
|
|
238
|
+
private roots(): ToolchainSettings {
|
|
239
|
+
return this.options.settings()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Status of one tool, from its configured path. */
|
|
243
|
+
private async toolStatus(tool: ToolchainId): Promise<ToolStatus> {
|
|
244
|
+
const settings = this.roots()
|
|
245
|
+
const root = tool === 'kicad' ? settings.kicadRoot : settings.solidcraftRoot
|
|
246
|
+
const persistedVersion = tool === 'kicad' ? settings.kicadVersion : settings.solidcraftVersion
|
|
247
|
+
const persistedSource = tool === 'kicad' ? settings.kicadSource : settings.solidcraftSource
|
|
248
|
+
const validatedAt = tool === 'kicad' ? settings.kicadValidatedAt : settings.solidcraftValidatedAt
|
|
249
|
+
const resolution = await this.resolve(tool, root)
|
|
250
|
+
const source = persistedSource === 'manual' || persistedSource === 'browse' || persistedSource === 'download' || persistedSource === 'detected'
|
|
251
|
+
? persistedSource as ToolchainSource
|
|
252
|
+
: undefined
|
|
253
|
+
const entry = await this.selectedEntry(tool)
|
|
254
|
+
const base = {
|
|
255
|
+
tool,
|
|
256
|
+
discovery: this.discovery[tool],
|
|
257
|
+
...(entry === undefined ? {} : { download: { version: entry.version, url: entry.url } }),
|
|
258
|
+
}
|
|
259
|
+
if (resolution.configured) {
|
|
260
|
+
return {
|
|
261
|
+
...base,
|
|
262
|
+
state: 'configured',
|
|
263
|
+
installPath: resolution.installPath,
|
|
264
|
+
...(resolution.cli === undefined ? {} : { cliPath: resolution.cli }),
|
|
265
|
+
...(resolution.app === undefined ? {} : { appPath: resolution.app }),
|
|
266
|
+
...(resolution.version === undefined ? {} : { version: resolution.version }),
|
|
267
|
+
...(source === undefined ? {} : { source }),
|
|
268
|
+
...(validatedAt === undefined || validatedAt === 0 ? {} : { validatedAt }),
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (root === undefined || root.trim() === '') {
|
|
272
|
+
return { ...base, state: 'unconfigured' }
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
...base,
|
|
276
|
+
state: 'invalid',
|
|
277
|
+
installPath: resolution.installPath,
|
|
278
|
+
...(resolution.cli === undefined ? {} : { cliPath: resolution.cli }),
|
|
279
|
+
...(resolution.app === undefined ? {} : { appPath: resolution.app }),
|
|
280
|
+
...(persistedVersion === undefined || persistedVersion === '' ? {} : { version: persistedVersion }),
|
|
281
|
+
...(source === undefined ? {} : { source }),
|
|
282
|
+
...(validatedAt === undefined || validatedAt === 0 ? {} : { validatedAt }),
|
|
283
|
+
...(resolution.error === undefined ? {} : { error: resolution.error }),
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Full status for the settings/wizard surfaces.
|
|
289
|
+
* @param jobIds - optional jobs whose progress the caller wants included. Both
|
|
290
|
+
* tools can be downloading at once, so this is a list: asking for one id at a
|
|
291
|
+
* time is exactly how the second download used to erase the first one's bar.
|
|
292
|
+
* @returns both tools plus the manifest and target facts.
|
|
293
|
+
*/
|
|
294
|
+
async status(jobIds?: readonly string[]): Promise<ToolchainStatus> {
|
|
295
|
+
const [kicad, solidcraft] = await Promise.all([this.toolStatus('kicad'), this.toolStatus('solidcraft')])
|
|
296
|
+
const manifest = await this.manifestState()
|
|
297
|
+
const jobs = (jobIds ?? [])
|
|
298
|
+
.map(id => this.installer.snapshot(id))
|
|
299
|
+
.filter((job): job is InstallJobSnapshot => job !== undefined)
|
|
300
|
+
const downloadsSupported = this.platform === 'win32' || this.platform === 'darwin' || this.platform === 'linux'
|
|
301
|
+
return {
|
|
302
|
+
kicad,
|
|
303
|
+
solidcraft,
|
|
304
|
+
manifest,
|
|
305
|
+
target: { platform: this.platform, arch: this.arch, downloadsSupported },
|
|
306
|
+
official: OFFICIAL_DOWNLOAD_PAGES,
|
|
307
|
+
...(jobs.length === 0 ? {} : { jobs }),
|
|
308
|
+
...(jobs.length === 0 ? {} : { job: jobs[0] }),
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Manifest projection for the status poll. */
|
|
313
|
+
private async manifestState(): Promise<ToolchainStatus['manifest']> {
|
|
314
|
+
if (this.manifestCache !== undefined && this.now() - this.manifestCache.at < 10 * 60_000) {
|
|
315
|
+
return this.manifestProjection(this.manifestCache.result)
|
|
316
|
+
}
|
|
317
|
+
const pending = this.loadManifest()
|
|
318
|
+
if (pending === undefined) return { state: 'unknown' }
|
|
319
|
+
return this.manifestProjection(await pending)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private manifestProjection(result: Awaited<ReturnType<typeof fetchManifest>>): ToolchainStatus['manifest'] {
|
|
323
|
+
if (result.ok) {
|
|
324
|
+
return { state: 'ready', schemaVersion: result.manifest.schemaVersion, url: this.options.manifestUrl ?? MANIFEST_URL }
|
|
325
|
+
}
|
|
326
|
+
return { state: 'unavailable', code: result.code, message: result.message }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Fetch the manifest once; concurrent callers share one request. */
|
|
330
|
+
loadManifest(force = false): Promise<Awaited<ReturnType<typeof fetchManifest>>> | undefined {
|
|
331
|
+
if (!force && this.manifestCache !== undefined && this.now() - this.manifestCache.at < 10 * 60_000) {
|
|
332
|
+
return Promise.resolve(this.manifestCache.result)
|
|
333
|
+
}
|
|
334
|
+
if (this.manifestPending !== undefined) return this.manifestPending
|
|
335
|
+
if (this.platform !== 'win32' && this.platform !== 'darwin' && this.platform !== 'linux') return undefined
|
|
336
|
+
this.manifestPending = fetchManifest({
|
|
337
|
+
...(this.options.manifestUrl === undefined ? {} : { url: this.options.manifestUrl }),
|
|
338
|
+
...(this.options.fetchImpl === undefined ? {} : { fetchImpl: this.options.fetchImpl }),
|
|
339
|
+
platform: this.platform,
|
|
340
|
+
arch: this.arch,
|
|
341
|
+
}).then((result) => {
|
|
342
|
+
this.manifestCache = { at: this.now(), result }
|
|
343
|
+
this.manifestPending = undefined
|
|
344
|
+
return result
|
|
345
|
+
})
|
|
346
|
+
return this.manifestPending
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The validated manifest entry for one tool, when the manifest answered. */
|
|
350
|
+
private async selectedEntry(tool: ToolchainId): Promise<ManifestEntry | undefined> {
|
|
351
|
+
const result = await this.loadManifest()
|
|
352
|
+
if (result === undefined || !result.ok) return undefined
|
|
353
|
+
return result.manifest.selected[tool]
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Validated manifest for the installer; a refusal keeps manual paths usable. */
|
|
357
|
+
async manifest(): Promise<{ readonly ok: true; readonly manifest: ToolchainManifest } | ManifestFailure> {
|
|
358
|
+
const result = await this.loadManifest(true)
|
|
359
|
+
return result ?? { ok: false, code: 'network', message: 'the toolchain manifest is unavailable on this platform' }
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Validate one user-supplied path without persisting anything (§6.3).
|
|
364
|
+
* @param tool - which toolchain.
|
|
365
|
+
* @param input - installation root, `bin` directory or executable path.
|
|
366
|
+
* @returns resolution verdict with the normalised root to persist on success.
|
|
367
|
+
*/
|
|
368
|
+
async validate(tool: ToolchainId, input: string): Promise<ValidationOutcome> {
|
|
369
|
+
const root = installationRootOf(tool, input)
|
|
370
|
+
if (root === undefined) return { ok: false, error: 'a path is required' }
|
|
371
|
+
const resolution = await this.resolve(tool, root)
|
|
372
|
+
return {
|
|
373
|
+
ok: resolution.configured,
|
|
374
|
+
installPath: resolution.installPath,
|
|
375
|
+
...(resolution.cli === undefined ? {} : { cliPath: resolution.cli }),
|
|
376
|
+
...(resolution.app === undefined ? {} : { appPath: resolution.app }),
|
|
377
|
+
...(resolution.version === undefined ? {} : { version: resolution.version }),
|
|
378
|
+
...(resolution.error === undefined ? {} : { error: resolution.error }),
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Persist a validated configuration; the host settings service stays authoritative. */
|
|
383
|
+
async save(tool: ToolchainId, input: string, source: ToolchainSource): Promise<ValidationOutcome> {
|
|
384
|
+
const outcome = await this.validate(tool, input)
|
|
385
|
+
if (!outcome.ok || outcome.installPath === undefined) return outcome
|
|
386
|
+
const patch: ToolchainConfigPatch = tool === 'kicad'
|
|
387
|
+
? {
|
|
388
|
+
kicadRoot: outcome.installPath,
|
|
389
|
+
kicadVersion: outcome.version ?? '',
|
|
390
|
+
kicadSource: source,
|
|
391
|
+
kicadValidatedAt: this.now(),
|
|
392
|
+
}
|
|
393
|
+
: {
|
|
394
|
+
solidcraftRoot: outcome.installPath,
|
|
395
|
+
solidcraftVersion: outcome.version ?? '',
|
|
396
|
+
solidcraftSource: source,
|
|
397
|
+
solidcraftValidatedAt: this.now(),
|
|
398
|
+
}
|
|
399
|
+
const written = await this.options.persist?.(patch) ?? false
|
|
400
|
+
if (!written) {
|
|
401
|
+
return { ...outcome, ok: false, error: 'the Host refused the configuration write' }
|
|
402
|
+
}
|
|
403
|
+
this.probeCache.clear()
|
|
404
|
+
return outcome
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Start background discovery for one tool (or both).
|
|
409
|
+
*
|
|
410
|
+
* Returns the current discovery state immediately: the run continues off-request
|
|
411
|
+
* and the UI polls {@link ToolchainManager.status} (§4.2/§5.5).
|
|
412
|
+
* @param tool - tool to discover; both when omitted.
|
|
413
|
+
* @returns the discovery state at the moment the request was answered.
|
|
414
|
+
*/
|
|
415
|
+
startDiscovery(tool?: ToolchainId): { kicad: ToolDiscovery; solidcraft: ToolDiscovery } {
|
|
416
|
+
for (const candidate of tool === undefined ? (['kicad', 'solidcraft'] as const) : [tool]) {
|
|
417
|
+
if (this.discovering.has(candidate)) continue
|
|
418
|
+
this.discovering.add(candidate)
|
|
419
|
+
this.discovery[candidate] = { ...this.discovery[candidate], state: 'running' }
|
|
420
|
+
void this.runDiscovery(candidate)
|
|
421
|
+
}
|
|
422
|
+
return { kicad: this.discovery.kicad, solidcraft: this.discovery.solidcraft }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** One bounded discovery run; a failure is recorded, never thrown at the UI. */
|
|
426
|
+
private async runDiscovery(tool: ToolchainId): Promise<void> {
|
|
427
|
+
try {
|
|
428
|
+
const roots = candidateRoots(tool, {
|
|
429
|
+
platform: this.platform,
|
|
430
|
+
env: this.env,
|
|
431
|
+
managedRoot: this.managedRoot,
|
|
432
|
+
home: this.home,
|
|
433
|
+
}, this.options.discoveryInternals ?? {})
|
|
434
|
+
const validated = await this.validateDiscoveryCandidates(tool, roots)
|
|
435
|
+
this.discovery[tool] = {
|
|
436
|
+
tool,
|
|
437
|
+
state: 'done',
|
|
438
|
+
candidates: validated.candidates,
|
|
439
|
+
...(validated.selected === undefined ? {} : { selected: validated.selected }),
|
|
440
|
+
finishedAt: this.now(),
|
|
441
|
+
}
|
|
442
|
+
} catch (error) {
|
|
443
|
+
this.discovery[tool] = {
|
|
444
|
+
tool,
|
|
445
|
+
state: 'failed',
|
|
446
|
+
candidates: [],
|
|
447
|
+
error: error instanceof Error ? error.message : String(error),
|
|
448
|
+
finishedAt: this.now(),
|
|
449
|
+
}
|
|
450
|
+
} finally {
|
|
451
|
+
this.discovering.delete(tool)
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Probe candidates asynchronously, then rank them with the shared policy.
|
|
457
|
+
*
|
|
458
|
+
* The ranking stays in {@link validateCandidates}; this method only supplies
|
|
459
|
+
* the asynchronous probes it needs, keyed by the executable the resolver will
|
|
460
|
+
* actually ask about, so discovery and manual validation cannot disagree about
|
|
461
|
+
* what counts as valid.
|
|
462
|
+
*/
|
|
463
|
+
private async validateDiscoveryCandidates(
|
|
464
|
+
tool: ToolchainId,
|
|
465
|
+
roots: readonly { path: string; origin: 'managed' | 'system' | 'user-local' }[],
|
|
466
|
+
): Promise<{ candidates: ToolDiscovery['candidates']; selected?: NonNullable<ToolDiscovery['selected']> }> {
|
|
467
|
+
const versions = new Map<string, string | undefined>()
|
|
468
|
+
for (const root of roots) {
|
|
469
|
+
const layout = resolveToolchain(tool, root.path, { platform: this.platform })
|
|
470
|
+
const executable = layout.cli ?? layout.app
|
|
471
|
+
if (executable === undefined) continue
|
|
472
|
+
versions.set(executable, await this.versionOf(tool, executable))
|
|
473
|
+
}
|
|
474
|
+
return validateCandidates(tool, roots, {
|
|
475
|
+
...(this.options.discoveryInternals ?? {}),
|
|
476
|
+
probeVersion: (executable) => versions.get(executable),
|
|
477
|
+
})
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** One directory probe for a diagnostics path (no version process). */
|
|
481
|
+
probeDirectory(tool: ToolchainId, root: string): ReturnType<typeof probeInstallation> {
|
|
482
|
+
return probeInstallation(tool, root, this.options.discoveryInternals ?? {})
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** Install the manifest entry for one tool; the job reports progress. */
|
|
486
|
+
async install(tool: ToolchainId): Promise<{ readonly snapshot?: InstallJobSnapshot; readonly error?: ManifestFailure }> {
|
|
487
|
+
const running = this.installer.activeFor(tool)
|
|
488
|
+
if (running !== undefined) return { snapshot: running }
|
|
489
|
+
const result = await this.manifest()
|
|
490
|
+
if (!result.ok) return { error: result }
|
|
491
|
+
const entry = result.manifest.selected[tool]
|
|
492
|
+
if (entry === undefined) {
|
|
493
|
+
return { error: { ok: false, code: 'no-entry', message: `the toolchain manifest offers no ${label(tool)} build for this platform` } }
|
|
494
|
+
}
|
|
495
|
+
// The stage validates through the same resolver a manual save uses, so a
|
|
496
|
+
// freshly extracted tree is judged by exactly the rules §8.5 step 8 states.
|
|
497
|
+
const { snapshot, done } = this.installer.start(tool, entry, {
|
|
498
|
+
validate: async (installPath) => {
|
|
499
|
+
this.probeCache.clear()
|
|
500
|
+
const resolution = await this.resolve(tool, installPath)
|
|
501
|
+
return resolution.configured ? { ok: true } : { ok: false, error: resolution.error ?? 'the installation did not validate' }
|
|
502
|
+
},
|
|
503
|
+
onProgress: () => {},
|
|
504
|
+
})
|
|
505
|
+
void done.then(async (outcome) => {
|
|
506
|
+
await this.afterInstall(tool, entry, outcome)
|
|
507
|
+
})
|
|
508
|
+
return { snapshot }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** Persist the completed installation and drop stale probes (§8.5). */
|
|
512
|
+
private async afterInstall(tool: ToolchainId, entry: ManifestEntry, outcome: InstallOutcome): Promise<void> {
|
|
513
|
+
if (!outcome.ok || outcome.installPath === undefined) return
|
|
514
|
+
this.probeCache.clear()
|
|
515
|
+
const probe = await this.resolve(tool, outcome.installPath)
|
|
516
|
+
if (!probe.configured) return
|
|
517
|
+
const patch: ToolchainConfigPatch = tool === 'kicad'
|
|
518
|
+
? {
|
|
519
|
+
kicadRoot: canonicalPath(outcome.installPath),
|
|
520
|
+
kicadVersion: probe.version ?? entry.version,
|
|
521
|
+
kicadSource: 'download',
|
|
522
|
+
kicadValidatedAt: this.now(),
|
|
523
|
+
}
|
|
524
|
+
: {
|
|
525
|
+
solidcraftRoot: canonicalPath(outcome.installPath),
|
|
526
|
+
solidcraftVersion: probe.version ?? entry.version,
|
|
527
|
+
solidcraftSource: 'download',
|
|
528
|
+
solidcraftValidatedAt: this.now(),
|
|
529
|
+
}
|
|
530
|
+
await this.options.persist?.(patch)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** One job's snapshot. */
|
|
534
|
+
job(id: string): InstallJobSnapshot | undefined {
|
|
535
|
+
return this.installer.snapshot(id)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Cancel one running job. */
|
|
539
|
+
cancel(id: string): boolean {
|
|
540
|
+
return this.installer.cancel(id)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Open the native chooser for one tool.
|
|
545
|
+
*
|
|
546
|
+
* The plugin-owned file dialog answers first (it can filter to the expected
|
|
547
|
+
* executable); when it is unavailable the Host's own native folder chooser
|
|
548
|
+
* answers instead, so a native picker exists on every supported desktop.
|
|
549
|
+
* @param tool - which toolchain the choice is for.
|
|
550
|
+
* @param signal - request lifetime; abort dismisses the chooser.
|
|
551
|
+
* @returns the picked path, a cancellation, or an unsupported notice.
|
|
552
|
+
*/
|
|
553
|
+
async browse(tool: ToolchainId, signal?: AbortSignal): Promise<BrowseOutcome> {
|
|
554
|
+
const request = {
|
|
555
|
+
title: tool === 'kicad' ? 'Select kicad-cli' : 'Select SolidCraft',
|
|
556
|
+
filters: TOOLCHAIN_FILTERS[tool],
|
|
557
|
+
}
|
|
558
|
+
const dialog = this.options.pickFile ?? pickFile
|
|
559
|
+
const outcome = await dialog(request, {
|
|
560
|
+
...(signal === undefined ? {} : { signal }),
|
|
561
|
+
})
|
|
562
|
+
if (outcome.kind === 'picked') return { kind: 'picked', path: outcome.path, mechanism: outcome.mechanism }
|
|
563
|
+
if (outcome.kind === 'cancelled') return { kind: 'cancelled', mechanism: outcome.mechanism }
|
|
564
|
+
if (this.options.pickFolder !== undefined) {
|
|
565
|
+
const folder = await this.options.pickFolder(request.title)
|
|
566
|
+
if (folder !== null) return { kind: 'picked', path: folder, mechanism: 'dsh-directory-picker' }
|
|
567
|
+
return { kind: 'cancelled', mechanism: 'dsh-directory-picker' }
|
|
568
|
+
}
|
|
569
|
+
return { kind: 'unsupported', message: outcome.message }
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Compact capability facts for the agent (§12.1).
|
|
574
|
+
*
|
|
575
|
+
* Versions and availability only: an agent needs to know whether an export is
|
|
576
|
+
* possible, not where the binary lives, and the tools resolve the executable
|
|
577
|
+
* themselves from the same configuration.
|
|
578
|
+
*/
|
|
579
|
+
async capabilities(): Promise<{
|
|
580
|
+
readonly kicad: { readonly available: boolean; readonly version?: string }
|
|
581
|
+
readonly solidcraft: { readonly available: boolean; readonly version?: string; readonly cliAvailable: boolean }
|
|
582
|
+
}> {
|
|
583
|
+
const [kicad, solidcraft] = await Promise.all([this.toolStatus('kicad'), this.toolStatus('solidcraft')])
|
|
584
|
+
return {
|
|
585
|
+
kicad: {
|
|
586
|
+
available: kicad.state === 'configured',
|
|
587
|
+
...(kicad.version === undefined ? {} : { version: kicad.version }),
|
|
588
|
+
},
|
|
589
|
+
solidcraft: {
|
|
590
|
+
available: solidcraft.state === 'configured',
|
|
591
|
+
...(solidcraft.version === undefined ? {} : { version: solidcraft.version }),
|
|
592
|
+
cliAvailable: solidcraft.state === 'configured' && solidcraft.cliPath !== undefined,
|
|
593
|
+
},
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|