1use anyhow::{anyhow, Result};
2use client::http::{self, HttpClient};
3use gpui::{
4 action,
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 smol::{fs::File, io::AsyncReadExt, process::Command};
13use std::{ffi::OsString, path::PathBuf, sync::Arc, time::Duration};
14use surf::Request;
15use workspace::{ItemHandle, Settings, StatusItemView};
16
17const POLL_INTERVAL: Duration = Duration::from_secs(60 * 60);
18const ACCESS_TOKEN: &'static str = "618033988749894";
19
20lazy_static! {
21 pub static ref ZED_APP_VERSION: Option<AppVersion> = std::env::var("ZED_APP_VERSION")
22 .ok()
23 .and_then(|v| v.parse().ok());
24 pub static ref ZED_APP_PATH: Option<PathBuf> =
25 std::env::var("ZED_APP_PATH").ok().map(PathBuf::from);
26}
27
28#[derive(Clone, PartialEq, Eq)]
29pub enum AutoUpdateStatus {
30 Idle,
31 Checking,
32 Downloading,
33 Updated,
34 Errored,
35}
36
37pub struct AutoUpdater {
38 status: AutoUpdateStatus,
39 current_version: AppVersion,
40 http_client: Arc<dyn HttpClient>,
41 pending_poll: Option<Task<()>>,
42 server_url: String,
43}
44
45pub struct AutoUpdateIndicator {
46 updater: Option<ModelHandle<AutoUpdater>>,
47}
48
49action!(DismissErrorMessage);
50
51#[derive(Deserialize)]
52struct JsonRelease {
53 version: String,
54 url: http::Url,
55}
56
57impl Entity for AutoUpdater {
58 type Event = ();
59}
60
61pub fn init(http_client: Arc<dyn HttpClient>, server_url: String, cx: &mut MutableAppContext) {
62 if let Some(version) = ZED_APP_VERSION.clone().or(cx.platform().app_version().ok()) {
63 let auto_updater = cx.add_model(|cx| {
64 let updater = AutoUpdater::new(version, http_client, server_url);
65 updater.start_polling(cx).detach();
66 updater
67 });
68 cx.set_global(Some(auto_updater));
69 cx.add_action(AutoUpdateIndicator::dismiss_error_message);
70 }
71}
72
73pub fn check(cx: &mut MutableAppContext) {
74 if let Some(updater) = AutoUpdater::get(cx) {
75 updater.update(cx, |updater, cx| updater.poll(cx));
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() {
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 .send(Request::new(
138 http::Method::Get,
139 http::Url::parse(&format!(
140 "{server_url}/api/releases/latest?token={ACCESS_TOKEN}&asset=Zed.dmg"
141 ))?,
142 ))
143 .await?;
144 let release = response
145 .body_json::<JsonRelease>()
146 .await
147 .map_err(|err| anyhow!("error deserializing release {:?}", err))?;
148 let latest_version = release.version.parse::<AppVersion>()?;
149 if latest_version <= current_version {
150 this.update(&mut cx, |this, cx| {
151 this.status = AutoUpdateStatus::Idle;
152 cx.notify();
153 });
154 return Ok(());
155 }
156
157 this.update(&mut cx, |this, cx| {
158 this.status = AutoUpdateStatus::Downloading;
159 cx.notify();
160 });
161
162 let temp_dir = tempdir::TempDir::new("zed-auto-update")?;
163 let dmg_path = temp_dir.path().join("Zed.dmg");
164 let mount_path = temp_dir.path().join("Zed");
165 let mut mounted_app_path: OsString = mount_path.join("Zed.app").into();
166 mounted_app_path.push("/");
167 let running_app_path = ZED_APP_PATH
168 .clone()
169 .map_or_else(|| cx.platform().app_path(), Ok)?;
170
171 let mut dmg_file = File::create(&dmg_path).await?;
172 let response = client
173 .send(Request::new(http::Method::Get, release.url))
174 .await?;
175 smol::io::copy(response.bytes(), &mut dmg_file).await?;
176 log::info!("downloaded update. path:{:?}", dmg_path);
177
178 let output = Command::new("hdiutil")
179 .args(&["attach", "-nobrowse"])
180 .arg(&dmg_path)
181 .arg("-mountroot")
182 .arg(&temp_dir.path())
183 .output()
184 .await?;
185 if !output.status.success() {
186 Err(anyhow!(
187 "failed to mount: {:?}",
188 String::from_utf8_lossy(&output.stderr)
189 ))?;
190 }
191
192 let output = Command::new("rsync")
193 .args(&["-av", "--delete"])
194 .arg(&mounted_app_path)
195 .arg(&running_app_path)
196 .output()
197 .await?;
198 if !output.status.success() {
199 Err(anyhow!(
200 "failed to copy app: {:?}",
201 String::from_utf8_lossy(&output.stderr)
202 ))?;
203 }
204
205 let output = Command::new("hdiutil")
206 .args(&["detach"])
207 .arg(&mount_path)
208 .output()
209 .await?;
210 if !output.status.success() {
211 Err(anyhow!(
212 "failed to unmount: {:?}",
213 String::from_utf8_lossy(&output.stderr)
214 ))?;
215 }
216
217 this.update(&mut cx, |this, cx| {
218 this.status = AutoUpdateStatus::Idle;
219 cx.notify();
220 });
221 Ok(())
222 }
223}
224
225impl Entity for AutoUpdateIndicator {
226 type Event = ();
227}
228
229impl View for AutoUpdateIndicator {
230 fn ui_name() -> &'static str {
231 "AutoUpdateIndicator"
232 }
233
234 fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
235 if let Some(updater) = &self.updater {
236 let theme = &cx.global::<Settings>().theme.workspace.status_bar;
237 match &updater.read(cx).status {
238 AutoUpdateStatus::Checking => Text::new(
239 "Checking for updates…".to_string(),
240 theme.auto_update_progress_message.clone(),
241 )
242 .boxed(),
243 AutoUpdateStatus::Downloading => Text::new(
244 "Downloading update…".to_string(),
245 theme.auto_update_progress_message.clone(),
246 )
247 .boxed(),
248 AutoUpdateStatus::Updated => Text::new(
249 "Restart to update Zed".to_string(),
250 theme.auto_update_done_message.clone(),
251 )
252 .boxed(),
253 AutoUpdateStatus::Errored => {
254 MouseEventHandler::new::<Self, _, _>(0, cx, |_, cx| {
255 let theme = &cx.global::<Settings>().theme.workspace.status_bar;
256 Text::new(
257 "Auto update failed".to_string(),
258 theme.auto_update_done_message.clone(),
259 )
260 .boxed()
261 })
262 .on_click(|cx| cx.dispatch_action(DismissErrorMessage))
263 .boxed()
264 }
265 AutoUpdateStatus::Idle => Empty::new().boxed(),
266 }
267 } else {
268 Empty::new().boxed()
269 }
270 }
271}
272
273impl StatusItemView for AutoUpdateIndicator {
274 fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
275}
276
277impl AutoUpdateIndicator {
278 pub fn new(cx: &mut ViewContext<Self>) -> Self {
279 let updater = AutoUpdater::get(cx);
280 if let Some(updater) = &updater {
281 cx.observe(updater, |_, _, cx| cx.notify()).detach();
282 }
283 Self { updater }
284 }
285
286 fn dismiss_error_message(&mut self, _: &DismissErrorMessage, cx: &mut ViewContext<Self>) {
287 if let Some(updater) = &self.updater {
288 updater.update(cx, |updater, cx| {
289 updater.status = AutoUpdateStatus::Idle;
290 cx.notify();
291 });
292 }
293 }
294}