Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
cli: Treat first argument as name of release channel to use for the cli (#10856)
Piotr Osiewicza111b959d282
diff --git a/Cargo.lock b/Cargo.lockindex 3b243f7d590..e8d019f3ac3 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2047,6 +2047,7 @@ dependencies = ["core-services","ipc-channel","plist",+ "release_channel","serde","util",]diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.tomlindex d118d9d8737..5b982e9bfbc 100644--- a/crates/cli/Cargo.toml+++ b/crates/cli/Cargo.toml@@ -20,6 +20,7 @@ path = "src/main.rs"anyhow.workspace = trueclap.workspace = trueipc-channel = "0.18"+release_channel.workspace = trueserde.workspace = trueutil.workspace = truediff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rsindex 9ae0ed0465b..80c892583f1 100644--- a/crates/cli/src/main.rs+++ b/crates/cli/src/main.rs@@ -7,7 +7,7 @@ use serde::Deserialize;use std::{env,ffi::OsStr,- fs::{self},+ fs,path::{Path, PathBuf},};use util::paths::PathLikeWithPosition;@@ -53,6 +53,16 @@ struct InfoPlist {}fn main() -> Result<()> {+ // Intercept version designators+ #[cfg(target_os = "macos")]+ if let Some(channel) = std::env::args().nth(1) {+ // When the first argument is a name of a release channel, we're gonna spawn off a cli of that version, with trailing args passed along.+ use std::str::FromStr as _;++ if let Ok(channel) = release_channel::ReleaseChannel::from_str(&channel) {+ return mac_os::spawn_channel_cli(channel, std::env::args().skip(2).collect());+ }+ }let args = Args::parse();let bundle = Bundle::detect(args.bundle_path.as_deref()).context("Bundle detection")?;@@ -200,7 +210,7 @@ mod windows {#[cfg(target_os = "macos")]mod mac_os {- use anyhow::Context;+ use anyhow::{Context, Result};use core_foundation::{array::{CFArray, CFIndex},string::kCFStringEncodingUTF8,@@ -348,4 +358,33 @@ mod mac_os {)}}+ pub(super) fn spawn_channel_cli(+ channel: release_channel::ReleaseChannel,+ leftover_args: Vec<String>,+ ) -> Result<()> {+ use anyhow::bail;+ use std::process::Command;++ let app_id_prompt = format!("id of app \"{}\"", channel.display_name());+ let app_id_output = Command::new("osascript")+ .arg("-e")+ .arg(&app_id_prompt)+ .output()?;+ if !app_id_output.status.success() {+ bail!("Could not determine app id for {}", channel.display_name());+ }+ let app_name = String::from_utf8(app_id_output.stdout)?.trim().to_owned();+ let app_path_prompt = format!("kMDItemCFBundleIdentifier == '{app_name}'");+ let app_path_output = Command::new("mdfind").arg(app_path_prompt).output()?;+ if !app_path_output.status.success() {+ bail!(+ "Could not determine app path for {}",+ channel.display_name()+ );+ }+ let app_path = String::from_utf8(app_path_output.stdout)?.trim().to_owned();+ let cli_path = format!("{app_path}/Contents/MacOS/cli");+ Command::new(cli_path).args(leftover_args).spawn()?;+ Ok(())+ }}diff --git a/crates/release_channel/src/lib.rs b/crates/release_channel/src/lib.rsindex 864df387c00..5418d4f22e7 100644--- a/crates/release_channel/src/lib.rs+++ b/crates/release_channel/src/lib.rs@@ -2,7 +2,7 @@#![deny(missing_docs)]-use std::env;+use std::{env, str::FromStr};use gpui::{AppContext, Global, SemanticVersion};use once_cell::sync::Lazy;@@ -18,11 +18,8 @@ static RELEASE_CHANNEL_NAME: Lazy<String> = if cfg!(debug_assertions) {#[doc(hidden)]pub static RELEASE_CHANNEL: Lazy<ReleaseChannel> =- Lazy::new(|| match RELEASE_CHANNEL_NAME.as_str() {- "dev" => ReleaseChannel::Dev,- "nightly" => ReleaseChannel::Nightly,- "preview" => ReleaseChannel::Preview,- "stable" => ReleaseChannel::Stable,+ Lazy::new(|| match ReleaseChannel::from_str(&RELEASE_CHANNEL_NAME) {+ Ok(channel) => channel,_ => panic!("invalid release channel {}", *RELEASE_CHANNEL_NAME),});@@ -149,3 +146,21 @@ impl ReleaseChannel {}}}++/// Error indicating that release channel string does not match any known release channel names.+#[derive(Copy, Clone, Debug, Hash, PartialEq)]+pub struct InvalidReleaseChannel;++impl FromStr for ReleaseChannel {+ type Err = InvalidReleaseChannel;++ fn from_str(channel: &str) -> Result<Self, Self::Err> {+ Ok(match channel {+ "dev" => ReleaseChannel::Dev,+ "nightly" => ReleaseChannel::Nightly,+ "preview" => ReleaseChannel::Preview,+ "stable" => ReleaseChannel::Stable,+ _ => return Err(InvalidReleaseChannel),+ })+ }+}