Skip to content

Commit b4e59c4

Browse files
committed
ci: detect local harmonyos hdc tool
1 parent 6701f5d commit b4e59c4

2 files changed

Lines changed: 72 additions & 14 deletions

File tree

‎ohos/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ hdc version
6868
hdc list targets
6969
```
7070

71+
If `hdc` is not on `PATH`, either add the DevEco SDK `toolchains` directory to
72+
`PATH` or pass it explicitly:
73+
74+
```bash
75+
HDC=/path/to/hdc script/run-harmonyos-device.sh
76+
```
77+
78+
The run script also checks common SDK roots from `DEVECO_SDK_HOME`,
79+
`OHOS_SDK_HOME`, `OHOS_BASE_SDK_HOME`, `~/Library/Huawei/Sdk`, and
80+
`~/Library/OpenHarmony/Sdk`.
81+
7182
Build the package, then install and start the entry ability:
7283

7384
```bash

‎script/run-harmonyos-device.sh‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ DEFAULT_HAP_GLOB="${ROOT_DIR}/ohos/entry/build/default/outputs/default/"*.hap
66
BUNDLE_NAME="${OHOS_BUNDLE_NAME:-com.onetcli.app}"
77
ABILITY_NAME="${OHOS_ABILITY_NAME:-EntryAbility}"
88
HDC_TARGET="${HDC_TARGET:-}"
9+
HDC_BIN="${HDC:-}"
910

1011
usage() {
1112
cat <<'USAGE'
@@ -15,6 +16,7 @@ Installs a built HarmonyOS/OpenHarmony HAP on the connected hdc target and
1516
starts the OnetCli entry ability.
1617
1718
Environment:
19+
HDC Optional path to the hdc executable.
1820
HDC_TARGET Optional hdc target id. Defaults to the first listed target.
1921
OHOS_BUNDLE_NAME Bundle name to start. Default: com.onetcli.app
2022
OHOS_ABILITY_NAME Ability name to start. Default: EntryAbility
@@ -26,13 +28,54 @@ Build first when no explicit HAP path is supplied:
2628
USAGE
2729
}
2830

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
31+
find_hdc() {
32+
if [[ -n "${HDC_BIN}" ]]; then
33+
if [[ ! -x "${HDC_BIN}" ]]; then
34+
echo "HDC is not executable: ${HDC_BIN}" >&2
35+
exit 127
36+
fi
37+
echo "${HDC_BIN}"
38+
return
39+
fi
40+
41+
if command -v hdc >/dev/null 2>&1; then
42+
command -v hdc
43+
return
3544
fi
45+
46+
local candidates=()
47+
local roots=(
48+
"${DEVECO_SDK_HOME:-}"
49+
"${OHOS_SDK_HOME:-}"
50+
"${OHOS_BASE_SDK_HOME:-}"
51+
"${HOME}/Library/Huawei/Sdk"
52+
"${HOME}/Library/OpenHarmony/Sdk"
53+
)
54+
local root
55+
for root in "${roots[@]}"; do
56+
[[ -n "${root}" && -d "${root}" ]] || continue
57+
shopt -s nullglob
58+
candidates+=(
59+
"${root}"/toolchains/hdc
60+
"${root}"/*/toolchains/hdc
61+
"${root}"/*/*/toolchains/hdc
62+
)
63+
shopt -u nullglob
64+
done
65+
66+
local candidate
67+
if [[ "${#candidates[@]}" -gt 0 ]]; then
68+
for candidate in "${candidates[@]}"; do
69+
if [[ -x "${candidate}" ]]; then
70+
echo "${candidate}"
71+
return
72+
fi
73+
done
74+
fi
75+
76+
echo "missing required tool: hdc" >&2
77+
echo "Install DevEco Studio and add hdc to PATH, or set HDC=/path/to/hdc." >&2
78+
exit 127
3679
}
3780

3881
resolve_hap() {
@@ -62,14 +105,15 @@ resolve_hap() {
62105
}
63106

64107
select_target() {
108+
local hdc_bin="$1"
65109
if [[ -n "${HDC_TARGET}" ]]; then
66110
echo "${HDC_TARGET}"
67111
return
68112
fi
69113

70114
local target
71115
target="$(
72-
hdc list targets \
116+
"${hdc_bin}" list targets \
73117
| sed '/^[[:space:]]*$/d;/^\[Empty\]$/d;/^List of devices/d' \
74118
| head -1
75119
)"
@@ -82,12 +126,14 @@ select_target() {
82126
}
83127

84128
hdc_target_cmd() {
129+
local hdc_bin="$1"
130+
shift
85131
local target="$1"
86132
shift
87133
if [[ -n "${target}" ]]; then
88-
hdc -t "${target}" "$@"
134+
"${hdc_bin}" -t "${target}" "$@"
89135
else
90-
hdc "$@"
136+
"${hdc_bin}" "$@"
91137
fi
92138
}
93139

@@ -97,19 +143,20 @@ main() {
97143
exit 0
98144
fi
99145

100-
require_tool hdc
101-
146+
local hdc_bin
102147
local hap_path
103148
local target
104149
hap_path="$(resolve_hap "${1:-}")"
105-
target="$(select_target)"
150+
hdc_bin="$(find_hdc)"
151+
target="$(select_target "${hdc_bin}")"
106152

153+
echo "Using hdc: ${hdc_bin}"
107154
echo "Using hdc target: ${target}"
108155
echo "Installing HAP: ${hap_path}"
109-
hdc_target_cmd "${target}" install -r "${hap_path}"
156+
hdc_target_cmd "${hdc_bin}" "${target}" install -r "${hap_path}"
110157

111158
echo "Starting ability: ${BUNDLE_NAME}/${ABILITY_NAME}"
112-
hdc_target_cmd "${target}" shell aa start -b "${BUNDLE_NAME}" -a "${ABILITY_NAME}"
159+
hdc_target_cmd "${hdc_bin}" "${target}" shell aa start -b "${BUNDLE_NAME}" -a "${ABILITY_NAME}"
113160
}
114161

115162
main "$@"

0 commit comments

Comments
 (0)