WIP: Start on auto-update

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
Co-Authored-By: Keith Simmons <keith@zed.dev>
This commit is contained in:
Antonio Scandurra
2022-04-04 18:59:57 +02:00
co-authored by Nathan Sobo Max Brunsfeld Keith Simmons
parent cbf6d827db
commit 38e902b241
8 changed files with 189 additions and 20 deletions
+117
View File
@@ -0,0 +1,117 @@
use anyhow::{anyhow, Result};
use client::http::{self, HttpClient};
use gpui::{platform::AppVersion, AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
use serde::Deserialize;
use smol::io::AsyncReadExt;
use std::{sync::Arc, time::Duration};
use surf::Request;
const POLL_INTERVAL: Duration = Duration::from_secs(60 * 60);
#[derive(Clone, PartialEq, Eq)]
pub enum AutoUpdateStatus {
Idle,
Checking,
Downloading,
Updated,
Errored { error: String },
}
pub struct AutoUpdater {
status: AutoUpdateStatus,
current_version: AppVersion,
http_client: Arc<dyn HttpClient>,
pending_poll: Option<Task<()>>,
server_url: String,
}
#[derive(Deserialize)]
struct JsonRelease {
version: String,
url: http::Url,
}
impl Entity for AutoUpdater {
type Event = ();
}
impl AutoUpdater {
pub fn new(
current_version: AppVersion,
http_client: Arc<dyn HttpClient>,
server_url: String,
) -> Self {
Self {
status: AutoUpdateStatus::Idle,
current_version,
http_client,
server_url,
pending_poll: None,
}
}
pub fn start_polling(&mut self, cx: &mut ModelContext<Self>) -> Task<()> {
cx.spawn(|this, mut cx| async move {
loop {
this.update(&mut cx, |this, cx| this.poll(cx));
cx.background().timer(POLL_INTERVAL).await;
}
})
}
pub fn poll(&mut self, cx: &mut ModelContext<Self>) {
if self.pending_poll.is_some() {
return;
}
self.status = AutoUpdateStatus::Checking;
self.pending_poll = Some(cx.spawn(|this, mut cx| async move {
if let Err(error) = Self::update(this.clone(), cx.clone()).await {
this.update(&mut cx, |this, cx| {
this.status = AutoUpdateStatus::Errored {
error: error.to_string(),
};
cx.notify();
});
}
this.update(&mut cx, |this, _| this.pending_poll = None);
}));
cx.notify();
}
async fn update(this: ModelHandle<Self>, mut cx: AsyncAppContext) -> Result<()> {
let (client, server_url) = this.read_with(&cx, |this, _| {
(this.http_client.clone(), this.server_url.clone())
});
let mut response = client
.send(Request::new(
http::Method::Get,
http::Url::parse(&format!("{server_url}/api/releases/latest"))?,
))
.await?;
let release = response
.body_json::<JsonRelease>()
.await
.map_err(|err| anyhow!("error deserializing release {:?}", err))?;
let latest_version = release.version.parse::<AppVersion>()?;
let current_version = cx.platform().app_version()?;
if latest_version <= current_version {
this.update(&mut cx, |this, cx| {
this.status = AutoUpdateStatus::Idle;
cx.notify();
});
return Ok(());
}
let temp_dir = tempdir::TempDir::new("zed")?;
let dmg_path = temp_dir.path().join("Zed.dmg");
let mut dmg_file = smol::fs::File::create(dmg_path).await?;
let response = client
.send(Request::new(http::Method::Get, release.url))
.await?;
smol::io::copy(response.bytes(), &mut dmg_file).await?;
Ok(())
}
}
+9 -1
View File
@@ -19,7 +19,8 @@ use workspace::{
AppState, OpenNew, OpenParams, OpenPaths, Settings,
};
use zed::{
self, assets::Assets, build_window_options, build_workspace, fs::RealFs, languages, menus,
self, assets::Assets, auto_updater::AutoUpdater, build_window_options, build_workspace,
fs::RealFs, languages, menus,
};
fn main() {
@@ -64,6 +65,13 @@ fn main() {
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
let channel_list =
cx.add_model(|cx| ChannelList::new(user_store.clone(), client.clone(), cx));
let auto_updater = if let Ok(current_version) = cx.platform().app_version() {
Some(cx.add_model(|cx| {
AutoUpdater::new(current_version, http, client::ZED_SERVER_URL.clone())
}))
} else {
None
};
project::Project::init(&client);
client::Channel::init(&client);
+1
View File
@@ -1,4 +1,5 @@
pub mod assets;
pub mod auto_updater;
pub mod languages;
pub mod menus;
#[cfg(any(test, feature = "test-support"))]