auto_update.rs

  1use anyhow::{anyhow, Context, Result};
  2use client::http::HttpClient;
  3
  4use gpui::{
  5    actions,
  6    elements::{Empty, MouseEventHandler, Text},
  7    platform::AppVersion,
  8    AsyncAppContext, Element, Entity, ModelContext, ModelHandle, MutableAppContext, Task, View,
  9    ViewContext,
 10};
 11use lazy_static::lazy_static;
 12use serde::Deserialize;
 13use settings::Settings;
 14use smol::{fs::File, io::AsyncReadExt, process::Command};
 15use std::{env, ffi::OsString, path::PathBuf, sync::Arc, time::Duration};
 16use workspace::{ItemHandle, StatusItemView};
 17
 18const POLL_INTERVAL: Duration = Duration::from_secs(60 * 60);
 19const ACCESS_TOKEN: &'static str = "618033988749894";
 20
 21lazy_static! {
 22    pub static ref ZED_APP_VERSION: Option<AppVersion> = env::var("ZED_APP_VERSION")
 23        .ok()
 24        .and_then(|v| v.parse().ok());
 25    pub static ref ZED_APP_PATH: Option<PathBuf> = env::var("ZED_APP_PATH").ok().map(PathBuf::from);
 26}
 27
 28actions!(auto_update, [Check, DismissErrorMessage]);
 29
 30#[derive(Clone, PartialEq, Eq)]
 31pub enum AutoUpdateStatus {
 32    Idle,
 33    Checking,
 34    Downloading,
 35    Installing,
 36    Updated,
 37    Errored,
 38}
 39
 40pub struct AutoUpdater {
 41    status: AutoUpdateStatus,
 42    current_version: AppVersion,
 43    http_client: Arc<dyn HttpClient>,
 44    pending_poll: Option<Task<()>>,
 45    server_url: String,
 46}
 47
 48pub struct AutoUpdateIndicator {
 49    updater: Option<ModelHandle<AutoUpdater>>,
 50}
 51
 52#[derive(Deserialize)]
 53struct JsonRelease {
 54    version: String,
 55    url: String,
 56}
 57
 58impl Entity for AutoUpdater {
 59    type Event = ();
 60}
 61
 62pub fn init(http_client: Arc<dyn HttpClient>, server_url: String, cx: &mut MutableAppContext) {
 63    if let Some(version) = ZED_APP_VERSION.clone().or(cx.platform().app_version().ok()) {
 64        let auto_updater = cx.add_model(|cx| {
 65            let updater = AutoUpdater::new(version, http_client, server_url);
 66            updater.start_polling(cx).detach();
 67            updater
 68        });
 69        cx.set_global(Some(auto_updater));
 70        cx.add_global_action(|_: &Check, cx| {
 71            if let Some(updater) = AutoUpdater::get(cx) {
 72                updater.update(cx, |updater, cx| updater.poll(cx));
 73            }
 74        });
 75        cx.add_action(AutoUpdateIndicator::dismiss_error_message);
 76    }
 77}
 78
 79impl AutoUpdater {
 80    fn get(cx: &mut MutableAppContext) -> Option<ModelHandle<Self>> {
 81        cx.default_global::<Option<ModelHandle<Self>>>().clone()
 82    }
 83
 84    fn new(
 85        current_version: AppVersion,
 86        http_client: Arc<dyn HttpClient>,
 87        server_url: String,
 88    ) -> Self {
 89        Self {
 90            status: AutoUpdateStatus::Idle,
 91            current_version,
 92            http_client,
 93            server_url,
 94            pending_poll: None,
 95        }
 96    }
 97
 98    pub fn start_polling(&self, cx: &mut ModelContext<Self>) -> Task<()> {
 99        cx.spawn(|this, mut cx| async move {
100            loop {
101                this.update(&mut cx, |this, cx| this.poll(cx));
102                cx.background().timer(POLL_INTERVAL).await;
103            }
104        })
105    }
106
107    pub fn poll(&mut self, cx: &mut ModelContext<Self>) {
108        if self.pending_poll.is_some() || self.status == AutoUpdateStatus::Updated {
109            return;
110        }
111
112        self.status = AutoUpdateStatus::Checking;
113        cx.notify();
114
115        self.pending_poll = Some(cx.spawn(|this, mut cx| async move {
116            let result = Self::update(this.clone(), cx.clone()).await;
117            this.update(&mut cx, |this, cx| {
118                this.pending_poll = None;
119                if let Err(error) = result {
120                    log::error!("auto-update failed: error:{:?}", error);
121                    this.status = AutoUpdateStatus::Errored;
122                    cx.notify();
123                }
124            });
125        }));
126    }
127
128    async fn update(this: ModelHandle<Self>, mut cx: AsyncAppContext) -> Result<()> {
129        let (client, server_url, current_version) = this.read_with(&cx, |this, _| {
130            (
131                this.http_client.clone(),
132                this.server_url.clone(),
133                this.current_version,
134            )
135        });
136        let mut response = client
137            .get(
138                &format!("{server_url}/api/releases/latest?token={ACCESS_TOKEN}&asset=Zed.dmg"),
139                Default::default(),
140                true,
141            )
142            .await?;
143
144        let mut body = Vec::new();
145        response
146            .body_mut()
147            .read_to_end(&mut body)
148            .await
149            .context("error reading release")?;
150        let release: JsonRelease =
151            serde_json::from_slice(body.as_slice()).context("error deserializing release")?;
152
153        let latest_version = release.version.parse::<AppVersion>()?;
154        if latest_version <= current_version {
155            this.update(&mut cx, |this, cx| {
156                this.status = AutoUpdateStatus::Idle;
157                cx.notify();
158            });
159            return Ok(());
160        }
161
162        this.update(&mut cx, |this, cx| {
163            this.status = AutoUpdateStatus::Downloading;
164            cx.notify();
165        });
166
167        let temp_dir = tempdir::TempDir::new("zed-auto-update")?;
168        let dmg_path = temp_dir.path().join("Zed.dmg");
169        let mount_path = temp_dir.path().join("Zed");
170        let mut mounted_app_path: OsString = mount_path.join("Zed.app").into();
171        mounted_app_path.push("/");
172        let running_app_path = ZED_APP_PATH
173            .clone()
174            .map_or_else(|| cx.platform().app_path(), Ok)?;
175
176        let mut dmg_file = File::create(&dmg_path).await?;
177        let mut response = client.get(&release.url, Default::default(), true).await?;
178        smol::io::copy(response.body_mut(), &mut dmg_file).await?;
179        log::info!("downloaded update. path:{:?}", dmg_path);
180
181        this.update(&mut cx, |this, cx| {
182            this.status = AutoUpdateStatus::Installing;
183            cx.notify();
184        });
185
186        let output = Command::new("hdiutil")
187            .args(&["attach", "-nobrowse"])
188            .arg(&dmg_path)
189            .arg("-mountroot")
190            .arg(&temp_dir.path())
191            .output()
192            .await?;
193        if !output.status.success() {
194            Err(anyhow!(
195                "failed to mount: {:?}",
196                String::from_utf8_lossy(&output.stderr)
197            ))?;
198        }
199
200        let output = Command::new("rsync")
201            .args(&["-av", "--delete"])
202            .arg(&mounted_app_path)
203            .arg(&running_app_path)
204            .output()
205            .await?;
206        if !output.status.success() {
207            Err(anyhow!(
208                "failed to copy app: {:?}",
209                String::from_utf8_lossy(&output.stderr)
210            ))?;
211        }
212
213        let output = Command::new("hdiutil")
214            .args(&["detach"])
215            .arg(&mount_path)
216            .output()
217            .await?;
218        if !output.status.success() {
219            Err(anyhow!(
220                "failed to unmount: {:?}",
221                String::from_utf8_lossy(&output.stderr)
222            ))?;
223        }
224
225        this.update(&mut cx, |this, cx| {
226            this.status = AutoUpdateStatus::Updated;
227            cx.notify();
228        });
229        Ok(())
230    }
231}
232
233impl Entity for AutoUpdateIndicator {
234    type Event = ();
235}
236
237impl View for AutoUpdateIndicator {
238    fn ui_name() -> &'static str {
239        "AutoUpdateIndicator"
240    }
241
242    fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
243        if let Some(updater) = &self.updater {
244            let theme = &cx.global::<Settings>().theme.workspace.status_bar;
245            match &updater.read(cx).status {
246                AutoUpdateStatus::Checking => Text::new(
247                    "Checking for updates…".to_string(),
248                    theme.auto_update_progress_message.clone(),
249                )
250                .boxed(),
251                AutoUpdateStatus::Downloading => Text::new(
252                    "Downloading update…".to_string(),
253                    theme.auto_update_progress_message.clone(),
254                )
255                .boxed(),
256                AutoUpdateStatus::Installing => Text::new(
257                    "Installing update…".to_string(),
258                    theme.auto_update_progress_message.clone(),
259                )
260                .boxed(),
261                AutoUpdateStatus::Updated => Text::new(
262                    "Restart to update Zed".to_string(),
263                    theme.auto_update_done_message.clone(),
264                )
265                .boxed(),
266                AutoUpdateStatus::Errored => {
267                    MouseEventHandler::new::<Self, _, _>(0, cx, |_, cx| {
268                        let theme = &cx.global::<Settings>().theme.workspace.status_bar;
269                        Text::new(
270                            "Auto update failed".to_string(),
271                            theme.auto_update_done_message.clone(),
272                        )
273                        .boxed()
274                    })
275                    .on_click(|cx| cx.dispatch_action(DismissErrorMessage))
276                    .boxed()
277                }
278                AutoUpdateStatus::Idle => Empty::new().boxed(),
279            }
280        } else {
281            Empty::new().boxed()
282        }
283    }
284}
285
286impl StatusItemView for AutoUpdateIndicator {
287    fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
288}
289
290impl AutoUpdateIndicator {
291    pub fn new(cx: &mut ViewContext<Self>) -> Self {
292        let updater = AutoUpdater::get(cx);
293        if let Some(updater) = &updater {
294            cx.observe(updater, |_, _, cx| cx.notify()).detach();
295        }
296        Self { updater }
297    }
298
299    fn dismiss_error_message(&mut self, _: &DismissErrorMessage, cx: &mut ViewContext<Self>) {
300        if let Some(updater) = &self.updater {
301            updater.update(cx, |updater, cx| {
302                updater.status = AutoUpdateStatus::Idle;
303                cx.notify();
304            });
305        }
306    }
307}