|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 5 | +DEFAULT_HAP_GLOB="${ROOT_DIR}/ohos/entry/build/default/outputs/default/"*.hap |
| 6 | +BUNDLE_NAME="${OHOS_BUNDLE_NAME:-com.onetcli.app}" |
| 7 | +ABILITY_NAME="${OHOS_ABILITY_NAME:-EntryAbility}" |
| 8 | +HDC_TARGET="${HDC_TARGET:-}" |
| 9 | + |
| 10 | +usage() { |
| 11 | + cat <<'USAGE' |
| 12 | +Usage: script/run-harmonyos-device.sh [path/to/app.hap] |
| 13 | +
|
| 14 | +Installs a built HarmonyOS/OpenHarmony HAP on the connected hdc target and |
| 15 | +starts the OnetCli entry ability. |
| 16 | +
|
| 17 | +Environment: |
| 18 | + HDC_TARGET Optional hdc target id. Defaults to the first listed target. |
| 19 | + OHOS_BUNDLE_NAME Bundle name to start. Default: com.onetcli.app |
| 20 | + OHOS_ABILITY_NAME Ability name to start. Default: EntryAbility |
| 21 | +
|
| 22 | +Build first when no explicit HAP path is supplied: |
| 23 | +
|
| 24 | + script/build-harmonyos.sh --local |
| 25 | + script/run-harmonyos-device.sh |
| 26 | +USAGE |
| 27 | +} |
| 28 | + |
| 29 | +require_tool() { |
| 30 | + local tool="$1" |
| 31 | + if ! command -v "${tool}" >/dev/null 2>&1; then |
| 32 | + echo "missing required tool: ${tool}" >&2 |
| 33 | + echo "Install DevEco Studio and add its command-line tools to PATH." >&2 |
| 34 | + exit 127 |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +resolve_hap() { |
| 39 | + if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then |
| 40 | + usage |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | + if [[ -n "${1:-}" ]]; then |
| 44 | + if [[ ! -f "$1" ]]; then |
| 45 | + echo "HAP file does not exist: $1" >&2 |
| 46 | + exit 2 |
| 47 | + fi |
| 48 | + echo "$1" |
| 49 | + return |
| 50 | + fi |
| 51 | + |
| 52 | + local matches=() |
| 53 | + shopt -s nullglob |
| 54 | + matches=(${DEFAULT_HAP_GLOB}) |
| 55 | + shopt -u nullglob |
| 56 | + if [[ "${#matches[@]}" -eq 0 ]]; then |
| 57 | + echo "no HAP found under ohos/entry/build/default/outputs/default" >&2 |
| 58 | + echo "Run script/build-harmonyos.sh --local first, or pass a HAP path." >&2 |
| 59 | + exit 2 |
| 60 | + fi |
| 61 | + printf '%s\n' "${matches[@]}" | sort | tail -1 |
| 62 | +} |
| 63 | + |
| 64 | +select_target() { |
| 65 | + if [[ -n "${HDC_TARGET}" ]]; then |
| 66 | + echo "${HDC_TARGET}" |
| 67 | + return |
| 68 | + fi |
| 69 | + |
| 70 | + local target |
| 71 | + target="$( |
| 72 | + hdc list targets \ |
| 73 | + | sed '/^[[:space:]]*$/d;/^\[Empty\]$/d;/^List of devices/d' \ |
| 74 | + | head -1 |
| 75 | + )" |
| 76 | + if [[ -z "${target}" ]]; then |
| 77 | + echo "no hdc target found" >&2 |
| 78 | + echo "Start a DevEco Studio emulator or connect a HarmonyOS/OpenHarmony device." >&2 |
| 79 | + exit 2 |
| 80 | + fi |
| 81 | + echo "${target%%[[:space:]]*}" |
| 82 | +} |
| 83 | + |
| 84 | +hdc_target_cmd() { |
| 85 | + local target="$1" |
| 86 | + shift |
| 87 | + if [[ -n "${target}" ]]; then |
| 88 | + hdc -t "${target}" "$@" |
| 89 | + else |
| 90 | + hdc "$@" |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +main() { |
| 95 | + if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then |
| 96 | + usage |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | + |
| 100 | + require_tool hdc |
| 101 | + |
| 102 | + local hap_path |
| 103 | + local target |
| 104 | + hap_path="$(resolve_hap "${1:-}")" |
| 105 | + target="$(select_target)" |
| 106 | + |
| 107 | + echo "Using hdc target: ${target}" |
| 108 | + echo "Installing HAP: ${hap_path}" |
| 109 | + hdc_target_cmd "${target}" install -r "${hap_path}" |
| 110 | + |
| 111 | + echo "Starting ability: ${BUNDLE_NAME}/${ABILITY_NAME}" |
| 112 | + hdc_target_cmd "${target}" shell aa start -b "${BUNDLE_NAME}" -a "${ABILITY_NAME}" |
| 113 | +} |
| 114 | + |
| 115 | +main "$@" |
0 commit comments