use std::sync::mpsc::{self, Receiver};
use std::thread;
use anyhow::{anyhow, Result};
use update_informer::{registry, Check};
const REPO_OWNER: &str = "stekovinbranturry";
const REPO_NAME: &str = "vkit-rs";
pub type UpdateHandle = Receiver<String>;
pub fn check_in_background() -> UpdateHandle {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let name = env!("CARGO_PKG_NAME");
let overridden = std::env::var("VKIT_CURRENT_VERSION").ok();
let version = overridden
.clone()
.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
let mut informer = update_informer::new(registry::Crates, name, version);
if overridden.is_some() {
informer = informer.interval(std::time::Duration::ZERO);
}
if let Ok(Some(new_version)) = informer.check_version() {
let _ = tx.send(new_version.to_string());
}
});
rx
}
pub fn run() -> Result<()> {
let current = env!("CARGO_PKG_VERSION");
let status = self_update::backends::github::Update::configure()
.repo_owner(REPO_OWNER)
.repo_name(REPO_NAME)
.bin_name(env!("CARGO_PKG_NAME"))
.show_download_progress(true)
.current_version(current)
.build()
.map_err(|e| anyhow!("初始化更新器失败:{e}"))?
.update()
.map_err(|e| anyhow!("更新失败:{e}"))?;
if status.updated() {
println!("✓ 已更新到 v{}", status.version());
} else {
println!("✓ 已是最新版本 v{current}");
}
Ok(())
}