Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add `install_cli2`
Antonio Scandurra06c22206af84
diff --git a/Cargo.lock b/Cargo.lockindex 7213f9a48a5..cbbcbc7914b 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3929,6 +3929,17 @@ dependencies = ["util",]+[[package]]+name = "install_cli2"+version = "0.1.0"+dependencies = [+ "anyhow",+ "gpui2",+ "log",+ "smol",+ "util",+]+[[package]]name = "instant"version = "0.1.12"diff --git a/Cargo.toml b/Cargo.tomlindex 6bab473f2c3..f8ce95ea6b7 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -46,6 +46,7 @@ members = ["crates/gpui2","crates/gpui2_macros","crates/install_cli",+ "crates/install_cli2","crates/journal","crates/language","crates/language2",diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rsindex 65b021413a0..75f202e7106 100644--- a/crates/gpui2/src/app.rs+++ b/crates/gpui2/src/app.rs@@ -30,6 +30,7 @@ use std::{marker::PhantomData,mem,ops::{Deref, DerefMut},+ path::PathBuf,sync::{atomic::Ordering::SeqCst, Arc, Weak},time::Duration,};@@ -722,6 +723,10 @@ wherepub fn open_url(&self, url: &str) {self.platform().open_url(url);}++ pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {+ self.platform().path_for_auxiliary_executable(name)+ }}impl MainThread<AppContext> {diff --git a/crates/install_cli2/Cargo.toml b/crates/install_cli2/Cargo.tomlnew file mode 100644index 00000000000..0dd1b907fdd--- /dev/null+++ b/crates/install_cli2/Cargo.toml@@ -0,0 +1,18 @@+[package]+name = "install_cli2"+version = "0.1.0"+edition = "2021"+publish = false++[lib]+path = "src/install_cli2.rs"++[features]+test-support = []++[dependencies]+smol.workspace = true+anyhow.workspace = true+log.workspace = true+gpui2 = { path = "../gpui2" }+util = { path = "../util" }diff --git a/crates/install_cli2/src/install_cli2.rs b/crates/install_cli2/src/install_cli2.rsnew file mode 100644index 00000000000..ecdf2a0f2ac--- /dev/null+++ b/crates/install_cli2/src/install_cli2.rs@@ -0,0 +1,57 @@+use anyhow::{anyhow, Result};+use gpui2::AsyncAppContext;+use std::path::Path;+use util::ResultExt;++// todo!()+// actions!(cli, [Install]);++pub async fn install_cli(cx: &AsyncAppContext) -> Result<()> {+ let cli_path = cx+ .run_on_main(|cx| cx.path_for_auxiliary_executable("cli"))?+ .await?;+ let link_path = Path::new("/usr/local/bin/zed");+ let bin_dir_path = link_path.parent().unwrap();++ // Don't re-create symlink if it points to the same CLI binary.+ if smol::fs::read_link(link_path).await.ok().as_ref() == Some(&cli_path) {+ return Ok(());+ }++ // If the symlink is not there or is outdated, first try replacing it+ // without escalating.+ smol::fs::remove_file(link_path).await.log_err();+ if smol::fs::unix::symlink(&cli_path, link_path)+ .await+ .log_err()+ .is_some()+ {+ return Ok(());+ }++ // The symlink could not be created, so use osascript with admin privileges+ // to create it.+ let status = smol::process::Command::new("/usr/bin/osascript")+ .args([+ "-e",+ &format!(+ "do shell script \" \+ mkdir -p \'{}\' && \+ ln -sf \'{}\' \'{}\' \+ \" with administrator privileges",+ bin_dir_path.to_string_lossy(),+ cli_path.to_string_lossy(),+ link_path.to_string_lossy(),+ ),+ ])+ .stdout(smol::process::Stdio::inherit())+ .stderr(smol::process::Stdio::inherit())+ .output()+ .await?+ .status;+ if status.success() {+ Ok(())+ } else {+ Err(anyhow!("error running osascript"))+ }+}