Merge pull request #2196 from zed-industries/open_urls

Mikayla Maki created

Fix open URLs, restarts, and make bundling easier to use

Change summary

crates/gpui/src/platform/mac/platform.rs | 21 ++++++---
crates/zed/src/main.rs                   | 41 ++++++++++-------
script/bundle                            | 59 ++++++++++++++++++-------
3 files changed, 80 insertions(+), 41 deletions(-)

Detailed changes

crates/gpui/src/platform/mac/platform.rs 🔗

@@ -829,17 +829,24 @@ impl platform::Platform for MacPlatform {
 
     fn restart(&self) {
         #[cfg(debug_assertions)]
-        let path = std::env::current_exe();
+        let path = std::env::current_exe().unwrap();
 
         #[cfg(not(debug_assertions))]
-        let path = self.app_path().or_else(|_| std::env::current_exe());
+        let path = self
+            .app_path()
+            .unwrap_or_else(|_| std::env::current_exe().unwrap());
 
-        let command = path.and_then(|path| Command::new("/usr/bin/open").arg(path).spawn());
+        let script = r#"lsof -p "$0" +r 1 &>/dev/null && open "$1""#;
 
-        match command {
-            Err(err) => log::error!("Unable to restart application {}", err),
-            Ok(_child) => self.quit(),
-        }
+        Command::new("/bin/bash")
+            .arg("-c")
+            .arg(script)
+            .arg(std::process::id().to_string())
+            .arg(path)
+            .spawn()
+            .ok();
+
+        self.quit();
     }
 }
 

crates/zed/src/main.rs 🔗

@@ -78,7 +78,7 @@ fn main() {
     };
 
     let (cli_connections_tx, mut cli_connections_rx) = mpsc::unbounded();
-    let (open_paths_tx, open_paths_rx) = mpsc::unbounded();
+    let (open_paths_tx, mut open_paths_rx) = mpsc::unbounded();
     app.on_open_urls(move |urls, _| {
         if let Some(server_name) = urls.first().and_then(|url| url.strip_prefix("zed-cli://")) {
             if let Some(cli_connection) = connect_to_cli(server_name).log_err() {
@@ -189,9 +189,6 @@ fn main() {
 
         cx.set_menus(menus::menus());
 
-        cx.spawn(|cx| handle_open_paths(open_paths_rx, app_state.clone(), cx))
-            .detach();
-
         if stdout_is_a_pty() {
             cx.platform().activate(true);
             let paths = collect_path_args();
@@ -205,13 +202,32 @@ fn main() {
             if let Ok(Some(connection)) = cli_connections_rx.try_next() {
                 cx.spawn(|cx| handle_cli_connection(connection, app_state.clone(), cx))
                     .detach();
+            } else if let Ok(Some(paths)) = open_paths_rx.try_next() {
+                cx.update(|cx| workspace::open_paths(&paths, &app_state, cx))
+                    .detach();
             } else {
                 cx.spawn(|cx| async move { restore_or_create_workspace(cx).await })
                     .detach()
             }
-            cx.spawn(|cx| async move {
-                while let Some(connection) = cli_connections_rx.next().await {
-                    handle_cli_connection(connection, app_state.clone(), cx.clone()).await;
+
+            cx.spawn(|cx| {
+                let app_state = app_state.clone();
+                async move {
+                    while let Some(connection) = cli_connections_rx.next().await {
+                        handle_cli_connection(connection, app_state.clone(), cx.clone()).await;
+                    }
+                }
+            })
+            .detach();
+
+            cx.spawn(|mut cx| {
+                let app_state = app_state.clone();
+                async move {
+                    while let Some(paths) = open_paths_rx.next().await {
+                        log::error!("OPEN PATHS FROM HANDLE");
+                        cx.update(|cx| workspace::open_paths(&paths, &app_state, cx))
+                            .detach();
+                    }
                 }
             })
             .detach();
@@ -521,17 +537,6 @@ fn load_config_files(
     rx
 }
 
-async fn handle_open_paths(
-    mut rx: mpsc::UnboundedReceiver<Vec<PathBuf>>,
-    app_state: Arc<AppState>,
-    mut cx: AsyncAppContext,
-) {
-    while let Some(paths) = rx.next().await {
-        cx.update(|cx| workspace::open_paths(&paths, &app_state, cx))
-            .detach();
-    }
-}
-
 fn connect_to_cli(
     server_name: &str,
 ) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {

script/bundle 🔗

@@ -2,6 +2,24 @@
 
 set -e
 
+build_flag="--release"
+target_dir="release"
+open_result=false
+
+# If -o option is specified, the folder of the resulting dmg will be opened in finder
+# If -d is specified, Zed will be compiled in debug mode and the application's path printed
+# If -od or -do is specified Zed will be bundled in debug and the application will be run. 
+while getopts 'od' flag
+do
+    case "${flag}" in
+        o) open_result=true;;
+        d) 
+            build_flag="";
+            target_dir="debug"
+            ;;
+    esac
+done
+
 export ZED_BUNDLE=true
 export MACOSX_DEPLOYMENT_TARGET=10.15.7
 
@@ -12,13 +30,13 @@ rustup target add wasm32-wasi
 export CXXFLAGS="-stdlib=libc++"
 
 echo "Compiling zed binary for aarch64-apple-darwin"
-cargo build --release --package zed --target aarch64-apple-darwin
+cargo build ${build_flag} --package zed --target aarch64-apple-darwin
 echo "Compiling zed binary for x86_64-apple-darwin"
-cargo build --release --package zed --target x86_64-apple-darwin
+cargo build ${build_flag} --package zed --target x86_64-apple-darwin
 echo "Compiling cli binary for aarch64-apple-darwin"
-cargo build --release --package cli --target aarch64-apple-darwin
+cargo build ${build_flag} --package cli --target aarch64-apple-darwin
 echo "Compiling cli binary for x86_64-apple-darwin"
-cargo build --release --package cli --target x86_64-apple-darwin
+cargo build ${build_flag} --package cli --target x86_64-apple-darwin
 
 echo "Creating application bundle"
 pushd crates/zed
@@ -28,7 +46,10 @@ sed \
     -i .backup \
     "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
     Cargo.toml
-app_path=$(cargo bundle --release --target x86_64-apple-darwin | xargs)
+app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin | xargs)
+
+echo app_path
+
 mv Cargo.toml.backup Cargo.toml
 popd
 echo "Bundled ${app_path}"
@@ -36,18 +57,18 @@ echo "Bundled ${app_path}"
 echo "Creating fat binaries"
 lipo \
     -create \
-    target/{x86_64-apple-darwin,aarch64-apple-darwin}/release/Zed \
+    target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/Zed \
     -output \
     "${app_path}/Contents/MacOS/zed"
 lipo \
     -create \
-    target/{x86_64-apple-darwin,aarch64-apple-darwin}/release/cli \
+    target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
     -output \
     "${app_path}/Contents/MacOS/cli"
 
 echo "Copying WebRTC.framework into the frameworks folder"
 mkdir "${app_path}/Contents/Frameworks"
-cp -R target/x86_64-apple-darwin/release/WebRTC.framework "${app_path}/Contents/Frameworks/"
+cp -R target/x86_64-apple-darwin/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
 
 mv "${app_path}/Contents/Info.plist" "${app_path}/Contents/WithoutDocumentTypes.plist"
 awk \
@@ -73,7 +94,17 @@ else
     codesign --force --deep --sign - "${app_path}" -v
 fi
 
-dmg_target_directory="target/release"
+if [ "$target_dir" = "debug" ]; then
+    if [ "$open_result" = true ]; then
+        open "$app_path"
+    else
+        echo "Created application bundle:"
+        echo "$app_path"
+    fi
+    exit 0
+fi
+
+dmg_target_directory="target/${target_dir}"
 dmg_source_directory="${dmg_target_directory}/dmg"
 dmg_file_path="${dmg_target_directory}/Zed.dmg"
 
@@ -94,10 +125,6 @@ if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTAR
     npx notarize-cli --file ${dmg_file_path} --bundle-id dev.zed.Zed --username "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD"
 fi
 
-# If -o option is specified, open the $dmg_target_directory directory in Finder to reveal the DMG
-while getopts o flag
-do
-    case "${flag}" in
-        o) open $dmg_target_directory;;
-    esac
-done
+if [ "$open_result" = true ]; then
+    open $dmg_target_directory
+fi