-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.sh
More file actions
133 lines (116 loc) · 3.97 KB
/
cli.sh
File metadata and controls
133 lines (116 loc) · 3.97 KB
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
#!/usr/bin/env bash -il
SCRIPT_VERSION="v1.0.0"
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
SCRIPT_PATH=$(realpath --relative-to="$GIT_ROOT" "$0" 2>/dev/null || realpath "$0")
SUBCOMMAND="$1"
EXTRA_ARGS=$@
CACHE_ROOT="${HOME}/.cache/browserstack/devtools/cli/"
BINARY_ZIP_PATH="${CACHE_ROOT}/browserstack-cli.zip"
BINARY_PATH="${CACHE_ROOT}/browserstack-cli"
mkdir -p "$CACHE_ROOT"
get_os() {
local uname_out
uname_out="$(uname -s)"
case "${uname_out}" in
Linux*) os_type=linux;;
Darwin*) os_type=macos;;
*) os_type="UNKNOWN:${uname_out}"
esac
echo "${os_type}"
}
get_arch() {
local arch_out
arch_out="$(uname -m)"
case "${arch_out}" in
x86_64*) arch_type=x64;;
arm64*) arch_type=arm64;;
*) arch_type="UNKNOWN:${arch_out}"
esac
echo "${arch_type}"
}
OS=$(get_os)
ARCH=$(get_arch)
register_git_hook() {
local hook_name="pre-commit"
local hook_path="${GIT_ROOT}/.git/hooks/${hook_name}"
# Check if the hook file already exists
if [ -f "${hook_path}" ]; then
# Append the script execution if not already present
if ! grep -q "${SCRIPT_PATH}" "${hook_path}"; then
echo "" >> "${hook_path}"
echo "# Hook to run accessibility scan before commit" >> "${hook_path}"
echo "${SCRIPT_PATH}" >> "${hook_path}"
echo "if [ \$? -ne 0 ]; then" >> "${hook_path}"
echo " echo \"Accessibility scan failed. Commit aborted.\"" >> "${hook_path}"
echo " exit 1" >> "${hook_path}"
echo "fi" >> "${hook_path}"
fi
else
# Create a new hook file
cat > "${hook_path}" <<EOF
#!/bin/sh
# Hook to run accessibility scan before commit
"${SCRIPT_PATH}"
if [ \$? -ne 0 ]; then
echo "Accessibility scan failed. Commit aborted."
exit 1
fi
EOF
chmod +x "${hook_path}" # Make the hook executable
fi
}
a11y_scan() {
if [[ -z "$EXTRA_ARGS" ]]; then
EXTRA_ARGS="--include **/*.swift --include **/*.xib --include **/*.storyboard"
fi
env -i HOME="$HOME" \
XCODE_VERSION_ACTUAL="$XCODE_VERSION_ACTUAL"\
BROWSERSTACK_USERNAME="$BROWSERSTACK_USERNAME"\
BROWSERSTACK_ACCESS_KEY="$BROWSERSTACK_ACCESS_KEY"\
PATH="$PATH" \
$BINARY_PATH a11y $EXTRA_ARGS
}
script_self_update() {
local repo_base="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools"
local version_url="${repo_base}/refs/heads/main/scripts/latest-version.txt"
local script_rel_path="bash/cli.sh"
# Fetch remote version (lightweight metadata from main, not executable code)
local remote_version
remote_version=$(curl -fsSL --max-time 10 "$version_url" 2>/dev/null | tr -d '[:space:]')
if [[ -z "$remote_version" || "$remote_version" == "$SCRIPT_VERSION" ]]; then
return 0
fi
# Fetch script and checksums from immutable tagged ref
local tag_base="${repo_base}/refs/tags/${remote_version}/scripts"
local tmp_script tmp_sums
tmp_script=$(mktemp)
tmp_sums=$(mktemp)
trap 'rm -f "$tmp_script" "$tmp_sums"' RETURN
if ! curl -fsSL --max-time 30 "${tag_base}/${script_rel_path}" -o "$tmp_script" 2>/dev/null; then
return 0
fi
if ! curl -fsSL --max-time 10 "${tag_base}/SHA256SUMS" -o "$tmp_sums" 2>/dev/null; then
return 0
fi
# Verify SHA-256 checksum
local expected actual
expected=$(grep " ${script_rel_path}$" "$tmp_sums" | cut -d' ' -f1)
actual=$(shasum -a 256 "$tmp_script" | cut -d' ' -f1)
if [[ -z "$expected" || "$actual" != "$expected" ]]; then
echo "[self-update] WARNING: Checksum verification failed for ${script_rel_path}. Update aborted." >&2
return 1
fi
cp "$tmp_script" "$0"
echo "[self-update] Updated to ${remote_version}." >&2
}
download_binary() {
curl -R -z "$BINARY_ZIP_PATH" -L "https://api.browserstack.com/sdk/v1/download_cli?os=${OS}&os_arch=${ARCH}" -o "$BINARY_ZIP_PATH"
bsdtar -xvf "$BINARY_ZIP_PATH" -O > "$BINARY_PATH" && chmod 0775 "$BINARY_PATH"
}
script_self_update
if [[ $SUBCOMMAND == "register-pre-commit-hook" ]]; then
register_git_hook
exit 0
fi
download_binary
a11y_scan