-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitpod-autopwf
executable file
·175 lines (172 loc) · 6.11 KB
/
gitpod-autopwf
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
#!/usr/bin/env bash
main@bashbox%gitpod-autopwf ()
{
if test "${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}" -lt 43; then
{
printf '[!!!] \033[1;31m%s\033[0m[%s]: %s\n' ERROR 1 "At least bash 4.3 is required to run this." "Please upgrade bash or use the correct interpreter." "If you're on MacOS, you can install latest bash using brew or nix." 1>&2;
exit 1
};
fi;
function process::self::exit ()
{
local _r=$?;
( kill -USR1 "$___self_PID" 2> /dev/null || : ) & exit $_r
};
function process::self::forcekill ()
{
kill -9 "$___self_PID" 2> /dev/null
};
function log::error ()
{
local _retcode="${2:-$?}";
local _exception_line="$1";
local _source="${BB_ERR_SOURCE:-"${BASH_SOURCE[-1]}"}";
function ___errmsg ()
{
printf '[!!!] \033[1;31m%s\033[0m[%s]: %s\n' ERROR "$_retcode" "$@" 1>&2
};
if [[ ! "$_exception_line" == \(*\) ]]; then
{
___errmsg "${_source##*/}[${BASH_LINENO[0]}]: ${BB_ERR_MSG:-"$_exception_line"}";
if test -v BB_ERR_MSG; then
{
printf "STACK TRACE: (TOKEN: %s)\n" "$_exception_line" 1>&2;
local -i _frame=0;
local _treestack='|-';
local _line _caller _source;
while read -r _line _caller _source < <(caller "$_frame"); do
{
printf '%s >> %s\n' "$_treestack ${_caller}" "${_source##*/}:${_line}" 1>&2;
_frame+=1;
_treestack+='-'
};
done
};
fi
};
else
{
___errmsg "${_source##*/}[${BASH_LINENO[0]}]: SUBSHELL EXITED WITH NON-ZERO STATUS"
};
fi;
return "$_retcode"
};
\command unalias -a || true;
set -eEuT -o pipefail;
shopt -sq inherit_errexit expand_aliases nullglob;
trap 'exit' USR1;
trap 'BB_ERR_MSG="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" || process::self::exit' ERR;
___self="$0";
___self_PID="$$";
___self_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)";
___MAIN_FUNCNAME='main@bashbox%gitpod-autopwf';
___self_NAME="playwright-local-server-for-gitpod";
___self_CODENAME="gitpod-autopwf";
___self_AUTHORS=("AXON <[email protected]>");
___self_VERSION="1.0";
___self_DEPENDENCIES=();
___self_REPOSITORY="";
___self_BASHBOX_COMPAT="0.4.1~";
function bashbox::build::after ()
{
cp "$_target_workfile" "$_arg_path/$CODENAME";
chmod +x "$_arg_path/$CODENAME"
};
function selfinstall ()
{
echo "INFO: requesting sudo privileges to install playwright dependencies...";
if test -e "$HOME/.npm"; then
sudo chown -R "$(id -un)":"$(id -gn)" "$HOME/.npm";
fi;
npm install @playwright/test;
sudo env PATH="$PATH" npx --yes playwright install-deps;
npx --yes playwright install;
local id=com.gitpodsupport.autopwf;
local startup_plist="$HOME/Library/LaunchAgents/${id}.plist";
local script_path="$HOME/.gitpod-autopwf";
printf '%s\n' '#!/opt/homebrew/bin/bash' "$(declare -f "${___MAIN_FUNCNAME}")" "${___MAIN_FUNCNAME}"' "$@";' > "${script_path}";
chmod +x "${script_path}";
mkdir -p -m 0755 "$HOME/Library/LaunchAgents";
cat > "${startup_plist}" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${id}</string>
<key>ProgramArguments</key>
<array>
<string>${script_path}</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>${PATH}:/opt/homebrew/bin</string>
</dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
<key>Crashed</key>
<true/>
</dict>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/${id}.out.log</string>
<key>StandardErrorPath</key>
<string>/tmp/${id}.err.log</string>
</dict>
</plist>
EOF
launchctl unload "${startup_plist}" 2> /dev/null || :;
launchctl load "${startup_plist}";
echo "INFO: installed background script to ${script_path}, service ID: ${id}"
};
function main ()
{
if test "${1:-}" == "selfinstall"; then
{
selfinstall;
return 0
};
fi;
local trap_str;
if ! command -v gitpod > /dev/null; then
{
echo "ERROR: gitpod CLI is not installed. Please install it via something like brew";
return 1
};
fi;
if ! gitpod whoami > /dev/null 2>&1; then
{
echo "ERROR: not logged into Gitpod";
return 1
};
fi;
local pw_port=9999;
local ssh_daemons=();
local workspaces_store=();
local pw_cmd=(playwright run-server --port 9999 --host 0.0.0.0);
pkill -9 -f "${pw_cmd[*]}" || :;
npx "${pw_cmd[@]}" & trap_str+="for p in $! \${ssh_daemons[@]}; do kill -9 \$p; done";
trap "${trap_str}" EXIT SIGINT;
while sleep 2; do
while read -r workspace; do
{
if [[ ! "${workspaces_store[*]}" =~ (^| )${workspace}($| ) ]]; then
{
workspaces_store+=("${workspace}");
echo "${workspace}";
$(gitpod workspace ssh "${workspace}" --dry-run) -R ${pw_port}:localhost:${pw_port} -N & ssh_daemons+=($!)
};
fi
};
done < <(gitpod workspace list -r -f id);
done
};
main "$@";
wait;
exit
}
"main@bashbox%gitpod-autopwf" "$@";