Use ipc_channel crate to communicate between cli and app
We still aren't handling CLI requests in the app, but this lays the foundation for bi-directional communication. Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
01eb2dce24
commit
75f0326e54
10 changed files with 134 additions and 52 deletions
|
@ -1,14 +1,14 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use clap::Parser;
|
||||
use cli::{CliRequest, CliResponse, IpcHandshake};
|
||||
use core_foundation::{
|
||||
array::{CFArray, CFIndex},
|
||||
string::kCFStringEncodingUTF8,
|
||||
url::{CFURLCreateWithBytes, CFURL},
|
||||
};
|
||||
use core_services::{kLSLaunchDefaults, LSLaunchURLSpec, LSOpenFromURLSpec, TCFType};
|
||||
use ipc_channel::ipc::IpcOneShotServer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{path::PathBuf, process, ptr};
|
||||
use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender};
|
||||
use std::{fs, path::PathBuf, ptr};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(name = "zed")]
|
||||
|
@ -21,61 +21,55 @@ struct Args {
|
|||
paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct OpenResult {
|
||||
exit_status: i32,
|
||||
stdout_message: Option<String>,
|
||||
stderr_message: Option<String>,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let (server, server_name) = IpcOneShotServer::<OpenResult>::new()?;
|
||||
let app_path = locate_app()?;
|
||||
launch_app(app_path, args.paths, server_name)?;
|
||||
let (tx, rx) = launch_app(app_path)?;
|
||||
|
||||
let (_, result) = server.accept()?;
|
||||
if let Some(message) = result.stdout_message {
|
||||
println!("{}", message);
|
||||
}
|
||||
if let Some(message) = result.stderr_message {
|
||||
eprintln!("{}", message);
|
||||
tx.send(CliRequest::Open {
|
||||
paths: args
|
||||
.paths
|
||||
.into_iter()
|
||||
.map(|path| fs::canonicalize(path).map_err(|error| anyhow!(error)))
|
||||
.collect::<Result<Vec<PathBuf>>>()?,
|
||||
wait: false,
|
||||
})?;
|
||||
|
||||
while let Ok(response) = rx.recv() {
|
||||
match response {
|
||||
CliResponse::Stdout { message } => println!("{message}"),
|
||||
CliResponse::Stderr { message } => eprintln!("{message}"),
|
||||
CliResponse::Exit { status } => std::process::exit(status),
|
||||
}
|
||||
}
|
||||
|
||||
process::exit(result.exit_status)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn locate_app() -> Result<PathBuf> {
|
||||
Ok("/Applications/Zed.app".into())
|
||||
Ok("/Users/nathan/src/zed/target/debug/bundle/osx/Zed.app".into())
|
||||
// Ok("/Applications/Zed.app".into())
|
||||
}
|
||||
|
||||
fn launch_app(app_path: PathBuf, paths_to_open: Vec<PathBuf>, server_name: String) -> Result<()> {
|
||||
fn launch_app(app_path: PathBuf) -> Result<(IpcSender<CliRequest>, IpcReceiver<CliResponse>)> {
|
||||
let (server, server_name) = IpcOneShotServer::<IpcHandshake>::new()?;
|
||||
|
||||
let status = unsafe {
|
||||
let app_url =
|
||||
CFURL::from_path(&app_path, true).ok_or_else(|| anyhow!("invalid app path"))?;
|
||||
let mut urls_to_open = paths_to_open
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
CFURL::from_path(&path, true).ok_or_else(|| anyhow!("{:?} is invalid", path))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let server_url = format!("zed_cli_response://{server_name}");
|
||||
urls_to_open.push(CFURL::wrap_under_create_rule(CFURLCreateWithBytes(
|
||||
let url = format!("zed-cli://{server_name}");
|
||||
let url_to_open = CFURL::wrap_under_create_rule(CFURLCreateWithBytes(
|
||||
ptr::null(),
|
||||
server_url.as_ptr(),
|
||||
server_url.len() as CFIndex,
|
||||
url.as_ptr(),
|
||||
url.len() as CFIndex,
|
||||
kCFStringEncodingUTF8,
|
||||
ptr::null(),
|
||||
)));
|
||||
));
|
||||
|
||||
let urls_to_open = CFArray::from_copyable(&[url_to_open.as_concrete_TypeRef()]);
|
||||
|
||||
let urls_to_open = CFArray::from_copyable(
|
||||
&urls_to_open
|
||||
.iter()
|
||||
.map(|url| url.as_concrete_TypeRef())
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
LSOpenFromURLSpec(
|
||||
&LSLaunchURLSpec {
|
||||
appURL: app_url.as_concrete_TypeRef(),
|
||||
|
@ -87,8 +81,10 @@ fn launch_app(app_path: PathBuf, paths_to_open: Vec<PathBuf>, server_name: Strin
|
|||
ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
|
||||
if status == 0 {
|
||||
Ok(())
|
||||
let (_, handshake) = server.accept()?;
|
||||
Ok((handshake.requests, handshake.responses))
|
||||
} else {
|
||||
Err(anyhow!("cannot start {:?}", app_path))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue