WIP: Start on a new cli crate
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
0c98168227
commit
01eb2dce24
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "cli"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
core-foundation = "0.9"
|
||||
core-services = "0.2"
|
||||
clap = { version = "3.1", features = ["derive"] }
|
||||
ipc-channel = "0.16"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
@@ -0,0 +1,95 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::Parser;
|
||||
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};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(name = "zed")]
|
||||
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>,
|
||||
}
|
||||
|
||||
#[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 (_, result) = server.accept()?;
|
||||
if let Some(message) = result.stdout_message {
|
||||
println!("{}", message);
|
||||
}
|
||||
if let Some(message) = result.stderr_message {
|
||||
eprintln!("{}", message);
|
||||
}
|
||||
|
||||
process::exit(result.exit_status)
|
||||
}
|
||||
|
||||
fn locate_app() -> Result<PathBuf> {
|
||||
Ok("/Applications/Zed.app".into())
|
||||
}
|
||||
|
||||
fn launch_app(app_path: PathBuf, paths_to_open: Vec<PathBuf>, server_name: String) -> Result<()> {
|
||||
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(
|
||||
ptr::null(),
|
||||
server_url.as_ptr(),
|
||||
server_url.len() as CFIndex,
|
||||
kCFStringEncodingUTF8,
|
||||
ptr::null(),
|
||||
)));
|
||||
|
||||
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(),
|
||||
itemURLs: urls_to_open.as_concrete_TypeRef(),
|
||||
passThruParams: ptr::null(),
|
||||
launchFlags: kLSLaunchDefaults,
|
||||
asyncRefCon: ptr::null_mut(),
|
||||
},
|
||||
ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if status == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("cannot start {:?}", app_path))
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ async-std = { version = "1.8.0", features = ["attributes"] }
|
||||
async-trait = "0.1.50"
|
||||
async-tungstenite = "0.16"
|
||||
base64 = "0.13"
|
||||
clap = "=3.0.0-beta.2"
|
||||
clap = "3.1"
|
||||
comrak = "0.10"
|
||||
either = "1.6"
|
||||
envy = "0.4.2"
|
||||
|
||||
@@ -94,6 +94,10 @@ unsafe fn build_classes() {
|
||||
sel!(application:openFiles:),
|
||||
open_files as extern "C" fn(&mut Object, Sel, id, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(application:openURLs:),
|
||||
open_urls as extern "C" fn(&mut Object, Sel, id, id),
|
||||
);
|
||||
decl.register()
|
||||
}
|
||||
}
|
||||
@@ -702,6 +706,30 @@ extern "C" fn open_files(this: &mut Object, _: Sel, _: id, paths: id) {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, paths: id) {
|
||||
let paths = unsafe {
|
||||
(0..paths.count())
|
||||
.into_iter()
|
||||
.filter_map(|i| {
|
||||
let path = paths.objectAtIndex(i);
|
||||
match dbg!(
|
||||
CStr::from_ptr(path.absoluteString().UTF8String() as *mut c_char).to_str()
|
||||
) {
|
||||
Ok(string) => Some(PathBuf::from(string)),
|
||||
Err(err) => {
|
||||
log::error!("error converting path to string: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
// let platform = unsafe { get_foreground_platform(this) };
|
||||
// if let Some(callback) = platform.0.borrow_mut().open_files.as_mut() {
|
||||
// callback(paths);
|
||||
// }
|
||||
}
|
||||
|
||||
extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
|
||||
unsafe {
|
||||
let platform = get_foreground_platform(this);
|
||||
|
||||
Reference in New Issue
Block a user