scripts/scripts/lib/android.sh
Copilot 18b9f020bb
Fix shell script formatting and add PR workflow validation (#3)
* Initial plan

* fix: format shell scripts with shfmt (convert tabs to 2 spaces)

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

* feat: enhance shell-check workflow for PR pre-merge validation

- Add pull_request_target trigger to check PRs from forks
- Add explicit failure message with instructions
- Create BRANCH_PROTECTION.md with setup guide
- Ensure workflow runs on all PRs targeting main/master

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

* refactor: improve workflow security and remove redundant exit code

- Remove pull_request_target to avoid executing untrusted fork code
- Remove redundant exit 1 from failure step
- Update documentation to reflect changes
- Standard pull_request trigger handles forks securely

Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>
2026-01-07 22:52:20 +01:00

60 lines
1.4 KiB
Bash

#!/bin/bash
# Shared functions for Android-related scripts
# Source this file after sourcing common.sh
# Prevent multiple sourcing
[[ -n ${_LIB_ANDROID_LOADED:-} ]] && return 0
_LIB_ANDROID_LOADED=1
ANDROID_WORK_DIR="${HOME}/.cache/android-adblock"
ensure_dir "$ANDROID_WORK_DIR"
# Exit with error message
die() {
echo "[ERROR] $*" >&2
exit 1
}
# Print section header
print_header() {
echo
echo "========================================"
echo " $1"
echo "========================================"
echo
}
# Initialize an Android script with common setup
# Usage: init_android_script "$@"
# This combines: require_hosts_readable, sets WORK_DIR
init_android_script() {
require_hosts_readable "$@"
WORK_DIR="$ANDROID_WORK_DIR"
export WORK_DIR
}
# Check if ADB device is connected
check_adb_device() {
log "Checking device connection..."
if ! adb devices | grep -q "device$"; then
die "No device connected. Enable USB debugging and connect your phone."
fi
log "Device connected"
}
# Check if device has root access
check_adb_root() {
log "Checking root access..."
if ! adb shell "su -c 'echo test'" 2> /dev/null | grep -q "test"; then
die "Root access not available. Make sure Magisk is installed and grant root to Shell."
fi
log "Root access confirmed"
}
# Re-exec with sudo if needed to read /etc/hosts
require_hosts_readable() {
if [[ $EUID -ne 0 ]] && [[ ! -r /etc/hosts ]]; then
exec sudo -E bash "$0" "$@"
fi
}