install_cli.rs

 1use anyhow::{anyhow, Result};
 2use gpui::{actions, AsyncAppContext};
 3use std::path::Path;
 4use util::ResultExt;
 5
 6actions!(cli, [Install]);
 7
 8pub async fn install_cli(cx: &AsyncAppContext) -> Result<()> {
 9    let cli_path = cx.update(|cx| cx.path_for_auxiliary_executable("cli"))??;
10    let link_path = Path::new("/usr/local/bin/zed");
11    let bin_dir_path = link_path.parent().unwrap();
12
13    // Don't re-create symlink if it points to the same CLI binary.
14    if smol::fs::read_link(link_path).await.ok().as_ref() == Some(&cli_path) {
15        return Ok(());
16    }
17
18    // If the symlink is not there or is outdated, first try replacing it
19    // without escalating.
20    smol::fs::remove_file(link_path).await.log_err();
21    if smol::fs::unix::symlink(&cli_path, link_path)
22        .await
23        .log_err()
24        .is_some()
25    {
26        return Ok(());
27    }
28
29    // The symlink could not be created, so use osascript with admin privileges
30    // to create it.
31    let status = smol::process::Command::new("/usr/bin/osascript")
32        .args([
33            "-e",
34            &format!(
35                "do shell script \" \
36                    mkdir -p \'{}\' && \
37                    ln -sf \'{}\' \'{}\' \
38                \" with administrator privileges",
39                bin_dir_path.to_string_lossy(),
40                cli_path.to_string_lossy(),
41                link_path.to_string_lossy(),
42            ),
43        ])
44        .stdout(smol::process::Stdio::inherit())
45        .stderr(smol::process::Stdio::inherit())
46        .output()
47        .await?
48        .status;
49    if status.success() {
50        Ok(())
51    } else {
52        Err(anyhow!("error running osascript"))
53    }
54}