diff --git a/README.md b/README.md index b3d4987526a46be3304ca649d90166566c03029e..eed8dd4d91c249dfa4de57f47d79c6ad1e2749e9 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,7 @@ foreman start If you want to run Zed pointed at the local servers, you can run: ``` -script/zed-with-local-servers -# or... -script/zed-with-local-servers --release +script/zed-local ``` ### Dump element JSON diff --git a/docs/building-zed.md b/docs/building-zed.md index 6981913285b5ec8aab22cc73813b86301237e120..ec4538cf85f47d99ee11b24bb876b602eba8249e 100644 --- a/docs/building-zed.md +++ b/docs/building-zed.md @@ -75,8 +75,7 @@ Expect this to take 30min to an hour! Some of these steps will take quite a whil - If you are just using the latest version, but not working on zed: - `cargo run --release` - If you need to run the collaboration server locally: - - `script/zed-with-local-servers` - - If you need to test collaboration with mutl + - `script/zed-local` ## Troubleshooting diff --git a/docs/local-collaboration.md b/docs/local-collaboration.md index 7d8054af673b5fb38c68bfff0c04a08ded7c2f0a..4c059c0878b4df38a3450a5e4d44787ee10aaf0f 100644 --- a/docs/local-collaboration.md +++ b/docs/local-collaboration.md @@ -17,6 +17,6 @@ ## Testing collab locally 1. Run `foreman start` from the root of the repo. -1. In another terminal run `script/start-local-collaboration`. +1. In another terminal run `script/zed-local -2`. 1. Two copies of Zed will open. Add yourself as a contact in the one that is not you. 1. Start a collaboration session as normal with any open project. diff --git a/script/start-local-collaboration b/script/start-local-collaboration deleted file mode 100755 index 0c4e60f9c3d16af4ba545d2cb6cfdfe9b5062148..0000000000000000000000000000000000000000 --- a/script/start-local-collaboration +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash - -set -e - -if [[ -z "$GITHUB_TOKEN" ]]; then - cat <<-MESSAGE -Missing \`GITHUB_TOKEN\` environment variable. This token is needed -for fetching your GitHub identity from the command-line. - -Create an access token here: https://github.com/settings/tokens -Then edit your \`~/.zshrc\` (or other shell initialization script), -adding a line like this: - - export GITHUB_TOKEN="(the token)" - -MESSAGE - exit 1 -fi - -# Install jq if it's not installed -if ! command -v jq &> /dev/null; then - echo "Installing jq..." - brew install jq -fi - -# Start one Zed instance as the current user and a second instance with a different user. -username_1=$(curl -sH "Authorization: bearer $GITHUB_TOKEN" https://api.github.com/user | jq -r .login) -username_2=nathansobo -if [[ $username_1 == $username_2 ]]; then - username_2=as-cii -fi - -# Make each Zed instance take up half of the screen. -output=$(system_profiler SPDisplaysDataType -json) -main_display=$(echo "$output" | jq '.SPDisplaysDataType[].spdisplays_ndrvs[] | select(.spdisplays_main == "spdisplays_yes")') -resolution=$(echo "$main_display" | jq -r '._spdisplays_resolution') -width=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[0].string') -half_width=$(($width / 2)) -height=$(echo "$resolution" | jq -Rr 'match("(\\d+) x (\\d+)").captures[1].string') -y=0 - -position_1=0,${y} -position_2=${half_width},${y} - -# Authenticate using the collab server's admin secret. -export ZED_STATELESS=1 -export ZED_ALWAYS_ACTIVE=1 -export ZED_ADMIN_API_TOKEN=secret -export ZED_SERVER_URL=http://localhost:8080 -export ZED_WINDOW_SIZE=${half_width},${height} - -cargo build -sleep 0.5 - -# Start the two Zed child processes. Open the given paths with the first instance. -trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT -ZED_IMPERSONATE=${ZED_IMPERSONATE:=${username_1}} ZED_WINDOW_POSITION=${position_1} target/debug/Zed $@ & -SECOND=true ZED_IMPERSONATE=${username_2} ZED_WINDOW_POSITION=${position_2} target/debug/Zed & -wait diff --git a/script/zed-local b/script/zed-local new file mode 100755 index 0000000000000000000000000000000000000000..683e31ef14d82800b8040383ee1e61709407456b --- /dev/null +++ b/script/zed-local @@ -0,0 +1,88 @@ +#!/usr/bin/env node + +const {spawn, execFileSync} = require('child_process') + +const RESOLUTION_REGEX = /(\d+) x (\d+)/ +const DIGIT_FLAG_REGEX = /^--?(\d+)$/ + +const args = process.argv.slice(2) + +// Parse the number of Zed instances to spawn. +let instanceCount = 1 +const digitMatch = args[0]?.match(DIGIT_FLAG_REGEX) +if (digitMatch) { + instanceCount = parseInt(digitMatch[1]) + args.shift() +} +if (instanceCount > 4) { + throw new Error('Cannot spawn more than 4 instances') +} + +// Parse the resolution of the main screen +const displayInfo = JSON.parse( + execFileSync( + 'system_profiler', + ['SPDisplaysDataType', '-json'], + {encoding: 'utf8'} + ) +) +const mainDisplayResolution = displayInfo + ?.SPDisplaysDataType[0] + ?.spdisplays_ndrvs + ?.find(entry => entry.spdisplays_main === "spdisplays_yes") + ?._spdisplays_resolution + ?.match(RESOLUTION_REGEX) +if (!mainDisplayResolution) { + throw new Error('Could not parse screen resolution') +} +const screenWidth = parseInt(mainDisplayResolution[1]) +const screenHeight = parseInt(mainDisplayResolution[2]) + +// Determine the window size for each instance +let instanceWidth = screenWidth +let instanceHeight = screenHeight +if (instanceCount > 1) { + instanceWidth = Math.floor(screenWidth / 2) + if (instanceCount > 2) { + instanceHeight = Math.floor(screenHeight / 2) + } +} + +let users = [ + 'nathansobo', + 'as-cii', + 'maxbrunsfeld', + 'iamnbutler' +] + +// If a user is specified, make sure it's first in the list +const user = process.env.ZED_IMPERSONATE +if (user) { + users = [user].concat(users.filter(u => u !== user)) +} + +const positions = [ + '0,0', + `${instanceWidth},0`, + `0,${instanceHeight}`, + `${instanceWidth},${instanceHeight}` +] + +execFileSync('cargo', ['build'], {stdio: 'inherit'}) + +setTimeout(() => { + for (let i = 0; i < instanceCount; i++) { + spawn('target/debug/Zed', i == 0 ? args : [], { + stdio: 'inherit', + env: { + ZED_IMPERSONATE: users[i], + ZED_WINDOW_POSITION: positions[i], + ZED_STATELESS: '1', + ZED_ALWAYS_ACTIVE: '1', + ZED_SERVER_URL: 'http://localhost:8080', + ZED_ADMIN_API_TOKEN: 'secret', + ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}` + } + }) + } +}, 0.1) diff --git a/script/zed-with-local-servers b/script/zed-with-local-servers deleted file mode 100755 index e1b224de600cea9c961978920f01921896ca4f51..0000000000000000000000000000000000000000 --- a/script/zed-with-local-servers +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -: "${ZED_IMPERSONATE:=as-cii}" -export ZED_IMPERSONATE - -ZED_ADMIN_API_TOKEN=secret ZED_SERVER_URL=http://localhost:8080 cargo run $@