2022-04-15 14:55:26 +00:00
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
|
use clap::Parser;
|
2022-04-15 23:33:56 +00:00
|
|
|
|
use cli::{CliRequest, CliResponse, IpcHandshake};
|
2022-04-15 14:55:26 +00:00
|
|
|
|
use core_foundation::{
|
|
|
|
|
array::{CFArray, CFIndex},
|
|
|
|
|
string::kCFStringEncodingUTF8,
|
|
|
|
|
url::{CFURLCreateWithBytes, CFURL},
|
|
|
|
|
};
|
|
|
|
|
use core_services::{kLSLaunchDefaults, LSLaunchURLSpec, LSOpenFromURLSpec, TCFType};
|
2022-04-15 23:33:56 +00:00
|
|
|
|
use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender};
|
2022-04-20 14:00:41 +00:00
|
|
|
|
use serde::Deserialize;
|
2022-04-20 14:34:22 +00:00
|
|
|
|
use std::{ffi::OsStr, fs, path::PathBuf, ptr};
|
2022-04-15 14:55:26 +00:00
|
|
|
|
|
|
|
|
|
#[derive(Parser)]
|
2022-04-20 14:00:41 +00:00
|
|
|
|
#[clap(name = "zed", global_setting(clap::AppSettings::NoAutoVersion))]
|
2022-04-15 14:55:26 +00:00
|
|
|
|
struct Args {
|
|
|
|
|
/// Wait for all of the given paths to be closed before exiting.
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
wait: bool,
|
|
|
|
|
/// A sequence of space-separated paths that you want to open.
|
|
|
|
|
#[clap()]
|
|
|
|
|
paths: Vec<PathBuf>,
|
2022-04-20 14:00:41 +00:00
|
|
|
|
/// Print Zed's version and the app path.
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
version: bool,
|
2022-04-20 14:34:22 +00:00
|
|
|
|
/// Custom Zed.app path
|
|
|
|
|
#[clap(short, long)]
|
|
|
|
|
bundle_path: Option<PathBuf>,
|
2022-04-20 14:00:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct InfoPlist {
|
|
|
|
|
#[serde(rename = "CFBundleShortVersionString")]
|
|
|
|
|
bundle_short_version_string: String,
|
2022-04-15 14:55:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
|
let args = Args::parse();
|
|
|
|
|
|
2022-04-20 14:34:22 +00:00
|
|
|
|
let bundle_path = if let Some(bundle_path) = args.bundle_path {
|
|
|
|
|
bundle_path.canonicalize()?
|
|
|
|
|
} else {
|
|
|
|
|
locate_bundle()?
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-20 14:00:41 +00:00
|
|
|
|
if args.version {
|
2022-04-20 14:34:22 +00:00
|
|
|
|
let plist_path = bundle_path.join("Contents/Info.plist");
|
2022-04-20 14:00:41 +00:00
|
|
|
|
let plist = plist::from_file::<_, InfoPlist>(plist_path)?;
|
|
|
|
|
println!(
|
|
|
|
|
"Zed {} – {}",
|
|
|
|
|
plist.bundle_short_version_string,
|
2022-04-20 14:34:22 +00:00
|
|
|
|
bundle_path.to_string_lossy()
|
2022-04-20 14:00:41 +00:00
|
|
|
|
);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 14:34:22 +00:00
|
|
|
|
let (tx, rx) = launch_app(bundle_path)?;
|
2022-04-15 14:55:26 +00:00
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
tx.send(CliRequest::Open {
|
|
|
|
|
paths: args
|
|
|
|
|
.paths
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|path| fs::canonicalize(path).map_err(|error| anyhow!(error)))
|
|
|
|
|
.collect::<Result<Vec<PathBuf>>>()?,
|
2022-04-20 10:54:34 +00:00
|
|
|
|
wait: args.wait,
|
2022-04-15 23:33:56 +00:00
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
while let Ok(response) = rx.recv() {
|
|
|
|
|
match response {
|
2022-04-20 10:54:34 +00:00
|
|
|
|
CliResponse::Ping => {}
|
2022-04-15 23:33:56 +00:00
|
|
|
|
CliResponse::Stdout { message } => println!("{message}"),
|
|
|
|
|
CliResponse::Stderr { message } => eprintln!("{message}"),
|
|
|
|
|
CliResponse::Exit { status } => std::process::exit(status),
|
|
|
|
|
}
|
2022-04-15 14:55:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
Ok(())
|
2022-04-15 14:55:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 14:34:22 +00:00
|
|
|
|
fn locate_bundle() -> Result<PathBuf> {
|
|
|
|
|
let cli_path = std::env::current_exe()?.canonicalize()?;
|
|
|
|
|
let mut app_path = cli_path.clone();
|
|
|
|
|
while app_path.extension() != Some(OsStr::new("app")) {
|
|
|
|
|
if !app_path.pop() {
|
|
|
|
|
return Err(anyhow!("cannot find app bundle containing {:?}", cli_path));
|
2022-04-19 20:42:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-20 14:34:22 +00:00
|
|
|
|
Ok(app_path)
|
2022-04-15 14:55:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
fn launch_app(app_path: PathBuf) -> Result<(IpcSender<CliRequest>, IpcReceiver<CliResponse>)> {
|
|
|
|
|
let (server, server_name) = IpcOneShotServer::<IpcHandshake>::new()?;
|
2022-04-19 20:42:33 +00:00
|
|
|
|
let url = format!("zed-cli://{server_name}");
|
2022-04-15 23:33:56 +00:00
|
|
|
|
|
2022-04-15 14:55:26 +00:00
|
|
|
|
let status = unsafe {
|
|
|
|
|
let app_url =
|
|
|
|
|
CFURL::from_path(&app_path, true).ok_or_else(|| anyhow!("invalid app path"))?;
|
2022-04-15 23:33:56 +00:00
|
|
|
|
let url_to_open = CFURL::wrap_under_create_rule(CFURLCreateWithBytes(
|
2022-04-15 14:55:26 +00:00
|
|
|
|
ptr::null(),
|
2022-04-15 23:33:56 +00:00
|
|
|
|
url.as_ptr(),
|
|
|
|
|
url.len() as CFIndex,
|
2022-04-15 14:55:26 +00:00
|
|
|
|
kCFStringEncodingUTF8,
|
|
|
|
|
ptr::null(),
|
2022-04-15 23:33:56 +00:00
|
|
|
|
));
|
|
|
|
|
let urls_to_open = CFArray::from_copyable(&[url_to_open.as_concrete_TypeRef()]);
|
2022-04-15 14:55:26 +00:00
|
|
|
|
LSOpenFromURLSpec(
|
|
|
|
|
&LSLaunchURLSpec {
|
|
|
|
|
appURL: app_url.as_concrete_TypeRef(),
|
|
|
|
|
itemURLs: urls_to_open.as_concrete_TypeRef(),
|
|
|
|
|
passThruParams: ptr::null(),
|
|
|
|
|
launchFlags: kLSLaunchDefaults,
|
|
|
|
|
asyncRefCon: ptr::null_mut(),
|
|
|
|
|
},
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
)
|
|
|
|
|
};
|
2022-04-15 23:33:56 +00:00
|
|
|
|
|
2022-04-15 14:55:26 +00:00
|
|
|
|
if status == 0 {
|
2022-04-15 23:33:56 +00:00
|
|
|
|
let (_, handshake) = server.accept()?;
|
|
|
|
|
Ok((handshake.requests, handshake.responses))
|
2022-04-15 14:55:26 +00:00
|
|
|
|
} else {
|
|
|
|
|
Err(anyhow!("cannot start {:?}", app_path))
|
|
|
|
|
}
|
|
|
|
|
}
|