@adrifer/winix 0.2.0-preview.4 → 0.2.1

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.
@@ -1,7 +1,12 @@
1
+ import { escapeNixDoubleQuoted, nixStringLiteral } from "../nix/serialize.js";
1
2
  const expr = (value) => ({ __winixNixExpr: true, expr: value });
2
3
  const pkg = Object.assign((name) => expr(`pkgs.${name}`), {
3
4
  stable: (name) => expr(`pkgs.stable.${name}`),
4
5
  });
6
+ function pathUnder(base, relativePath) {
7
+ const normalized = relativePath.replace(/^\/+/, "");
8
+ return normalized === "" ? nix.str `${base}` : nix.str `${base}/${normalized}`;
9
+ }
5
10
  const script = Object.assign((first, ...values) => {
6
11
  const body = typeof first === "string"
7
12
  ? first
@@ -23,6 +28,8 @@ export const nix = {
23
28
  expr,
24
29
  pkg,
25
30
  bin: (packageName, executable) => nix.str `${pkg(packageName)}/bin/${executable}`,
31
+ homePath: (relativePath) => pathUnder(expr("config.home.homeDirectory"), relativePath),
32
+ pkgPath: (packageName, relativePath) => pathUnder(pkg(packageName), relativePath),
26
33
  str: (strings, ...values) => {
27
34
  const parts = [];
28
35
  const rawStrings = strings.raw;
@@ -123,12 +130,16 @@ function binaryRelease(opts) {
123
130
  const sourcesAttrs = filtered
124
131
  .map(([arch, platform]) => {
125
132
  const parts = [
126
- `file = ${JSON.stringify(platform.file)};`,
127
- `hash = ${JSON.stringify(platform.hash)};`,
128
- `binary = ${JSON.stringify(platform.binary ?? opts.binary)};`,
133
+ `file = ${nixStringLiteral(platform.file)};`,
134
+ `hash = ${nixStringLiteral(platform.hash)};`,
135
+ `binary = ${nixStringLiteral(platform.binary ?? opts.binary)};`,
129
136
  ];
130
137
  if (usesPlatformPlaceholder) {
131
- parts.push(`platform = ${JSON.stringify(platform.platform)};`);
138
+ const platformName = platform.platform;
139
+ if (platformName === undefined) {
140
+ throw new Error(`nix.binaryRelease: "${arch}" must define \`platform\` because \`urlTemplate\` uses \`{platform}\``);
141
+ }
142
+ parts.push(`platform = ${nixStringLiteral(platformName)};`);
132
143
  }
133
144
  return ` ${arch} = { ${parts.join(" ")} };`;
134
145
  })
@@ -142,15 +153,15 @@ function binaryRelease(opts) {
142
153
  ? license.expr
143
154
  : `pkgs.lib.licenses.${license}`;
144
155
  const metaLines = [
145
- ` description = ${JSON.stringify(opts.meta.description)};`,
156
+ ` description = ${nixStringLiteral(opts.meta.description)};`,
146
157
  ];
147
158
  if (opts.meta.homepage !== undefined) {
148
- metaLines.push(` homepage = ${JSON.stringify(opts.meta.homepage)};`);
159
+ metaLines.push(` homepage = ${nixStringLiteral(opts.meta.homepage)};`);
149
160
  }
150
161
  if (licenseExpr !== undefined) {
151
162
  metaLines.push(` license = ${licenseExpr};`);
152
163
  }
153
- metaLines.push(` mainProgram = ${JSON.stringify(mainProgram)};`);
164
+ metaLines.push(` mainProgram = ${nixStringLiteral(mainProgram)};`);
154
165
  metaLines.push(` platforms = builtins.attrNames sources;`);
155
166
  const extraInstallBlock = opts.extraInstall
156
167
  ? `\n ${dedent(opts.extraInstall).split("\n").join("\n ")}`
@@ -181,13 +192,13 @@ function binaryRelease(opts) {
181
192
  ? `\n\n dontStrip = pkgs.stdenv.hostPlatform.isDarwin;`
182
193
  : "";
183
194
  const text = `(let
184
- version = ${JSON.stringify(opts.version)};
195
+ version = ${nixStringLiteral(opts.version)};
185
196
  sources = {
186
197
  ${sourcesAttrs}
187
198
  };
188
199
  source = sources.\${pkgs.stdenv.hostPlatform.system};
189
200
  in pkgs.stdenvNoCC.mkDerivation {
190
- pname = ${JSON.stringify(opts.name)};
201
+ pname = ${nixStringLiteral(opts.name)};
191
202
  inherit version;
192
203
 
193
204
  src = pkgs.fetchurl {
@@ -289,7 +300,7 @@ function dedent(value) {
289
300
  return lines.map((line) => (line.length >= min ? line.slice(min) : line)).join("\n");
290
301
  }
291
302
  function stringPartToNix(value) {
292
- return isNixExpr(value) ? value.expr : JSON.stringify(value);
303
+ return isNixExpr(value) ? value.expr : nixStringLiteral(value);
293
304
  }
294
305
  function conditionToNix(condition) {
295
306
  if (isNixExpr(condition))
@@ -302,7 +313,7 @@ function nixLiteral(value) {
302
313
  if (isNixExpr(value))
303
314
  return value.expr;
304
315
  if (typeof value === "string")
305
- return JSON.stringify(value);
316
+ return nixStringLiteral(value);
306
317
  if (typeof value === "number")
307
318
  return String(value);
308
319
  if (typeof value === "boolean")
@@ -324,7 +335,7 @@ function formatAttrs(attrs) {
324
335
  .join(" ");
325
336
  }
326
337
  function formatAttrKey(key) {
327
- return needsQuoting(key) ? JSON.stringify(key) : key;
338
+ return needsQuoting(key) ? nixStringLiteral(key) : key;
328
339
  }
329
340
  function needsQuoting(key) {
330
341
  if (NIX_KEYWORDS.has(key))
@@ -354,12 +365,6 @@ function joinRawTemplate(strings, values, formatValue) {
354
365
  function trimScript(body) {
355
366
  return body.replace(/^\n/, "").replace(/\n\s*$/, "");
356
367
  }
357
- function escapeNixDoubleQuoted(value) {
358
- return value
359
- .replace(/\\/g, "\\\\")
360
- .replace(/"/g, '\\"')
361
- .replace(/\$\{/g, "\\${");
362
- }
363
368
  function isNixExpr(value) {
364
369
  return (typeof value === "object" &&
365
370
  value !== null &&
@@ -1,5 +1,5 @@
1
1
  import type { ResourceHandle } from "../core/types.js";
2
- import type { WinDscProperties, WinPackageSource } from "../types/index.js";
2
+ import type { WinDscProperties, WinPackageSource, WinSettings } from "../types/index.js";
3
3
  /**
4
4
  * Accepted call shapes for `windows.package(...)`.
5
5
  *
@@ -43,6 +43,10 @@ export interface WinDscSpec {
43
43
  properties?: WinDscProperties;
44
44
  dependsOn?: ResourceHandle | ResourceHandle[];
45
45
  }
46
+ /** Options for `windows.setting(...)`. */
47
+ export interface WinSettingOpts {
48
+ dependsOn?: ResourceHandle | ResourceHandle[];
49
+ }
46
50
  /** Scope for Windows environment and PATH writes. */
47
51
  export type WinEnvScope = "user" | "machine";
48
52
  /** Options shared by `windows.env.*` and `windows.path.*` methods. */
@@ -50,6 +54,16 @@ export interface WinEnvOpts {
50
54
  scope?: WinEnvScope;
51
55
  dependsOn?: ResourceHandle | ResourceHandle[];
52
56
  }
57
+ export type WinFileEncoding = "utf8" | "utf8bom" | "ascii";
58
+ /** Options shared by `windows.file.*` methods. */
59
+ export interface WinFileOpts {
60
+ force?: boolean;
61
+ backup?: boolean;
62
+ recursive?: boolean;
63
+ elevate?: boolean;
64
+ encoding?: WinFileEncoding;
65
+ dependsOn?: ResourceHandle | ResourceHandle[];
66
+ }
53
67
  export interface WinEnvNamespace {
54
68
  set(name: string, value: string, opts?: WinEnvOpts): ResourceHandle;
55
69
  remove(name: string, opts?: WinEnvOpts): ResourceHandle;
@@ -58,6 +72,12 @@ export interface WinPathNamespace {
58
72
  add(value: string, opts?: WinEnvOpts): ResourceHandle;
59
73
  remove(value: string, opts?: WinEnvOpts): ResourceHandle;
60
74
  }
75
+ export interface WinFileNamespace {
76
+ text(target: string, content: string, opts?: WinFileOpts): ResourceHandle;
77
+ symlink(target: string, source: string, opts?: WinFileOpts): ResourceHandle;
78
+ copy(target: string, source: string, opts?: WinFileOpts): ResourceHandle;
79
+ remove(target: string, opts?: WinFileOpts): ResourceHandle;
80
+ }
61
81
  export interface WindowsHelper {
62
82
  /**
63
83
  * Declare a winget/msstore package.
@@ -98,6 +118,18 @@ export interface WindowsHelper {
98
118
  * ```
99
119
  */
100
120
  dsc(arg: WinDscSpec): ResourceHandle;
121
+ /**
122
+ * Manage Windows OS settings through the native
123
+ * `Microsoft.Windows.Settings/WindowsSettings` DSC resource. The emitter
124
+ * automatically ensures the required `Microsoft.Windows.Settings` module is
125
+ * installed and wires settings resources to depend on it.
126
+ *
127
+ * ```ts
128
+ * windows.setting({ DeveloperMode: true });
129
+ * windows.setting({ SystemColorMode: "Dark", TaskbarAlignment: "Left" });
130
+ * ```
131
+ */
132
+ setting(settings: WinSettings, opts?: WinSettingOpts): ResourceHandle;
101
133
  /**
102
134
  * Manage a user/machine environment variable declaratively via the native
103
135
  * `Microsoft.Windows/Registry` DSC resource.
@@ -118,5 +150,17 @@ export interface WindowsHelper {
118
150
  * ```
119
151
  */
120
152
  path: WinPathNamespace;
153
+ /**
154
+ * Manage files, copies, and dotfile symlinks declaratively via an idempotent
155
+ * `Microsoft.DSC.Transitional/WindowsPowerShellScript` resource.
156
+ *
157
+ * ```ts
158
+ * windows.file.text("%USERPROFILE%\\.gitconfig", "[user]\n");
159
+ * windows.file.symlink("%LOCALAPPDATA%\\nvim", "%USERPROFILE%\\dotfiles\\nvim");
160
+ * windows.file.copy("%APPDATA%\\tool\\config.json", ".\\config.json");
161
+ * windows.file.remove("%USERPROFILE%\\.oldrc");
162
+ * ```
163
+ */
164
+ file: WinFileNamespace;
121
165
  }
122
166
  export declare const windows: WindowsHelper;