1use anyhow::{Context as _, Result};
2use client::ZED_URL_SCHEME;
3use gpui::{AppContext as _, AsyncApp, Context, PromptLevel, Window, actions};
4use release_channel::ReleaseChannel;
5use std::ops::Deref;
6use std::path::{Path, PathBuf};
7use util::ResultExt;
8use workspace::notifications::{DetachAndPromptErr, NotificationId};
9use workspace::{Toast, Workspace};
10
11actions!(cli, [Install, RegisterZedScheme]);
12
13async fn install_script(cx: &AsyncApp) -> Result<PathBuf> {
14 let cli_path = cx.update(|cx| cx.path_for_auxiliary_executable("cli"))??;
15 let link_path = Path::new("/usr/local/bin/zed");
16 let bin_dir_path = link_path.parent().unwrap();
17
18 // Don't re-create symlink if it points to the same CLI binary.
19 if smol::fs::read_link(link_path).await.ok().as_ref() == Some(&cli_path) {
20 return Ok(link_path.into());
21 }
22
23 // If the symlink is not there or is outdated, first try replacing it
24 // without escalating.
25 smol::fs::remove_file(link_path).await.log_err();
26 // todo("windows")
27 #[cfg(not(windows))]
28 {
29 if smol::fs::unix::symlink(&cli_path, link_path)
30 .await
31 .log_err()
32 .is_some()
33 {
34 return Ok(link_path.into());
35 }
36 }
37
38 // The symlink could not be created, so use osascript with admin privileges
39 // to create it.
40 let status = smol::process::Command::new("/usr/bin/osascript")
41 .args([
42 "-e",
43 &format!(
44 "do shell script \" \
45 mkdir -p \'{}\' && \
46 ln -sf \'{}\' \'{}\' \
47 \" with administrator privileges",
48 bin_dir_path.to_string_lossy(),
49 cli_path.to_string_lossy(),
50 link_path.to_string_lossy(),
51 ),
52 ])
53 .stdout(smol::process::Stdio::inherit())
54 .stderr(smol::process::Stdio::inherit())
55 .output()
56 .await?
57 .status;
58 anyhow::ensure!(status.success(), "error running osascript");
59 Ok(link_path.into())
60}
61
62pub async fn register_zed_scheme(cx: &AsyncApp) -> anyhow::Result<()> {
63 cx.update(|cx| cx.register_url_scheme(ZED_URL_SCHEME))?
64 .await
65}
66
67pub fn install_cli(window: &mut Window, cx: &mut Context<Workspace>) {
68 const LINUX_PROMPT_DETAIL: &str = "If you installed Zed from our official release add ~/.local/bin to your PATH.\n\nIf you installed Zed from a different source like your package manager, then you may need to create an alias/symlink manually.\n\nDepending on your package manager, the CLI might be named zeditor, zedit, zed-editor or something else.";
69
70 cx.spawn_in(window, async move |workspace, cx| {
71 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
72 let prompt = cx.prompt(
73 PromptLevel::Warning,
74 "CLI should already be installed",
75 Some(LINUX_PROMPT_DETAIL),
76 &["Ok"],
77 );
78 cx.background_spawn(prompt).detach();
79 return Ok(());
80 }
81 let path = install_script(cx.deref())
82 .await
83 .context("error creating CLI symlink")?;
84
85 workspace.update_in(cx, |workspace, _, cx| {
86 struct InstalledZedCli;
87
88 workspace.show_toast(
89 Toast::new(
90 NotificationId::unique::<InstalledZedCli>(),
91 format!(
92 "Installed `zed` to {}. You can launch {} from your terminal.",
93 path.to_string_lossy(),
94 ReleaseChannel::global(cx).display_name()
95 ),
96 ),
97 cx,
98 )
99 })?;
100 register_zed_scheme(&cx).await.log_err();
101 Ok(())
102 })
103 .detach_and_prompt_err("Error installing zed cli", window, cx, |_, _, _| None);
104}