mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-04 10:12:47 +00:00
Use osascript to escalate privileges and copy the CLI to /usr/local/bin
This commit is contained in:
parent
0d9a0e2cbe
commit
b3f2b7a92c
1 changed files with 56 additions and 22 deletions
|
@ -4,7 +4,7 @@ pub mod settings_file;
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub mod test;
|
pub mod test;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::{anyhow, Context, Result};
|
||||||
use breadcrumbs::Breadcrumbs;
|
use breadcrumbs::Breadcrumbs;
|
||||||
use chat_panel::ChatPanel;
|
use chat_panel::ChatPanel;
|
||||||
pub use client;
|
pub use client;
|
||||||
|
@ -16,7 +16,7 @@ use gpui::{
|
||||||
actions,
|
actions,
|
||||||
geometry::vector::vec2f,
|
geometry::vector::vec2f,
|
||||||
platform::{WindowBounds, WindowOptions},
|
platform::{WindowBounds, WindowOptions},
|
||||||
ModelHandle, ViewContext,
|
AsyncAppContext, ModelHandle, ViewContext,
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
pub use lsp;
|
pub use lsp;
|
||||||
|
@ -26,7 +26,10 @@ use project_panel::ProjectPanel;
|
||||||
use search::{BufferSearchBar, ProjectSearchBar};
|
use search::{BufferSearchBar, ProjectSearchBar};
|
||||||
use serde_json::to_string_pretty;
|
use serde_json::to_string_pretty;
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
pub use workspace;
|
pub use workspace;
|
||||||
use workspace::{AppState, Workspace, WorkspaceParams};
|
use workspace::{AppState, Workspace, WorkspaceParams};
|
||||||
|
@ -69,25 +72,8 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
cx.add_global_action(move |_: &InstallCommandLineTool, cx| {
|
cx.add_global_action(move |_: &InstallCommandLineTool, cx| {
|
||||||
cx.spawn(|cx| async move {
|
cx.spawn(|cx| async move { install_cli(&cx).await.context("error creating CLI symlink") })
|
||||||
log::info!("installing command line launcher");
|
.detach_and_log_err(cx);
|
||||||
let cli_path = cx
|
|
||||||
.platform()
|
|
||||||
.path_for_auxiliary_executable("cli")
|
|
||||||
.log_err()?;
|
|
||||||
let link_path = "/opt/homebrew/bin/zed";
|
|
||||||
smol::fs::unix::symlink(cli_path.as_path(), link_path)
|
|
||||||
.await
|
|
||||||
.context("failed to install cli symlink")
|
|
||||||
.log_err()?;
|
|
||||||
log::info!(
|
|
||||||
"created symlink {} -> {}",
|
|
||||||
link_path,
|
|
||||||
cli_path.to_string_lossy()
|
|
||||||
);
|
|
||||||
Some(())
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
});
|
});
|
||||||
cx.add_action({
|
cx.add_action({
|
||||||
let app_state = app_state.clone();
|
let app_state = app_state.clone();
|
||||||
|
@ -253,6 +239,54 @@ fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {
|
||||||
cx.platform().quit();
|
cx.platform().quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn install_cli(cx: &AsyncAppContext) -> Result<()> {
|
||||||
|
let cli_path = cx.platform().path_for_auxiliary_executable("cli")?;
|
||||||
|
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("osascript")
|
||||||
|
.args([
|
||||||
|
"-e",
|
||||||
|
&dbg!(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"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue