-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalet-pro
executable file
·210 lines (171 loc) · 6.43 KB
/
valet-pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env bash
SOURCE="${BASH_SOURCE[0]}"
# If the current source is a symbolic link, we need to resolve it to an
# actual directory name. We'll use PHP to do this easier than we can
# do it in pure Bash. So, we'll call into PHP CLI here to resolve.
if [[ -L "$SOURCE" ]]
then
DIR=$(php -r "echo dirname(realpath('$SOURCE'));")
else
DIR="$( cd "$( dirname "$SOURCE" )" && pwd )"
fi
# If we are in the global Composer "bin" directory, we need to bump our
# current directory up two, so that we will correctly proxy into the
# Valet CLI script which is written in PHP. Will use PHP to do it.
if [ ! -f "$DIR/cli/valet.php" ]
then
DIR=$(php -r "echo realpath('$DIR/../nntoan/valet-pro-max');")
fi
# Valet is a dependency of Valet Pro Max. So, when we're developing on it, it's always in the vendor directory.
# When we are a user (composer global require nntoan/valet-pro-max) the following directories are used.
# laravel/valet is installed in /Users/<user>/.composer/vendor/laravel/valet/valet
# nntoan/valet-pro-max is installed in /Users/<user>/.composer/vendor/nntoan/valet-pro-max/valet
LARAVEL_DIR="$DIR/vendor/laravel/valet"
if [ ! -d "$LARAVEL_DIR" ]; then
LARAVEL_DIR=$(php -r "echo realpath('$DIR/../../laravel/valet');")
fi
# Get a command-line executable we can use for the required php version; if this
# is the inside loop (Valet runs itself 2x in some settings), skip
# checking and pulling again by reading the exported env var
if [[ $PHP_EXECUTABLE = "" ]]
then
PHP=$(php $DIR/cli/includes/find-usable-php.php)
# Validate output before running it on the CLI
if [[ ! -f $PHP ]]; then
echo "Error finding executable PHP. Quitting for safety."
echo "Provided output from find-usable-php.php:"
echo $PHP
exit
fi
export PHP_EXECUTABLE=$PHP
else
PHP=$PHP_EXECUTABLE
fi
# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
if [[ "$1" = "share" ]]
then
SHARETOOL="$($PHP "$DIR/cli/valet.php" share-tool)"
if [[ $SHARETOOL = "ngrok" ]]
then
# ngrok
# Check to make sure ngrok is configured correctly
BREW_PREFIX=$(brew --prefix)
$($BREW_PREFIX/bin/ngrok config check >/dev/null 2>&1)
if [[ $? -ne 0 ]]; then
echo "Please sign up for a free ngrok account and then run valet set-ngrok-token {yourTokenHere}."
exit
fi
# Check for parameters to pass through to ngrok (these will start with '-' or '--')
PARAMS=(${@:2})
for PARAM in ${PARAMS[@]}
do
if [[ ${PARAM:0:1} != '-' ]]; then
PARAMS=("${PARAMS[@]/$PARAM}") # Quotes when working with strings
fi
done
PARAMS=${PARAMS[@]}
HOST="${PWD##*/}"
# Find the first linked site for the current dir, if one exists
for linkname in ~/.config/valet/Sites/*; do
if [[ "$(readlink $linkname)" = "$PWD" ]]
then
HOST="${linkname##*/}"
break
fi
done
TLD=$($PHP "$DIR/cli/valet.php" tld)
# Decide the correct PORT: uses 60 for secure, else 80
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
then
PORT=60
else
PORT=80
fi
# Lowercase the host to match how the rest of our domains are looked up
HOST=$(echo "$HOST" | tr '[:upper:]' '[:lower:]')
sudo -u "$USER" "$BREW_PREFIX/bin/ngrok" http "$HOST.$TLD:$PORT" --host-header=rewrite $PARAMS
exit
elif [[ $SHARETOOL = "expose" ]]
then
# expose
# Check for parameters to pass through to Expose (these will start with '-' or '--')
PARAMS=(${@:2})
for PARAM in ${PARAMS[@]}
do
if [[ ${PARAM:0:1} != '-' ]]; then
PARAMS=("${PARAMS[@]/$PARAM}") #Quotes when working with strings
fi
done
PARAMS=${PARAMS[@]}
HOST="${PWD##*/}"
# Find the first linked site for the current dir, if one exists
for linkname in ~/.config/valet/Sites/*; do
if [[ "$(readlink $linkname)" = "$PWD" ]]
then
HOST="${linkname##*/}"
break
fi
done
TLD=$($PHP "$DIR/cli/valet.php" tld)
# Decide the correct PORT: uses 443 for secure, else 80
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
then
PORT=443
else
PORT=80
fi
# Lowercase the host to match how the rest of our domains are looked up
HOST=$(echo "$HOST" | tr '[:upper:]' '[:lower:]')
sudo -u "$USER" expose share "$HOST.$TLD:$PORT" $PARAMS
exit
else
echo ''
echo "Please use 'valet share-tool ngrok' or 'valet share-tool expose'"
echo "to set your preferred share tool."
exit
fi
# Proxy PHP commands to the "php" executable on the isolated site
elif [[ "$1" = "php" ]]
then
if [[ $2 == *"--site="* ]]; then
SITE=${2#*=}
$(php "$DIR/cli/valet.php" which-php $SITE) "${@:3}"
else
$(php "$DIR/cli/valet.php" which-php) "${@:2}"
fi
exit
# Proxy Composer commands with the "php" executable on the isolated site
elif [[ "$1" = "composer" ]]
then
if [[ $2 == *"--site="* ]]; then
SITE=${2#*=}
$(php "$DIR/cli/valet.php" which-php $SITE) $(which composer) "${@:3}"
else
$(php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"
fi
exit
# Proxy Drush commands with the "php" executable on the isolated site
elif [[ "$1" = "drush" ]]
then
if [[ $2 == *"--site="* ]]; then
SITE=${2#*=}
$(php "$DIR/cli/valet.php" which-php $SITE) $(which drush) "${@:3}"
elif [[ $2 == "uli" ]]; then
$(php "$DIR/cli/valet.php" which-php) $(which drush) uli --uri=$(php "$DIR/cli/valet.php" which-uri) "${@:3}"
else
$(php "$DIR/cli/valet.php" which-php) $(which drush) "${@:2}"
fi
exit
# Finally, for every other command we will just proxy into the PHP tool
# and let it handle the request. These are commands which can be run
# without sudo and don't require taking over terminals like Ngrok.
else
if [[ "$EUID" -ne 0 ]]
then
sudo USER="$USER" --preserve-env "$SOURCE" "$@"
exit
fi
$PHP "$DIR/cli/valet.php" "$@"
fi