use std::path::PathBuf;
use crate::cache::InstallSource;
use crate::config::{KitConfig, KitPaths};
use crate::error::{KitError, KitResult};
use crate::install::{remove_dir_if_exists, Installed};
use crate::manifest::PluginManifest;
use crate::registry::Registry;
const MAX_ASSET_BYTES: u64 = 128 * 1024 * 1024;
pub fn try_install(
cfg: &KitConfig,
paths: &KitPaths,
registry: &Registry,
crate_name: &str,
version: &str,
) -> KitResult<Option<Installed>> {
let Some(info) = registry.view(crate_name)? else {
return Ok(None);
};
let Some(repo) = info.repository.as_deref() else {
return Ok(None);
};
let Some((owner, name)) = parse_github_repo(repo) else {
return Ok(None);
};
let target = cfg.effective_target();
let ext = crate::loader::lib_ext_for_target(target);
let base = format!("https://github.com/{owner}/{name}/releases/download/v{version}/{crate_name}-{version}-{target}");
let lib_url = format!("{base}.{ext}");
let manifest_url = format!("{base}.toml");
let Some(lib_bytes) = registry.try_download(&lib_url, MAX_ASSET_BYTES)? else {
return Ok(None);
};
let Some(manifest_bytes) = registry.try_download(&manifest_url, MAX_ASSET_BYTES)? else {
return Ok(None);
};
let manifest_text = String::from_utf8(manifest_bytes)
.map_err(|e| KitError::Registry(format!("{manifest_url} is not valid UTF-8: {e}")))?;
let manifest = PluginManifest::parse(&manifest_text, &PathBuf::from(&manifest_url))?;
if manifest.plugin.version != version {
return Err(KitError::Registry(format!(
"prebuilt manifest version mismatch: the asset declares {}, but {version} was requested",
manifest.plugin.version
)));
}
let plugin_dir = paths.plugin_dir(crate_name);
remove_dir_if_exists(&plugin_dir)?;
std::fs::create_dir_all(&plugin_dir)?;
let lib_name = format!("{}.{ext}", manifest.effective_lib_stem(cfg, crate_name));
let dest_lib = plugin_dir.join(&lib_name);
std::fs::write(&dest_lib, lib_bytes)?;
manifest.write(&paths.manifest_path(crate_name, &cfg.manifest_name))?;
Ok(Some(Installed {
crate_name: crate_name.to_string(),
version: version.to_string(),
dir: plugin_dir,
library: dest_lib,
source: InstallSource::Prebuilt,
}))
}
pub fn parse_github_repo(url: &str) -> Option<(String, String)> {
let rest = url
.trim()
.strip_prefix("git+")
.unwrap_or(url)
.strip_prefix("https://github.com/")
.or_else(|| url.strip_prefix("http://github.com/"))?;
let mut parts = rest.trim_end_matches('/').split('/');
let owner = parts.next()?.trim();
let repo = parts.next()?.trim().trim_end_matches(".git");
if owner.is_empty() || repo.is_empty() {
return None;
}
Some((owner.to_string(), repo.to_string()))
}
#[cfg(test)]
mod tests {
use super::parse_github_repo;
#[test]
fn parses_common_forms() {
let want = Some(("imba97".to_string(), "bmux-plugin-cargo".to_string()));
assert_eq!(
parse_github_repo("https://github.com/imba97/bmux-plugin-cargo"),
want
);
assert_eq!(
parse_github_repo("https://github.com/imba97/bmux-plugin-cargo.git"),
want
);
assert_eq!(
parse_github_repo("https://github.com/imba97/bmux-plugin-cargo/"),
want
);
assert_eq!(
parse_github_repo("git+https://github.com/imba97/bmux-plugin-cargo"),
want
);
}
#[test]
fn rejects_non_github() {
assert_eq!(parse_github_repo("https://gitlab.com/a/b"), None);
assert_eq!(parse_github_repo("https://github.com/onlyowner"), None);
}
}