1mod update_notification;
2
3use anyhow::{anyhow, Context, Result};
4use client::{Client, TelemetrySettings, ZED_APP_PATH};
5use db::kvp::KEY_VALUE_STORE;
6use db::RELEASE_CHANNEL;
7use editor::{Editor, MultiBuffer};
8use gpui::{
9 actions, AppContext, AsyncAppContext, Context as _, Global, Model, ModelContext,
10 SemanticVersion, SharedString, Task, View, ViewContext, VisualContext, WindowContext,
11};
12use isahc::AsyncBody;
13
14use markdown_preview::markdown_preview_view::{MarkdownPreviewMode, MarkdownPreviewView};
15use schemars::JsonSchema;
16use serde::Deserialize;
17use serde_derive::Serialize;
18use smol::io::AsyncReadExt;
19
20use settings::{Settings, SettingsStore};
21use smol::{fs::File, process::Command};
22
23use release_channel::{AppCommitSha, AppVersion, ReleaseChannel};
24use std::{
25 env::consts::{ARCH, OS},
26 ffi::OsString,
27 sync::Arc,
28 time::Duration,
29};
30use update_notification::UpdateNotification;
31use util::{
32 http::{HttpClient, HttpClientWithUrl},
33 ResultExt,
34};
35use workspace::Workspace;
36
37const SHOULD_SHOW_UPDATE_NOTIFICATION_KEY: &str = "auto-updater-should-show-updated-notification";
38const POLL_INTERVAL: Duration = Duration::from_secs(60 * 60);
39
40actions!(
41 auto_update,
42 [
43 Check,
44 DismissErrorMessage,
45 ViewReleaseNotes,
46 ViewReleaseNotesLocally
47 ]
48);
49
50#[derive(Serialize)]
51struct UpdateRequestBody {
52 installation_id: Option<Arc<str>>,
53 release_channel: Option<&'static str>,
54 telemetry: bool,
55}
56
57#[derive(Clone, Copy, PartialEq, Eq)]
58pub enum AutoUpdateStatus {
59 Idle,
60 Checking,
61 Downloading,
62 Installing,
63 Updated,
64 Errored,
65}
66
67pub struct AutoUpdater {
68 status: AutoUpdateStatus,
69 current_version: SemanticVersion,
70 http_client: Arc<HttpClientWithUrl>,
71 pending_poll: Option<Task<Option<()>>>,
72}
73
74#[derive(Deserialize)]
75struct JsonRelease {
76 version: String,
77 url: String,
78}
79
80struct AutoUpdateSetting(bool);
81
82/// Whether or not to automatically check for updates.
83///
84/// Default: true
85#[derive(Clone, Default, JsonSchema, Deserialize, Serialize)]
86#[serde(transparent)]
87struct AutoUpdateSettingOverride(Option<bool>);
88
89impl Settings for AutoUpdateSetting {
90 const KEY: Option<&'static str> = Some("auto_update");
91
92 type FileContent = AutoUpdateSettingOverride;
93
94 fn load(
95 default_value: &Self::FileContent,
96 user_values: &[&Self::FileContent],
97 _: &mut AppContext,
98 ) -> Result<Self> {
99 Ok(Self(
100 Self::json_merge(default_value, user_values)?
101 .0
102 .ok_or_else(Self::missing_default)?,
103 ))
104 }
105}
106
107#[derive(Default)]
108struct GlobalAutoUpdate(Option<Model<AutoUpdater>>);
109
110impl Global for GlobalAutoUpdate {}
111
112#[derive(Deserialize)]
113struct ReleaseNotesBody {
114 title: String,
115 release_notes: String,
116}
117
118pub fn init(http_client: Arc<HttpClientWithUrl>, cx: &mut AppContext) {
119 AutoUpdateSetting::register(cx);
120
121 cx.observe_new_views(|workspace: &mut Workspace, _cx| {
122 workspace.register_action(|_, action: &Check, cx| check(action, cx));
123
124 workspace.register_action(|_, action, cx| {
125 view_release_notes(action, cx);
126 });
127
128 workspace.register_action(|workspace, _: &ViewReleaseNotesLocally, cx| {
129 view_release_notes_locally(workspace, cx);
130 });
131 })
132 .detach();
133
134 let version = release_channel::AppVersion::global(cx);
135 let auto_updater = cx.new_model(|cx| {
136 let updater = AutoUpdater::new(version, http_client);
137
138 let mut update_subscription = AutoUpdateSetting::get_global(cx)
139 .0
140 .then(|| updater.start_polling(cx));
141
142 cx.observe_global::<SettingsStore>(move |updater, cx| {
143 if AutoUpdateSetting::get_global(cx).0 {
144 if update_subscription.is_none() {
145 update_subscription = Some(updater.start_polling(cx))
146 }
147 } else {
148 update_subscription.take();
149 }
150 })
151 .detach();
152
153 updater
154 });
155 cx.set_global(GlobalAutoUpdate(Some(auto_updater)));
156}
157
158pub fn check(_: &Check, cx: &mut WindowContext) {
159 if let Some(updater) = AutoUpdater::get(cx) {
160 updater.update(cx, |updater, cx| updater.poll(cx));
161 } else {
162 drop(cx.prompt(
163 gpui::PromptLevel::Info,
164 "Could not check for updates",
165 Some("Auto-updates disabled for non-bundled app."),
166 &["Ok"],
167 ));
168 }
169}
170
171pub fn view_release_notes(_: &ViewReleaseNotes, cx: &mut AppContext) -> Option<()> {
172 let auto_updater = AutoUpdater::get(cx)?;
173 let release_channel = ReleaseChannel::try_global(cx)?;
174
175 if matches!(
176 release_channel,
177 ReleaseChannel::Stable | ReleaseChannel::Preview
178 ) {
179 let auto_updater = auto_updater.read(cx);
180 let release_channel = release_channel.dev_name();
181 let current_version = auto_updater.current_version;
182 let url = &auto_updater
183 .http_client
184 .build_url(&format!("/releases/{release_channel}/{current_version}"));
185 cx.open_url(&url);
186 }
187
188 None
189}
190
191fn view_release_notes_locally(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
192 let release_channel = ReleaseChannel::global(cx);
193 let version = AppVersion::global(cx).to_string();
194
195 let client = client::Client::global(cx).http_client();
196 let url = client.build_url(&format!(
197 "/api/release_notes/{}/{}",
198 release_channel.dev_name(),
199 version
200 ));
201
202 let markdown = workspace
203 .app_state()
204 .languages
205 .language_for_name("Markdown");
206
207 workspace
208 .with_local_workspace(cx, move |_, cx| {
209 cx.spawn(|workspace, mut cx| async move {
210 let markdown = markdown.await.log_err();
211 let response = client.get(&url, Default::default(), true).await;
212 let Some(mut response) = response.log_err() else {
213 return;
214 };
215
216 let mut body = Vec::new();
217 response.body_mut().read_to_end(&mut body).await.ok();
218
219 let body: serde_json::Result<ReleaseNotesBody> =
220 serde_json::from_slice(body.as_slice());
221
222 if let Ok(body) = body {
223 workspace
224 .update(&mut cx, |workspace, cx| {
225 let project = workspace.project().clone();
226 let buffer = project
227 .update(cx, |project, cx| project.create_buffer("", markdown, cx))
228 .expect("creating buffers on a local workspace always succeeds");
229 buffer.update(cx, |buffer, cx| {
230 buffer.edit([(0..0, body.release_notes)], None, cx)
231 });
232 let language_registry = project.read(cx).languages().clone();
233
234 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
235
236 let tab_description = SharedString::from(body.title.to_string());
237 let editor = cx
238 .new_view(|cx| Editor::for_multibuffer(buffer, Some(project), cx));
239 let workspace_handle = workspace.weak_handle();
240 let view: View<MarkdownPreviewView> = MarkdownPreviewView::new(
241 MarkdownPreviewMode::Default,
242 editor,
243 workspace_handle,
244 language_registry,
245 Some(tab_description),
246 cx,
247 );
248 workspace.add_item_to_active_pane(Box::new(view.clone()), cx);
249 cx.notify();
250 })
251 .log_err();
252 }
253 })
254 .detach();
255 })
256 .detach();
257}
258
259pub fn notify_of_any_new_update(cx: &mut ViewContext<Workspace>) -> Option<()> {
260 let updater = AutoUpdater::get(cx)?;
261 let version = updater.read(cx).current_version;
262 let should_show_notification = updater.read(cx).should_show_update_notification(cx);
263
264 cx.spawn(|workspace, mut cx| async move {
265 let should_show_notification = should_show_notification.await?;
266 if should_show_notification {
267 workspace.update(&mut cx, |workspace, cx| {
268 workspace.show_notification(0, cx, |cx| {
269 cx.new_view(|_| UpdateNotification::new(version))
270 });
271 updater
272 .read(cx)
273 .set_should_show_update_notification(false, cx)
274 .detach_and_log_err(cx);
275 })?;
276 }
277 anyhow::Ok(())
278 })
279 .detach();
280
281 None
282}
283
284impl AutoUpdater {
285 pub fn get(cx: &mut AppContext) -> Option<Model<Self>> {
286 cx.default_global::<GlobalAutoUpdate>().0.clone()
287 }
288
289 fn new(current_version: SemanticVersion, http_client: Arc<HttpClientWithUrl>) -> Self {
290 Self {
291 status: AutoUpdateStatus::Idle,
292 current_version,
293 http_client,
294 pending_poll: None,
295 }
296 }
297
298 pub fn start_polling(&self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
299 cx.spawn(|this, mut cx| async move {
300 loop {
301 this.update(&mut cx, |this, cx| this.poll(cx))?;
302 cx.background_executor().timer(POLL_INTERVAL).await;
303 }
304 })
305 }
306
307 pub fn poll(&mut self, cx: &mut ModelContext<Self>) {
308 if self.pending_poll.is_some() || self.status == AutoUpdateStatus::Updated {
309 return;
310 }
311
312 self.status = AutoUpdateStatus::Checking;
313 cx.notify();
314
315 self.pending_poll = Some(cx.spawn(|this, mut cx| async move {
316 let result = Self::update(this.upgrade()?, cx.clone()).await;
317 this.update(&mut cx, |this, cx| {
318 this.pending_poll = None;
319 if let Err(error) = result {
320 log::error!("auto-update failed: error:{:?}", error);
321 this.status = AutoUpdateStatus::Errored;
322 cx.notify();
323 }
324 })
325 .ok()
326 }));
327 }
328
329 pub fn status(&self) -> AutoUpdateStatus {
330 self.status
331 }
332
333 pub fn dismiss_error(&mut self, cx: &mut ModelContext<Self>) {
334 self.status = AutoUpdateStatus::Idle;
335 cx.notify();
336 }
337
338 async fn update(this: Model<Self>, mut cx: AsyncAppContext) -> Result<()> {
339 let (client, current_version) = this.read_with(&cx, |this, _| {
340 (this.http_client.clone(), this.current_version)
341 })?;
342
343 let mut url_string = client.build_url(&format!(
344 "/api/releases/latest?asset=Zed.dmg&os={}&arch={}",
345 OS, ARCH
346 ));
347 cx.update(|cx| {
348 if let Some(param) = ReleaseChannel::try_global(cx)
349 .and_then(|release_channel| release_channel.release_query_param())
350 {
351 url_string += "&";
352 url_string += param;
353 }
354 })?;
355
356 let mut response = client.get(&url_string, Default::default(), true).await?;
357
358 let mut body = Vec::new();
359 response
360 .body_mut()
361 .read_to_end(&mut body)
362 .await
363 .context("error reading release")?;
364 let release: JsonRelease =
365 serde_json::from_slice(body.as_slice()).context("error deserializing release")?;
366
367 let should_download = match *RELEASE_CHANNEL {
368 ReleaseChannel::Nightly => cx
369 .update(|cx| AppCommitSha::try_global(cx).map(|sha| release.version != sha.0))
370 .ok()
371 .flatten()
372 .unwrap_or(true),
373 _ => release.version.parse::<SemanticVersion>()? > current_version,
374 };
375
376 if !should_download {
377 this.update(&mut cx, |this, cx| {
378 this.status = AutoUpdateStatus::Idle;
379 cx.notify();
380 })?;
381 return Ok(());
382 }
383
384 this.update(&mut cx, |this, cx| {
385 this.status = AutoUpdateStatus::Downloading;
386 cx.notify();
387 })?;
388
389 let temp_dir = tempfile::Builder::new()
390 .prefix("zed-auto-update")
391 .tempdir()?;
392 let dmg_path = temp_dir.path().join("Zed.dmg");
393 let mount_path = temp_dir.path().join("Zed");
394 let running_app_path = ZED_APP_PATH
395 .clone()
396 .map_or_else(|| cx.update(|cx| cx.app_path())?, Ok)?;
397 let running_app_filename = running_app_path
398 .file_name()
399 .ok_or_else(|| anyhow!("invalid running app path"))?;
400 let mut mounted_app_path: OsString = mount_path.join(running_app_filename).into();
401 mounted_app_path.push("/");
402
403 let mut dmg_file = File::create(&dmg_path).await?;
404
405 let (installation_id, release_channel, telemetry) = cx.update(|cx| {
406 let installation_id = Client::global(cx).telemetry().installation_id();
407 let release_channel = ReleaseChannel::try_global(cx)
408 .map(|release_channel| release_channel.display_name());
409 let telemetry = TelemetrySettings::get_global(cx).metrics;
410
411 (installation_id, release_channel, telemetry)
412 })?;
413
414 let request_body = AsyncBody::from(serde_json::to_string(&UpdateRequestBody {
415 installation_id,
416 release_channel,
417 telemetry,
418 })?);
419
420 let mut response = client.get(&release.url, request_body, true).await?;
421 smol::io::copy(response.body_mut(), &mut dmg_file).await?;
422 log::info!("downloaded update. path:{:?}", dmg_path);
423
424 this.update(&mut cx, |this, cx| {
425 this.status = AutoUpdateStatus::Installing;
426 cx.notify();
427 })?;
428
429 let output = Command::new("hdiutil")
430 .args(&["attach", "-nobrowse"])
431 .arg(&dmg_path)
432 .arg("-mountroot")
433 .arg(&temp_dir.path())
434 .output()
435 .await?;
436 if !output.status.success() {
437 Err(anyhow!(
438 "failed to mount: {:?}",
439 String::from_utf8_lossy(&output.stderr)
440 ))?;
441 }
442
443 let output = Command::new("rsync")
444 .args(&["-av", "--delete"])
445 .arg(&mounted_app_path)
446 .arg(&running_app_path)
447 .output()
448 .await?;
449 if !output.status.success() {
450 Err(anyhow!(
451 "failed to copy app: {:?}",
452 String::from_utf8_lossy(&output.stderr)
453 ))?;
454 }
455
456 let output = Command::new("hdiutil")
457 .args(&["detach"])
458 .arg(&mount_path)
459 .output()
460 .await?;
461 if !output.status.success() {
462 Err(anyhow!(
463 "failed to unmount: {:?}",
464 String::from_utf8_lossy(&output.stderr)
465 ))?;
466 }
467
468 this.update(&mut cx, |this, cx| {
469 this.set_should_show_update_notification(true, cx)
470 .detach_and_log_err(cx);
471 this.status = AutoUpdateStatus::Updated;
472 cx.notify();
473 })?;
474 Ok(())
475 }
476
477 fn set_should_show_update_notification(
478 &self,
479 should_show: bool,
480 cx: &AppContext,
481 ) -> Task<Result<()>> {
482 cx.background_executor().spawn(async move {
483 if should_show {
484 KEY_VALUE_STORE
485 .write_kvp(
486 SHOULD_SHOW_UPDATE_NOTIFICATION_KEY.to_string(),
487 "".to_string(),
488 )
489 .await?;
490 } else {
491 KEY_VALUE_STORE
492 .delete_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY.to_string())
493 .await?;
494 }
495 Ok(())
496 })
497 }
498
499 fn should_show_update_notification(&self, cx: &AppContext) -> Task<Result<bool>> {
500 cx.background_executor().spawn(async move {
501 Ok(KEY_VALUE_STORE
502 .read_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)?
503 .is_some())
504 })
505 }
506}