Skip to content

Instantly share code, notes, and snippets.

@evan-goode
Created February 17, 2025 04:34
Show Gist options
  • Save evan-goode/e8cda17ece175ef3e3da39ce8638944f to your computer and use it in GitHub Desktop.
Save evan-goode/e8cda17ece175ef3e3da39ce8638944f to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# SPDX-License-Identifier: CC0-1.0
#
# dump-launch-script-wrapper.sh
#
# This is a wrapper command for Prism Launcher. As a side effect to launching
# an instance, it dumps a shell script to $1 that launches the instance.
#
# Usage: In Prism/Fjord Launcher, either in Edit Instance -> Settings or global
# Settings, go to "Custom Commands" and then enter
# `/path/to/dump-launch-script-wrapper.sh /path/to/launch-script-to-generate.sh`
#
# When you launch the instance, /path/to/launch-script-to-generate.sh will be
# created, and running /path/to/launch-script-to-generate.sh _should_ launch
# the exact same instance with the exact same access token, etc. Careful,
# /path/to/generated-launch-script.sh will contain secrets! Do not share it!
shell_escape() {
echo -n "$1" | sed -e "s/'/'\\\\''/g; 1s/^/'/; \$s/\$/'/"
}
output_path="$1"
shift
echo '#!/usr/bin/env sh' > "$output_path"
# PWD
escaped_PWD="$(shell_escape "$PWD")"
echo "cd $escaped_PWD" >> "$output_path"
# Environment variables
env | while IFS= read -r line; do
variable="$(echo "$line" | sed 's/=.*//')"
value="$(echo "$line" | sed 's/^[^=]*=//')"
escaped_value="$(shell_escape "$value")"
echo "export $variable=$escaped_value" >> "$output_path"
done
# stdin
captured_stdin_file="$(mktemp)"
while IFS= read -r line; do
# Read the launch script until the `launch` command
echo "$line" >> "$captured_stdin_file"
if [ "$line" == 'launch' ]; then
break
fi
done
captured_stdin="$(cat "$captured_stdin_file")"
rm "$captured_stdin_file"
escaped_captured_stdin="$(shell_escape "$captured_stdin")"
echo "echo $escaped_captured_stdin | $@" >> "$output_path"
chmod +x "$output_path"
# Run the wrapped program
echo "$captured_stdin" | $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment