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