use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use kitchen_downloads::{Artifact, Hub, dir_size};
use crate::plugin::PluginInfo;
use crate::plugin::discover::{PluginKind, info};
#[derive(Debug, Clone)]
pub struct Installed {
pub name: String,
pub program: PathBuf,
pub info: Result<PluginInfo, String>,
pub bytes: u64,
pub removable: bool,
}
#[derive(Debug, Clone)]
pub struct Manager {
pub kind: PluginKind,
pub hub: Option<Hub>,
}
impl Manager {
pub fn new(kind: PluginKind, hub: Option<Hub>) -> Self {
Self { kind, hub }
}
pub fn list(&self) -> Vec<Installed> {
self.kind
.discover()
.into_iter()
.map(|(program, info)| {
let name = program.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let data = self.hub.as_ref().map_or(0, |h| h.disk_use(Some(self.kind.short_name(&name))));
let bytes = std::fs::metadata(&program).map(|m| m.len()).unwrap_or(0) + data;
let removable = program.parent() == Some(self.kind.user_dir.as_path());
Installed { name, program, info, bytes, removable }
})
.collect()
}
pub fn install_from(&self, source: &Path) -> Result<PluginInfo, String> {
let name = source.file_name().map(|n| n.to_string_lossy().into_owned()).ok_or("no file name")?;
if !self.kind.is_plugin(source) {
return Err(format!("{name}: not a plugin of this app (its name starts with {})", self.kind.prefix));
}
info(&name, source)?;
std::fs::create_dir_all(&self.kind.user_dir).map_err(|e| format!("{}: {e}", self.kind.user_dir.display()))?;
let dest = self.kind.user_dir.join(&name);
let part = dest.with_extension("part");
std::fs::copy(source, &part).map_err(|e| format!("{}: {e}", part.display()))?;
make_executable(&part)?;
std::fs::rename(&part, &dest).map_err(|e| format!("{}: {e}", dest.display()))?;
info(&name, &dest)
}
pub fn install_artifact(
&self,
artifact: &Artifact,
progress: &mut dyn FnMut(u64, u64),
cancel: Option<&AtomicBool>,
) -> Result<PluginInfo, String> {
let hub = self.hub.as_ref().ok_or("no download cache set")?;
let file = hub.pull(artifact, progress, cancel)?;
let named = file.with_file_name(&artifact.file_name);
make_executable(&named)?;
self.install_from(&named)
}
pub fn remove(&self, name: &str, data: bool) -> Result<(), String> {
let program = self.kind.user_dir.join(name);
if !program.is_file() {
return Err(format!("{name} is not in {}: it was not installed here", self.kind.user_dir.display()));
}
std::fs::remove_file(&program).map_err(|e| format!("{}: {e}", program.display()))?;
if data && let Some(hub) = &self.hub {
let dir = hub.family_dir(self.kind.short_name(name));
if dir.exists() {
std::fs::remove_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
}
}
Ok(())
}
pub fn disk_use(&self) -> u64 {
dir_size(&self.kind.user_dir) + self.hub.as_ref().map_or(0, |h| h.disk_use(None))
}
}
#[cfg(unix)]
fn make_executable(path: &Path) -> Result<(), String> {
use std::os::unix::fs::PermissionsExt as _;
let mut p = std::fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?.permissions();
p.set_mode(p.mode() | 0o755);
std::fs::set_permissions(path, p).map_err(|e| format!("{}: {e}", path.display()))
}
#[cfg(not(unix))]
fn make_executable(_: &Path) -> Result<(), String> {
Ok(())
}