Skip to content

Commit 017e9a4

Browse files
committed
[JIRA DEVA11Y-128] Added user-setup scripts
1 parent b65f420 commit 017e9a4

6 files changed

Lines changed: 631 additions & 0 deletions

File tree

scripts/bash/cli.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash -il
2+
3+
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
4+
SCRIPT_PATH=$(realpath --relative-to="$GIT_ROOT" "$0" 2>/dev/null || realpath "$0")
5+
SUBCOMMAND="$1"
6+
EXTRA_ARGS=$@
7+
CACHE_ROOT="${HOME}/.cache/browserstack/devtools/cli/"
8+
BINARY_ZIP_PATH="${CACHE_ROOT}/browserstack-cli.zip"
9+
BINARY_PATH="${CACHE_ROOT}/browserstack-cli"
10+
11+
mkdir -p "$CACHE_ROOT"
12+
13+
get_os() {
14+
local uname_out
15+
uname_out="$(uname -s)"
16+
case "${uname_out}" in
17+
Linux*) os_type=linux;;
18+
Darwin*) os_type=macos;;
19+
*) os_type="UNKNOWN:${uname_out}"
20+
esac
21+
echo "${os_type}"
22+
}
23+
24+
get_arch() {
25+
local arch_out
26+
arch_out="$(uname -m)"
27+
case "${arch_out}" in
28+
x86_64*) arch_type=x64;;
29+
arm64*) arch_type=arm64;;
30+
*) arch_type="UNKNOWN:${arch_out}"
31+
esac
32+
echo "${arch_type}"
33+
}
34+
35+
OS=$(get_os)
36+
ARCH=$(get_arch)
37+
38+
register_git_hook() {
39+
local hook_name="pre-commit"
40+
local hook_path="${GIT_ROOT}/.git/hooks/${hook_name}"
41+
42+
# Check if the hook file already exists
43+
if [ -f "${hook_path}" ]; then
44+
# Append the script execution if not already present
45+
if ! grep -q "${SCRIPT_PATH}" "${hook_path}"; then
46+
echo "" >> "${hook_path}"
47+
echo "# Hook to run accessibility scan before commit" >> "${hook_path}"
48+
echo "${SCRIPT_PATH}" >> "${hook_path}"
49+
echo "if [ \$? -ne 0 ]; then" >> "${hook_path}"
50+
echo " echo \"Accessibility scan failed. Commit aborted.\"" >> "${hook_path}"
51+
echo " exit 1" >> "${hook_path}"
52+
echo "fi" >> "${hook_path}"
53+
fi
54+
else
55+
# Create a new hook file
56+
cat > "${hook_path}" <<EOF
57+
#!/bin/sh
58+
# Hook to run accessibility scan before commit
59+
"${SCRIPT_PATH}"
60+
if [ \$? -ne 0 ]; then
61+
echo "Accessibility scan failed. Commit aborted."
62+
exit 1
63+
fi
64+
EOF
65+
chmod +x "${hook_path}" # Make the hook executable
66+
fi
67+
}
68+
69+
a11y_scan() {
70+
if [[ -z "$EXTRA_ARGS" ]]; then
71+
EXTRA_ARGS="--include **/*.swift"
72+
fi
73+
env -i HOME="$HOME" \
74+
XCODE_VERSION_ACTUAL="$XCODE_VERSION_ACTUAL"\
75+
BROWSERSTACK_USERNAME="$BROWSERSTACK_USERNAME"\
76+
BROWSERSTACK_ACCESS_KEY="$BROWSERSTACK_ACCESS_KEY"\
77+
PATH="$PATH" \
78+
$BINARY_PATH a11y $EXTRA_ARGS
79+
}
80+
81+
script_self_update() {
82+
local remote_url="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools/refs/heads/main/scripts/bash/spm.sh"
83+
84+
updated_script=$(curl -R -z "$SCRIPT_PATH" "$remote_url")
85+
if [[ $updated_script =~ ^#! ]]; then
86+
echo "$updated_script" > "$SCRIPT_PATH"
87+
fi
88+
}
89+
90+
download_binary() {
91+
curl -R -z "$BINARY_ZIP_PATH" -L "http://api.browserstack.com/sdk/v1/download_cli?os=${OS}&os_arch=${ARCH}" -o "$BINARY_ZIP_PATH"
92+
bsdtar -xvf "$BINARY_ZIP_PATH" -O > "$BINARY_PATH" && chmod 0775 "$BINARY_PATH"
93+
}
94+
95+
script_self_update
96+
if [[ $SUBCOMMAND == "register-pre-commit-hook" ]]; then
97+
register_git_hook
98+
exit 0
99+
fi
100+
101+
a11y_scan

scripts/bash/spm.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash -il
2+
3+
[ -f "${PWD}/Package.swift" ]
4+
PACKAGE_EXISTS="$?"
5+
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
6+
SCRIPT_PATH=$(realpath --relative-to="$GIT_ROOT" "$0" 2>/dev/null || realpath "$0")
7+
SUBCOMMAND="$1"
8+
EXTRA_ARGS=$@
9+
10+
register_git_hook() {
11+
local hook_name="pre-commit"
12+
local hook_path="${GIT_ROOT}/.git/hooks/${hook_name}"
13+
14+
# Check if the hook file already exists
15+
if [ -f "${hook_path}" ]; then
16+
# Append the script execution if not already present
17+
if ! grep -q "${SCRIPT_PATH}" "${hook_path}"; then
18+
echo "" >> "${hook_path}"
19+
echo "# Hook to run accessibility scan before commit" >> "${hook_path}"
20+
echo "${SCRIPT_PATH}" >> "${hook_path}"
21+
echo "if [ \$? -ne 0 ]; then" >> "${hook_path}"
22+
echo " echo \"Accessibility scan failed. Commit aborted.\"" >> "${hook_path}"
23+
echo " exit 1" >> "${hook_path}"
24+
echo "fi" >> "${hook_path}"
25+
fi
26+
else
27+
# Create a new hook file
28+
cat > "${hook_path}" <<EOF
29+
#!/bin/sh
30+
# Hook to run accessibility scan before commit
31+
"${SCRIPT_PATH}"
32+
if [ \$? -ne 0 ]; then
33+
echo "Accessibility scan failed. Commit aborted."
34+
exit 1
35+
fi
36+
EOF
37+
chmod +x "${hook_path}" # Make the hook executable
38+
fi
39+
}
40+
41+
a11y_scan() {
42+
# Ensure Package.swift is removed on exit (acts like a finally block)
43+
cleanup() {
44+
if [ $PACKAGE_EXISTS -eq 0 ]; then
45+
return
46+
fi
47+
rm -f -- "${PWD}/Package.swift" "${PWD}/Package.resolved"
48+
}
49+
trap cleanup EXIT
50+
51+
setup() {
52+
if [ $PACKAGE_EXISTS -eq 0 ]; then
53+
return
54+
fi
55+
56+
cat > Package.swift <<EOF
57+
// swift-tools-version: 5.9
58+
import PackageDescription
59+
60+
let package = Package(
61+
name: "Dummy",
62+
dependencies: [
63+
.package(url: "https://github.com/browserstack/AccessibilityDevTools.git", branch: "main")
64+
],
65+
targets: []
66+
)
67+
EOF
68+
}
69+
70+
setup
71+
if [[ -z "$EXTRA_ARGS" ]]; then
72+
EXTRA_ARGS="--include **/*.swift"
73+
fi
74+
env -i HOME="$HOME" \
75+
XCODE_VERSION_ACTUAL="$XCODE_VERSION_ACTUAL"\
76+
BROWSERSTACK_USERNAME="$BROWSERSTACK_USERNAME"\
77+
BROWSERSTACK_ACCESS_KEY="$BROWSERSTACK_ACCESS_KEY"\
78+
PATH="$PATH" \
79+
swift package plugin \
80+
--allow-writing-to-directory ~/.cache\
81+
--allow-writing-to-package-directory\
82+
--allow-network-connections 'all(ports: [])'\
83+
scan $EXTRA_ARGS
84+
}
85+
86+
script_self_update() {
87+
local remote_url="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools/refs/heads/main/scripts/bash/spm.sh"
88+
89+
updated_script=$(curl -R -z "$SCRIPT_PATH" "$remote_url")
90+
if [[ $updated_script =~ ^#! ]]; then
91+
echo "$updated_script" > "$SCRIPT_PATH"
92+
fi
93+
}
94+
95+
script_self_update
96+
if [[ $SUBCOMMAND == "register-pre-commit-hook" ]]; then
97+
register_git_hook
98+
exit 0
99+
fi
100+
101+
a11y_scan

scripts/fish/cli.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash -il
2+
3+
# Shell specific
4+
fish_bin=$(command -v fish)
5+
BROWSERSTACK_USERNAME=$($fish_bin -c 'echo $BROWSERSTACK_USERNAME')
6+
BROWSERSTACK_ACCESS_KEY=$($fish_bin -c 'echo $BROWSERSTACK_ACCESS_KEY')
7+
8+
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
9+
SCRIPT_PATH=$(realpath --relative-to="$GIT_ROOT" "$0" 2>/dev/null || realpath "$0")
10+
SUBCOMMAND="$1"
11+
EXTRA_ARGS=$@
12+
CACHE_ROOT="${HOME}/.cache/browserstack/devtools/cli/"
13+
BINARY_ZIP_PATH="${CACHE_ROOT}/browserstack-cli.zip"
14+
BINARY_PATH="${CACHE_ROOT}/browserstack-cli"
15+
16+
mkdir -p "$CACHE_ROOT"
17+
18+
get_os() {
19+
local uname_out
20+
uname_out="$(uname -s)"
21+
case "${uname_out}" in
22+
Linux*) os_type=linux;;
23+
Darwin*) os_type=macos;;
24+
*) os_type="UNKNOWN:${uname_out}"
25+
esac
26+
echo "${os_type}"
27+
}
28+
29+
get_arch() {
30+
local arch_out
31+
arch_out="$(uname -m)"
32+
case "${arch_out}" in
33+
x86_64*) arch_type=x64;;
34+
arm64*) arch_type=arm64;;
35+
*) arch_type="UNKNOWN:${arch_out}"
36+
esac
37+
echo "${arch_type}"
38+
}
39+
40+
OS=$(get_os)
41+
ARCH=$(get_arch)
42+
43+
register_git_hook() {
44+
local hook_name="pre-commit"
45+
local hook_path="${GIT_ROOT}/.git/hooks/${hook_name}"
46+
47+
# Check if the hook file already exists
48+
if [ -f "${hook_path}" ]; then
49+
# Append the script execution if not already present
50+
if ! grep -q "${SCRIPT_PATH}" "${hook_path}"; then
51+
echo "" >> "${hook_path}"
52+
echo "# Hook to run accessibility scan before commit" >> "${hook_path}"
53+
echo "${SCRIPT_PATH}" >> "${hook_path}"
54+
echo "if [ \$? -ne 0 ]; then" >> "${hook_path}"
55+
echo " echo \"Accessibility scan failed. Commit aborted.\"" >> "${hook_path}"
56+
echo " exit 1" >> "${hook_path}"
57+
echo "fi" >> "${hook_path}"
58+
fi
59+
else
60+
# Create a new hook file
61+
cat > "${hook_path}" <<EOF
62+
#!/bin/sh
63+
# Hook to run accessibility scan before commit
64+
"${SCRIPT_PATH}"
65+
if [ \$? -ne 0 ]; then
66+
echo "Accessibility scan failed. Commit aborted."
67+
exit 1
68+
fi
69+
EOF
70+
chmod +x "${hook_path}" # Make the hook executable
71+
fi
72+
}
73+
74+
a11y_scan() {
75+
if [[ -z "$EXTRA_ARGS" ]]; then
76+
EXTRA_ARGS="--include **/*.swift"
77+
fi
78+
env -i HOME="$HOME" \
79+
XCODE_VERSION_ACTUAL="$XCODE_VERSION_ACTUAL"\
80+
BROWSERSTACK_USERNAME="$BROWSERSTACK_USERNAME"\
81+
BROWSERSTACK_ACCESS_KEY="$BROWSERSTACK_ACCESS_KEY"\
82+
PATH="$PATH" \
83+
$BINARY_PATH a11y $EXTRA_ARGS
84+
}
85+
86+
script_self_update() {
87+
local remote_url="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools/refs/heads/main/scripts/bash/spm.sh"
88+
89+
updated_script=$(curl -R -z "$SCRIPT_PATH" "$remote_url")
90+
if [[ $updated_script =~ ^#! ]]; then
91+
echo "$updated_script" > "$SCRIPT_PATH"
92+
fi
93+
}
94+
95+
download_binary() {
96+
curl -R -z "$BINARY_ZIP_PATH" -L "http://api.browserstack.com/sdk/v1/download_cli?os=${OS}&os_arch=${ARCH}" -o "$BINARY_ZIP_PATH"
97+
bsdtar -xvf "$BINARY_ZIP_PATH" -O > "$BINARY_PATH" && chmod 0775 "$BINARY_PATH"
98+
}
99+
100+
script_self_update
101+
if [[ $SUBCOMMAND == "register-pre-commit-hook" ]]; then
102+
register_git_hook
103+
exit 0
104+
fi
105+
106+
a11y_scan
107+

0 commit comments

Comments
 (0)