diff --git a/crates/tw-adopt/src/clients.rs b/crates/tw-adopt/src/clients.rs index 44e3e82d..5cb13ec2 100644 --- a/crates/tw-adopt/src/clients.rs +++ b/crates/tw-adopt/src/clients.rs @@ -287,7 +287,7 @@ pub fn adoptable() -> Vec { Client { id: "zed", name: "Zed", - config: ".config/zed/settings.json", + config: crate::paths::zed_settings(), format: Format::Json, takes_effect: TakesEffect::Immediately, shadowed_by: &[], @@ -298,7 +298,7 @@ pub fn adoptable() -> Vec { "Zed keeps its key outside the configuration file, so it has to be filled in once in Zed's settings.", )], verified: Verified::FieldsOnly, - marker: &[".config/zed"], + marker: &[crate::paths::ZED_DIR], process: &["Zed"], env_vars: &[], key_elsewhere: Some(( @@ -346,7 +346,7 @@ pub struct ManualOnly { /// 为它生成专用密钥时用的标识:`cursor` / `continue` / `gemini-cli` pub id: &'static str, pub name: &'static str, - /// 这个客户端的码前缀:每一步是 `.<后缀>`,提醒是 `.caveat` + /// 这个客户端的码前缀:每一步是 `.<后缀>`,提醒也是 `.<后缀>`(见 `caveat`) code: &'static str, /// 手动配置的几步,每步「码的后缀,英文原句」。 /// @@ -356,7 +356,9 @@ pub struct ManualOnly { steps: &'static [(&'static str, &'static str)], /// 填带 `/v1` 的地址还是不带的 v1: bool, - caveat: &'static str, + /// 提醒,「码的后缀,英文原句」。**后缀也可以按平台分** —— 两个平台 + /// 说的不是一句话时,用各自的码,界面才能各翻各的 + caveat: (&'static str, &'static str), } impl ManualOnly { @@ -384,10 +386,11 @@ impl ManualOnly { /// 接管不了的那一句提醒。**必须和步骤一起给** —— 只说怎么配、不说 /// 配完还漏什么,等于说了假话 pub fn caveat(&self) -> Msg { + let (suffix, text) = self.caveat; Msg { - code: format!("{}.caveat", self.code), + code: format!("{}.{suffix}", self.code), args: BTreeMap::new(), - text: self.caveat.to_string(), + text: text.to_string(), } } } @@ -410,14 +413,20 @@ pub fn manual_only() -> Vec { ), ], v1: true, - caveat: "Tab completion and inline edit still go to Cursor's own service rather than the gateway, so only part of Cursor is covered.", + caveat: ( + "caveat", + "Tab completion and inline edit still go to Cursor's own service rather than the gateway, so only part of Cursor is covered.", + ), }, ManualOnly { id: "continue", name: "Continue", code: "adopt.manual.continue", steps: &[ + #[cfg(not(windows))] ("open", "Open ~/.continue/config.yaml."), + #[cfg(windows)] + ("open_windows", r"Open %USERPROFILE%\.continue\config.yaml."), ( "entry", "Add an entry to the models list with provider set to openai, apiBase set to the gateway address and apiKey set to the key.", @@ -428,12 +437,18 @@ pub fn manual_only() -> Vec { // 改写,不是替换一个标量。我们的 YAML 补丁只做后者 // (见 crate::yaml 开头那段)。宁可少接管一个客户端,也不 // 要写一段我们自己没把握的结构。 - caveat: "This needs a new entry in the models list, which is not written automatically; follow the steps above.", + caveat: ( + "caveat", + "This needs a new entry in the models list, which is not written automatically; follow the steps above.", + ), }, ManualOnly { id: "gemini-cli", name: "Gemini CLI", code: "adopt.manual.gemini_cli", + // **Windows 上没有 shell 配置文件可 export**:用户级环境变量用 setx + // 写,写完只对之后打开的终端生效。两个平台各用各的码 + #[cfg(not(windows))] steps: &[ ( "export", @@ -441,11 +456,28 @@ pub fn manual_only() -> Vec { ), ("reopen", "Then reopen the terminal."), ], + #[cfg(windows)] + steps: &[ + ( + "setx", + "In a terminal, run setx GOOGLE_GEMINI_BASE_URL followed by the gateway address, and setx GEMINI_API_KEY followed by the key.", + ), + ("reopen", "Then reopen the terminal."), + ], v1: false, // 它只认环境变量,没有可写的配置字段。改 .zshrc 超出了 // 「只改 endpoint 和 key 字段」的边界 —— // **报告是我们的职责,修改是他的权利。** - caveat: "Gemini CLI reads the endpoint only from the environment. ThinkWatch does not edit shell configuration files, so add it by hand.", + #[cfg(not(windows))] + caveat: ( + "caveat", + "Gemini CLI reads the endpoint only from the environment. ThinkWatch does not edit shell configuration files, so add it by hand.", + ), + #[cfg(windows)] + caveat: ( + "caveat_windows", + "Gemini CLI reads the endpoint only from the environment. ThinkWatch does not change environment variables, so add them by hand.", + ), }, ] } @@ -592,7 +624,7 @@ impl Client { /// **没检测到它的时候也要给。**配置文件不在默认位置、或者装在别的 /// 用户目录下时,检测不到不等于用不了 —— 照着做一样能接上。 pub fn manual_steps(&self) -> Vec { - let file = format!("~/{}", self.config); + let file = crate::paths::shown(self.config); let mut out = vec![Msg { code: "adopt.manual.file".into(), args: BTreeMap::from([("file".to_string(), file.clone())]), @@ -620,10 +652,13 @@ impl Client { } pub fn config_path(&self, home: &std::path::Path) -> PathBuf { - home.join(self.config) + crate::paths::under(home, self.config) } pub fn shadow_paths(&self, home: &std::path::Path) -> Vec { - self.shadowed_by.iter().map(|p| home.join(p)).collect() + self.shadowed_by + .iter() + .map(|p| crate::paths::under(home, p)) + .collect() } /// 注释前缀。JSON 没有 —— 那时哨兵走旁文件。 pub fn comment_prefix(&self) -> Option<&'static str> { @@ -726,9 +761,9 @@ mod tests { let m = manual_only(); let cursor = m.iter().find(|c| c.name == "Cursor").unwrap(); assert!( - cursor.caveat.contains("Tab completion"), + cursor.caveat.1.contains("Tab completion"), "{}", - cursor.caveat + cursor.caveat.1 ); // 地址是真实的地址,而不是一个让用户自己去找的说法;**不在句子里**, // 界面单独给它一个复制按钮 @@ -802,7 +837,13 @@ mod tests { } for c in adoptable() { let steps = c.manual_steps(); - assert_eq!(steps[0].arg("file"), format!("~/{}", c.config), "{}", c.id); + // 写法按平台(`~/…` 或 `%USERPROFILE%\…`),各自的样子见 paths 里那条测试 + assert_eq!( + steps[0].arg("file"), + crate::paths::shown(c.config), + "{}", + c.id + ); assert!(!edits(&c, &gw).is_empty(), "{}:没有要写的字段", c.id); } // Zed 的密钥不在配置文件里,多一步在它自己的设置里填 diff --git a/crates/tw-adopt/src/detect.rs b/crates/tw-adopt/src/detect.rs index 6de12295..4e8e1c39 100644 --- a/crates/tw-adopt/src/detect.rs +++ b/crates/tw-adopt/src/detect.rs @@ -86,7 +86,11 @@ pub fn detect_one(c: &Client, home: &Path) -> Detected { Detected { id: c.id, name: c.name, - installed: c.marker.iter().any(|m| home.join(m).exists()) || text.is_some(), + installed: c + .marker + .iter() + .any(|m| crate::paths::under(home, m).exists()) + || text.is_some(), has_config: text.is_some(), adopted_at_ms: rec .filter(|r: &SidecarRecord| r.client == c.id) @@ -573,7 +577,7 @@ pub fn diagnose(c: &Client, home: &Path, project: Option<&Path>) -> Vec // 三、项目级配置盖住了用户级 if let Some(proj) = project { - let local = proj.join(c.config); + let local = crate::paths::under(proj, c.config); if local.exists() { out.push(Finding { level: Level::Suspect, diff --git a/crates/tw-adopt/src/mcp.rs b/crates/tw-adopt/src/mcp.rs index acc515ff..ce236871 100644 --- a/crates/tw-adopt/src/mcp.rs +++ b/crates/tw-adopt/src/mcp.rs @@ -149,7 +149,7 @@ pub fn targets() -> Vec { Target { client: "zed", name: "Zed", - config: ".config/zed/settings.json", + config: crate::paths::zed_settings(), format: Format::Json, key: "context_servers", copyable: false, @@ -170,7 +170,7 @@ pub fn target(client: &str) -> Result { impl Target { pub fn path(&self, home: &Path) -> PathBuf { - home.join(self.config) + crate::paths::under(home, self.config) } fn check(&self) -> Result<(), McpError> { if self.copyable { diff --git a/crates/tw-adopt/src/paths.rs b/crates/tw-adopt/src/paths.rs index a89466ef..776cf3d4 100644 --- a/crates/tw-adopt/src/paths.rs +++ b/crates/tw-adopt/src/paths.rs @@ -17,7 +17,57 @@ //! //! 机器级的那一个(管理策略)没有 home 可言,它读环境变量。 -use std::path::PathBuf; +use std::path::{Path, PathBuf}; + +/// 把一条用 `/` 写的相对路径接到 `base` 下面。 +/// +/// **按 `/` 拆开逐段接,不直接 `join`。**这些相对路径在源码里一律用 `/` 写, +/// 直接 `base.join(".claude/settings.json")` 在 Windows 上得到的是 +/// `C:\Users\x\.claude/settings.json` —— 文件照样找得到(Windows 两种分隔符 +/// 都认),但界面上显示的就是这么一串正反斜杠混着的路径。逐段接出来的是 +/// 那个平台自己的写法。 +pub fn under(base: &Path, rel: &str) -> PathBuf { + rel.split('/') + .filter(|c| !c.is_empty()) + .fold(base.to_path_buf(), |p, c| p.join(c)) +} + +/// 给人看的写法:「打开 … 」那一步里的路径。 +/// +/// macOS 上是 `~/.claude/settings.json`;Windows 上没有 `~`,写成资源管理器 +/// 地址栏里能直接粘贴的 `%USERPROFILE%\.claude\settings.json`。 +pub fn shown(rel: &str) -> String { + #[cfg(windows)] + { + format!(r"%USERPROFILE%\{}", rel.replace('/', r"\")) + } + #[cfg(not(windows))] + { + format!("~/{rel}") + } +} + +/// Zed 的设置文件,相对 home。 +/// +/// Windows 上的 Zed 按那个平台的习惯放在漫游的 AppData 下,不是 `~/.config`。 +pub fn zed_settings() -> &'static str { + #[cfg(windows)] + { + "AppData/Roaming/Zed/settings.json" + } + #[cfg(not(windows))] + { + ".config/zed/settings.json" + } +} + +/// Zed 装过的痕迹:它的设置目录。**是常量不是函数**:它要放进 +/// `marker: &[…]` 那个静态切片里,函数的返回值进不去。 +pub const ZED_DIR: &str = if cfg!(windows) { + "AppData/Roaming/Zed" +} else { + ".config/zed" +}; /// Claude Desktop 的配置,相对 home。 /// @@ -74,6 +124,34 @@ mod tests { ); } + /// 接出来的每一段都是一个组件:没有哪一段里还夹着 `/`。 + #[test] + fn a_relative_path_is_joined_one_component_at_a_time() { + let p = under(Path::new("/h"), ".claude/settings.json"); + let parts: Vec<_> = p.components().map(|c| c.as_os_str().to_owned()).collect(); + assert_eq!(parts.last().unwrap(), "settings.json"); + assert_eq!(parts[parts.len() - 2], ".claude"); + assert!( + parts + .iter() + .all(|c| !c.to_string_lossy().contains('/') || c == "/") + ); + assert_eq!( + under(Path::new("/h"), "a//b/"), + Path::new("/h").join("a").join("b") + ); + } + + #[test] + fn the_shown_path_is_written_the_platform_way() { + let s = shown(".claude/settings.json"); + if cfg!(windows) { + assert_eq!(s, r"%USERPROFILE%\.claude\settings.json"); + } else { + assert_eq!(s, "~/.claude/settings.json"); + } + } + /// 机器级的那一个反过来:必须是绝对的。 #[test] fn the_managed_policy_is_machine_wide() { diff --git a/crates/tw-control/tests/adopt.rs b/crates/tw-control/tests/adopt.rs index bba52212..01e2ad62 100644 --- a/crates/tw-control/tests/adopt.rs +++ b/crates/tw-control/tests/adopt.rs @@ -751,7 +751,10 @@ async fn every_client_says_how_to_connect_it_by_hand() { let cc = v.clients.iter().find(|c| c.id == "claude-code").unwrap(); assert_eq!(cc.manual.endpoint, base, "Claude Code 要的地址不带 /v1"); - assert_eq!(cc.manual.steps[0].arg("file"), "~/.claude/settings.json"); + assert_eq!( + cc.manual.steps[0].arg("file"), + tw_adopt::paths::shown(".claude/settings.json") + ); let url = cc .manual .fields diff --git a/crates/tw-scan/src/sources.rs b/crates/tw-scan/src/sources.rs index 7d02ddf6..262ba880 100644 --- a/crates/tw-scan/src/sources.rs +++ b/crates/tw-scan/src/sources.rs @@ -22,6 +22,8 @@ use std::path::{Path, PathBuf}; +use tw_adopt::paths::under; + /// 这份文件属于哪类攻击面。**顺序就是危险度**(那张表)。 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Kind { @@ -145,45 +147,49 @@ pub fn user_level(home: &Path) -> Vec { f( "claude-code", Kind::Hooks, - home.join(".claude/settings.json"), + under(home, ".claude/settings.json"), ), f( "claude-code", Kind::Hooks, - home.join(".claude/settings.local.json"), + under(home, ".claude/settings.local.json"), ), // 危险度第二:MCP - f("claude-code", Kind::Mcp, home.join(".claude.json")), + f("claude-code", Kind::Mcp, under(home, ".claude.json")), // **Claude Desktop 只在这张表里**:它是订阅制,接管不了,但它的 // MCP 配置是危险度第二高的攻击面。漏掉它等于扫描留了个洞 f( "claude-desktop", Kind::Mcp, - home.join(tw_adopt::paths::claude_desktop_config()), + under(home, tw_adopt::paths::claude_desktop_config()), ), - f("cursor", Kind::Mcp, home.join(".cursor/mcp.json")), - f("codex", Kind::Mcp, home.join(".codex/config.toml")), + f("cursor", Kind::Mcp, under(home, ".cursor/mcp.json")), + f("codex", Kind::Mcp, under(home, ".codex/config.toml")), f( "opencode", Kind::Mcp, - home.join(".config/opencode/opencode.json"), + under(home, ".config/opencode/opencode.json"), + ), + f( + "zed", + Kind::Mcp, + under(home, tw_adopt::paths::zed_settings()), ), - f("zed", Kind::Mcp, home.join(".config/zed/settings.json")), // 指令类 f( "claude-code", Kind::Instructions, - home.join(".claude/CLAUDE.md"), + under(home, ".claude/CLAUDE.md"), ), - f("codex", Kind::Instructions, home.join(".codex/AGENTS.md")), + f("codex", Kind::Instructions, under(home, ".codex/AGENTS.md")), ]; - for p in skills_in(&home.join(".claude/skills")) { + for p in skills_in(&under(home, ".claude/skills")) { v.push(f("claude-code", Kind::Skill, p)); } - for p in md_in(&home.join(".claude/commands")) { + for p in md_in(&under(home, ".claude/commands")) { v.push(f("claude-code", Kind::Command, p)); } - for p in md_in(&home.join(".claude/agents")) { + for p in md_in(&under(home, ".claude/agents")) { v.push(f("claude-code", Kind::Agent, p)); } v.retain(|s| s.path.exists()); @@ -199,25 +205,25 @@ pub fn in_project(dir: &Path) -> Vec { f( "claude-code", Kind::Hooks, - dir.join(".claude/settings.json"), + under(dir, ".claude/settings.json"), ), f( "claude-code", Kind::Hooks, - dir.join(".claude/settings.local.json"), + under(dir, ".claude/settings.local.json"), ), - f("claude-code", Kind::Mcp, dir.join(".mcp.json")), - f("claude-code", Kind::Instructions, dir.join("CLAUDE.md")), - f("codex", Kind::Instructions, dir.join("AGENTS.md")), - f("cursor", Kind::Instructions, dir.join(".cursorrules")), + f("claude-code", Kind::Mcp, under(dir, ".mcp.json")), + f("claude-code", Kind::Instructions, under(dir, "CLAUDE.md")), + f("codex", Kind::Instructions, under(dir, "AGENTS.md")), + f("cursor", Kind::Instructions, under(dir, ".cursorrules")), ]; - for p in md_in(&dir.join(".claude/commands")) { + for p in md_in(&under(dir, ".claude/commands")) { v.push(f("claude-code", Kind::Command, p)); } - for p in md_in(&dir.join(".claude/agents")) { + for p in md_in(&under(dir, ".claude/agents")) { v.push(f("claude-code", Kind::Agent, p)); } - for p in skills_in(&dir.join(".claude/skills")) { + for p in skills_in(&under(dir, ".claude/skills")) { v.push(f("claude-code", Kind::Skill, p)); } v.retain(|s| s.path.exists());