|
| 1 | +use crate::cli::SkillsAction; |
| 2 | +use crate::error::{ErrorCode, NetError}; |
| 3 | +use crate::output::Protocol; |
| 4 | +use std::fs; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +const PLUGIN_FILES: &[(&str, &str)] = &[ |
| 8 | + ( |
| 9 | + ".claude-plugin/plugin.json", |
| 10 | + include_str!("../../claude-plugin/.claude-plugin/plugin.json"), |
| 11 | + ), |
| 12 | + ("README.md", include_str!("../../claude-plugin/README.md")), |
| 13 | + ("LICENSE", include_str!("../../claude-plugin/LICENSE")), |
| 14 | + ( |
| 15 | + "skills/http-requests/SKILL.md", |
| 16 | + include_str!("../../claude-plugin/skills/http-requests/SKILL.md"), |
| 17 | + ), |
| 18 | + ( |
| 19 | + "skills/websocket-debugging/SKILL.md", |
| 20 | + include_str!("../../claude-plugin/skills/websocket-debugging/SKILL.md"), |
| 21 | + ), |
| 22 | + ( |
| 23 | + "skills/network-diagnostics/SKILL.md", |
| 24 | + include_str!("../../claude-plugin/skills/network-diagnostics/SKILL.md"), |
| 25 | + ), |
| 26 | + ( |
| 27 | + "skills/mqtt-messaging/SKILL.md", |
| 28 | + include_str!("../../claude-plugin/skills/mqtt-messaging/SKILL.md"), |
| 29 | + ), |
| 30 | + ( |
| 31 | + "skills/tcp-udp-testing/SKILL.md", |
| 32 | + include_str!("../../claude-plugin/skills/tcp-udp-testing/SKILL.md"), |
| 33 | + ), |
| 34 | + ( |
| 35 | + "skills/sse-monitoring/SKILL.md", |
| 36 | + include_str!("../../claude-plugin/skills/sse-monitoring/SKILL.md"), |
| 37 | + ), |
| 38 | + ( |
| 39 | + "skills/output-filtering/SKILL.md", |
| 40 | + include_str!("../../claude-plugin/skills/output-filtering/SKILL.md"), |
| 41 | + ), |
| 42 | + ( |
| 43 | + "references/cli-reference.md", |
| 44 | + include_str!("../../claude-plugin/references/cli-reference.md"), |
| 45 | + ), |
| 46 | + ( |
| 47 | + "references/output-schema.md", |
| 48 | + include_str!("../../claude-plugin/references/output-schema.md"), |
| 49 | + ), |
| 50 | + ( |
| 51 | + "references/error-codes.md", |
| 52 | + include_str!("../../claude-plugin/references/error-codes.md"), |
| 53 | + ), |
| 54 | +]; |
| 55 | + |
| 56 | +fn home_dir() -> Result<PathBuf, NetError> { |
| 57 | + std::env::var("HOME") |
| 58 | + .or_else(|_| std::env::var("USERPROFILE")) |
| 59 | + .map(PathBuf::from) |
| 60 | + .map_err(|_| NetError::new(ErrorCode::IoError, "could not determine home directory", Protocol::Http)) |
| 61 | +} |
| 62 | + |
| 63 | +fn write_plugin(target: &Path) -> Result<(), NetError> { |
| 64 | + for (rel_path, content) in PLUGIN_FILES { |
| 65 | + let dest = target.join(rel_path); |
| 66 | + if let Some(parent) = dest.parent() { |
| 67 | + fs::create_dir_all(parent).map_err(|e| { |
| 68 | + NetError::new( |
| 69 | + ErrorCode::IoError, |
| 70 | + format!("failed to create directory {}: {e}", parent.display()), |
| 71 | + Protocol::Http, |
| 72 | + ) |
| 73 | + })?; |
| 74 | + } |
| 75 | + fs::write(&dest, content).map_err(|e| { |
| 76 | + NetError::new( |
| 77 | + ErrorCode::IoError, |
| 78 | + format!("failed to write {}: {e}", dest.display()), |
| 79 | + Protocol::Http, |
| 80 | + ) |
| 81 | + })?; |
| 82 | + } |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +pub async fn run(action: SkillsAction) -> Result<(), NetError> { |
| 87 | + match action { |
| 88 | + SkillsAction::Install(args) => { |
| 89 | + let target = match args.path { |
| 90 | + Some(p) => PathBuf::from(p), |
| 91 | + None => home_dir()?.join(".claude/plugins/no"), |
| 92 | + }; |
| 93 | + write_plugin(&target)?; |
| 94 | + println!("Skills installed to {}", target.display()); |
| 95 | + } |
| 96 | + SkillsAction::Export(args) => { |
| 97 | + let target = PathBuf::from(&args.path); |
| 98 | + write_plugin(&target)?; |
| 99 | + println!("Skills exported to {}", target.display()); |
| 100 | + } |
| 101 | + } |
| 102 | + Ok(()) |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn plugin_files_not_empty() { |
| 111 | + for (path, content) in PLUGIN_FILES { |
| 112 | + assert!(!content.is_empty(), "embedded file {path} should not be empty"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn plugin_files_count() { |
| 118 | + assert_eq!(PLUGIN_FILES.len(), 13); |
| 119 | + } |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn install_to_temp_dir() { |
| 123 | + let dir = tempfile::tempdir().unwrap(); |
| 124 | + let target = dir.path().to_path_buf(); |
| 125 | + let action = SkillsAction::Install(crate::cli::SkillsInstallArgs { |
| 126 | + path: Some(target.to_string_lossy().into_owned()), |
| 127 | + }); |
| 128 | + run(action).await.unwrap(); |
| 129 | + |
| 130 | + assert!(target.join(".claude-plugin/plugin.json").exists()); |
| 131 | + assert!(target.join("skills/http-requests/SKILL.md").exists()); |
| 132 | + assert!(target.join("references/cli-reference.md").exists()); |
| 133 | + assert!(target.join("README.md").exists()); |
| 134 | + assert!(target.join("LICENSE").exists()); |
| 135 | + } |
| 136 | + |
| 137 | + #[tokio::test] |
| 138 | + async fn export_to_temp_dir() { |
| 139 | + let dir = tempfile::tempdir().unwrap(); |
| 140 | + let target = dir.path().join("exported"); |
| 141 | + let action = SkillsAction::Export(crate::cli::SkillsExportArgs { |
| 142 | + path: target.to_string_lossy().into_owned(), |
| 143 | + }); |
| 144 | + run(action).await.unwrap(); |
| 145 | + |
| 146 | + assert!(target.join(".claude-plugin/plugin.json").exists()); |
| 147 | + assert!(target.join("skills/mqtt-messaging/SKILL.md").exists()); |
| 148 | + assert!(target.join("references/error-codes.md").exists()); |
| 149 | + } |
| 150 | +} |
0 commit comments