Skip to content

Commit 8b91b32

Browse files
authored
feat(run-config): 解释器字段标出 shell 家族(shellFamily),宿主可列出已安装的同类 shell (#92)
脚本类运行配置(shell、PowerShell、批处理)的 interpreter 字段带上 shellFamily:posix、powershell 或 cmd。宿主按这个值筛选自己找到的已安装 shell,作为解释器的可选项,和“自动”、自定义路径并列;保存的值仍是程序名 或路径。程序类配置没有解释器,也就没有这个属性。
1 parent 5204e81 commit 8b91b32

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3928,7 +3928,7 @@ when this platform cannot run the kind, as in `runConfig.configurationTypes`;
39283928
the form still edits and saves it. `formVersion` is `1`; a host refuses a newer
39293929
one. `values` maps field IDs to the current values. `fields` is the ordered list
39303930
of `{id, kind, required?, placement?, automatic?, options?, visibleWhen?,
3931-
note?}`:
3931+
note?, shellFamily?}`:
39323932

39333933
| Attribute | Meaning |
39343934
| --- | --- |
@@ -3937,6 +3937,7 @@ note?}`:
39373937
| `automatic` | The value used while the field is empty, shown as its placeholder |
39383938
| `visibleWhen` | `{field, equals}`: shown only while that field has that value |
39393939
| `note` | An explanation key; `windowsOnly` says the field matters only on Windows |
3940+
| `shellFamily` | On an `executable` field, the family of shell it takes: `posix` (sh, bash, zsh and the like), `powershell` (Windows PowerShell and `pwsh`) or `cmd`. A host that finds the installed shells may offer those of the family as choices beside the automatic value and a path of the user's own; the value is still a program name or path. The `interpreter` field of each script kind has it |
39403941

39413942
Hosts draw the fields in order, skip a field whose `kind` they do not know and
39423943
send back every value they received, including keys they do not show, so a

‎rust/crates/core/src/tests/run_configuration.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,32 @@ fn built_in_process_runs_are_created_edited_moved_and_deleted_through_the_editor
17871787
assert!(field(&form, "executionPolicyBypass").is_null());
17881788
assert!(field(&form, "program").is_null());
17891789

1790+
// The interpreter field names the family of shell that runs the kind, so a
1791+
// host can offer the installed shells of it; a program has no interpreter.
1792+
for (provider, family) in [
1793+
("process.shell", "posix"),
1794+
("process.powershell", "powershell"),
1795+
("process.batch", "cmd"),
1796+
] {
1797+
let form = call(
1798+
"runConfig.editorForm",
1799+
serde_json::json!({"root":root,"provider":provider}),
1800+
);
1801+
assert_eq!(form["ok"], true, "{form}");
1802+
assert_eq!(
1803+
field(&form, "interpreter")["shellFamily"],
1804+
family,
1805+
"{provider}"
1806+
);
1807+
}
1808+
let program = call(
1809+
"runConfig.editorForm",
1810+
serde_json::json!({"root":root,"provider":"process.executable"}),
1811+
);
1812+
assert_eq!(program["ok"], true, "{program}");
1813+
assert!(field(&program, "interpreter").is_null());
1814+
assert!(field(&program, "program")["shellFamily"].is_null());
1815+
17901816
// Wrong values come back by field and change nothing.
17911817
let invalid = call(
17921818
"runConfig.saveConfiguration",

‎rust/crates/execution/src/configuration/form.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ fn fields(kind: Kind, root: &Path, draft: &Draft, candidates: Vec<Value>) -> Vec
506506
field("workingDirectory", "directory"),
507507
field("environment", "environment"),
508508
field("interpreter", "executable")
509-
.automatic(Some(automatic.executable.text().to_string())),
509+
.automatic(Some(automatic.executable.text().to_string()))
510+
.shell_family(kind.shell_family()),
510511
field("interpreterArguments", "arguments")
511512
.automatic(Some(argument_text(&effective.arguments))),
512513
]);
@@ -552,6 +553,15 @@ impl Field {
552553
self
553554
}
554555

556+
/// The family of shell an `executable` field takes, such as `posix`, so
557+
/// a host can offer the installed shells of that family as choices.
558+
fn shell_family(mut self, family: Option<&str>) -> Self {
559+
if let Some(family) = family {
560+
self.0["shellFamily"] = json!(family);
561+
}
562+
self
563+
}
564+
555565
fn options(mut self, options: &[&str]) -> Self {
556566
self.0["options"] = json!(options);
557567
self

‎rust/crates/execution/src/configuration/process.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ impl Kind {
8686
}
8787
}
8888

89+
/// The family of shell that runs scripts of the kind, which an editor's
90+
/// interpreter field names so a host can offer the installed shells of
91+
/// that family: `posix` (sh, bash, zsh and the like), `powershell`
92+
/// (Windows PowerShell and `pwsh`) or `cmd`. A program has no interpreter.
93+
pub(crate) fn shell_family(self) -> Option<&'static str> {
94+
match self {
95+
Self::Shell => Some("posix"),
96+
Self::PowerShell => Some("powershell"),
97+
Self::Batch => Some("cmd"),
98+
Self::Executable => None,
99+
}
100+
}
101+
89102
/// File name suffix of the temporary file an inline script is run from;
90103
/// PowerShell runs only `.ps1` files and `cmd` only `.cmd` or `.bat`.
91104
fn script_suffix(self) -> &'static str {

0 commit comments

Comments
 (0)