crate_plugin_kit/install/
prebuilt.rs1use std::path::PathBuf;
30
31use crate::cache::InstallSource;
32use crate::config::{KitConfig, KitPaths};
33use crate::error::{KitError, KitResult};
34use crate::install::{remove_dir_if_exists, Installed};
35use crate::manifest::PluginManifest;
36use crate::registry::Registry;
37
38const MAX_ASSET_BYTES: u64 = 128 * 1024 * 1024;
41
42pub fn try_install(
50 cfg: &KitConfig,
51 paths: &KitPaths,
52 registry: &Registry,
53 crate_name: &str,
54 version: &str,
55) -> KitResult<Option<Installed>> {
56 let Some(info) = registry.view(crate_name)? else {
58 return Ok(None);
59 };
60 let Some(repo) = info.repository.as_deref() else {
61 return Ok(None);
62 };
63 let Some((owner, name)) = parse_github_repo(repo) else {
64 return Ok(None);
66 };
67
68 let target = cfg.effective_target();
69 let ext = crate::loader::lib_ext_for_target(target);
70 let base = format!("https://github.com/{owner}/{name}/releases/download/v{version}/{crate_name}-{version}-{target}");
71
72 let lib_url = format!("{base}.{ext}");
73 let manifest_url = format!("{base}.toml");
74
75 let Some(lib_bytes) = registry.try_download(&lib_url, MAX_ASSET_BYTES)? else {
77 return Ok(None);
78 };
79
80 let Some(manifest_bytes) = registry.try_download(&manifest_url, MAX_ASSET_BYTES)? else {
84 return Ok(None);
85 };
86
87 let manifest_text = String::from_utf8(manifest_bytes)
90 .map_err(|e| KitError::Registry(format!("{manifest_url} is not valid UTF-8: {e}")))?;
91 let manifest = PluginManifest::parse(&manifest_text, &PathBuf::from(&manifest_url))?;
92
93 if manifest.plugin.version != version {
94 return Err(KitError::Registry(format!(
95 "prebuilt manifest version mismatch: the asset declares {}, but {version} was requested",
96 manifest.plugin.version
97 )));
98 }
99
100 let plugin_dir = paths.plugin_dir(crate_name);
102 remove_dir_if_exists(&plugin_dir)?;
103 std::fs::create_dir_all(&plugin_dir)?;
104
105 let lib_name = format!("{}.{ext}", manifest.effective_lib_stem(cfg, crate_name));
106 let dest_lib = plugin_dir.join(&lib_name);
107 std::fs::write(&dest_lib, lib_bytes)?;
108
109 manifest.write(&paths.manifest_path(crate_name, &cfg.manifest_name))?;
110
111 Ok(Some(Installed {
112 crate_name: crate_name.to_string(),
113 version: version.to_string(),
114 dir: plugin_dir,
115 library: dest_lib,
116 source: InstallSource::Prebuilt,
117 }))
118}
119
120pub fn parse_github_repo(url: &str) -> Option<(String, String)> {
131 let rest = url
132 .trim()
133 .strip_prefix("git+")
134 .unwrap_or(url)
135 .strip_prefix("https://github.com/")
136 .or_else(|| url.strip_prefix("http://github.com/"))?;
137
138 let mut parts = rest.trim_end_matches('/').split('/');
139 let owner = parts.next()?.trim();
140 let repo = parts.next()?.trim().trim_end_matches(".git");
141
142 if owner.is_empty() || repo.is_empty() {
143 return None;
144 }
145 Some((owner.to_string(), repo.to_string()))
146}
147
148#[cfg(test)]
149mod tests {
150 use super::parse_github_repo;
151
152 #[test]
153 fn parses_common_forms() {
154 let want = Some(("imba97".to_string(), "bmux-plugin-cargo".to_string()));
155 assert_eq!(
156 parse_github_repo("https://github.com/imba97/bmux-plugin-cargo"),
157 want
158 );
159 assert_eq!(
160 parse_github_repo("https://github.com/imba97/bmux-plugin-cargo.git"),
161 want
162 );
163 assert_eq!(
164 parse_github_repo("https://github.com/imba97/bmux-plugin-cargo/"),
165 want
166 );
167 assert_eq!(
168 parse_github_repo("git+https://github.com/imba97/bmux-plugin-cargo"),
169 want
170 );
171 }
172
173 #[test]
174 fn rejects_non_github() {
175 assert_eq!(parse_github_repo("https://gitlab.com/a/b"), None);
176 assert_eq!(parse_github_repo("https://github.com/onlyowner"), None);
177 }
178}