Skip to content

Commit

Permalink
Merge pull request #1066 from savetheclocktower/fix-pulsar-p-on-mac-a…
Browse files Browse the repository at this point in the history
…nd-linux

Experiment: Redirect `-p`/`--package` to `ppm` via `pulsar.sh`…
  • Loading branch information
savetheclocktower authored Sep 15, 2024
2 parents a1095a8 + 66426db commit 71a3fad
Showing 1 changed file with 119 additions and 31 deletions.
150 changes: 119 additions & 31 deletions pulsar.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/bin/bash

echoerr() { echo "$@" 1>&2; }

if [ "$(uname)" == 'Darwin' ]; then
OS='Mac'
elif [ "$(expr substr $(uname -s) 1 5)" == 'Linux' ]; then
OS='Linux'
else
echo "Your platform ($(uname -a)) is not supported."
echoerr "Your platform ($(uname -a)) is not supported."
exit 1
fi

# Only set the ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT env var if it hasn't been set.
# Only set the ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT env var if it hasn't
# been set.
if [ -z "$ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT" ]
then
export ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT=true
Expand All @@ -35,9 +38,12 @@ while getopts ":anwtfvhp-:" opt; do
help|version)
EXPECT_OUTPUT=1
;;
foreground|benchmark|benchmark-test|test|package)
foreground|benchmark|benchmark-test|test)
EXPECT_OUTPUT=1
;;
package)
PACKAGE_MODE=1
;;
enable-electron-logging)
export ELECTRON_ENABLE_LOGGING=1
;;
Expand All @@ -52,7 +58,10 @@ while getopts ":anwtfvhp-:" opt; do
w)
WAIT=1
;;
f|t|h|v|p)
p)
PACKAGE_MODE=1
;;
f|t|h|v)
EXPECT_OUTPUT=1
;;
esac
Expand All @@ -70,50 +79,82 @@ fi
ATOM_HOME="${ATOM_HOME:-$HOME/.pulsar}"
mkdir -p "$ATOM_HOME"

if [ $PACKAGE_MODE ]; then
# If `-p` or `--package` is present, then we'll be discarding all arguments
# prior to (and including) `-p`/`--package` and passing the rest to `ppm`.
loop_done=0
while [ $loop_done -eq 0 ]
do
if [[ "$1" == "-p" || "$1" == "--package" || "$1" == "" ]]; then
# We'll shift one last time and then we'll be done.
loop_done=1
fi
shift
done
fi

if [ $OS == 'Mac' ]; then
if [ -L "$0" ]; then
SCRIPT="$(readlink "$0")"
else
SCRIPT="$0"
fi

ATOM_APP="$(dirname "$(dirname "$(dirname "$SCRIPT")")")"

if [ "$ATOM_APP" == . ]; then
# If this is a `pulsar.sh` from a built version of Pulsar, then `$ATOM_APP`
# should now be the path to the user's instance of Pulsar.app.
if [[ "$ATOM_APP" == . || "$ATOM_APP" != *".app" ]]; then
# This is a `pulsar.sh` that's in the source code of Pulsar or has been
# copied to a location outside of the app (instead of symlinked). We'll try
# another tactic.
unset ATOM_APP
else
# We found the location of the Pulsar.app that this script lives in.
PULSAR_PATH="$(dirname "$ATOM_APP")"
ATOM_APP_NAME="$(basename "$ATOM_APP")"
fi

if [ -n "${ATOM_APP_NAME}" ]; then
# If ATOM_APP_NAME is known, use it as the executable name
ATOM_EXECUTABLE_NAME="${ATOM_APP_NAME%.*}"
else
# Else choose it from the inferred channel name
ATOM_EXECUTABLE_NAME="Pulsar"
fi
# Fall back to the default Pulsar.app as the app name.
ATOM_APP_NAME=${ATOM_APP_NAME:-Pulsar.app}
# The executable name will be the same thing but without the `.app` suffix.
ATOM_EXECUTABLE_NAME="${ATOM_APP_NAME%.*}"

if [ -z "${PULSAR_PATH}" ]; then
# If PULSAR_PATH isn't set, check /Applications and then ~/Applications for Atom.app
if [ -x "/Applications/$ATOM_APP_NAME" ]; then
# If PULSAR_PATH isn't set, check /Applications and then ~/Applications for
# Pulsar.app.
if [ -x "/Applications/${ATOM_APP_NAME}" ]; then
PULSAR_PATH="/Applications"
elif [ -x "$HOME/Applications/$ATOM_APP_NAME" ]; then
elif [ -x "$HOME/Applications/${ATOM_APP_NAME}" ]; then
PULSAR_PATH="$HOME/Applications"
else
# We haven't found an Pulsar.app, use spotlight to search for Pulsar
PULSAR_PATH="$(mdfind "kMDItemCFBundleIdentifier == 'dev.pulsar-edit.pulsar'" | grep -v ShipIt | head -1 | xargs -0 dirname)"

# Exit if Pulsar can't be found
if [ ! -x "$PULSAR_PATH/$ATOM_APP_NAME" ]; then
echo "Cannot locate ${ATOM_APP_NAME}, it is usually located in /Applications. Set the PULSAR_PATH environment variable to the directory containing ${ATOM_APP_NAME}."
exit 1
# We still haven't found a Pulsar.app. Let's try searching for it via
# Spotlight.
PULSAR_APP_SEARCH_RESULT="$(mdfind "kMDItemCFBundleIdentifier == 'dev.pulsar-edit.pulsar'" | grep -v ShipIt | head -1)"
if [ ! -z "$PULSAR_APP_SEARCH_RESULT" ]; then
PULSAR_PATH="$(dirname "$PULSAR_APP_SEARCH_RESULT")"
ATOM_APP_NAME="$(basename "$PULSAR_APP_SEARCH_RESULT")"
fi
fi
fi

PULSAR_EXECUTABLE="$PULSAR_PATH/$ATOM_APP_NAME/Contents/MacOS/$ATOM_EXECUTABLE_NAME"
PPM_EXECUTABLE="$PULSAR_PATH/$ATOM_APP_NAME/Contents/Resources/app/ppm/bin/ppm"

# Exit if Pulsar can't be found.
if [ ! -x "${PULSAR_EXECUTABLE}" ]; then
echoerr "Cannot locate ${ATOM_APP_NAME}; it is usually located in /Applications. Set the PULSAR_PATH environment variable to the directory containing ${ATOM_APP_NAME}."
exit 1
fi

# If `-p` or `--package` was specified, call `ppm` with all the arguments
# that followed it instead of calling the Pulsar executable directly.
if [ $PACKAGE_MODE ]; then
"$PPM_EXECUTABLE" "$@"
exit $?
fi

if [ $EXPECT_OUTPUT ]; then
"$PULSAR_PATH/$ATOM_APP_NAME/Contents/MacOS/$ATOM_EXECUTABLE_NAME" --executed-from="$(pwd)" --pid=$$ "$@"
"$PULSAR_EXECUTABLE" --executed-from="$(pwd)" --pid=$$ "$@"
ATOM_EXIT=$?
if [ ${ATOM_EXIT} -eq 0 ] && [ -n "${EXIT_CODE_OVERRIDE}" ]; then
exit "${EXIT_CODE_OVERRIDE}"
Expand All @@ -124,17 +165,64 @@ if [ $OS == 'Mac' ]; then
open -a "$PULSAR_PATH/$ATOM_APP_NAME" -n -g --args --executed-from="$(pwd)" --pid=$$ --path-environment="$PATH" "$@"
fi
elif [ $OS == 'Linux' ]; then
SCRIPT=$(readlink -f "$0")

PULSAR_PATH="/opt/Pulsar/pulsar"

#Set tmpdir only if tmpdir is unset
# Set tmpdir only if it's unset.
: ${TMPDIR:=/tmp}

[ -x "$PULSAR_PATH" ] || PULSAR_PATH="$TMPDIR/pulsar-build/Pulsar/pulsar"
ATOM_EXECUTABLE_NAME="pulsar"

# If `PULSAR_PATH` is set by the user, we'll assume they know what they're
# doing. Otherwise we should try to find it ourselves.
if [ -z "${PULSAR_PATH}" ]; then
# Attempt to infer the installation directory of Pulsar from the location
# of this script. When symlinked to a common location like
# `/usr/local/bin`, this approach should find the true location of the
# Pulsar installation.
if [ -L "$0" ]; then
SCRIPT="$(readlink -f "$0")"
else
SCRIPT="$0"
fi

# The `pulsar.sh` file lives one directory deeper than the root directory
# that contains the `pulsar` binary.
ATOM_APP="$(dirname "$(dirname "$SCRIPT")")"
PULSAR_PATH="$(realpath "$ATOM_APP")"

if [ ! -f "$PULSAR_PATH/pulsar" ]; then
# If that path doesn't contain a `pulsar` executable, then it's not a
# valid path. We'll try something else.
unset ATOM_APP
unset PULSAR_PATH
fi

if [ -z "${PULSAR_PATH}" ]; then
if [ -f "/opt/Pulsar/pulsar" ]; then
# Check the default installation directory for RPM and DEB
# distributions.
PULSAR_PATH="/opt/Pulsar"
elif [ -f "$TMPDIR/pulsar-build/Pulsar/pulsar" ]; then
# This is where Pulsar can be found during some CI build tasks.
PULSAR_PATH="$TMPDIR/pulsar-build/Pulsar"
else
echoerr "Cannot locate Pulsar. Set the PULSAR_PATH environment variable to the directory containing the \`pulsar\` executable."
exit 1
fi
fi
fi

PULSAR_EXECUTABLE="$PULSAR_PATH/$ATOM_EXECUTABLE_NAME"
PPM_EXECUTABLE="$PULSAR_PATH/resources/app/ppm/bin/ppm"

# If `-p` or `--package` was specified, call `ppm` with all the arguments
# that followed it instead of calling the Pulsar executable directly.
if [ $PACKAGE_MODE ]; then
"$PPM_EXECUTABLE" "$@"
exit $?
fi

if [ $EXPECT_OUTPUT ]; then
"$PULSAR_PATH" --executed-from="$(pwd)" --pid=$$ "$@" --no-sandbox
"$PULSAR_EXECUTABLE" --executed-from="$(pwd)" --pid=$$ "$@" --no-sandbox
ATOM_EXIT=$?
if [ ${ATOM_EXIT} -eq 0 ] && [ -n "${EXIT_CODE_OVERRIDE}" ]; then
exit "${EXIT_CODE_OVERRIDE}"
Expand All @@ -143,7 +231,7 @@ elif [ $OS == 'Linux' ]; then
fi
else
(
nohup "$PULSAR_PATH" --executed-from="$(pwd)" --pid=$$ "$@" --no-sandbox > "$ATOM_HOME/nohup.out" 2>&1
nohup "$PULSAR_EXECUTABLE" --executed-from="$(pwd)" --pid=$$ "$@" --no-sandbox > "$ATOM_HOME/nohup.out" 2>&1
if [ $? -ne 0 ]; then
cat "$ATOM_HOME/nohup.out"
exit $?
Expand Down

0 comments on commit 71a3fad

Please sign in to comment.