@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.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/CHANGELOG.md +26 -1
- package/dist/core/cli/dispatch.mjs +214 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.mjs +901 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.mjs +208 -4
- package/dist/core/cli/root-surface.mjs +27 -1
- package/dist/core/cli/runtime-preflight.mjs +168 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.mjs +304 -3
- package/dist/core/completion/shells/fish.mjs +159 -2
- package/dist/core/completion/shells/powershell.mjs +353 -2
- package/dist/core/completion/shells/shared.mjs +154 -1
- package/dist/core/completion/shells/zsh.mjs +244 -2
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.mjs +385 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.mjs +189 -1
- package/dist/core/help/ansi.mjs +122 -2
- package/dist/core/help/index.mjs +329 -7
- package/dist/core/json-schema/index.mjs +487 -9
- package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- package/dist/core/output/contracts.mjs +57 -1
- package/dist/core/output/display-value.mjs +32 -1
- package/dist/core/output/index.d.mts +21 -0
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.mjs +555 -1
- package/dist/core/prompt/index.d.mts +0 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.mjs +279 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.mjs +196 -1
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +0 -1
- package/dist/core/resolve/property.mjs +45 -1
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +18 -0
- package/dist/core/schema/command.mjs +712 -1
- package/dist/core/schema/flag.mjs +453 -1
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.mjs +82 -1
- package/dist/runtime/auto.mjs +46 -1
- package/dist/runtime/bun.mjs +28 -1
- package/dist/runtime/deno.mjs +217 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/node.mjs +169 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.mjs +7 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.mjs +5 -1
- package/dist/version.mjs +7 -1
- package/package.json +17 -26
- package/src/core/cli/dispatch.ts +0 -355
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1614
- package/src/core/cli/planner.ts +0 -656
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -342
- package/src/core/cli/root-surface.ts +0 -87
- package/src/core/cli/runtime-preflight.ts +0 -456
- package/src/core/completion/index.ts +0 -138
- package/src/core/completion/shells/bash.ts +0 -459
- package/src/core/completion/shells/fish.ts +0 -259
- package/src/core/completion/shells/powershell.ts +0 -439
- package/src/core/completion/shells/shared.ts +0 -310
- package/src/core/completion/shells/zsh.ts +0 -381
- package/src/core/config/index.ts +0 -417
- package/src/core/config/package-json.ts +0 -560
- package/src/core/errors/index.ts +0 -193
- package/src/core/execution/index.ts +0 -265
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -563
- package/src/core/json-schema/index.ts +0 -910
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
- package/src/core/output/activity.ts +0 -575
- package/src/core/output/bind.ts +0 -59
- package/src/core/output/contracts.ts +0 -194
- package/src/core/output/display-value.ts +0 -48
- package/src/core/output/index.ts +0 -704
- package/src/core/output/renderers.ts +0 -125
- package/src/core/output/writer.ts +0 -46
- package/src/core/parse/index.ts +0 -742
- package/src/core/prompt/index.ts +0 -623
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -500
- package/src/core/resolve/config.ts +0 -26
- package/src/core/resolve/contracts.ts +0 -135
- package/src/core/resolve/errors.ts +0 -227
- package/src/core/resolve/flags.ts +0 -269
- package/src/core/resolve/index.ts +0 -108
- package/src/core/resolve/property.ts +0 -99
- package/src/core/schema/activity.ts +0 -240
- package/src/core/schema/arg.ts +0 -841
- package/src/core/schema/command.ts +0 -1500
- package/src/core/schema/flag.ts +0 -917
- package/src/core/schema/index.ts +0 -96
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/number-constraints.ts +0 -153
- package/src/core/schema/prompt.ts +0 -171
- package/src/core/schema/run.ts +0 -198
- package/src/core/schema-dsl/index.ts +0 -109
- package/src/core/schema-dsl/parse.ts +0 -262
- package/src/core/schema-dsl/runtime.ts +0 -609
- package/src/core/schema-dsl/to-json-schema.ts +0 -102
- package/src/core/testkit/index.ts +0 -147
- package/src/index.ts +0 -181
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -86
- package/src/runtime/bun.ts +0 -48
- package/src/runtime/deno.ts +0 -367
- package/src/runtime/detect.ts +0 -105
- package/src/runtime/index.ts +0 -21
- package/src/runtime/node-builtins.d.ts +0 -27
- package/src/runtime/node.ts +0 -289
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -77
- package/src/runtime.ts +0 -31
- package/src/schema.ts +0 -16
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
|
@@ -1,2 +1,159 @@
|
|
|
1
|
-
import{getFlagAliasNames
|
|
2
|
-
|
|
1
|
+
import { getFlagAliasNames } from "../../schema/flag.mjs";
|
|
2
|
+
import { quoteShellArg, resolveRootCompletionSurface, sanitizeShellIdentifier, versionTag, walkCommandTree } from "./shared.mjs";
|
|
3
|
+
//#region src/core/completion/shells/fish.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generate a fish completion script for the CLI.
|
|
6
|
+
*
|
|
7
|
+
* @param schema - The CLI schema.
|
|
8
|
+
* @param options - Optional generator configuration.
|
|
9
|
+
* @returns A complete fish completion script.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const script = generateFishCompletion(app.schema);
|
|
14
|
+
* // install with: mycli completions fish > ~/.config/fish/completions/mycli.fish
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function generateFishCompletion(schema, options) {
|
|
18
|
+
const helperPrefix = `__${sanitizeShellIdentifier(options?.functionPrefix ?? schema.name)}_completions`;
|
|
19
|
+
const pathFuncName = `${helperPrefix}_path`;
|
|
20
|
+
const pathIsFuncName = `${helperPrefix}_path_is`;
|
|
21
|
+
const rootSurface = resolveRootCompletionSurface(schema, options?.rootMode);
|
|
22
|
+
const visibleCommands = rootSurface.visibleCommands;
|
|
23
|
+
const nodes = walkCommandTree(visibleCommands);
|
|
24
|
+
const commandName = quoteShellArg(schema.name);
|
|
25
|
+
const lines = [];
|
|
26
|
+
lines.push(`# Fish completion for ${schema.name}`);
|
|
27
|
+
lines.push(`# Generated by ${versionTag()}`);
|
|
28
|
+
lines.push("#");
|
|
29
|
+
lines.push(`# Usage: source (${schema.name} completions fish | psub)`);
|
|
30
|
+
lines.push("#");
|
|
31
|
+
lines.push("# Install permanently:");
|
|
32
|
+
lines.push(`# ${schema.name} completions fish > ~/.config/fish/completions/${schema.name}.fish`);
|
|
33
|
+
lines.push("");
|
|
34
|
+
lines.push(`complete -c ${commandName} -f`);
|
|
35
|
+
lines.push("");
|
|
36
|
+
emitFishPathHelpers(lines, pathFuncName, pathIsFuncName, visibleCommands, nodes, rootSurface);
|
|
37
|
+
appendFishCommandCompletions(lines, commandName, pathIsFuncName, "", visibleCommands);
|
|
38
|
+
appendFishFlagCompletions(lines, commandName, pathIsFuncName, "", rootSurface.rootFlags);
|
|
39
|
+
if (rootSurface.includeDefaultFlags) appendFishFlagCompletions(lines, commandName, pathIsFuncName, "", rootSurface.defaultFlags);
|
|
40
|
+
for (const node of nodes) {
|
|
41
|
+
const path = node.path.join(" ");
|
|
42
|
+
appendFishCommandCompletions(lines, commandName, pathIsFuncName, path, node.children);
|
|
43
|
+
appendFishFlagCompletions(lines, commandName, pathIsFuncName, path, node.mergedFlags);
|
|
44
|
+
}
|
|
45
|
+
return lines.join("\n");
|
|
46
|
+
}
|
|
47
|
+
function emitFishPathHelpers(lines, pathFuncName, pathIsFuncName, visibleCommands, nodes, rootSurface) {
|
|
48
|
+
const valueFlags = collectFishValueFlagPatterns(nodes, rootSurface);
|
|
49
|
+
lines.push(`function ${pathFuncName}`);
|
|
50
|
+
lines.push(" set -l words (commandline -opc)");
|
|
51
|
+
lines.push(" if test (count $words) -gt 0");
|
|
52
|
+
lines.push(" set -e words[1]");
|
|
53
|
+
lines.push(" end");
|
|
54
|
+
lines.push(" set -l subcmd_path ''");
|
|
55
|
+
lines.push(" set -l expect_value 0");
|
|
56
|
+
lines.push(" for word in $words");
|
|
57
|
+
lines.push(" if test $expect_value -eq 1");
|
|
58
|
+
lines.push(" set expect_value 0");
|
|
59
|
+
lines.push(" continue");
|
|
60
|
+
lines.push(" end");
|
|
61
|
+
lines.push(" switch $word");
|
|
62
|
+
lines.push(" case '--'");
|
|
63
|
+
lines.push(" break");
|
|
64
|
+
if (valueFlags.exact.length > 0) {
|
|
65
|
+
lines.push(`\t\t\tcase ${valueFlags.exact.map(quoteShellArg).join(" ")}`);
|
|
66
|
+
lines.push(" set expect_value 1");
|
|
67
|
+
lines.push(" continue");
|
|
68
|
+
}
|
|
69
|
+
if (valueFlags.prefix.length > 0) {
|
|
70
|
+
lines.push(`\t\t\tcase ${valueFlags.prefix.map(quoteShellArg).join(" ")}`);
|
|
71
|
+
lines.push(" continue");
|
|
72
|
+
}
|
|
73
|
+
lines.push(" case '-*'");
|
|
74
|
+
lines.push(" continue");
|
|
75
|
+
lines.push(" end");
|
|
76
|
+
lines.push(" switch \"$subcmd_path|$word\"");
|
|
77
|
+
for (const command of visibleCommands) for (const name of [command.name, ...command.aliases]) {
|
|
78
|
+
lines.push(`\t\t\tcase ${quoteShellArg(`|${name}`)}`);
|
|
79
|
+
lines.push(`\t\t\t\tset subcmd_path ${quoteShellArg(command.name)}`);
|
|
80
|
+
}
|
|
81
|
+
for (const node of nodes) for (const child of node.children) for (const name of [child.name, ...child.aliases]) {
|
|
82
|
+
lines.push(`\t\t\tcase ${quoteShellArg(`${node.path.join(" ")}|${name}`)}`);
|
|
83
|
+
lines.push(`\t\t\t\tset subcmd_path ${quoteShellArg(`${node.path.join(" ")} ${child.name}`)}`);
|
|
84
|
+
}
|
|
85
|
+
lines.push(" case '*'");
|
|
86
|
+
lines.push(" break");
|
|
87
|
+
lines.push(" end");
|
|
88
|
+
lines.push(" end");
|
|
89
|
+
lines.push(" printf '%s\\n' \"$subcmd_path\"");
|
|
90
|
+
lines.push("end");
|
|
91
|
+
lines.push("");
|
|
92
|
+
lines.push(`function ${pathIsFuncName}`);
|
|
93
|
+
lines.push(" set -l expected \"$argv[1]\"");
|
|
94
|
+
lines.push(`\ttest (${pathFuncName}) = "$expected"`);
|
|
95
|
+
lines.push("end");
|
|
96
|
+
lines.push("");
|
|
97
|
+
}
|
|
98
|
+
function appendFishCommandCompletions(lines, commandName, pathIsFuncName, path, commands) {
|
|
99
|
+
const condition = buildFishCondition(pathIsFuncName, path);
|
|
100
|
+
for (const command of commands) {
|
|
101
|
+
const description = quoteShellArg(command.description ?? command.name);
|
|
102
|
+
for (const name of [command.name, ...command.aliases]) lines.push(`complete -c ${commandName} -n "${condition}" -a ${quoteShellArg(name)} -d ${description}`);
|
|
103
|
+
}
|
|
104
|
+
if (commands.length > 0) lines.push("");
|
|
105
|
+
}
|
|
106
|
+
function appendFishFlagCompletions(lines, commandName, pathIsFuncName, path, flags) {
|
|
107
|
+
const condition = buildFishCondition(pathIsFuncName, path);
|
|
108
|
+
for (const [name, schema] of Object.entries(flags)) {
|
|
109
|
+
const parts = [`complete -c ${commandName}`, `-n "${condition}"`];
|
|
110
|
+
for (const alias of getFlagAliasNames(schema, { kind: "short" })) parts.push(`-s ${quoteShellArg(alias)}`);
|
|
111
|
+
parts.push(`-l ${quoteShellArg(name)}`);
|
|
112
|
+
for (const alias of getFlagAliasNames(schema, { kind: "long" })) parts.push(`-l ${quoteShellArg(alias)}`);
|
|
113
|
+
parts.push(`-d ${quoteShellArg(schema.description ?? name)}`);
|
|
114
|
+
if (schema.kind !== "boolean") parts.push("-r", "-f");
|
|
115
|
+
if (schema.kind === "enum" && schema.enumValues !== void 0) {
|
|
116
|
+
for (const enumValue of schema.enumValues) lines.push([...parts, `-a ${quoteShellArg(enumValue)}`].join(" "));
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
lines.push(parts.join(" "));
|
|
120
|
+
}
|
|
121
|
+
if (Object.keys(flags).length > 0) lines.push("");
|
|
122
|
+
}
|
|
123
|
+
function collectFishValueFlagPatterns(nodes, rootSurface) {
|
|
124
|
+
const exact = /* @__PURE__ */ new Set();
|
|
125
|
+
const prefix = /* @__PURE__ */ new Set();
|
|
126
|
+
const flagSets = [
|
|
127
|
+
rootSurface.rootFlags,
|
|
128
|
+
...rootSurface.includeDefaultFlags ? [rootSurface.defaultFlags] : [],
|
|
129
|
+
...nodes.map((node) => node.mergedFlags)
|
|
130
|
+
];
|
|
131
|
+
for (const flags of flagSets) for (const [name, schema] of Object.entries(flags)) {
|
|
132
|
+
if (schema.kind === "boolean") continue;
|
|
133
|
+
exact.add(`--${name}`);
|
|
134
|
+
prefix.add(`--${name}=*`);
|
|
135
|
+
for (const alias of getFlagAliasNames(schema, {
|
|
136
|
+
kind: "short",
|
|
137
|
+
includeHidden: true
|
|
138
|
+
})) {
|
|
139
|
+
exact.add(`-${alias}`);
|
|
140
|
+
prefix.add(`-${alias}*`);
|
|
141
|
+
}
|
|
142
|
+
for (const alias of getFlagAliasNames(schema, {
|
|
143
|
+
kind: "long",
|
|
144
|
+
includeHidden: true
|
|
145
|
+
})) {
|
|
146
|
+
exact.add(`--${alias}`);
|
|
147
|
+
prefix.add(`--${alias}=*`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
exact: [...exact],
|
|
152
|
+
prefix: [...prefix]
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function buildFishCondition(pathIsFuncName, path) {
|
|
156
|
+
return `${pathIsFuncName} ${quoteShellArg(path)}`;
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
export { generateFishCompletion };
|
|
@@ -1,2 +1,353 @@
|
|
|
1
|
-
import{getFlagAliasNames
|
|
2
|
-
|
|
1
|
+
import { getFlagAliasNames } from "../../schema/flag.mjs";
|
|
2
|
+
import { resolveRootCompletionSurface, sanitizeShellIdentifier, versionTag, walkCommandTree } from "./shared.mjs";
|
|
3
|
+
//#region src/core/completion/shells/powershell.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generate a PowerShell completion script for the CLI.
|
|
6
|
+
*
|
|
7
|
+
* @param schema - The CLI schema.
|
|
8
|
+
* @param options - Optional generator configuration.
|
|
9
|
+
* @returns A complete PowerShell completion script.
|
|
10
|
+
*/
|
|
11
|
+
function generatePowerShellCompletion(schema, options) {
|
|
12
|
+
const helperPrefix = `__${sanitizeShellIdentifier(options?.functionPrefix ?? schema.name)}_completions`;
|
|
13
|
+
const dataVarName = `${helperPrefix}_paths`;
|
|
14
|
+
const rootSurface = resolveRootCompletionSurface(schema, options?.rootMode);
|
|
15
|
+
const visibleCommands = rootSurface.visibleCommands;
|
|
16
|
+
const nodes = walkCommandTree(visibleCommands);
|
|
17
|
+
const lines = [];
|
|
18
|
+
lines.push(`# PowerShell completion for ${schema.name}`);
|
|
19
|
+
lines.push(`# Generated by ${versionTag()}`);
|
|
20
|
+
lines.push("#");
|
|
21
|
+
lines.push("# Install for the current session:");
|
|
22
|
+
lines.push(`# ${schema.name} completions powershell | Out-String | Invoke-Expression`);
|
|
23
|
+
lines.push("#");
|
|
24
|
+
lines.push("# Install permanently:");
|
|
25
|
+
lines.push(`# ${schema.name} completions powershell > $PROFILE.CurrentUserCurrentHost`);
|
|
26
|
+
lines.push("");
|
|
27
|
+
lines.push("using namespace System.Management.Automation");
|
|
28
|
+
lines.push("using namespace System.Management.Automation.Language");
|
|
29
|
+
lines.push("");
|
|
30
|
+
emitPowerShellPathTable(lines, dataVarName, rootSurface, nodes);
|
|
31
|
+
lines.push("");
|
|
32
|
+
emitPowerShellHelpers(lines, helperPrefix, dataVarName);
|
|
33
|
+
lines.push("");
|
|
34
|
+
lines.push(`Register-ArgumentCompleter -Native -CommandName ${quotePowerShellString(schema.name)} -ScriptBlock {`);
|
|
35
|
+
lines.push(" param($wordToComplete, $commandAst, $cursorPosition)");
|
|
36
|
+
lines.push(`\t$state = ${helperPrefix}_GetState -CommandAst $commandAst -CursorPosition $cursorPosition`);
|
|
37
|
+
lines.push(" if ($null -eq $state.PathData) {");
|
|
38
|
+
lines.push(" return");
|
|
39
|
+
lines.push(" }");
|
|
40
|
+
lines.push("");
|
|
41
|
+
lines.push(" $results = [System.Collections.Generic.List[CompletionResult]]::new()");
|
|
42
|
+
lines.push(" if ($state.AfterSeparator) {");
|
|
43
|
+
lines.push(" return $results");
|
|
44
|
+
lines.push(" }");
|
|
45
|
+
lines.push("");
|
|
46
|
+
lines.push(`\t$wordPrefix = if ([string]::IsNullOrEmpty($wordToComplete)) { '' } else { $wordToComplete }`);
|
|
47
|
+
lines.push("");
|
|
48
|
+
lines.push(" if ($state.ExpectingValue) {");
|
|
49
|
+
lines.push(`\t\t${helperPrefix}_AddEnumResults -Results $results -Flag $state.ValueFlag -WordToComplete $wordPrefix`);
|
|
50
|
+
lines.push(" return $results");
|
|
51
|
+
lines.push(" }");
|
|
52
|
+
lines.push("");
|
|
53
|
+
lines.push(`\t$inlineValue = ${helperPrefix}_ResolveInlineValueCompletion -PathData $state.PathData -WordToComplete $wordPrefix`);
|
|
54
|
+
lines.push(" if ($null -ne $inlineValue) {");
|
|
55
|
+
lines.push(`\t\t${helperPrefix}_AddInlineEnumResults -Results $results -InlineValue $inlineValue -WordToComplete $wordPrefix`);
|
|
56
|
+
lines.push(" return $results");
|
|
57
|
+
lines.push(" }");
|
|
58
|
+
lines.push("");
|
|
59
|
+
lines.push(" if ($wordPrefix.StartsWith('-')) {");
|
|
60
|
+
lines.push(`\t\t${helperPrefix}_AddFlagResults -Results $results -PathData $state.PathData -WordToComplete $wordPrefix`);
|
|
61
|
+
lines.push(" return $results");
|
|
62
|
+
lines.push(" }");
|
|
63
|
+
lines.push("");
|
|
64
|
+
lines.push(`\t${helperPrefix}_AddCommandResults -Results $results -PathData $state.PathData -WordToComplete $wordPrefix`);
|
|
65
|
+
lines.push(`\t${helperPrefix}_AddFlagResults -Results $results -PathData $state.PathData -WordToComplete $wordPrefix`);
|
|
66
|
+
lines.push(" return $results");
|
|
67
|
+
lines.push("}");
|
|
68
|
+
return lines.join("\n");
|
|
69
|
+
}
|
|
70
|
+
function emitPowerShellPathTable(lines, dataVarName, rootSurface, nodes) {
|
|
71
|
+
lines.push(`$script:${dataVarName} = @{`);
|
|
72
|
+
appendPowerShellPathEntry(lines, "", rootSurface.visibleCommands, {
|
|
73
|
+
...rootSurface.rootFlags,
|
|
74
|
+
...rootSurface.includeDefaultFlags ? rootSurface.defaultFlags : {}
|
|
75
|
+
});
|
|
76
|
+
for (const node of nodes) appendPowerShellPathEntry(lines, node.path.join(" "), node.children, node.mergedFlags);
|
|
77
|
+
lines.push("}");
|
|
78
|
+
}
|
|
79
|
+
function appendPowerShellPathEntry(lines, path, commands, flags) {
|
|
80
|
+
lines.push(`\t${quotePowerShellString(path)} = @{`);
|
|
81
|
+
lines.push(" Commands = @(");
|
|
82
|
+
for (const command of commands) {
|
|
83
|
+
lines.push(" @{");
|
|
84
|
+
lines.push(`\t\t\t\tCanonicalName = ${quotePowerShellString(command.name)}`);
|
|
85
|
+
lines.push(`\t\t\t\tNames = ${formatPowerShellArray([command.name, ...command.aliases])}`);
|
|
86
|
+
lines.push(`\t\t\t\tDescription = ${quotePowerShellString(command.description ?? command.name)}`);
|
|
87
|
+
lines.push(" }");
|
|
88
|
+
}
|
|
89
|
+
lines.push(" )");
|
|
90
|
+
lines.push(" Flags = @(");
|
|
91
|
+
for (const [name, schema] of Object.entries(flags)) {
|
|
92
|
+
const forms = dedupeValues([
|
|
93
|
+
`--${name}`,
|
|
94
|
+
...getFlagAliasNames(schema, { kind: "short" }).map((alias) => `-${alias}`),
|
|
95
|
+
...getFlagAliasNames(schema, { kind: "long" }).map((alias) => `--${alias}`)
|
|
96
|
+
]);
|
|
97
|
+
const parseForms = dedupeValues([
|
|
98
|
+
`--${name}`,
|
|
99
|
+
...getFlagAliasNames(schema, {
|
|
100
|
+
kind: "short",
|
|
101
|
+
includeHidden: true
|
|
102
|
+
}).map((alias) => `-${alias}`),
|
|
103
|
+
...getFlagAliasNames(schema, {
|
|
104
|
+
kind: "long",
|
|
105
|
+
includeHidden: true
|
|
106
|
+
}).map((alias) => `--${alias}`)
|
|
107
|
+
]);
|
|
108
|
+
lines.push(" @{");
|
|
109
|
+
lines.push(`\t\t\t\tCanonicalName = ${quotePowerShellString(name)}`);
|
|
110
|
+
lines.push(`\t\t\t\tForms = ${formatPowerShellArray(forms)}`);
|
|
111
|
+
lines.push(`\t\t\t\tParseForms = ${formatPowerShellArray(parseForms)}`);
|
|
112
|
+
lines.push(`\t\t\t\tRequiresValue = ${schema.kind === "boolean" ? "$false" : "$true"}`);
|
|
113
|
+
lines.push(`\t\t\t\tDescription = ${quotePowerShellString(schema.description ?? name)}`);
|
|
114
|
+
lines.push(`\t\t\t\tEnumValues = ${formatPowerShellArray(schema.enumValues ?? [])}`);
|
|
115
|
+
lines.push(" }");
|
|
116
|
+
}
|
|
117
|
+
lines.push(" )");
|
|
118
|
+
lines.push(" }");
|
|
119
|
+
}
|
|
120
|
+
function emitPowerShellHelpers(lines, helperPrefix, dataVarName) {
|
|
121
|
+
lines.push(`function ${helperPrefix}_UnquoteToken {`);
|
|
122
|
+
lines.push(" param([string]$TokenText)");
|
|
123
|
+
lines.push(" if ($TokenText.Length -lt 2) {");
|
|
124
|
+
lines.push(" return $TokenText");
|
|
125
|
+
lines.push(" }");
|
|
126
|
+
lines.push("");
|
|
127
|
+
lines.push(" if (($TokenText.StartsWith(\"'\") -and $TokenText.EndsWith(\"'\")) -or");
|
|
128
|
+
lines.push(" ($TokenText.StartsWith('\"') -and $TokenText.EndsWith('\"'))) {");
|
|
129
|
+
lines.push(" return $TokenText.Substring(1, $TokenText.Length - 2)");
|
|
130
|
+
lines.push(" }");
|
|
131
|
+
lines.push("");
|
|
132
|
+
lines.push(" return $TokenText");
|
|
133
|
+
lines.push("}");
|
|
134
|
+
lines.push("");
|
|
135
|
+
lines.push(`function ${helperPrefix}_FindCommand {`);
|
|
136
|
+
lines.push(" param($PathData, [string]$Token)");
|
|
137
|
+
lines.push(" foreach ($command in $PathData.Commands) {");
|
|
138
|
+
lines.push(" foreach ($name in $command.Names) {");
|
|
139
|
+
lines.push(" if ($name -eq $Token) {");
|
|
140
|
+
lines.push(" return $command");
|
|
141
|
+
lines.push(" }");
|
|
142
|
+
lines.push(" }");
|
|
143
|
+
lines.push(" }");
|
|
144
|
+
lines.push(" return $null");
|
|
145
|
+
lines.push("}");
|
|
146
|
+
lines.push("");
|
|
147
|
+
lines.push(`function ${helperPrefix}_ResolveFlagToken {`);
|
|
148
|
+
lines.push(" param($PathData, [string]$Token)");
|
|
149
|
+
lines.push(" foreach ($flag in $PathData.Flags) {");
|
|
150
|
+
lines.push(" foreach ($form in $flag.ParseForms) {");
|
|
151
|
+
lines.push(" if ($Token -eq $form) {");
|
|
152
|
+
lines.push(" return @{ Flag = $flag; InlinePrefix = $null }");
|
|
153
|
+
lines.push(" }");
|
|
154
|
+
lines.push("");
|
|
155
|
+
lines.push(" if (-not $flag.RequiresValue) {");
|
|
156
|
+
lines.push(" continue");
|
|
157
|
+
lines.push(" }");
|
|
158
|
+
lines.push("");
|
|
159
|
+
lines.push(" if ($form.StartsWith('--') -and $Token.StartsWith(\"$form=\")) {");
|
|
160
|
+
lines.push(" return @{ Flag = $flag; InlinePrefix = \"$form=\" }");
|
|
161
|
+
lines.push(" }");
|
|
162
|
+
lines.push("");
|
|
163
|
+
lines.push(" if ($form.StartsWith('-') -and -not $form.StartsWith('--') -and $Token.StartsWith($form) -and $Token.Length -gt $form.Length) {");
|
|
164
|
+
lines.push(" return @{ Flag = $flag; InlinePrefix = $form }");
|
|
165
|
+
lines.push(" }");
|
|
166
|
+
lines.push(" }");
|
|
167
|
+
lines.push(" }");
|
|
168
|
+
lines.push(" return $null");
|
|
169
|
+
lines.push("}");
|
|
170
|
+
lines.push("");
|
|
171
|
+
lines.push(`function ${helperPrefix}_GetState {`);
|
|
172
|
+
lines.push(" param([CommandAst]$CommandAst, [int]$CursorPosition)");
|
|
173
|
+
lines.push(`\t$pathData = $script:${dataVarName}[${quotePowerShellString("")}]`);
|
|
174
|
+
lines.push(" $path = \"\"");
|
|
175
|
+
lines.push(" $afterSeparator = $false");
|
|
176
|
+
lines.push(" $expectingValue = $false");
|
|
177
|
+
lines.push(" $valueFlag = $null");
|
|
178
|
+
lines.push(" $tokens = [System.Collections.Generic.List[string]]::new()");
|
|
179
|
+
lines.push("");
|
|
180
|
+
lines.push(" foreach ($element in $CommandAst.CommandElements | Select-Object -Skip 1) {");
|
|
181
|
+
lines.push(" if ($element.Extent.EndOffset -ge $CursorPosition) {");
|
|
182
|
+
lines.push(" break");
|
|
183
|
+
lines.push(" }");
|
|
184
|
+
lines.push(`\t\t$tokenText = ${helperPrefix}_UnquoteToken $element.Extent.Text`);
|
|
185
|
+
lines.push(" [void]$tokens.Add($tokenText)");
|
|
186
|
+
lines.push(" }");
|
|
187
|
+
lines.push("");
|
|
188
|
+
lines.push(" foreach ($token in $tokens) {");
|
|
189
|
+
lines.push(" if ($expectingValue) {");
|
|
190
|
+
lines.push(" $expectingValue = $false");
|
|
191
|
+
lines.push(" $valueFlag = $null");
|
|
192
|
+
lines.push(" continue");
|
|
193
|
+
lines.push(" }");
|
|
194
|
+
lines.push("");
|
|
195
|
+
lines.push(" if ($token -eq '--') {");
|
|
196
|
+
lines.push(" $afterSeparator = $true");
|
|
197
|
+
lines.push(" break");
|
|
198
|
+
lines.push(" }");
|
|
199
|
+
lines.push("");
|
|
200
|
+
lines.push(`\t\t$flagMatch = ${helperPrefix}_ResolveFlagToken -PathData $pathData -Token $token`);
|
|
201
|
+
lines.push(" if ($null -ne $flagMatch) {");
|
|
202
|
+
lines.push(" if ($flagMatch.InlinePrefix -eq $null -and $flagMatch.Flag.RequiresValue) {");
|
|
203
|
+
lines.push(" $expectingValue = $true");
|
|
204
|
+
lines.push(" $valueFlag = $flagMatch.Flag");
|
|
205
|
+
lines.push(" }");
|
|
206
|
+
lines.push(" continue");
|
|
207
|
+
lines.push(" }");
|
|
208
|
+
lines.push("");
|
|
209
|
+
lines.push(" if ($token.StartsWith('-')) {");
|
|
210
|
+
lines.push(" continue");
|
|
211
|
+
lines.push(" }");
|
|
212
|
+
lines.push("");
|
|
213
|
+
lines.push(`\t\t$command = ${helperPrefix}_FindCommand -PathData $pathData -Token $token`);
|
|
214
|
+
lines.push(" if ($null -eq $command) {");
|
|
215
|
+
lines.push(" break");
|
|
216
|
+
lines.push(" }");
|
|
217
|
+
lines.push("");
|
|
218
|
+
lines.push(" $path = if ([string]::IsNullOrEmpty($path)) {");
|
|
219
|
+
lines.push(" $command.CanonicalName");
|
|
220
|
+
lines.push(" } else {");
|
|
221
|
+
lines.push(" \"$path $($command.CanonicalName)\"");
|
|
222
|
+
lines.push(" }");
|
|
223
|
+
lines.push(`\t\t$nextPathData = $script:${dataVarName}[$path]`);
|
|
224
|
+
lines.push(" if ($null -eq $nextPathData) {");
|
|
225
|
+
lines.push(" break");
|
|
226
|
+
lines.push(" }");
|
|
227
|
+
lines.push(" $pathData = $nextPathData");
|
|
228
|
+
lines.push(" }");
|
|
229
|
+
lines.push("");
|
|
230
|
+
lines.push(" return @{");
|
|
231
|
+
lines.push(" Path = $path");
|
|
232
|
+
lines.push(" PathData = $pathData");
|
|
233
|
+
lines.push(" AfterSeparator = $afterSeparator");
|
|
234
|
+
lines.push(" ExpectingValue = $expectingValue");
|
|
235
|
+
lines.push(" ValueFlag = $valueFlag");
|
|
236
|
+
lines.push(" }");
|
|
237
|
+
lines.push("}");
|
|
238
|
+
lines.push("");
|
|
239
|
+
lines.push(`function ${helperPrefix}_ResolveInlineValueCompletion {`);
|
|
240
|
+
lines.push(" param($PathData, [string]$WordToComplete)");
|
|
241
|
+
lines.push(`\t$flagMatch = ${helperPrefix}_ResolveFlagToken -PathData $PathData -Token $WordToComplete`);
|
|
242
|
+
lines.push(" if ($null -eq $flagMatch -or $flagMatch.InlinePrefix -eq $null) {");
|
|
243
|
+
lines.push(" return $null");
|
|
244
|
+
lines.push(" }");
|
|
245
|
+
lines.push(" return $flagMatch");
|
|
246
|
+
lines.push("}");
|
|
247
|
+
lines.push("");
|
|
248
|
+
lines.push(`function ${helperPrefix}_FormatCompletionValue {`);
|
|
249
|
+
lines.push(" param([string]$Value)");
|
|
250
|
+
lines.push(" if ($Value.Length -eq 0) {");
|
|
251
|
+
lines.push(" return \"''\"");
|
|
252
|
+
lines.push(" }");
|
|
253
|
+
lines.push("");
|
|
254
|
+
lines.push(" if ($Value -match '^[a-zA-Z0-9_./:\\\\-]+$') {");
|
|
255
|
+
lines.push(" return $Value");
|
|
256
|
+
lines.push(" }");
|
|
257
|
+
lines.push("");
|
|
258
|
+
lines.push(" return \"'$([CodeGeneration]::EscapeSingleQuotedStringContent($Value))'\"");
|
|
259
|
+
lines.push("}");
|
|
260
|
+
lines.push("");
|
|
261
|
+
lines.push(`function ${helperPrefix}_MatchesCompletionPrefix {`);
|
|
262
|
+
lines.push(" param([string]$WordToComplete, [string]$RawText, [string]$CompletionText)");
|
|
263
|
+
lines.push(" if ([string]::IsNullOrEmpty($WordToComplete)) {");
|
|
264
|
+
lines.push(" return $true");
|
|
265
|
+
lines.push(" }");
|
|
266
|
+
lines.push("");
|
|
267
|
+
lines.push(" return $RawText.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase) -or $CompletionText.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)");
|
|
268
|
+
lines.push("}");
|
|
269
|
+
lines.push("");
|
|
270
|
+
lines.push(`function ${helperPrefix}_AddCommandResults {`);
|
|
271
|
+
lines.push(" param($Results, $PathData, [string]$WordToComplete)");
|
|
272
|
+
lines.push(" foreach ($command in $PathData.Commands) {");
|
|
273
|
+
lines.push(" foreach ($name in $command.Names) {");
|
|
274
|
+
lines.push(" if (-not [string]::IsNullOrEmpty($WordToComplete) -and -not $name.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {");
|
|
275
|
+
lines.push(" continue");
|
|
276
|
+
lines.push(" }");
|
|
277
|
+
lines.push("");
|
|
278
|
+
lines.push(" $Results.Add([CompletionResult]::new(");
|
|
279
|
+
lines.push(" $name,");
|
|
280
|
+
lines.push(" $name,");
|
|
281
|
+
lines.push(" [CompletionResultType]::Command,");
|
|
282
|
+
lines.push(" $command.Description");
|
|
283
|
+
lines.push(" ))");
|
|
284
|
+
lines.push(" }");
|
|
285
|
+
lines.push(" }");
|
|
286
|
+
lines.push("}");
|
|
287
|
+
lines.push("");
|
|
288
|
+
lines.push(`function ${helperPrefix}_AddFlagResults {`);
|
|
289
|
+
lines.push(" param($Results, $PathData, [string]$WordToComplete)");
|
|
290
|
+
lines.push(" foreach ($flag in $PathData.Flags) {");
|
|
291
|
+
lines.push(" foreach ($form in $flag.Forms) {");
|
|
292
|
+
lines.push(" if (-not [string]::IsNullOrEmpty($WordToComplete) -and -not $form.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {");
|
|
293
|
+
lines.push(" continue");
|
|
294
|
+
lines.push(" }");
|
|
295
|
+
lines.push("");
|
|
296
|
+
lines.push(" $Results.Add([CompletionResult]::new(");
|
|
297
|
+
lines.push(" $form,");
|
|
298
|
+
lines.push(" $form,");
|
|
299
|
+
lines.push(" [CompletionResultType]::ParameterName,");
|
|
300
|
+
lines.push(" $flag.Description");
|
|
301
|
+
lines.push(" ))");
|
|
302
|
+
lines.push(" }");
|
|
303
|
+
lines.push(" }");
|
|
304
|
+
lines.push("}");
|
|
305
|
+
lines.push("");
|
|
306
|
+
lines.push(`function ${helperPrefix}_AddEnumResults {`);
|
|
307
|
+
lines.push(" param($Results, $Flag, [string]$WordToComplete)");
|
|
308
|
+
lines.push(" foreach ($value in $Flag.EnumValues) {");
|
|
309
|
+
lines.push(`\t\t$completionText = ${helperPrefix}_FormatCompletionValue -Value $value`);
|
|
310
|
+
lines.push(`\t\tif (-not (${helperPrefix}_MatchesCompletionPrefix -WordToComplete $WordToComplete -RawText $value -CompletionText $completionText)) {`);
|
|
311
|
+
lines.push(" continue");
|
|
312
|
+
lines.push(" }");
|
|
313
|
+
lines.push("");
|
|
314
|
+
lines.push(" $Results.Add([CompletionResult]::new(");
|
|
315
|
+
lines.push(" $completionText,");
|
|
316
|
+
lines.push(" $value,");
|
|
317
|
+
lines.push(" [CompletionResultType]::ParameterValue,");
|
|
318
|
+
lines.push(" $Flag.Description");
|
|
319
|
+
lines.push(" ))");
|
|
320
|
+
lines.push(" }");
|
|
321
|
+
lines.push("}");
|
|
322
|
+
lines.push("");
|
|
323
|
+
lines.push(`function ${helperPrefix}_AddInlineEnumResults {`);
|
|
324
|
+
lines.push(" param($Results, $InlineValue, [string]$WordToComplete)");
|
|
325
|
+
lines.push(" foreach ($value in $InlineValue.Flag.EnumValues) {");
|
|
326
|
+
lines.push(" $rawCompletion = \"$($InlineValue.InlinePrefix)$value\"");
|
|
327
|
+
lines.push(`\t\t$formattedValue = ${helperPrefix}_FormatCompletionValue -Value $value`);
|
|
328
|
+
lines.push(" $completionText = \"$($InlineValue.InlinePrefix)$formattedValue\"");
|
|
329
|
+
lines.push(`\t\tif (-not (${helperPrefix}_MatchesCompletionPrefix -WordToComplete $WordToComplete -RawText $rawCompletion -CompletionText $completionText)) {`);
|
|
330
|
+
lines.push(" continue");
|
|
331
|
+
lines.push(" }");
|
|
332
|
+
lines.push("");
|
|
333
|
+
lines.push(" $Results.Add([CompletionResult]::new(");
|
|
334
|
+
lines.push(" $completionText,");
|
|
335
|
+
lines.push(" $value,");
|
|
336
|
+
lines.push(" [CompletionResultType]::ParameterValue,");
|
|
337
|
+
lines.push(" $InlineValue.Flag.Description");
|
|
338
|
+
lines.push(" ))");
|
|
339
|
+
lines.push(" }");
|
|
340
|
+
lines.push("}");
|
|
341
|
+
}
|
|
342
|
+
function quotePowerShellString(value) {
|
|
343
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
344
|
+
}
|
|
345
|
+
function formatPowerShellArray(values) {
|
|
346
|
+
if (values.length === 0) return "@()";
|
|
347
|
+
return `@(${values.map(quotePowerShellString).join(", ")})`;
|
|
348
|
+
}
|
|
349
|
+
function dedupeValues(values) {
|
|
350
|
+
return [...new Set(values)];
|
|
351
|
+
}
|
|
352
|
+
//#endregion
|
|
353
|
+
export { generatePowerShellCompletion };
|