@huaqiu/hardware-artifacts 0.7.17 → 0.7.19
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 +594 -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,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A real native file chooser, owned by this plugin.
|
|
3
|
+
*
|
|
4
|
+
* The spec requires an actual OS dialog, not a simulated one (§4.5/§6.2), and
|
|
5
|
+
* the Host's own `directoryPicker` seam can only choose *directories* while this
|
|
6
|
+
* feature must also accept an executable. Rather than change a shared Host
|
|
7
|
+
* capability for one caller, the plugin drives the platform's own dialog in a
|
|
8
|
+
* child process: `IFileOpenDialog` through the Windows PowerShell host, an
|
|
9
|
+
* `NSOpenPanel` through the system JXA host, and `zenity`/`kdialog` on Linux.
|
|
10
|
+
* Nothing is rendered by the browser, and nothing is fabricated: a cancelled
|
|
11
|
+
* dialog reports cancellation.
|
|
12
|
+
*
|
|
13
|
+
* The directory-only case stays on the Host's seam (see the plugin's
|
|
14
|
+
* `browse` route), so a machine where this picker is unavailable still offers a
|
|
15
|
+
* native chooser backed by DSH itself.
|
|
16
|
+
*
|
|
17
|
+
* @module @huaqiu/hardware-artifacts/toolchain/dialog
|
|
18
|
+
*/
|
|
19
|
+
import { execFile } from 'node:child_process'
|
|
20
|
+
|
|
21
|
+
/** One file-type filter row. */
|
|
22
|
+
export interface FileFilter {
|
|
23
|
+
readonly name: string
|
|
24
|
+
/** Glob patterns, e.g. `['kicad-cli.exe']`. */
|
|
25
|
+
readonly patterns: readonly string[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What to ask the operator for. */
|
|
29
|
+
export interface PickFileRequest {
|
|
30
|
+
readonly title: string
|
|
31
|
+
readonly filters?: readonly FileFilter[]
|
|
32
|
+
readonly initialDirectory?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** How the chooser ended. */
|
|
36
|
+
export type PickFileResult =
|
|
37
|
+
| { readonly kind: 'picked'; readonly path: string; readonly mechanism: string }
|
|
38
|
+
| { readonly kind: 'cancelled'; readonly mechanism: string }
|
|
39
|
+
| { readonly kind: 'unsupported'; readonly message: string }
|
|
40
|
+
|
|
41
|
+
/** Injectable command runner so every platform branch is testable. */
|
|
42
|
+
export type DialogRunner = (
|
|
43
|
+
command: string,
|
|
44
|
+
args: readonly string[],
|
|
45
|
+
options: { readonly timeoutMs: number; readonly signal?: AbortSignal },
|
|
46
|
+
) => Promise<{ readonly stdout: string; readonly stderr: string; readonly exitCode: number | null }>
|
|
47
|
+
|
|
48
|
+
/** Default runner: a child process, never a shell (§6.2 of the runtime constraints). */
|
|
49
|
+
const defaultRunner: DialogRunner = (command, args, options) => new Promise((resolvePromise, reject) => {
|
|
50
|
+
execFile(command, [...args], {
|
|
51
|
+
timeout: options.timeoutMs,
|
|
52
|
+
encoding: 'utf8',
|
|
53
|
+
windowsHide: true,
|
|
54
|
+
shell: false,
|
|
55
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
56
|
+
}, (error, stdout, stderr) => {
|
|
57
|
+
if (error === null) {
|
|
58
|
+
resolvePromise({ stdout: String(stdout), stderr: String(stderr), exitCode: 0 })
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
const code = (error as { code?: unknown }).code
|
|
62
|
+
if (typeof code === 'number') {
|
|
63
|
+
resolvePromise({ stdout: String(stdout), stderr: String(stderr), exitCode: code })
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
reject(error)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/** PowerShell single-quoted literal: only `'` needs doubling. */
|
|
71
|
+
function quotePowerShell(value: string): string {
|
|
72
|
+
return `'${value.replaceAll("'", "''")}'`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One platform's filter string (`Windows` requires `name|pattern|name|pattern`). */
|
|
76
|
+
function windowsFilter(filters: readonly FileFilter[]): string {
|
|
77
|
+
const rows = filters.map(filter => `${filter.name}|${filter.patterns.join(';')}`)
|
|
78
|
+
rows.push('All files (*.*)|*.*')
|
|
79
|
+
return rows.join('|')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Windows dialog script; the whole conversation runs in one child process. */
|
|
83
|
+
function windowsScript(request: PickFileRequest): string {
|
|
84
|
+
return [
|
|
85
|
+
'$ErrorActionPreference = "Stop"',
|
|
86
|
+
'Add-Type -AssemblyName System.Windows.Forms',
|
|
87
|
+
'$dialog = New-Object System.Windows.Forms.OpenFileDialog',
|
|
88
|
+
`$dialog.Title = ${quotePowerShell(request.title)}`,
|
|
89
|
+
`$dialog.Filter = ${quotePowerShell(windowsFilter(request.filters ?? []))}`,
|
|
90
|
+
'$dialog.CheckFileExists = $true',
|
|
91
|
+
'$dialog.Multiselect = $false',
|
|
92
|
+
...(request.initialDirectory === undefined || request.initialDirectory === ''
|
|
93
|
+
? []
|
|
94
|
+
: [
|
|
95
|
+
'try {',
|
|
96
|
+
` $dialog.InitialDirectory = ${quotePowerShell(request.initialDirectory)}`,
|
|
97
|
+
'} catch { }',
|
|
98
|
+
]),
|
|
99
|
+
// A process spawned by a background host cannot take the foreground; an
|
|
100
|
+
// invisible topmost owner is what makes the dialog appear in front of it.
|
|
101
|
+
'$owner = New-Object System.Windows.Forms.Form',
|
|
102
|
+
'$owner.TopMost = $true',
|
|
103
|
+
'$owner.ShowInTaskbar = $false',
|
|
104
|
+
'$owner.WindowState = [System.Windows.Forms.FormWindowState]::Minimized',
|
|
105
|
+
'$owner.Opacity = 0',
|
|
106
|
+
'try {',
|
|
107
|
+
' if ($dialog.ShowDialog($owner) -eq [System.Windows.Forms.DialogResult]::OK) {',
|
|
108
|
+
' [Console]::Out.WriteLine($dialog.FileName)',
|
|
109
|
+
' } else {',
|
|
110
|
+
' exit 2',
|
|
111
|
+
' }',
|
|
112
|
+
'} finally {',
|
|
113
|
+
' $owner.Dispose()',
|
|
114
|
+
'}',
|
|
115
|
+
].join('\n')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** macOS `NSOpenPanel` source (JXA), with the copy encoded as JSON literals. */
|
|
119
|
+
function macPanelScript(request: PickFileRequest): string {
|
|
120
|
+
const title = JSON.stringify(request.title)
|
|
121
|
+
const directory = JSON.stringify(request.initialDirectory ?? '')
|
|
122
|
+
return `ObjC.import('AppKit');
|
|
123
|
+
function run() {
|
|
124
|
+
var app = $.NSApplication.sharedApplication;
|
|
125
|
+
app.setActivationPolicy($.NSApplicationActivationPolicyAccessory);
|
|
126
|
+
var panel = $.NSOpenPanel.openPanel;
|
|
127
|
+
panel.canChooseFiles = true;
|
|
128
|
+
panel.canChooseDirectories = false;
|
|
129
|
+
panel.allowsMultipleSelection = false;
|
|
130
|
+
panel.message = ${title};
|
|
131
|
+
var start = ${directory};
|
|
132
|
+
if (start !== '') panel.directoryURL = $.NSURL.fileURLWithPath(start);
|
|
133
|
+
$.NSOperationQueue.mainQueue.addOperationWithBlock(function() {
|
|
134
|
+
app.activateIgnoringOtherApps(true);
|
|
135
|
+
});
|
|
136
|
+
if (panel.runModal !== $.NSModalResponseOK) return '';
|
|
137
|
+
var urls = panel.URLs;
|
|
138
|
+
if (urls.count < 1) return '';
|
|
139
|
+
return ObjC.unwrap(urls.objectAtIndex(0).path);
|
|
140
|
+
}`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function firstLine(text: string): string {
|
|
144
|
+
return text.replace(/[\r\n]+$/u, '').split(/\r?\n/u)[0] ?? ''
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Map a missing binary into the "unsupported" verdict instead of a crash. */
|
|
148
|
+
function isMissingBinary(error: unknown): boolean {
|
|
149
|
+
return (error as { code?: unknown } | null)?.code === 'ENOENT'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Show the platform's file chooser and wait for the operator.
|
|
154
|
+
* @param request - title, filters and starting directory.
|
|
155
|
+
* @param internals - platform/runner/timeout injection.
|
|
156
|
+
* @returns the picked path, a cancellation, or an unsupported-platform notice.
|
|
157
|
+
*/
|
|
158
|
+
export async function pickFile(
|
|
159
|
+
request: PickFileRequest,
|
|
160
|
+
internals: {
|
|
161
|
+
readonly platform?: NodeJS.Platform
|
|
162
|
+
readonly run?: DialogRunner
|
|
163
|
+
readonly timeoutMs?: number
|
|
164
|
+
readonly signal?: AbortSignal
|
|
165
|
+
} = {},
|
|
166
|
+
): Promise<PickFileResult> {
|
|
167
|
+
const platform = internals.platform ?? process.platform
|
|
168
|
+
const run = internals.run ?? defaultRunner
|
|
169
|
+
const timeoutMs = internals.timeoutMs ?? 15 * 60_000
|
|
170
|
+
const options = { timeoutMs, ...(internals.signal === undefined ? {} : { signal: internals.signal }) }
|
|
171
|
+
|
|
172
|
+
if (platform === 'win32') {
|
|
173
|
+
const encoded = Buffer.from(windowsScript(request), 'utf16le').toString('base64')
|
|
174
|
+
const result = await run('powershell.exe', [
|
|
175
|
+
'-NoProfile', '-NonInteractive', '-STA', '-WindowStyle', 'Hidden',
|
|
176
|
+
'-ExecutionPolicy', 'Bypass', '-EncodedCommand', encoded,
|
|
177
|
+
], options)
|
|
178
|
+
if (result.exitCode === 2) return { kind: 'cancelled', mechanism: 'win32' }
|
|
179
|
+
if (result.exitCode !== 0) {
|
|
180
|
+
return { kind: 'unsupported', message: result.stderr.trim() === '' ? `dialog exited with ${String(result.exitCode)}` : result.stderr.trim() }
|
|
181
|
+
}
|
|
182
|
+
const path = firstLine(result.stdout)
|
|
183
|
+
return path === '' ? { kind: 'cancelled', mechanism: 'win32' } : { kind: 'picked', path, mechanism: 'win32' }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (platform === 'darwin') {
|
|
187
|
+
try {
|
|
188
|
+
const result = await run('osascript', ['-l', 'JavaScript', '-e', macPanelScript(request)], options)
|
|
189
|
+
const path = firstLine(result.stdout)
|
|
190
|
+
return path === '' ? { kind: 'cancelled', mechanism: 'darwin' } : { kind: 'picked', path, mechanism: 'darwin' }
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (isMissingBinary(error)) return { kind: 'unsupported', message: 'osascript is unavailable' }
|
|
193
|
+
throw error
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (platform === 'linux') {
|
|
198
|
+
const filters = (request.filters ?? []).flatMap(filter =>
|
|
199
|
+
filter.patterns.map(pattern => `--file-filter=${filter.name} | ${pattern}`))
|
|
200
|
+
try {
|
|
201
|
+
const result = await run('zenity', [
|
|
202
|
+
'--file-selection', `--title=${request.title}`,
|
|
203
|
+
...(request.initialDirectory === undefined || request.initialDirectory === ''
|
|
204
|
+
? []
|
|
205
|
+
: [`--filename=${request.initialDirectory}`]),
|
|
206
|
+
...filters,
|
|
207
|
+
], options)
|
|
208
|
+
if (result.exitCode === 1) return { kind: 'cancelled', mechanism: 'zenity' }
|
|
209
|
+
const path = firstLine(result.stdout)
|
|
210
|
+
return path === '' ? { kind: 'cancelled', mechanism: 'zenity' } : { kind: 'picked', path, mechanism: 'zenity' }
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (!isMissingBinary(error)) throw error
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
const result = await run('kdialog', [
|
|
216
|
+
'--title', request.title,
|
|
217
|
+
...(request.initialDirectory === undefined || request.initialDirectory === ''
|
|
218
|
+
? []
|
|
219
|
+
: [request.initialDirectory]),
|
|
220
|
+
...(request.filters ?? []).flatMap(filter => filter.patterns),
|
|
221
|
+
], options)
|
|
222
|
+
if (result.exitCode === 1) return { kind: 'cancelled', mechanism: 'kdialog' }
|
|
223
|
+
const path = firstLine(result.stdout)
|
|
224
|
+
return path === '' ? { kind: 'cancelled', mechanism: 'kdialog' } : { kind: 'picked', path, mechanism: 'kdialog' }
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (isMissingBinary(error)) return { kind: 'unsupported', message: 'no native file chooser is installed (zenity or kdialog)' }
|
|
227
|
+
throw error
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return { kind: 'unsupported', message: `no native file chooser on ${platform}` }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Filters the two toolchains accept, per §6.2's suggested list. */
|
|
235
|
+
export const TOOLCHAIN_FILTERS: Readonly<Record<'kicad' | 'solidcraft', readonly FileFilter[]>> = Object.freeze({
|
|
236
|
+
kicad: Object.freeze([{ name: 'KiCad CLI (kicad-cli.exe)', patterns: Object.freeze(['kicad-cli.exe', 'kicad-cli']) }]),
|
|
237
|
+
solidcraft: Object.freeze([
|
|
238
|
+
{ name: 'SolidCraft (solidcraft*.exe)', patterns: Object.freeze(['solidcraft.exe', 'solidcraft-cli.exe', 'solidcraft']) },
|
|
239
|
+
]),
|
|
240
|
+
})
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded background discovery of already-installed toolchains.
|
|
3
|
+
*
|
|
4
|
+
* Discovery exists so a user who already has KiCad is never asked where it is,
|
|
5
|
+
* and it must not cost anything if it finds nothing: the candidate set is a
|
|
6
|
+
* short list of installation conventions (§5.1), each candidate is one `stat`
|
|
7
|
+
* plus at most one version probe, and the whole run happens off the request
|
|
8
|
+
* that started it (§4.2/§5.2). Nothing here recurses a drive.
|
|
9
|
+
*
|
|
10
|
+
* @module @huaqiu/hardware-artifacts/toolchain/discovery
|
|
11
|
+
*/
|
|
12
|
+
import { readdirSync, statSync } from 'node:fs'
|
|
13
|
+
import { join, resolve } from 'node:path'
|
|
14
|
+
import { installRootFromExecutable, resolveToolchain, type ToolchainId } from './resolve.js'
|
|
15
|
+
|
|
16
|
+
/** Where one candidate came from; used for display and for the tie-break. */
|
|
17
|
+
export type CandidateOrigin = 'managed' | 'system' | 'user-local'
|
|
18
|
+
|
|
19
|
+
/** One installation directory discovery looked at. */
|
|
20
|
+
export interface DiscoveryCandidate {
|
|
21
|
+
/** Installation root that was checked. */
|
|
22
|
+
readonly path: string
|
|
23
|
+
readonly origin: CandidateOrigin
|
|
24
|
+
/** True only when the executable exists and answered a version probe. */
|
|
25
|
+
readonly valid: boolean
|
|
26
|
+
readonly version?: string
|
|
27
|
+
/** Normalised CLI the candidate resolved to, when it produced one. */
|
|
28
|
+
readonly cli?: string
|
|
29
|
+
readonly error?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Discovery state for one tool, as the UI shows it. */
|
|
33
|
+
export interface ToolDiscovery {
|
|
34
|
+
readonly tool: ToolchainId
|
|
35
|
+
readonly state: 'idle' | 'running' | 'done' | 'failed'
|
|
36
|
+
readonly candidates: readonly DiscoveryCandidate[]
|
|
37
|
+
/** The candidate discovery would configure, by the documented policy. */
|
|
38
|
+
readonly selected?: DiscoveryCandidate
|
|
39
|
+
readonly error?: string
|
|
40
|
+
readonly finishedAt?: number
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** One place discovery will look, with its provenance. */
|
|
44
|
+
export interface CandidateRoot {
|
|
45
|
+
readonly path: string
|
|
46
|
+
readonly origin: CandidateOrigin
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Facts the candidate list is a pure function of. */
|
|
50
|
+
export interface DiscoveryEnvironment {
|
|
51
|
+
readonly platform?: NodeJS.Platform
|
|
52
|
+
/** Process environment (`ProgramFiles`, `LOCALAPPDATA`, …). */
|
|
53
|
+
readonly env?: Readonly<Record<string, string | undefined>>
|
|
54
|
+
/** The per-user toolchain root the installer promotes into. */
|
|
55
|
+
readonly managedRoot: string
|
|
56
|
+
/** User home directory. */
|
|
57
|
+
readonly home: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Filesystem/probe injection so discovery is testable without a machine. */
|
|
61
|
+
export interface DiscoveryInternals {
|
|
62
|
+
readonly readdir?: (path: string) => readonly string[]
|
|
63
|
+
readonly exists?: (path: string) => boolean
|
|
64
|
+
readonly probeVersion?: (executable: string, tool: ToolchainId) => string | undefined
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** How many version-suffixed subdirectories one install prefix may contribute. */
|
|
68
|
+
const MAX_VERSIONED_DIRECTORIES = 3
|
|
69
|
+
|
|
70
|
+
function isDirectory(path: string, internals: DiscoveryInternals): boolean {
|
|
71
|
+
if (internals.exists !== undefined) return internals.exists(path)
|
|
72
|
+
try {
|
|
73
|
+
return statSync(path).isDirectory()
|
|
74
|
+
} catch {
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function listDirectories(path: string, internals: DiscoveryInternals): string[] {
|
|
80
|
+
if (internals.readdir !== undefined) return [...internals.readdir(path)]
|
|
81
|
+
try {
|
|
82
|
+
return readdirSync(path, { withFileTypes: true })
|
|
83
|
+
.filter(entry => entry.isDirectory())
|
|
84
|
+
.map(entry => entry.name)
|
|
85
|
+
} catch {
|
|
86
|
+
return []
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Version-ordered directory names, newest first, capped. */
|
|
91
|
+
function versionedDirectories(path: string, internals: DiscoveryInternals): string[] {
|
|
92
|
+
const names = listDirectories(path, internals).filter(name => /^\d/u.test(name))
|
|
93
|
+
names.sort((left, right) => compareVersions(right, left))
|
|
94
|
+
return names.slice(0, MAX_VERSIONED_DIRECTORIES)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Installation roots discovery should look at, most specific first.
|
|
99
|
+
*
|
|
100
|
+
* The managed root comes first because a distribution the product itself
|
|
101
|
+
* installed is the one it can reason about; system and user-local conventions
|
|
102
|
+
* follow, and only their version-suffixed children are considered (§5.1). This
|
|
103
|
+
* is a fixed, documented list — not a scan (§5.2).
|
|
104
|
+
* @param tool - which toolchain.
|
|
105
|
+
* @param environment - platform/env/managed root/home.
|
|
106
|
+
* @param internals - filesystem injection.
|
|
107
|
+
* @returns candidate roots in precedence order, de-duplicated.
|
|
108
|
+
*/
|
|
109
|
+
export function candidateRoots(
|
|
110
|
+
tool: ToolchainId,
|
|
111
|
+
environment: DiscoveryEnvironment,
|
|
112
|
+
internals: DiscoveryInternals = {},
|
|
113
|
+
): CandidateRoot[] {
|
|
114
|
+
const platform = environment.platform ?? process.platform
|
|
115
|
+
const env = environment.env ?? process.env
|
|
116
|
+
const roots: CandidateRoot[] = []
|
|
117
|
+
const push = (path: string, origin: CandidateOrigin): void => { roots.push({ path: resolve(path), origin }) }
|
|
118
|
+
|
|
119
|
+
const managed = join(environment.managedRoot, tool)
|
|
120
|
+
for (const version of versionedDirectories(managed, internals)) push(join(managed, version), 'managed')
|
|
121
|
+
if (isDirectory(managed, internals)) push(managed, 'managed')
|
|
122
|
+
|
|
123
|
+
const toolDirectory = tool === 'kicad' ? 'KiCad' : 'SolidCraft'
|
|
124
|
+
if (platform === 'win32') {
|
|
125
|
+
for (const prefix of [env.ProgramFiles, env['ProgramFiles(x86)']]) {
|
|
126
|
+
if (prefix === undefined || prefix === '') continue
|
|
127
|
+
const base = join(prefix, toolDirectory)
|
|
128
|
+
for (const version of versionedDirectories(base, internals)) push(join(base, version), 'system')
|
|
129
|
+
if (tool === 'solidcraft' && isDirectory(base, internals)) push(base, 'system')
|
|
130
|
+
}
|
|
131
|
+
const local = env.LOCALAPPDATA
|
|
132
|
+
if (local !== undefined && local !== '') {
|
|
133
|
+
const base = join(local, 'Programs', toolDirectory)
|
|
134
|
+
for (const version of versionedDirectories(base, internals)) push(join(base, version), 'user-local')
|
|
135
|
+
if (tool === 'solidcraft' && isDirectory(base, internals)) push(base, 'user-local')
|
|
136
|
+
}
|
|
137
|
+
} else if (platform === 'darwin') {
|
|
138
|
+
for (const base of ['/Applications', join(environment.home, 'Applications')]) {
|
|
139
|
+
const origin: CandidateOrigin = base.startsWith('/Applications') ? 'system' : 'user-local'
|
|
140
|
+
push(join(base, toolDirectory), origin)
|
|
141
|
+
push(join(base, `${toolDirectory}.app`), origin)
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
const bases: { readonly base: string; readonly origin: CandidateOrigin }[] = [
|
|
145
|
+
{ base: '/usr/local', origin: 'system' },
|
|
146
|
+
{ base: '/usr', origin: 'system' },
|
|
147
|
+
{ base: join(environment.home, '.local'), origin: 'user-local' },
|
|
148
|
+
]
|
|
149
|
+
for (const { base, origin } of bases) {
|
|
150
|
+
push(join(base, 'bin'), origin)
|
|
151
|
+
push(join(base, tool === 'kicad' ? 'kicad' : 'solidcraft'), origin)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const unique: CandidateRoot[] = []
|
|
156
|
+
const seen = new Set<string>()
|
|
157
|
+
for (const root of roots) {
|
|
158
|
+
const key = root.path.toLowerCase()
|
|
159
|
+
if (seen.has(key)) continue
|
|
160
|
+
seen.add(key)
|
|
161
|
+
unique.push(root)
|
|
162
|
+
}
|
|
163
|
+
return unique
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Validate candidates and choose one deterministically.
|
|
168
|
+
*
|
|
169
|
+
* Policy (§5.4/§5.2): a managed installation outranks a system one, then the
|
|
170
|
+
* highest reported version wins, then candidate order. A candidate is valid
|
|
171
|
+
* only when its executable answered a version probe, so a stale directory that
|
|
172
|
+
* merely exists never wins.
|
|
173
|
+
* @param tool - which toolchain.
|
|
174
|
+
* @param roots - candidate roots in precedence order.
|
|
175
|
+
* @param internals - probe/fs injection.
|
|
176
|
+
* @returns every candidate's verdict, plus the one discovery would configure.
|
|
177
|
+
*/
|
|
178
|
+
export function validateCandidates(
|
|
179
|
+
tool: ToolchainId,
|
|
180
|
+
roots: readonly CandidateRoot[],
|
|
181
|
+
internals: DiscoveryInternals = {},
|
|
182
|
+
): { candidates: DiscoveryCandidate[]; selected?: DiscoveryCandidate } {
|
|
183
|
+
const candidates: DiscoveryCandidate[] = []
|
|
184
|
+
for (const root of roots) {
|
|
185
|
+
const resolution = resolveToolchain(tool, root.path, {
|
|
186
|
+
...(internals.exists === undefined ? {} : { exists: internals.exists }),
|
|
187
|
+
...(internals.probeVersion === undefined ? {} : {
|
|
188
|
+
probeVersion: (executable: string) => internals.probeVersion?.(executable, tool),
|
|
189
|
+
}),
|
|
190
|
+
})
|
|
191
|
+
// Point the candidate at the root that actually owns the executable, so what
|
|
192
|
+
// gets stored is the installation and not one of its subdirectories.
|
|
193
|
+
const executable = resolution.cli ?? resolution.app
|
|
194
|
+
const effective = executable === undefined ? root.path : installRootFromExecutable(executable)
|
|
195
|
+
candidates.push({
|
|
196
|
+
path: effective,
|
|
197
|
+
origin: root.origin,
|
|
198
|
+
valid: resolution.configured,
|
|
199
|
+
...(resolution.version === undefined ? {} : { version: resolution.version }),
|
|
200
|
+
...(resolution.cli === undefined ? {} : { cli: resolution.cli }),
|
|
201
|
+
...(resolution.error === undefined ? {} : { error: resolution.error }),
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const valid = candidates.filter(candidate => candidate.valid)
|
|
206
|
+
const ranked = [...valid].sort((left, right) => {
|
|
207
|
+
if (left.origin !== right.origin) {
|
|
208
|
+
const weight = (origin: CandidateOrigin): number => (origin === 'managed' ? 0 : origin === 'system' ? 1 : 2)
|
|
209
|
+
return weight(left.origin) - weight(right.origin)
|
|
210
|
+
}
|
|
211
|
+
const byVersion = compareVersions(right.version, left.version)
|
|
212
|
+
return byVersion !== 0 ? byVersion : candidates.indexOf(left) - candidates.indexOf(right)
|
|
213
|
+
})
|
|
214
|
+
const selected = ranked[0]
|
|
215
|
+
return { candidates, ...(selected === undefined ? {} : { selected }) }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Compare two version strings, tolerant of packaging suffixes. */
|
|
219
|
+
export function compareVersions(left: string | undefined, right: string | undefined): number {
|
|
220
|
+
if (left === undefined || right === undefined) return left === right ? 0 : left === undefined ? -1 : 1
|
|
221
|
+
const split = (value: string): (number | string)[] =>
|
|
222
|
+
value.split(/[.+-]/u).map(part => (/^\d+$/u.test(part) ? Number(part) : part))
|
|
223
|
+
const a = split(left)
|
|
224
|
+
const b = split(right)
|
|
225
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
226
|
+
const x = a[index]
|
|
227
|
+
const y = b[index]
|
|
228
|
+
if (x === undefined) return -1
|
|
229
|
+
if (y === undefined) return 1
|
|
230
|
+
if (typeof x === 'number' && typeof y === 'number') {
|
|
231
|
+
if (x !== y) return x - y
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
const sx = String(x)
|
|
235
|
+
const sy = String(y)
|
|
236
|
+
if (sx !== sy) return sx < sy ? -1 : 1
|
|
237
|
+
}
|
|
238
|
+
return 0
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Probe one directory the user named or a dialog returned.
|
|
243
|
+
* @param tool - which toolchain.
|
|
244
|
+
* @param root - installation root to probe.
|
|
245
|
+
* @param internals - probe injection.
|
|
246
|
+
* @returns validity with the reason, for the "Validate and save" error path.
|
|
247
|
+
*/
|
|
248
|
+
export function probeInstallation(
|
|
249
|
+
tool: ToolchainId,
|
|
250
|
+
root: string,
|
|
251
|
+
internals: DiscoveryInternals = {},
|
|
252
|
+
): { ok: boolean; version?: string; error?: string; cli?: string; app?: string } {
|
|
253
|
+
const resolution = resolveToolchain(tool, root, {
|
|
254
|
+
...(internals.exists === undefined ? {} : { exists: internals.exists }),
|
|
255
|
+
...(internals.probeVersion === undefined ? {} : {
|
|
256
|
+
probeVersion: (executable: string) => internals.probeVersion?.(executable, tool),
|
|
257
|
+
}),
|
|
258
|
+
})
|
|
259
|
+
return {
|
|
260
|
+
ok: resolution.configured,
|
|
261
|
+
...(resolution.version === undefined ? {} : { version: resolution.version }),
|
|
262
|
+
...(resolution.cli === undefined ? {} : { cli: resolution.cli }),
|
|
263
|
+
...(resolution.app === undefined ? {} : { app: resolution.app }),
|
|
264
|
+
...(resolution.error === undefined ? {} : { error: resolution.error }),
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public surface of the toolchain capability.
|
|
3
|
+
* @module @huaqiu/hardware-artifacts/toolchain
|
|
4
|
+
*/
|
|
5
|
+
export {
|
|
6
|
+
OFFICIAL_DOWNLOAD_PAGES,
|
|
7
|
+
ToolchainManager,
|
|
8
|
+
defaultManagedRoot,
|
|
9
|
+
type BrowseOutcome,
|
|
10
|
+
type ToolchainConfigPatch,
|
|
11
|
+
type ToolchainManagerOptions,
|
|
12
|
+
type ToolchainSettings,
|
|
13
|
+
type ToolchainStatus,
|
|
14
|
+
type ToolStatus,
|
|
15
|
+
type ValidationOutcome,
|
|
16
|
+
} from './manager.js'
|
|
17
|
+
export {
|
|
18
|
+
MANIFEST_SCHEMA_VERSION, MANIFEST_URL, fetchManifest, parseManifest,
|
|
19
|
+
type ManifestEntry, type ManifestFailure, type ManifestResult, type ToolchainManifest,
|
|
20
|
+
} from './manifest.js'
|
|
21
|
+
export {
|
|
22
|
+
candidateRoots, compareVersions, probeInstallation, validateCandidates,
|
|
23
|
+
type CandidateOrigin, type CandidateRoot, type DiscoveryCandidate, type DiscoveryInternals,
|
|
24
|
+
type ToolDiscovery,
|
|
25
|
+
} from './discovery.js'
|
|
26
|
+
export {
|
|
27
|
+
ToolchainInstaller, expectedLayout,
|
|
28
|
+
type InstallErrorCode, type InstallJobSnapshot, type InstallOutcome, type InstallPhase,
|
|
29
|
+
} from './install.js'
|
|
30
|
+
export {
|
|
31
|
+
canonicalPath, executableCandidates, installRootFromExecutable, installationRootOf, label,
|
|
32
|
+
normalizeVersion, resolveToolchain, sameRelease,
|
|
33
|
+
type ToolchainId, type ToolchainResolution, type ToolchainSource,
|
|
34
|
+
} from './resolve.js'
|
|
35
|
+
export { commonDirectory, extractZip, readZipDirectory, safeEntrySegments, ZipError, type ZipDirectory } from './zip.js'
|
|
36
|
+
export { pickFile, TOOLCHAIN_FILTERS, type FileFilter, type PickFileResult } from './dialog.js'
|