@adrifer/winix 0.2.0-preview.0 → 0.2.0-preview.2
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/dist/backends/windows/host.d.ts +6 -0
- package/dist/backends/windows/host.js +6 -0
- package/dist/backends/windows/index.d.ts +3 -7
- package/dist/backends/windows/index.js +153 -21
- package/dist/backends/windows/lockfile.d.ts +35 -0
- package/dist/backends/windows/lockfile.js +198 -0
- package/dist/backends/windows/resolver.d.ts +7 -0
- package/dist/backends/windows/resolver.js +69 -0
- package/dist/cli/commands/apply.d.ts +3 -0
- package/dist/cli/commands/apply.js +44 -1
- package/dist/cli/commands/switch.js +1 -0
- package/dist/cli/commands/update.d.ts +3 -0
- package/dist/cli/commands/update.js +123 -2
- package/dist/cli/index.js +3 -0
- package/dist/core/types.d.ts +45 -0
- package/dist/evaluator/index.js +10 -1
- package/dist/helpers/account.js +2 -2
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/windows.d.ts +37 -2
- package/dist/helpers/windows.js +100 -3
- package/dist/index.d.ts +3 -2
- package/dist/sdk/collector.d.ts +22 -0
- package/dist/sdk/collector.js +70 -0
- package/dist/sdk/context.d.ts +32 -0
- package/dist/sdk/context.js +108 -0
- package/dist/sdk/index.d.ts +5 -3
- package/dist/sdk/index.js +76 -5
- package/dist/types/windows.d.ts +35 -0
- package/dist/types/windows.js +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { EvaluatedHost } from "../../evaluator/index.js";
|
|
2
|
+
import { type WindowsLock } from "./lockfile.js";
|
|
3
|
+
export { isWindowsHost } from "./host.js";
|
|
2
4
|
/**
|
|
3
5
|
* Per-host generated output for the Windows backend.
|
|
4
6
|
*/
|
|
@@ -15,14 +17,8 @@ export interface WindowsOutput {
|
|
|
15
17
|
hosts: Record<string, WindowsHostOutput>;
|
|
16
18
|
warnings: string[];
|
|
17
19
|
}
|
|
18
|
-
/**
|
|
19
|
-
* True when an evaluated host targets the Windows backend.
|
|
20
|
-
*/
|
|
21
|
-
export declare function isWindowsHost(host: {
|
|
22
|
-
windows?: Record<string, unknown>;
|
|
23
|
-
}): boolean;
|
|
24
20
|
/**
|
|
25
21
|
* Generate Windows bundles for every Windows host in the evaluated workspace.
|
|
26
22
|
* Non-Windows hosts are skipped.
|
|
27
23
|
*/
|
|
28
|
-
export declare function generateWindows(evaluatedHosts: EvaluatedHost[]): WindowsOutput;
|
|
24
|
+
export declare function generateWindows(evaluatedHosts: EvaluatedHost[], lock?: WindowsLock): WindowsOutput;
|
|
@@ -25,27 +25,25 @@
|
|
|
25
25
|
//
|
|
26
26
|
// `winget configure -f configuration.winget` runs this directly; the dscv3
|
|
27
27
|
// processor is selected by the metadata block.
|
|
28
|
+
import { emptyWindowsLock, reconcileInlinePins, } from "./lockfile.js";
|
|
29
|
+
import { isWindowsHost } from "./host.js";
|
|
30
|
+
export { isWindowsHost } from "./host.js";
|
|
28
31
|
const DSC_SCHEMA = "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json";
|
|
29
32
|
const WINGET_PACKAGE_TYPE = "Microsoft.WinGet/Package";
|
|
30
|
-
|
|
31
|
-
* True when an evaluated host targets the Windows backend.
|
|
32
|
-
*/
|
|
33
|
-
export function isWindowsHost(host) {
|
|
34
|
-
return Boolean(host.windows && Object.keys(host.windows).length > 0);
|
|
35
|
-
}
|
|
33
|
+
const RUN_COMMAND_ON_SET_TYPE = "Microsoft.DSC.Transitional/RunCommandOnSet";
|
|
36
34
|
/**
|
|
37
35
|
* Generate Windows bundles for every Windows host in the evaluated workspace.
|
|
38
36
|
* Non-Windows hosts are skipped.
|
|
39
37
|
*/
|
|
40
|
-
export function generateWindows(evaluatedHosts) {
|
|
38
|
+
export function generateWindows(evaluatedHosts, lock = emptyWindowsLock()) {
|
|
41
39
|
const hosts = {};
|
|
42
40
|
const warnings = [];
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
const windowsHosts = evaluatedHosts.filter(isWindowsHost);
|
|
42
|
+
const resolvedLock = reconcileInlinePins(lock, windowsHosts);
|
|
43
|
+
for (const host of windowsHosts) {
|
|
46
44
|
const win = host.windows;
|
|
47
45
|
hosts[host.name] = {
|
|
48
|
-
"configuration.winget": generateConfiguration(host.name, win),
|
|
46
|
+
"configuration.winget": generateConfiguration(host.name, win, resolvedLock),
|
|
49
47
|
"apply.ps1": generateApplyScript(),
|
|
50
48
|
};
|
|
51
49
|
}
|
|
@@ -54,8 +52,9 @@ export function generateWindows(evaluatedHosts) {
|
|
|
54
52
|
/**
|
|
55
53
|
* Render the WinGet Configuration YAML document (DSC v3) for one host.
|
|
56
54
|
*/
|
|
57
|
-
function generateConfiguration(hostName, win) {
|
|
55
|
+
function generateConfiguration(hostName, win, lock) {
|
|
58
56
|
const packages = sortedPackages(win.packages ?? {});
|
|
57
|
+
const commands = win.commands ?? [];
|
|
59
58
|
const lines = [];
|
|
60
59
|
lines.push(`# yaml-language-server: $schema=${DSC_SCHEMA}`);
|
|
61
60
|
lines.push(`# Generated by Winix for host "${hostName}". Do not edit.`);
|
|
@@ -63,16 +62,112 @@ function generateConfiguration(hostName, win) {
|
|
|
63
62
|
lines.push("metadata:");
|
|
64
63
|
lines.push(" winget:");
|
|
65
64
|
lines.push(" processor: dscv3");
|
|
66
|
-
if (packages.length === 0) {
|
|
65
|
+
if (packages.length === 0 && commands.length === 0) {
|
|
67
66
|
lines.push("resources: []");
|
|
68
67
|
return lines.join("\n") + "\n";
|
|
69
68
|
}
|
|
69
|
+
// Two-pass: first assign every resource a schema-valid, unique name and build
|
|
70
|
+
// the ref → (type, name) map; then render, resolving each resource's
|
|
71
|
+
// `dependsOn` against that map.
|
|
72
|
+
const naming = assignResourceNames(hostName, packages, commands);
|
|
70
73
|
lines.push("resources:");
|
|
71
74
|
for (const pkg of packages) {
|
|
72
|
-
|
|
75
|
+
const plan = naming.packages.get(pkg);
|
|
76
|
+
lines.push(...renderPackageResource(pkg, lockedVersionForPackage(pkg, lock), plan, naming));
|
|
77
|
+
}
|
|
78
|
+
for (const command of commands) {
|
|
79
|
+
const plan = naming.commands.get(command);
|
|
80
|
+
lines.push(...renderRawCommandResource(command, plan, naming));
|
|
73
81
|
}
|
|
74
82
|
return lines.join("\n") + "\n";
|
|
75
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Convert an arbitrary identifier into a DSC v3 instance name, which the schema
|
|
86
|
+
* restricts to `^[a-zA-Z0-9 ]+$` (letters, numbers, spaces only). Non-matching
|
|
87
|
+
* characters (dots, slashes, dashes, underscores) collapse to single spaces;
|
|
88
|
+
* the result is trimmed. Package ids like `Git.Git` become `Git Git`. The real
|
|
89
|
+
* package id is preserved separately in `properties.id`, which winget reads.
|
|
90
|
+
*/
|
|
91
|
+
function sanitizeName(raw) {
|
|
92
|
+
const cleaned = raw.replace(/[^a-zA-Z0-9 ]+/g, " ").replace(/\s+/g, " ").trim();
|
|
93
|
+
return cleaned.length > 0 ? cleaned : "resource";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Assign a unique, schema-valid name to every resource in a host and build the
|
|
97
|
+
* lookup maps used to resolve `dependsOn` references.
|
|
98
|
+
*
|
|
99
|
+
* Naming rules:
|
|
100
|
+
* - Packages default to the sanitized package id (`Git.Git` → `Git Git`), or an
|
|
101
|
+
* explicit `name` when one is later supported on packages.
|
|
102
|
+
* - Commands use their explicit `name` (sanitized) when given, else a generated
|
|
103
|
+
* `command N` counter in declaration order.
|
|
104
|
+
* - On a name collision, a ` N` suffix is appended to keep names unique, since
|
|
105
|
+
* DSC requires instance names to be unique within a document.
|
|
106
|
+
*/
|
|
107
|
+
function assignResourceNames(hostName, packages, commands) {
|
|
108
|
+
const used = new Set();
|
|
109
|
+
const plan = {
|
|
110
|
+
packages: new Map(),
|
|
111
|
+
commands: new Map(),
|
|
112
|
+
byPackageId: new Map(),
|
|
113
|
+
byCommandToken: new Map(),
|
|
114
|
+
hostName,
|
|
115
|
+
};
|
|
116
|
+
const unique = (base) => {
|
|
117
|
+
let name = base;
|
|
118
|
+
let n = 2;
|
|
119
|
+
while (used.has(name)) {
|
|
120
|
+
name = `${base} ${n}`;
|
|
121
|
+
n += 1;
|
|
122
|
+
}
|
|
123
|
+
used.add(name);
|
|
124
|
+
return name;
|
|
125
|
+
};
|
|
126
|
+
for (const pkg of packages) {
|
|
127
|
+
const name = unique(sanitizeName(pkg.id));
|
|
128
|
+
const rp = { type: WINGET_PACKAGE_TYPE, name };
|
|
129
|
+
plan.packages.set(pkg, rp);
|
|
130
|
+
plan.byPackageId.set(pkg.id, rp);
|
|
131
|
+
}
|
|
132
|
+
let commandCounter = 0;
|
|
133
|
+
for (const command of commands) {
|
|
134
|
+
commandCounter += 1;
|
|
135
|
+
const base = command.name
|
|
136
|
+
? sanitizeName(command.name)
|
|
137
|
+
: `command ${commandCounter}`;
|
|
138
|
+
const name = unique(base);
|
|
139
|
+
const rp = { type: RUN_COMMAND_ON_SET_TYPE, name };
|
|
140
|
+
plan.commands.set(command, rp);
|
|
141
|
+
if (command.token) {
|
|
142
|
+
plan.byCommandToken.set(command.token, rp);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return plan;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Resolve a resource's `dependsOn` references into DSC v3 `resourceId()`
|
|
149
|
+
* lookup strings. Throws when a referenced resource is not present in this
|
|
150
|
+
* host's naming plan, which happens if a handle from another host is passed to
|
|
151
|
+
* `dependsOn` (cross-host ordering is not expressible in a single document).
|
|
152
|
+
*/
|
|
153
|
+
function resolveDependsOnRefs(refs, naming) {
|
|
154
|
+
if (!refs || refs.length === 0)
|
|
155
|
+
return [];
|
|
156
|
+
const ids = [];
|
|
157
|
+
for (const ref of refs) {
|
|
158
|
+
const target = ref.kind === "package"
|
|
159
|
+
? naming.byPackageId.get(ref.id)
|
|
160
|
+
: naming.byCommandToken.get(ref.token);
|
|
161
|
+
if (!target) {
|
|
162
|
+
const what = ref.kind === "package" ? `package "${ref.id}"` : "a raw command";
|
|
163
|
+
throw new Error(`windows dependsOn references ${what} that is not declared in host ` +
|
|
164
|
+
`"${naming.hostName}". Resources can only depend on other resources ` +
|
|
165
|
+
`within the same host.`);
|
|
166
|
+
}
|
|
167
|
+
ids.push(`[resourceId('${target.type}', '${target.name}')]`);
|
|
168
|
+
}
|
|
169
|
+
return ids;
|
|
170
|
+
}
|
|
76
171
|
/**
|
|
77
172
|
* Render one package as a DSC v3 `Microsoft.WinGet/Package` resource.
|
|
78
173
|
*
|
|
@@ -83,24 +178,26 @@ function generateConfiguration(hostName, win) {
|
|
|
83
178
|
* id: Git.Git
|
|
84
179
|
* source: winget
|
|
85
180
|
* acceptAgreements: true
|
|
86
|
-
* version: "2.44.0" #
|
|
181
|
+
* version: "2.44.0" # from winix-windows.lock
|
|
87
182
|
* metadata:
|
|
88
183
|
* winget:
|
|
89
184
|
* securityContext: elevated
|
|
90
185
|
* ```
|
|
91
186
|
*/
|
|
92
|
-
function renderPackageResource(pkg) {
|
|
187
|
+
function renderPackageResource(pkg, version, plan, naming) {
|
|
93
188
|
const out = [];
|
|
94
|
-
out.push(` - name: ${yamlScalar(
|
|
189
|
+
out.push(` - name: ${yamlScalar(plan.name)}`);
|
|
95
190
|
out.push(` type: ${WINGET_PACKAGE_TYPE}`);
|
|
191
|
+
const deps = resolveDependsOnRefs(pkg.dependsOn, naming);
|
|
192
|
+
if (deps.length > 0) {
|
|
193
|
+
out.push(` dependsOn: ${yamlStringArray(deps)}`);
|
|
194
|
+
}
|
|
96
195
|
out.push(` properties:`);
|
|
97
196
|
out.push(` id: ${yamlScalar(pkg.id)}`);
|
|
98
197
|
out.push(` source: ${yamlScalar(pkg.source)}`);
|
|
99
198
|
out.push(` acceptAgreements: true`);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
out.push(` version: ${yamlQuoted(pkg.version)}`);
|
|
103
|
-
}
|
|
199
|
+
// Quote versions so values like "1.0" are not parsed as numbers.
|
|
200
|
+
out.push(` version: ${yamlQuoted(version)}`);
|
|
104
201
|
// Only request elevation when explicitly asked. Per-user packages install
|
|
105
202
|
// fine without it, and forcing elevation makes `winget configure` fail with
|
|
106
203
|
// an internal error when run from a non-elevated shell.
|
|
@@ -111,6 +208,25 @@ function renderPackageResource(pkg) {
|
|
|
111
208
|
}
|
|
112
209
|
return out;
|
|
113
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* Render one raw command as a DSC v3
|
|
213
|
+
* `Microsoft.DSC.Transitional/RunCommandOnSet` resource.
|
|
214
|
+
*/
|
|
215
|
+
function renderRawCommandResource(command, plan, naming) {
|
|
216
|
+
const out = [];
|
|
217
|
+
out.push(` - name: ${yamlScalar(plan.name)}`);
|
|
218
|
+
out.push(` type: ${RUN_COMMAND_ON_SET_TYPE}`);
|
|
219
|
+
const deps = resolveDependsOnRefs(command.dependsOn, naming);
|
|
220
|
+
if (deps.length > 0) {
|
|
221
|
+
out.push(` dependsOn: ${yamlStringArray(deps)}`);
|
|
222
|
+
}
|
|
223
|
+
out.push(` properties:`);
|
|
224
|
+
out.push(` executable: ${yamlScalar(command.executable)}`);
|
|
225
|
+
if (command.arguments !== undefined && command.arguments.length > 0) {
|
|
226
|
+
out.push(` arguments: ${yamlStringArray(command.arguments)}`);
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
114
230
|
/**
|
|
115
231
|
* The thin apply entry point. Idempotency, elevation, ordering, and error
|
|
116
232
|
* reporting all live inside `winget configure` / the DSC v3 processor.
|
|
@@ -137,6 +253,19 @@ function sortedPackages(packages) {
|
|
|
137
253
|
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
|
138
254
|
.map((id) => packages[id]);
|
|
139
255
|
}
|
|
256
|
+
function lockedVersionForPackage(pkg, lock) {
|
|
257
|
+
const entry = lock.packages[pkg.id];
|
|
258
|
+
if (!entry) {
|
|
259
|
+
throw new Error(`Windows package "${pkg.id}" is not locked. It has no inline version pin ` +
|
|
260
|
+
`and no entry in winix-windows.lock. Run \`winix update --windows\` first.`);
|
|
261
|
+
}
|
|
262
|
+
if (entry.source !== pkg.source) {
|
|
263
|
+
throw new Error(`Windows package "${pkg.id}" source mismatch in winix-windows.lock: ` +
|
|
264
|
+
`expected "${pkg.source}", found "${entry.source}". Update the lock entry ` +
|
|
265
|
+
`or change the package source in config.`);
|
|
266
|
+
}
|
|
267
|
+
return entry.version;
|
|
268
|
+
}
|
|
140
269
|
/**
|
|
141
270
|
* Render a YAML scalar. Package ids and sources are simple tokens; quote
|
|
142
271
|
* defensively only if the value contains YAML-significant characters.
|
|
@@ -149,3 +278,6 @@ function yamlScalar(value) {
|
|
|
149
278
|
function yamlQuoted(value) {
|
|
150
279
|
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
151
280
|
}
|
|
281
|
+
function yamlStringArray(values) {
|
|
282
|
+
return `[${values.map((value) => yamlQuoted(value)).join(", ")}]`;
|
|
283
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { EvaluatedHost } from "../../evaluator/index.js";
|
|
2
|
+
import type { WinPackage, WinPackageSource } from "../../types/index.js";
|
|
3
|
+
export declare const WINDOWS_LOCKFILE_NAME = "winix-windows.lock";
|
|
4
|
+
export declare const WINDOWS_LOCK_VERSION = 1;
|
|
5
|
+
export interface WindowsLockPackage {
|
|
6
|
+
source: WinPackageSource;
|
|
7
|
+
version: string;
|
|
8
|
+
resolvedAt: string;
|
|
9
|
+
}
|
|
10
|
+
export interface WindowsLock {
|
|
11
|
+
version: typeof WINDOWS_LOCK_VERSION;
|
|
12
|
+
generatedAt: string;
|
|
13
|
+
packages: Record<string, WindowsLockPackage>;
|
|
14
|
+
}
|
|
15
|
+
export type WindowsPackageVersionResolver = (id: string, source: WinPackageSource) => string;
|
|
16
|
+
export interface WindowsLockResolution {
|
|
17
|
+
id: string;
|
|
18
|
+
source: WinPackageSource;
|
|
19
|
+
version: string;
|
|
20
|
+
previousVersion?: string;
|
|
21
|
+
status: "new" | "updated" | "up-to-date";
|
|
22
|
+
}
|
|
23
|
+
export interface WindowsLockResolutionResult {
|
|
24
|
+
lock: WindowsLock;
|
|
25
|
+
resolutions: WindowsLockResolution[];
|
|
26
|
+
changed: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare function readWindowsLock(configDir: string): WindowsLock | null;
|
|
29
|
+
export declare function writeWindowsLock(configDir: string, lock: WindowsLock): void;
|
|
30
|
+
export declare function reconcileInlinePins(lock: WindowsLock, evaluatedWindowsHosts: EvaluatedHost[], now?: Date): WindowsLock;
|
|
31
|
+
export declare function emptyWindowsLock(now?: Date): WindowsLock;
|
|
32
|
+
export declare function resolveMissingWindowsLockEntries(lock: WindowsLock, evaluatedWindowsHosts: EvaluatedHost[], resolveVersion: WindowsPackageVersionResolver, now?: Date): WindowsLockResolutionResult;
|
|
33
|
+
export declare function resolveWindowsLockEntries(lock: WindowsLock, packages: Iterable<WinPackage>, resolveVersion: WindowsPackageVersionResolver, now?: Date): WindowsLockResolutionResult;
|
|
34
|
+
export declare function windowsLockChanged(before: WindowsLock, after: WindowsLock): boolean;
|
|
35
|
+
export declare function serializeWindowsLock(lock: WindowsLock): string;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export const WINDOWS_LOCKFILE_NAME = "winix-windows.lock";
|
|
4
|
+
export const WINDOWS_LOCK_VERSION = 1;
|
|
5
|
+
export function readWindowsLock(configDir) {
|
|
6
|
+
const path = windowsLockPath(configDir);
|
|
7
|
+
if (!existsSync(path))
|
|
8
|
+
return null;
|
|
9
|
+
let parsed;
|
|
10
|
+
try {
|
|
11
|
+
parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
throw new Error(`Malformed ${WINDOWS_LOCKFILE_NAME}: ${err.message}`);
|
|
15
|
+
}
|
|
16
|
+
return validateWindowsLock(parsed);
|
|
17
|
+
}
|
|
18
|
+
export function writeWindowsLock(configDir, lock) {
|
|
19
|
+
writeFileSync(windowsLockPath(configDir), serializeWindowsLock(lock));
|
|
20
|
+
}
|
|
21
|
+
export function reconcileInlinePins(lock, evaluatedWindowsHosts, now = new Date()) {
|
|
22
|
+
let changed = false;
|
|
23
|
+
const resolvedAt = now.toISOString();
|
|
24
|
+
const packages = {};
|
|
25
|
+
const inlinePins = new Map();
|
|
26
|
+
for (const [id, entry] of Object.entries(lock.packages)) {
|
|
27
|
+
packages[id] = { ...entry };
|
|
28
|
+
}
|
|
29
|
+
for (const host of evaluatedWindowsHosts) {
|
|
30
|
+
const win = host.windows;
|
|
31
|
+
for (const pkg of Object.values(win.packages ?? {})) {
|
|
32
|
+
if (pkg.version === undefined)
|
|
33
|
+
continue;
|
|
34
|
+
const priorPin = inlinePins.get(pkg.id);
|
|
35
|
+
if (priorPin !== undefined &&
|
|
36
|
+
(priorPin.source !== pkg.source || priorPin.version !== pkg.version)) {
|
|
37
|
+
throw new Error(`Windows package "${pkg.id}" has conflicting inline version pins ` +
|
|
38
|
+
`across Windows hosts. Use one source/version per package id.`);
|
|
39
|
+
}
|
|
40
|
+
inlinePins.set(pkg.id, { source: pkg.source, version: pkg.version });
|
|
41
|
+
const existing = packages[pkg.id];
|
|
42
|
+
if (existing?.version === pkg.version && existing.source === pkg.source) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
packages[pkg.id] = {
|
|
46
|
+
source: pkg.source,
|
|
47
|
+
version: pkg.version,
|
|
48
|
+
resolvedAt,
|
|
49
|
+
};
|
|
50
|
+
changed = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
version: WINDOWS_LOCK_VERSION,
|
|
55
|
+
generatedAt: changed ? resolvedAt : lock.generatedAt,
|
|
56
|
+
packages,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function emptyWindowsLock(now = new Date()) {
|
|
60
|
+
return {
|
|
61
|
+
version: WINDOWS_LOCK_VERSION,
|
|
62
|
+
generatedAt: now.toISOString(),
|
|
63
|
+
packages: {},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function resolveMissingWindowsLockEntries(lock, evaluatedWindowsHosts, resolveVersion, now = new Date()) {
|
|
67
|
+
const missing = new Map();
|
|
68
|
+
for (const host of evaluatedWindowsHosts) {
|
|
69
|
+
const win = host.windows;
|
|
70
|
+
for (const pkg of Object.values(win.packages ?? {})) {
|
|
71
|
+
if (pkg.version !== undefined || lock.packages[pkg.id] !== undefined)
|
|
72
|
+
continue;
|
|
73
|
+
const existing = missing.get(pkg.id);
|
|
74
|
+
if (existing && existing.source !== pkg.source) {
|
|
75
|
+
throw new Error(`Windows package "${pkg.id}" is declared with multiple sources. ` +
|
|
76
|
+
`Use one source per package id.`);
|
|
77
|
+
}
|
|
78
|
+
missing.set(pkg.id, pkg);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return resolveWindowsLockEntries(lock, missing.values(), resolveVersion, now);
|
|
82
|
+
}
|
|
83
|
+
export function resolveWindowsLockEntries(lock, packages, resolveVersion, now = new Date()) {
|
|
84
|
+
const resolvedAt = now.toISOString();
|
|
85
|
+
const next = {
|
|
86
|
+
version: WINDOWS_LOCK_VERSION,
|
|
87
|
+
generatedAt: lock.generatedAt,
|
|
88
|
+
packages: cloneLockPackages(lock.packages),
|
|
89
|
+
};
|
|
90
|
+
const resolutions = [];
|
|
91
|
+
let changed = false;
|
|
92
|
+
for (const pkg of packages) {
|
|
93
|
+
const previous = next.packages[pkg.id];
|
|
94
|
+
const version = resolveVersion(pkg.id, pkg.source);
|
|
95
|
+
if (previous?.source === pkg.source && previous.version === version) {
|
|
96
|
+
resolutions.push({
|
|
97
|
+
id: pkg.id,
|
|
98
|
+
source: pkg.source,
|
|
99
|
+
version,
|
|
100
|
+
status: "up-to-date",
|
|
101
|
+
});
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
next.packages[pkg.id] = {
|
|
105
|
+
source: pkg.source,
|
|
106
|
+
version,
|
|
107
|
+
resolvedAt,
|
|
108
|
+
};
|
|
109
|
+
next.generatedAt = resolvedAt;
|
|
110
|
+
changed = true;
|
|
111
|
+
resolutions.push({
|
|
112
|
+
id: pkg.id,
|
|
113
|
+
source: pkg.source,
|
|
114
|
+
version,
|
|
115
|
+
previousVersion: previous?.version,
|
|
116
|
+
status: previous ? "updated" : "new",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return { lock: next, resolutions, changed };
|
|
120
|
+
}
|
|
121
|
+
export function windowsLockChanged(before, after) {
|
|
122
|
+
return serializeWindowsLock(before) !== serializeWindowsLock(after);
|
|
123
|
+
}
|
|
124
|
+
export function serializeWindowsLock(lock) {
|
|
125
|
+
const sorted = {
|
|
126
|
+
version: WINDOWS_LOCK_VERSION,
|
|
127
|
+
generatedAt: lock.generatedAt,
|
|
128
|
+
packages: {},
|
|
129
|
+
};
|
|
130
|
+
for (const id of Object.keys(lock.packages).sort(comparePackageIds)) {
|
|
131
|
+
const entry = lock.packages[id];
|
|
132
|
+
sorted.packages[id] = {
|
|
133
|
+
source: entry.source,
|
|
134
|
+
version: entry.version,
|
|
135
|
+
resolvedAt: entry.resolvedAt,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return JSON.stringify(sorted, null, 2) + "\n";
|
|
139
|
+
}
|
|
140
|
+
function cloneLockPackages(packages) {
|
|
141
|
+
const out = {};
|
|
142
|
+
for (const [id, entry] of Object.entries(packages)) {
|
|
143
|
+
out[id] = { ...entry };
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
function windowsLockPath(configDir) {
|
|
148
|
+
return join(configDir, WINDOWS_LOCKFILE_NAME);
|
|
149
|
+
}
|
|
150
|
+
function validateWindowsLock(value) {
|
|
151
|
+
if (!isObject(value)) {
|
|
152
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} must be a JSON object.`);
|
|
153
|
+
}
|
|
154
|
+
if (value.version !== WINDOWS_LOCK_VERSION) {
|
|
155
|
+
throw new Error(`Unsupported ${WINDOWS_LOCKFILE_NAME} version ${String(value.version)}. ` +
|
|
156
|
+
`Expected version ${WINDOWS_LOCK_VERSION}.`);
|
|
157
|
+
}
|
|
158
|
+
if (typeof value.generatedAt !== "string") {
|
|
159
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} generatedAt must be a string.`);
|
|
160
|
+
}
|
|
161
|
+
if (!isObject(value.packages)) {
|
|
162
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} packages must be an object.`);
|
|
163
|
+
}
|
|
164
|
+
const packages = {};
|
|
165
|
+
for (const [id, entry] of Object.entries(value.packages)) {
|
|
166
|
+
if (!isObject(entry)) {
|
|
167
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} package "${id}" must be an object.`);
|
|
168
|
+
}
|
|
169
|
+
if (!isWinPackageSource(entry.source)) {
|
|
170
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} package "${id}" has an invalid source.`);
|
|
171
|
+
}
|
|
172
|
+
if (typeof entry.version !== "string" || entry.version.length === 0) {
|
|
173
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} package "${id}" version must be a string.`);
|
|
174
|
+
}
|
|
175
|
+
if (typeof entry.resolvedAt !== "string") {
|
|
176
|
+
throw new Error(`${WINDOWS_LOCKFILE_NAME} package "${id}" resolvedAt must be a string.`);
|
|
177
|
+
}
|
|
178
|
+
packages[id] = {
|
|
179
|
+
source: entry.source,
|
|
180
|
+
version: entry.version,
|
|
181
|
+
resolvedAt: entry.resolvedAt,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
version: WINDOWS_LOCK_VERSION,
|
|
186
|
+
generatedAt: value.generatedAt,
|
|
187
|
+
packages,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function isObject(value) {
|
|
191
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
192
|
+
}
|
|
193
|
+
function isWinPackageSource(value) {
|
|
194
|
+
return value === "winget" || value === "msstore";
|
|
195
|
+
}
|
|
196
|
+
function comparePackageIds(a, b) {
|
|
197
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
198
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { WinPackageSource } from "../../types/index.js";
|
|
2
|
+
export type WingetShowRunner = (id: string, source: WinPackageSource) => string;
|
|
3
|
+
export declare const WINGET_RESOLUTION_WINDOWS_ONLY_MESSAGE: string;
|
|
4
|
+
export declare const WINGET_NOT_FOUND_MESSAGE: string;
|
|
5
|
+
export declare function resolveWingetVersion(id: string, source: WinPackageSource, runWingetShow?: WingetShowRunner, osPlatform?: NodeJS.Platform): string;
|
|
6
|
+
export declare function assertWingetResolutionSupported(osPlatform?: NodeJS.Platform): void;
|
|
7
|
+
export declare function parseWingetShowVersion(id: string, source: WinPackageSource, output: string): string;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
export const WINGET_RESOLUTION_WINDOWS_ONLY_MESSAGE = "winget version resolution is only available on Windows. " +
|
|
3
|
+
"Run `winix update --windows` on a Windows machine.";
|
|
4
|
+
export const WINGET_NOT_FOUND_MESSAGE = "`winget` was not found on PATH. Install App Installer / winget, or run " +
|
|
5
|
+
"`winix update --windows` on a Windows machine where winget is available.";
|
|
6
|
+
export function resolveWingetVersion(id, source, runWingetShow = runWingetShowCommand, osPlatform = process.platform) {
|
|
7
|
+
assertWingetResolutionSupported(osPlatform);
|
|
8
|
+
let output;
|
|
9
|
+
try {
|
|
10
|
+
output = runWingetShow(id, source);
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
throw wingetError(id, source, err);
|
|
14
|
+
}
|
|
15
|
+
return parseWingetShowVersion(id, source, output);
|
|
16
|
+
}
|
|
17
|
+
export function assertWingetResolutionSupported(osPlatform = process.platform) {
|
|
18
|
+
if (osPlatform === "win32")
|
|
19
|
+
return;
|
|
20
|
+
throw new Error(WINGET_RESOLUTION_WINDOWS_ONLY_MESSAGE);
|
|
21
|
+
}
|
|
22
|
+
export function parseWingetShowVersion(id, source, output) {
|
|
23
|
+
if (/No package found/i.test(output)) {
|
|
24
|
+
throw new Error(`Windows package "${id}" was not found in source "${source}".`);
|
|
25
|
+
}
|
|
26
|
+
const versionLine = output
|
|
27
|
+
.split(/\r?\n/)
|
|
28
|
+
.find((line) => /^\s*Version\s*:/i.test(line));
|
|
29
|
+
const version = versionLine?.replace(/^\s*Version\s*:/i, "").trim();
|
|
30
|
+
if (version)
|
|
31
|
+
return version;
|
|
32
|
+
throw new Error(`winget show did not report a Version for Windows package "${id}" ` +
|
|
33
|
+
`from source "${source}".`);
|
|
34
|
+
}
|
|
35
|
+
function runWingetShowCommand(id, source) {
|
|
36
|
+
return execFileSync("winget", ["show", "--id", id, "--source", source, "--exact"], { encoding: "utf-8" });
|
|
37
|
+
}
|
|
38
|
+
function wingetError(id, source, err) {
|
|
39
|
+
if (isNodeError(err) && err.code === "ENOENT") {
|
|
40
|
+
return new Error(WINGET_NOT_FOUND_MESSAGE);
|
|
41
|
+
}
|
|
42
|
+
const output = wingetErrorOutput(err);
|
|
43
|
+
if (/No package found/i.test(output)) {
|
|
44
|
+
return new Error(`Windows package "${id}" was not found in source "${source}".`);
|
|
45
|
+
}
|
|
46
|
+
const details = output.trim() || (err instanceof Error ? err.message : String(err));
|
|
47
|
+
return new Error(`Failed to resolve Windows package "${id}" from source "${source}" with winget: ` +
|
|
48
|
+
details);
|
|
49
|
+
}
|
|
50
|
+
function wingetErrorOutput(err) {
|
|
51
|
+
if (!isObject(err))
|
|
52
|
+
return "";
|
|
53
|
+
const stdout = bufferOrStringToString(err.stdout);
|
|
54
|
+
const stderr = bufferOrStringToString(err.stderr);
|
|
55
|
+
return [stdout, stderr].filter(Boolean).join("\n");
|
|
56
|
+
}
|
|
57
|
+
function bufferOrStringToString(value) {
|
|
58
|
+
if (typeof value === "string")
|
|
59
|
+
return value;
|
|
60
|
+
if (Buffer.isBuffer(value))
|
|
61
|
+
return value.toString("utf-8");
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
function isNodeError(err) {
|
|
65
|
+
return err instanceof Error && "code" in err;
|
|
66
|
+
}
|
|
67
|
+
function isObject(value) {
|
|
68
|
+
return typeof value === "object" && value !== null;
|
|
69
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { type WindowsPackageVersionResolver } from "../../backends/windows/lockfile.js";
|
|
1
2
|
import { type ActivationPlatform } from "../activation.js";
|
|
2
3
|
interface ApplyOptions {
|
|
3
4
|
host?: string;
|
|
4
5
|
dry: boolean;
|
|
5
6
|
diff: boolean;
|
|
7
|
+
windowsLockDry?: boolean;
|
|
8
|
+
resolveWindowsPackageVersion?: WindowsPackageVersionResolver;
|
|
6
9
|
}
|
|
7
10
|
export interface ApplyResult {
|
|
8
11
|
configDir: string;
|
|
@@ -6,6 +6,8 @@ import { loadWorkspace } from "../loader.js";
|
|
|
6
6
|
import { evaluate } from "../../evaluator/index.js";
|
|
7
7
|
import { generateNix } from "../../backends/nix/index.js";
|
|
8
8
|
import { generateWindows } from "../../backends/windows/index.js";
|
|
9
|
+
import { emptyWindowsLock, readWindowsLock, reconcileInlinePins, resolveMissingWindowsLockEntries, windowsLockChanged, writeWindowsLock, } from "../../backends/windows/lockfile.js";
|
|
10
|
+
import { resolveWingetVersion } from "../../backends/windows/resolver.js";
|
|
9
11
|
import { platformForEvaluatedHost } from "../activation.js";
|
|
10
12
|
export async function applyWorkspace(cwd, opts) {
|
|
11
13
|
const { workspace, configDir } = await loadWorkspace(cwd);
|
|
@@ -21,8 +23,13 @@ export async function applyWorkspace(cwd, opts) {
|
|
|
21
23
|
const hostPlatforms = platformsForHosts(evaluated);
|
|
22
24
|
const nixHosts = evaluated.filter((host) => hostPlatforms.get(host.name) !== "windows");
|
|
23
25
|
const windowsHosts = evaluated.filter((host) => hostPlatforms.get(host.name) === "windows");
|
|
26
|
+
const windowsLock = windowsHosts.length > 0
|
|
27
|
+
? prepareWindowsLock(configDir, windowsHosts, opts.dry || opts.windowsLockDry === true, opts.resolveWindowsPackageVersion ?? resolveWingetVersion)
|
|
28
|
+
: undefined;
|
|
24
29
|
const nixOutput = nixHosts.length > 0 ? generateNix(workspace, nixHosts) : undefined;
|
|
25
|
-
const windowsOutput = windowsHosts.length > 0
|
|
30
|
+
const windowsOutput = windowsHosts.length > 0
|
|
31
|
+
? generateWindows(windowsHosts, windowsLock)
|
|
32
|
+
: undefined;
|
|
26
33
|
printWarnings(nixOutput?.warnings ?? []);
|
|
27
34
|
printWarnings(windowsOutput?.warnings ?? []);
|
|
28
35
|
const outDir = join(configDir, ".winix", "out");
|
|
@@ -73,6 +80,42 @@ export async function applyWorkspace(cwd, opts) {
|
|
|
73
80
|
export async function apply(cwd, opts) {
|
|
74
81
|
await applyWorkspace(cwd, opts);
|
|
75
82
|
}
|
|
83
|
+
function prepareWindowsLock(configDir, windowsHosts, dry, resolveVersion) {
|
|
84
|
+
const now = new Date();
|
|
85
|
+
const lock = readWindowsLock(configDir) ?? emptyWindowsLock(now);
|
|
86
|
+
const reconciled = reconcileInlinePins(lock, windowsHosts, now);
|
|
87
|
+
const resolved = resolveMissingWindowsLockEntries(reconciled, windowsHosts, resolveVersion, now);
|
|
88
|
+
const changed = windowsLockChanged(lock, resolved.lock);
|
|
89
|
+
if (!changed)
|
|
90
|
+
return resolved.lock;
|
|
91
|
+
if (dry) {
|
|
92
|
+
printWindowsLockDryRun(resolved.resolutions);
|
|
93
|
+
return resolved.lock;
|
|
94
|
+
}
|
|
95
|
+
writeWindowsLock(configDir, resolved.lock);
|
|
96
|
+
printWindowsLockUpdate(resolved.resolutions);
|
|
97
|
+
return resolved.lock;
|
|
98
|
+
}
|
|
99
|
+
function printWindowsLockDryRun(resolutions) {
|
|
100
|
+
if (resolutions.length === 0) {
|
|
101
|
+
console.warn("Would update winix-windows.lock with inline Windows package pins.");
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
console.warn("Would update winix-windows.lock with resolved Windows package versions:");
|
|
105
|
+
for (const resolution of resolutions) {
|
|
106
|
+
console.warn(` ${resolution.id} ${resolution.source} ${resolution.version}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function printWindowsLockUpdate(resolutions) {
|
|
110
|
+
if (resolutions.length === 0) {
|
|
111
|
+
console.log("✓ Updated winix-windows.lock with inline Windows package pins");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
console.log("✓ Updated winix-windows.lock with resolved Windows package versions");
|
|
115
|
+
for (const resolution of resolutions) {
|
|
116
|
+
console.log(` → ${resolution.id} ${resolution.source} ${resolution.version}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
76
119
|
function resultFor(configDir, outDir, evaluated) {
|
|
77
120
|
return {
|
|
78
121
|
configDir,
|