refactor(scripts): 重构AI环境生成脚本以优化工具选择逻辑

- 移除shell脚本中的冗余命令执行逻辑
- 添加select_tool函数统一处理工具偏好和回退逻辑
- 使用新函数重构搜索、JSON处理、脚本编写等工具选择
- 更新时间戳以反映代码变更后的重新生成
- 简化构建工具配置的回退策略
This commit is contained in:
GeWuYou 2026-03-21 12:49:51 +08:00
parent 3130a3bab2
commit 2001eddbff
4 changed files with 44 additions and 42 deletions

View File

@ -1,5 +1,5 @@
schema_version: 1 schema_version: 1
generated_at_utc: "2026-03-21T04:01:27Z" generated_at_utc: "2026-03-21T04:47:58Z"
generated_from: ".ai/environment/tools.raw.yaml" generated_from: ".ai/environment/tools.raw.yaml"
generator: "scripts/generate-ai-environment.py" generator: "scripts/generate-ai-environment.py"
platform: platform:

View File

@ -1,5 +1,5 @@
schema_version: 1 schema_version: 1
generated_at_utc: "2026-03-21T04:00:19Z" generated_at_utc: "2026-03-21T04:47:28Z"
generator: "scripts/collect-dev-environment.sh" generator: "scripts/collect-dev-environment.sh"
platform: platform:

View File

@ -266,13 +266,3 @@ if [[ "${MODE}" == "--write" ]]; then
else else
collect_inventory collect_inventory
fi fi
ensure_supported_mode
if [[ "${MODE}" == "--write" ]]; then
mkdir -p "$(dirname "${OUTPUT_PATH}")"
collect_inventory > "${OUTPUT_PATH}"
printf 'Wrote %s\n' "${OUTPUT_PATH}"
else
collect_inventory
fi

View File

@ -76,6 +76,18 @@ def available_tool(raw: dict[str, Any], section: str, name: str) -> bool:
return bool_value(raw, section, name, "installed") return bool_value(raw, section, name, "installed")
def select_tool(
use_for: str,
preferred: str | None,
fallback: str | None,
) -> dict[str, str]:
return {
"preferred": choose(preferred, fallback),
"fallback": fallback or "unavailable",
"use_for": use_for,
}
def build_ai_inventory(raw: dict[str, Any]) -> dict[str, Any]: def build_ai_inventory(raw: dict[str, Any]) -> dict[str, Any]:
has_python = available_tool(raw, "required_runtimes", "python3") has_python = available_tool(raw, "required_runtimes", "python3")
has_node = available_tool(raw, "required_runtimes", "node") has_node = available_tool(raw, "required_runtimes", "node")
@ -86,36 +98,36 @@ def build_ai_inventory(raw: dict[str, Any]) -> dict[str, Any]:
has_bash = available_tool(raw, "required_tools", "bash") has_bash = available_tool(raw, "required_tools", "bash")
has_docker = available_tool(raw, "project_tools", "docker") has_docker = available_tool(raw, "project_tools", "docker")
search = { search = select_tool(
"preferred": choose("rg" if has_rg else None, "grep"), use_for="Repository text search.",
"fallback": "grep" if has_rg else "unavailable", preferred="rg" if has_rg else None,
"use_for": "Repository text search.", fallback="grep",
} )
json = { json = select_tool(
"preferred": choose("jq" if has_jq else None, "python3" if has_python else None), use_for="Inspecting or transforming JSON command output.",
"fallback": "python3" if has_jq and has_python else "unavailable", preferred="jq" if has_jq else None,
"use_for": "Inspecting or transforming JSON command output.", fallback="python3" if has_python else None,
} )
scripting = { scripting = select_tool(
"preferred": choose("python3" if has_python else None, "bash" if has_bash else None), use_for="Non-trivial local automation and helper scripts.",
"fallback": "bash" if has_python and has_bash else "unavailable", preferred="python3" if has_python else None,
"use_for": "Non-trivial local automation and helper scripts.", fallback="bash" if has_bash else None,
} )
shell = { shell = select_tool(
"preferred": choose("bash" if has_bash else None, "sh"), use_for="Repository shell scripts and command execution.",
"fallback": "sh" if has_bash else "unavailable", preferred="bash" if has_bash else None,
"use_for": "Repository shell scripts and command execution.", fallback="sh",
} )
docs = { docs = select_tool(
"preferred": choose("bun" if has_bun else None, "npm" if has_node else None), use_for="Installing and previewing the docs site.",
"fallback": "npm" if has_bun and has_node else "unavailable", preferred="bun" if has_bun else None,
"use_for": "Installing and previewing the docs site.", fallback="npm" if has_node else None,
} )
build = { build = select_tool(
"preferred": choose("dotnet" if has_dotnet else None, None), use_for="Build, test, restore, and solution validation.",
"fallback": "unavailable", preferred="dotnet" if has_dotnet else None,
"use_for": "Build, test, restore, and solution validation.", fallback=None,
} )
if bool_value(raw, "platform", "wsl"): if bool_value(raw, "platform", "wsl"):
platform_family = "wsl-linux" platform_family = "wsl-linux"