#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::Duration;
use gpui::{Context, Task};
pub use gpui_updater_core::*;
type BoxedEngine = UpdateEngine<Box<dyn UpdateSource>>;
pub struct Updater {
status: UpdateStatus,
available: Option<Release>,
engine: Arc<BoxedEngine>,
task: Option<Task<()>>,
}
impl Updater {
pub fn new<S: UpdateSource>(source: S, config: EngineConfig, _cx: &mut Context<Self>) -> Self {
let engine = UpdateEngine::new(Box::new(source) as Box<dyn UpdateSource>, config);
Self {
status: UpdateStatus::Idle,
available: None,
engine: Arc::new(engine),
task: None,
}
}
#[must_use]
pub fn status(&self) -> &UpdateStatus {
&self.status
}
#[must_use]
pub fn available(&self) -> Option<&Release> {
self.available.as_ref()
}
fn set_status(&mut self, status: UpdateStatus, cx: &mut Context<Self>) {
self.status = status;
cx.notify();
}
pub fn check(&mut self, cx: &mut Context<Self>) {
if self.status.is_busy() {
return;
}
self.set_status(UpdateStatus::Checking, cx);
let engine = self.engine.clone();
self.task = Some(cx.spawn(async move |this, cx| {
let result = cx
.background_executor()
.spawn(async move { engine.check() })
.await;
this.update(cx, |this, cx| {
this.task = None;
match result {
Ok(Some(release)) => {
let version = release.version.clone();
this.available = Some(release);
this.set_status(UpdateStatus::Available(version), cx);
}
Ok(None) => this.set_status(UpdateStatus::UpToDate, cx),
Err(e) => this.set_status(UpdateStatus::Errored(e.to_string()), cx),
}
})
.ok();
}));
}
pub fn download_and_install(&mut self, cx: &mut Context<Self>) {
if self.status.is_busy() {
return;
}
let Some(release) = self.available.clone() else {
return;
};
let engine = self.engine.clone();
self.set_status(
UpdateStatus::Downloading {
downloaded: 0,
total: None,
},
cx,
);
self.task = Some(cx.spawn(async move |this, cx| {
let downloaded = Arc::new(AtomicU64::new(0));
let total = Arc::new(AtomicU64::new(0)); let done = Arc::new(AtomicBool::new(false));
let dl_task = {
let (engine, release) = (engine.clone(), release.clone());
let (d, t, fin) = (downloaded.clone(), total.clone(), done.clone());
cx.background_executor().spawn(async move {
let result = engine.download(&release, |got, tot| {
d.store(got, Ordering::Relaxed);
t.store(tot.unwrap_or(0), Ordering::Relaxed);
});
fin.store(true, Ordering::Relaxed);
result
})
};
loop {
let got = downloaded.load(Ordering::Relaxed);
let tot = total.load(Ordering::Relaxed);
this.update(cx, |this, cx| {
this.set_status(
UpdateStatus::Downloading {
downloaded: got,
total: (tot != 0).then_some(tot),
},
cx,
);
})
.ok();
if done.load(Ordering::Relaxed) {
break;
}
cx.background_executor()
.timer(Duration::from_millis(120))
.await;
}
let artifact = match dl_task.await {
Ok(path) => path,
Err(e) => {
this.update(cx, |this, cx| {
this.task = None;
this.set_status(UpdateStatus::Errored(e.to_string()), cx);
})
.ok();
return;
}
};
this.update(cx, |this, cx| this.set_status(UpdateStatus::Installing, cx))
.ok();
let installed = {
let engine = engine.clone();
cx.background_executor()
.spawn(async move { engine.install(&artifact) })
.await
};
this.update(cx, |this, cx| {
this.task = None;
let status = installed_status(installed, release.version, |path| {
cx.set_restart_path(path);
});
this.set_status(status, cx);
})
.ok();
}));
}
pub fn restart(&self, cx: &mut Context<Self>) {
cx.restart();
}
}
fn installed_status(
installed: Result<Installed>,
version: Version,
set_restart_path: impl FnOnce(std::path::PathBuf),
) -> UpdateStatus {
match installed {
Ok(installed) => {
if let Some(path) = installed.restart_path {
set_restart_path(path);
}
UpdateStatus::Staged(version)
}
Err(e) => UpdateStatus::Errored(e.to_string()),
}
}
#[cfg(test)]
mod tests;