@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,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed reader for the HuaQiu toolchain manifest.
|
|
3
|
+
*
|
|
4
|
+
* The manifest at `https://kicad.eda.cn/agent-toolchains.json` is the only
|
|
5
|
+
* authority on downloadable versions, checksums and URLs — the UI and the
|
|
6
|
+
* installer never hardcode them (§7.1). Everything that reaches an archive
|
|
7
|
+
* download passes through here first, so this module is where an unsupported
|
|
8
|
+
* schema version, a wrong platform entry or a malformed checksum is refused
|
|
9
|
+
* (§7.3) rather than two steps later inside a half-written file.
|
|
10
|
+
*
|
|
11
|
+
* @module @huaqiu/hardware-artifacts/toolchain/manifest
|
|
12
|
+
*/
|
|
13
|
+
import type { ToolchainId } from './resolve.js'
|
|
14
|
+
|
|
15
|
+
/** The manifest the installer trusts today. */
|
|
16
|
+
export const MANIFEST_URL = 'https://kicad.eda.cn/agent-toolchains.json'
|
|
17
|
+
|
|
18
|
+
/** Schema version this reader understands. */
|
|
19
|
+
export const MANIFEST_SCHEMA_VERSION = 1
|
|
20
|
+
|
|
21
|
+
/** One downloadable distribution. */
|
|
22
|
+
export interface ManifestEntry {
|
|
23
|
+
/** Distributor version string, e.g. `10.0.7-2`. */
|
|
24
|
+
readonly version: string
|
|
25
|
+
/** Lower-case hex SHA-256 of the archive the URL serves. */
|
|
26
|
+
readonly sha256: string
|
|
27
|
+
/** Platform the archive targets (`win` | `darwin` | `linux`). */
|
|
28
|
+
readonly platform: string
|
|
29
|
+
/** Architecture the archive targets (`x64` | `arm64`). */
|
|
30
|
+
readonly arch: string
|
|
31
|
+
/** HTTPS URL of the archive. */
|
|
32
|
+
readonly url: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A manifest that passed validation, with the per-tool selections. */
|
|
36
|
+
export interface ToolchainManifest {
|
|
37
|
+
readonly schemaVersion: number
|
|
38
|
+
/** Every validated entry, per tool, in manifest order. */
|
|
39
|
+
readonly entries: Readonly<Record<ToolchainId, readonly ManifestEntry[]>>
|
|
40
|
+
/** The entry this host may install, per tool (absent when none matches). */
|
|
41
|
+
readonly selected: Readonly<Partial<Record<ToolchainId, ManifestEntry>>>
|
|
42
|
+
/** Platform/architecture the selection was made for. */
|
|
43
|
+
readonly target: { readonly platform: string; readonly arch: string }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Why the manifest could not be used. */
|
|
47
|
+
export type ManifestFailureCode =
|
|
48
|
+
| 'network'
|
|
49
|
+
| 'http'
|
|
50
|
+
| 'invalid-json'
|
|
51
|
+
| 'unsupported-schema'
|
|
52
|
+
| 'invalid-entry'
|
|
53
|
+
| 'unsupported-platform'
|
|
54
|
+
| 'no-entry'
|
|
55
|
+
|
|
56
|
+
/** A manifest this host cannot install from, with an operator-facing reason. */
|
|
57
|
+
export interface ManifestFailure {
|
|
58
|
+
readonly ok: false
|
|
59
|
+
readonly code: ManifestFailureCode
|
|
60
|
+
readonly message: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A manifest this host may install from. */
|
|
64
|
+
export interface ManifestSuccess {
|
|
65
|
+
readonly ok: true
|
|
66
|
+
readonly manifest: ToolchainManifest
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Outcome of reading the manifest. */
|
|
70
|
+
export type ManifestResult = ManifestSuccess | ManifestFailure
|
|
71
|
+
|
|
72
|
+
/** Manifest platform token for the host platform, or undefined when unsupported. */
|
|
73
|
+
export function manifestPlatformOf(platform: NodeJS.Platform): string | undefined {
|
|
74
|
+
if (platform === 'win32') return 'win'
|
|
75
|
+
if (platform === 'darwin') return 'darwin'
|
|
76
|
+
if (platform === 'linux') return 'linux'
|
|
77
|
+
return undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Manifest architecture token for the host architecture. */
|
|
81
|
+
export function manifestArchOf(arch: string): string {
|
|
82
|
+
if (arch === 'arm64') return 'arm64'
|
|
83
|
+
if (arch === 'ia32' || arch === 'x64') return 'x64'
|
|
84
|
+
return arch
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const TOOLCHAIN_IDS: readonly ToolchainId[] = ['kicad', 'solidcraft']
|
|
88
|
+
const VERSION_PATTERN = /^[0-9A-Za-z][0-9A-Za-z.+_-]{0,63}$/u
|
|
89
|
+
const SHA256_PATTERN = /^[0-9a-f]{64}$/u
|
|
90
|
+
|
|
91
|
+
function failure(code: ManifestFailureCode, message: string): ManifestFailure {
|
|
92
|
+
return { ok: false, code, message }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
96
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Validate one entry against the manifest contract; the reason on refusal. */
|
|
100
|
+
function readEntry(tool: ToolchainId, value: unknown): ManifestEntry | string {
|
|
101
|
+
if (!isRecord(value)) return `${tool}: entry is not an object`
|
|
102
|
+
const { version, sha256, platform, arch, url } = value
|
|
103
|
+
if (typeof version !== 'string' || !VERSION_PATTERN.test(version)) return `${tool}: invalid version`
|
|
104
|
+
if (typeof sha256 !== 'string' || !SHA256_PATTERN.test(sha256)) return `${tool}: invalid sha256`
|
|
105
|
+
if (typeof platform !== 'string' || platform === '') return `${tool}: invalid platform`
|
|
106
|
+
if (typeof arch !== 'string' || arch === '') return `${tool}: invalid arch`
|
|
107
|
+
if (typeof url !== 'string' || !url.startsWith('https://')) return `${tool}: download url must be https`
|
|
108
|
+
return { version, sha256, platform, arch, url }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Version ordering used to pick the newest entry deterministically. */
|
|
112
|
+
function compareVersions(left: string, right: string): number {
|
|
113
|
+
const segments = (value: string): (number | string)[] =>
|
|
114
|
+
value.split(/[.+-]/u).map(part => (/^\d+$/u.test(part) ? Number(part) : part))
|
|
115
|
+
const a = segments(left)
|
|
116
|
+
const b = segments(right)
|
|
117
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
118
|
+
const x = a[index]
|
|
119
|
+
const y = b[index]
|
|
120
|
+
if (x === undefined) return -1
|
|
121
|
+
if (y === undefined) return 1
|
|
122
|
+
if (typeof x === 'number' && typeof y === 'number') {
|
|
123
|
+
if (x !== y) return x - y
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
const sx = String(x)
|
|
127
|
+
const sy = String(y)
|
|
128
|
+
if (sx !== sy) return sx < sy ? -1 : 1
|
|
129
|
+
}
|
|
130
|
+
return 0
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Parse and validate one manifest document for this host.
|
|
135
|
+
*
|
|
136
|
+
* Malformed or unsupported entries are rejected as a whole entry (§7.3): an
|
|
137
|
+
* entry with a bad checksum is not "probably fine", because a download it
|
|
138
|
+
* authorised could never be verified. Entries for other platforms/architectures
|
|
139
|
+
* are kept in `entries` (so the UI can explain the mismatch) but never selected.
|
|
140
|
+
* @param text - raw manifest body.
|
|
141
|
+
* @param options - host platform/architecture to select for.
|
|
142
|
+
* @returns the validated manifest, or the reason it cannot be used.
|
|
143
|
+
*/
|
|
144
|
+
export function parseManifest(
|
|
145
|
+
text: string,
|
|
146
|
+
options: { readonly platform?: NodeJS.Platform; readonly arch?: string } = {},
|
|
147
|
+
): ManifestResult {
|
|
148
|
+
let document: unknown
|
|
149
|
+
try {
|
|
150
|
+
document = JSON.parse(text)
|
|
151
|
+
} catch {
|
|
152
|
+
return failure('invalid-json', 'the toolchain manifest is not valid JSON')
|
|
153
|
+
}
|
|
154
|
+
if (!isRecord(document)) return failure('invalid-json', 'the toolchain manifest is not an object')
|
|
155
|
+
if (document.schemaVersion !== MANIFEST_SCHEMA_VERSION) {
|
|
156
|
+
return failure('unsupported-schema', `unsupported toolchain manifest schemaVersion ${String(document.schemaVersion)}`)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const platform = manifestPlatformOf(options.platform ?? process.platform)
|
|
160
|
+
if (platform === undefined) {
|
|
161
|
+
return failure('unsupported-platform', `toolchain downloads are not available on ${options.platform ?? process.platform}`)
|
|
162
|
+
}
|
|
163
|
+
const arch = manifestArchOf(options.arch ?? process.arch)
|
|
164
|
+
|
|
165
|
+
const entries: Record<ToolchainId, readonly ManifestEntry[]> = { kicad: [], solidcraft: [] }
|
|
166
|
+
const selected: Partial<Record<ToolchainId, ManifestEntry>> = {}
|
|
167
|
+
for (const tool of TOOLCHAIN_IDS) {
|
|
168
|
+
const list = document[tool]
|
|
169
|
+
if (list === undefined) continue
|
|
170
|
+
if (!Array.isArray(list)) return failure('invalid-entry', `${tool}: entries are not an array`)
|
|
171
|
+
const validated: ManifestEntry[] = []
|
|
172
|
+
for (const raw of list) {
|
|
173
|
+
const entry = readEntry(tool, raw)
|
|
174
|
+
if (typeof entry === 'string') return failure('invalid-entry', entry)
|
|
175
|
+
validated.push(entry)
|
|
176
|
+
}
|
|
177
|
+
entries[tool] = validated
|
|
178
|
+
const matching = validated
|
|
179
|
+
.filter(entry => entry.platform === platform && entry.arch === arch)
|
|
180
|
+
.sort((left, right) => compareVersions(right.version, left.version))
|
|
181
|
+
const newest = matching[0]
|
|
182
|
+
// `sort` above is descending, so the first match is the newest release; ties
|
|
183
|
+
// keep manifest order, which is the documented deterministic policy.
|
|
184
|
+
if (newest !== undefined) selected[tool] = newest
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (selected.kicad === undefined && selected.solidcraft === undefined) {
|
|
188
|
+
return failure('no-entry', `the toolchain manifest offers no ${platform}-${arch} distribution`)
|
|
189
|
+
}
|
|
190
|
+
return { ok: true, manifest: { schemaVersion: MANIFEST_SCHEMA_VERSION, entries, selected, target: { platform, arch } } }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Injectable HTTP so the manifest reader is testable without a network. */
|
|
194
|
+
export interface ManifestFetchOptions {
|
|
195
|
+
readonly url?: string
|
|
196
|
+
readonly fetchImpl?: typeof fetch
|
|
197
|
+
readonly signal?: AbortSignal
|
|
198
|
+
readonly timeoutMs?: number
|
|
199
|
+
readonly platform?: NodeJS.Platform
|
|
200
|
+
readonly arch?: string
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Fetch and validate the manifest.
|
|
205
|
+
*
|
|
206
|
+
* A network or HTTP failure is a *download-unavailable* state, not a setup
|
|
207
|
+
* error: manual configuration and browsing stay available (§14.2), so the
|
|
208
|
+
* failure is reported as data rather than thrown.
|
|
209
|
+
* @param options - URL/transport/timeout and the target platform.
|
|
210
|
+
* @returns the validated manifest or the reason it is unavailable.
|
|
211
|
+
*/
|
|
212
|
+
export async function fetchManifest(options: ManifestFetchOptions = {}): Promise<ManifestResult> {
|
|
213
|
+
const url = options.url ?? MANIFEST_URL
|
|
214
|
+
const fetchImpl = options.fetchImpl ?? fetch
|
|
215
|
+
const controller = new AbortController()
|
|
216
|
+
const timeout = setTimeout(() => { controller.abort() }, options.timeoutMs ?? 20_000)
|
|
217
|
+
const abort = (): void => { controller.abort() }
|
|
218
|
+
options.signal?.addEventListener('abort', abort)
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetchImpl(url, { signal: controller.signal, redirect: 'follow' })
|
|
221
|
+
if (!response.ok) return failure('http', `the toolchain manifest answered HTTP ${String(response.status)}`)
|
|
222
|
+
const text = await response.text()
|
|
223
|
+
return parseManifest(text, {
|
|
224
|
+
...(options.platform === undefined ? {} : { platform: options.platform }),
|
|
225
|
+
...(options.arch === undefined ? {} : { arch: options.arch }),
|
|
226
|
+
})
|
|
227
|
+
} catch (error) {
|
|
228
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
229
|
+
return failure('network', `the toolchain manifest could not be fetched: ${reason}`)
|
|
230
|
+
} finally {
|
|
231
|
+
clearTimeout(timeout)
|
|
232
|
+
options.signal?.removeEventListener('abort', abort)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One resolver for both external toolchains.
|
|
3
|
+
*
|
|
4
|
+
* Every consumer — the export/launch service, the settings panel, background
|
|
5
|
+
* discovery, the installer's post-extraction validation and the agent prompt —
|
|
6
|
+
* must agree on what "configured" means, or the product grows the two
|
|
7
|
+
* independent validation systems `hardware-engineering-toolchain-onboarding…`
|
|
8
|
+
* §10.5 rules out. This module is that agreement: pure functions from a
|
|
9
|
+
* *configured path* to the executables it owns, plus one version normaliser.
|
|
10
|
+
*
|
|
11
|
+
* The configured value stays the user's object (an installation root), and the
|
|
12
|
+
* executables are derived from it, because that is what the spec requires
|
|
13
|
+
* (§3.1): a user who pastes `…\KiCad\10.0\bin` or `…\bin\kicad-cli.exe` keeps
|
|
14
|
+
* whatever they pasted, while the product resolves around it.
|
|
15
|
+
*
|
|
16
|
+
* @module @huaqiu/hardware-artifacts/toolchain/resolve
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, statSync } from 'node:fs'
|
|
19
|
+
import { basename, dirname, join, resolve, sep } from 'node:path'
|
|
20
|
+
|
|
21
|
+
/** The two tools this product configures. */
|
|
22
|
+
export type ToolchainId = 'kicad' | 'solidcraft'
|
|
23
|
+
|
|
24
|
+
/** How a configured path was obtained; persisted for display only. */
|
|
25
|
+
export type ToolchainSource = 'manual' | 'browse' | 'download' | 'detected'
|
|
26
|
+
|
|
27
|
+
/** Everything one tool's configuration resolves to. */
|
|
28
|
+
export interface ToolchainResolution {
|
|
29
|
+
/** True only when the primary executable exists *and* answered a version probe. */
|
|
30
|
+
readonly configured: boolean
|
|
31
|
+
/** Installation root the executables are resolved from. */
|
|
32
|
+
readonly installPath?: string
|
|
33
|
+
/** Primary CLI the product runs (`kicad-cli` / `solidcraft-cli`). */
|
|
34
|
+
readonly cli?: string
|
|
35
|
+
/** CLI version normalised from the tool's own report. */
|
|
36
|
+
readonly version?: string
|
|
37
|
+
/** GUI application, when the distribution ships one. */
|
|
38
|
+
readonly app?: string
|
|
39
|
+
/** KiCad project manager (`kicad`). */
|
|
40
|
+
readonly projectManager?: string
|
|
41
|
+
/** KiCad schematic editor (`eeschema`). */
|
|
42
|
+
readonly schematicEditor?: string
|
|
43
|
+
/** KiCad board editor (`pcbnew`). */
|
|
44
|
+
readonly pcbEditor?: string
|
|
45
|
+
/** Operator-facing reason the tool is not configured. */
|
|
46
|
+
readonly error?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Candidates one configured root can resolve to, by executable name. */
|
|
50
|
+
export interface ExecutableCandidates {
|
|
51
|
+
/** Primary command-line executable(s), in resolution order. */
|
|
52
|
+
readonly cli: readonly string[]
|
|
53
|
+
/** GUI application executable(s), in resolution order. */
|
|
54
|
+
readonly app: readonly string[]
|
|
55
|
+
/** Named secondary executables (KiCad's editors). */
|
|
56
|
+
readonly named: Readonly<Partial<Record<'projectManager' | 'schematicEditor' | 'pcbEditor', readonly string[]>>>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Injectable facts so resolution is testable off the target platform. */
|
|
60
|
+
export interface ResolveInternals {
|
|
61
|
+
readonly platform?: NodeJS.Platform
|
|
62
|
+
/** Version probe; `undefined` means the executable did not answer. */
|
|
63
|
+
readonly probeVersion?: (executable: string, tool: ToolchainId) => string | undefined
|
|
64
|
+
readonly exists?: (path: string) => boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Canonical absolute form used for every persisted path. */
|
|
68
|
+
export function canonicalPath(path: string): string {
|
|
69
|
+
return resolve(path)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function platformOf(internals: ResolveInternals): NodeJS.Platform {
|
|
73
|
+
return internals.platform ?? process.platform
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function existing(paths: readonly string[], internals: ResolveInternals): string | undefined {
|
|
77
|
+
const exists = internals.exists ?? ((candidate: string) => {
|
|
78
|
+
try {
|
|
79
|
+
return existsSync(candidate) && statSync(candidate).isFile()
|
|
80
|
+
} catch {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
return paths.find(path => exists(path))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Normalise a version report to the version itself.
|
|
89
|
+
*
|
|
90
|
+
* `kicad-cli version` prints `10.0.7`; `solidcraft-cli --version` prints
|
|
91
|
+
* `solidcraft-cli 0.49.1`. The product needs one comparable token from both so
|
|
92
|
+
* a manifest version can be matched against an installed one. A report with no
|
|
93
|
+
* version-shaped token is not a version (§5.3 "unsupported version output").
|
|
94
|
+
* @param output - raw first line of the tool's version command.
|
|
95
|
+
* @returns the dotted version, or undefined when the output carries none.
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeVersion(output: string | undefined): string | undefined {
|
|
98
|
+
if (output === undefined) return undefined
|
|
99
|
+
const match = /\d+(?:\.\d+)+(?:[-+][0-9A-Za-z.-]+)?/u.exec(output)
|
|
100
|
+
return match?.[0]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Executable layout candidates for one configured path.
|
|
105
|
+
*
|
|
106
|
+
* The Windows portable distributions this product downloads put the CLI in
|
|
107
|
+
* `bin/` (KiCad) or beside the app (SolidCraft); a system installation uses the
|
|
108
|
+
* same shape under a versioned folder, and macOS wraps both in an app bundle.
|
|
109
|
+
* @param tool - which toolchain the root belongs to.
|
|
110
|
+
* @param root - configured root, a `bin` directory, or an installation root.
|
|
111
|
+
* @param internals - platform injection.
|
|
112
|
+
* @returns candidate paths in resolution order.
|
|
113
|
+
*/
|
|
114
|
+
export function executableCandidates(
|
|
115
|
+
tool: ToolchainId,
|
|
116
|
+
root: string,
|
|
117
|
+
internals: ResolveInternals = {},
|
|
118
|
+
): ExecutableCandidates {
|
|
119
|
+
const platform = platformOf(internals)
|
|
120
|
+
const exe = platform === 'win32' ? '.exe' : ''
|
|
121
|
+
const lower = basename(root).toLowerCase()
|
|
122
|
+
const isAppBundle = lower.endsWith('.app')
|
|
123
|
+
const mac = isAppBundle || platform === 'darwin'
|
|
124
|
+
|
|
125
|
+
if (tool === 'kicad') {
|
|
126
|
+
if (mac) {
|
|
127
|
+
const base = isAppBundle ? root : join(root, 'KiCad.app')
|
|
128
|
+
return {
|
|
129
|
+
cli: [join(base, 'Contents/MacOS/kicad-cli')],
|
|
130
|
+
app: [join(base, 'Contents/MacOS/kicad')],
|
|
131
|
+
named: {
|
|
132
|
+
projectManager: [join(base, 'Contents/MacOS/kicad')],
|
|
133
|
+
schematicEditor: [join(base, 'Contents/Applications/eeschema.app/Contents/MacOS/eeschema')],
|
|
134
|
+
pcbEditor: [join(base, 'Contents/Applications/pcbnew.app/Contents/MacOS/pcbnew')],
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// A configured `bin` directory is itself the bin; anything else owns one.
|
|
139
|
+
const bin = lower === 'bin' ? root : join(root, 'bin')
|
|
140
|
+
return {
|
|
141
|
+
cli: [join(bin, `kicad-cli${exe}`), join(root, `kicad-cli${exe}`)],
|
|
142
|
+
app: [join(bin, `kicad${exe}`)],
|
|
143
|
+
named: {
|
|
144
|
+
projectManager: [join(bin, `kicad${exe}`)],
|
|
145
|
+
schematicEditor: [join(bin, `eeschema${exe}`)],
|
|
146
|
+
pcbEditor: [join(bin, `pcbnew${exe}`)],
|
|
147
|
+
},
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (mac) {
|
|
152
|
+
const base = isAppBundle ? root : join(root, 'SolidCraft.app')
|
|
153
|
+
return {
|
|
154
|
+
cli: [join(base, 'Contents/MacOS/solidcraft-cli')],
|
|
155
|
+
app: [join(base, 'Contents/MacOS/SolidCraft'), join(base, 'Contents/MacOS/solidcraft')],
|
|
156
|
+
named: {},
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
cli: [join(root, `solidcraft-cli${exe}`), join(root, 'bin', `solidcraft-cli${exe}`)],
|
|
161
|
+
app: [
|
|
162
|
+
join(root, `SolidCraft${exe}`),
|
|
163
|
+
join(root, `solidcraft${exe}`),
|
|
164
|
+
join(root, 'bin', `SolidCraft${exe}`),
|
|
165
|
+
join(root, 'bin', `solidcraft${exe}`),
|
|
166
|
+
],
|
|
167
|
+
named: {},
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Installation root that owns one resolved executable, per layout convention. */
|
|
172
|
+
export function installRootFromExecutable(executable: string): string {
|
|
173
|
+
const segments = executable.split(sep)
|
|
174
|
+
const macIndex = segments.indexOf('Contents')
|
|
175
|
+
if (macIndex > 0) return segments.slice(0, macIndex).join(sep) || sep
|
|
176
|
+
if (segments[segments.length - 2]?.toLowerCase() === 'bin') return segments.slice(0, -2).join(sep) || sep
|
|
177
|
+
return dirname(executable)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Executable stems the product accepts as a pasted executable path. */
|
|
181
|
+
const KICAD_STEMS = ['kicad-cli', 'kicad', 'eeschema', 'pcbnew', 'cvpcb', 'gerbview']
|
|
182
|
+
const SOLIDCRAFT_STEMS = ['solidcraft-cli', 'solidcraft']
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Normalise what the user typed into the installation root the product stores.
|
|
186
|
+
*
|
|
187
|
+
* Accepts an installation root, a `bin` directory or a direct executable path
|
|
188
|
+
* (§3.1/§6.1). An already-correct root is returned unchanged — the product must
|
|
189
|
+
* never silently retarget a path the user chose (§6.1).
|
|
190
|
+
* @param tool - which toolchain the path belongs to.
|
|
191
|
+
* @param input - the raw user input or native-dialog selection.
|
|
192
|
+
* @returns the installation root to persist, or undefined for blank input.
|
|
193
|
+
*/
|
|
194
|
+
export function installationRootOf(tool: ToolchainId, input: string): string | undefined {
|
|
195
|
+
const trimmed = input.trim()
|
|
196
|
+
if (trimmed === '') return undefined
|
|
197
|
+
const full = canonicalPath(trimmed)
|
|
198
|
+
const name = basename(full).toLowerCase()
|
|
199
|
+
const stem = name.replace(/\.(exe|app|command|bat|cmd)$/u, '')
|
|
200
|
+
const stems = tool === 'kicad' ? KICAD_STEMS : SOLIDCRAFT_STEMS
|
|
201
|
+
// A `bin` directory is not an installation root, whatever else it is called.
|
|
202
|
+
if (name === 'bin') return dirname(full)
|
|
203
|
+
// An existing directory is the answer. This is the case that used to break real
|
|
204
|
+
// installations: `C:\Program Files\SolidCraft` and `…\KiCad\8.0` were read as
|
|
205
|
+
// executables because their *folder* name matched the CLI stem, and the
|
|
206
|
+
// configured root silently became the parent directory (§6.1).
|
|
207
|
+
if (isDirectory(full)) return full
|
|
208
|
+
const isExecutable = name.endsWith('.app')
|
|
209
|
+
? true
|
|
210
|
+
: stems.some(candidate => stem === candidate)
|
|
211
|
+
if (isExecutable) return installRootFromExecutable(full)
|
|
212
|
+
return full
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** True when the path exists and is a directory; a missing path is not a directory. */
|
|
216
|
+
function isDirectory(path: string): boolean {
|
|
217
|
+
try {
|
|
218
|
+
return statSync(path).isDirectory()
|
|
219
|
+
} catch {
|
|
220
|
+
return false
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Resolve one configured root into its executables and validated state.
|
|
226
|
+
*
|
|
227
|
+
* A candidate is configured only after its primary CLI exists *and* answers a
|
|
228
|
+
* version probe (§5.3). An executable that exists but reports nothing stays
|
|
229
|
+
* unconfigured, with the probe failure as its reason. SolidCraft additionally
|
|
230
|
+
* tolerates a distribution whose CLI is missing while its app is present —
|
|
231
|
+
* that is a usable installation with no version, not an error (§3.1).
|
|
232
|
+
* @param tool - which toolchain to resolve.
|
|
233
|
+
* @param root - configured installation root.
|
|
234
|
+
* @param internals - platform/probe injection.
|
|
235
|
+
* @returns the resolution, including the operator-facing failure reason.
|
|
236
|
+
*/
|
|
237
|
+
export function resolveToolchain(
|
|
238
|
+
tool: ToolchainId,
|
|
239
|
+
root: string | undefined,
|
|
240
|
+
internals: ResolveInternals = {},
|
|
241
|
+
): ToolchainResolution {
|
|
242
|
+
if (root === undefined || root.trim() === '') {
|
|
243
|
+
return { configured: false, error: `${label(tool)} installation is not configured` }
|
|
244
|
+
}
|
|
245
|
+
const installPath = canonicalPath(root)
|
|
246
|
+
const candidates = executableCandidates(tool, installPath, internals)
|
|
247
|
+
const cli = existing(candidates.cli, internals)
|
|
248
|
+
const app = existing(candidates.app, internals)
|
|
249
|
+
const pcbEditor = existing(candidates.named.pcbEditor ?? [], internals)
|
|
250
|
+
const schematicEditor = existing(candidates.named.schematicEditor ?? [], internals)
|
|
251
|
+
const projectManager = existing(candidates.named.projectManager ?? [], internals)
|
|
252
|
+
const editors: Partial<ToolchainResolution> = {
|
|
253
|
+
...(projectManager === undefined ? {} : { projectManager }),
|
|
254
|
+
...(schematicEditor === undefined ? {} : { schematicEditor }),
|
|
255
|
+
...(pcbEditor === undefined ? {} : { pcbEditor }),
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (tool === 'kicad') {
|
|
259
|
+
if (cli === undefined) {
|
|
260
|
+
return {
|
|
261
|
+
configured: false,
|
|
262
|
+
installPath,
|
|
263
|
+
...(app === undefined ? {} : { app }),
|
|
264
|
+
...editors,
|
|
265
|
+
error: `no kicad-cli executable under ${installPath}`,
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const version = internals.probeVersion?.(cli, tool)
|
|
269
|
+
if (version === undefined) {
|
|
270
|
+
return {
|
|
271
|
+
configured: false,
|
|
272
|
+
installPath,
|
|
273
|
+
cli,
|
|
274
|
+
...(app === undefined ? {} : { app }),
|
|
275
|
+
...editors,
|
|
276
|
+
error: `${cli} did not answer a version probe`,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
configured: true,
|
|
281
|
+
installPath,
|
|
282
|
+
cli,
|
|
283
|
+
version,
|
|
284
|
+
...(app === undefined ? {} : { app }),
|
|
285
|
+
...editors,
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (cli === undefined && app === undefined) {
|
|
290
|
+
return { configured: false, installPath, error: `no solidcraft executable under ${installPath}` }
|
|
291
|
+
}
|
|
292
|
+
const probeTarget = cli ?? app
|
|
293
|
+
const version = probeTarget === undefined ? undefined : internals.probeVersion?.(probeTarget, tool)
|
|
294
|
+
return {
|
|
295
|
+
configured: true,
|
|
296
|
+
installPath,
|
|
297
|
+
...(cli === undefined ? {} : { cli }),
|
|
298
|
+
...(app === undefined ? {} : { app }),
|
|
299
|
+
...(version === undefined ? {} : { version }),
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Display name of one toolchain, used in operator-facing messages. */
|
|
304
|
+
export function label(tool: ToolchainId): string {
|
|
305
|
+
return tool === 'kicad' ? 'KiCad' : 'SolidCraft'
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Whether a manifest version and an installed version describe one release.
|
|
310
|
+
*
|
|
311
|
+
* The Huaqiu KiCad archive is packaged as `10.0.7-2` while `kicad-cli` reports
|
|
312
|
+
* `10.0.7`, so a strict comparison would refuse to reuse an installation that
|
|
313
|
+
* is exactly the requested one (§8.6). The packaging revision therefore
|
|
314
|
+
* compares as a prefix, not as a different version.
|
|
315
|
+
* @param manifestVersion - version from the toolchain manifest.
|
|
316
|
+
* @param installedVersion - version the installed executable reports.
|
|
317
|
+
* @returns true when they name the same release.
|
|
318
|
+
*/
|
|
319
|
+
export function sameRelease(manifestVersion: string, installedVersion: string | undefined): boolean {
|
|
320
|
+
if (installedVersion === undefined) return false
|
|
321
|
+
const a = manifestVersion.trim().toLowerCase()
|
|
322
|
+
const b = installedVersion.trim().toLowerCase()
|
|
323
|
+
if (a === b) return true
|
|
324
|
+
return a.startsWith(`${b}-`) || a.startsWith(`${b}+`) || b.startsWith(`${a}-`) || b.startsWith(`${a}+`)
|
|
325
|
+
}
|