Skip to main content

vtcode_core/skills/loader/
resources.rs

1use std::fs;
2use std::path::Path;
3
4use anyhow::Result;
5
6pub fn load_skill_resources(skill_path: &Path) -> Result<Vec<crate::skills::types::SkillResource>> {
7    let mut resources = Vec::new();
8    let resource_dir = skill_path.join("scripts");
9
10    if resource_dir.exists() {
11        for entry in fs::read_dir(&resource_dir)? {
12            let entry = entry?;
13            let path = entry.path();
14
15            // Trust the dirent type instead of stat-ing each entry again.
16            if vtcode_commons::walk::entry_is_file(&entry) {
17                let rel_path = path
18                    .strip_prefix(skill_path)
19                    .map(|p| p.to_string_lossy().to_string())
20                    .unwrap_or_default();
21
22                let resource_type = match path.extension().and_then(|e| e.to_str()) {
23                    Some("py") | Some("sh") | Some("bash") => crate::skills::types::ResourceType::Script,
24                    Some("md") => crate::skills::types::ResourceType::Markdown,
25                    Some("json") | Some("yaml") | Some("yml") => crate::skills::types::ResourceType::Reference,
26                    _ => crate::skills::types::ResourceType::Other(format!("{:?}", path.extension())),
27                };
28
29                resources.push(crate::skills::types::SkillResource { path: rel_path, resource_type, content: None });
30            }
31        }
32    }
33
34    // Check for references/ directory
35    let references_dir = skill_path.join("references");
36    if references_dir.exists() {
37        for entry in fs::read_dir(&references_dir)? {
38            let entry = entry?;
39            let path = entry.path();
40
41            if vtcode_commons::walk::entry_is_file(&entry) {
42                let rel_path = path
43                    .strip_prefix(skill_path)
44                    .map(|p| p.to_string_lossy().to_string())
45                    .unwrap_or_default();
46
47                let resource_type = match path.extension().and_then(|e| e.to_str()) {
48                    Some("md") => crate::skills::types::ResourceType::Reference,
49                    Some("json") | Some("yaml") | Some("yml") | Some("txt") | Some("csv") => {
50                        crate::skills::types::ResourceType::Reference
51                    }
52                    _ => crate::skills::types::ResourceType::Other(format!("{:?}", path.extension())),
53                };
54
55                resources.push(crate::skills::types::SkillResource { path: rel_path, resource_type, content: None });
56            }
57        }
58    }
59
60    // Check for assets/ directory
61    let assets_dir = skill_path.join("assets");
62    if assets_dir.exists() {
63        for entry in fs::read_dir(&assets_dir)? {
64            let entry = entry?;
65            let path = entry.path();
66
67            if vtcode_commons::walk::entry_is_file(&entry) {
68                let rel_path = path
69                    .strip_prefix(skill_path)
70                    .map(|p| p.to_string_lossy().to_string())
71                    .unwrap_or_default();
72
73                let resource_type = match path.extension().and_then(|e| e.to_str()) {
74                    Some("png") | Some("jpg") | Some("jpeg") | Some("gif") | Some("svg") => {
75                        crate::skills::types::ResourceType::Asset
76                    }
77                    Some("json") | Some("yaml") | Some("yml") | Some("txt") | Some("csv") => {
78                        crate::skills::types::ResourceType::Asset
79                    }
80                    _ => crate::skills::types::ResourceType::Asset,
81                };
82
83                resources.push(crate::skills::types::SkillResource { path: rel_path, resource_type, content: None });
84            }
85        }
86    }
87
88    Ok(resources)
89}
90
91pub(crate) fn is_executable_file(path: &Path) -> bool {
92    #[cfg(unix)]
93    {
94        use std::os::unix::fs::PermissionsExt;
95        if let Ok(meta) = path.metadata() {
96            return meta.permissions().mode() & 0o111 != 0;
97        }
98    }
99    #[cfg(windows)]
100    {
101        if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
102            return matches!(ext.to_lowercase().as_str(), "exe" | "bat" | "cmd");
103        }
104    }
105    false
106}