Skip to content

Commit 1e33b70

Browse files
authored
fix: improve version normalization in build script (#90)
1 parent 5635540 commit 1e33b70

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

build-android-lib.sh

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,68 @@
11
#!/bin/bash
22
# Script to build NetBird mobile bindings using gomobile
33
# Usage: ./script.sh [version]
4-
# - If a version is provided, it will be used.
4+
# - If a version is provided, it will be used (with leading 'v' stripped if present).
55
# - If no version is provided:
6-
# * Uses the latest Git tag if available.
6+
# * Uses the latest Git tag if available (with leading 'v' stripped if present).
77
# * Otherwise, defaults to "dev-<short-hash>".
88
# - When running in GitHub Actions, uses "ci-<short-hash>" instead of "dev-<short-hash>".
99

10-
set -e
10+
set -euo pipefail
1111

1212
app_path=$(pwd)
1313

14+
# Normalize semantic versions to drop a leading 'v' (e.g., v1.2.3 -> 1.2.3).
15+
# Only strips if the string starts with 'v' followed by a digit, so it won't affect
16+
# dev/ci strings or other non-semver values.
17+
normalize_version() {
18+
local ver="$1"
19+
if [[ "$ver" =~ ^v[0-9] ]]; then
20+
ver="${ver#v}"
21+
fi
22+
echo "$ver"
23+
}
1424

1525
get_version() {
16-
if [ -n "$1" ]; then
17-
echo "$1"
26+
if [ -n "${1:-}" ]; then
27+
normalize_version "$1"
1828
return
1929
fi
2030

2131
# Try to get an exact tag
22-
local tag=$(git describe --tags --exact-match 2>/dev/null || true)
32+
local tag
33+
tag=$(git describe --tags --exact-match 2>/dev/null || true)
2334

2435
if [ -n "$tag" ]; then
25-
echo "$tag"
36+
normalize_version "$tag"
2637
return
2738
fi
2839

2940
# Fallback to "<prefix>-<short-hash>"
30-
local short_hash=$(git rev-parse --short HEAD)
41+
local short_hash
42+
short_hash=$(git rev-parse --short HEAD)
3143

32-
if [ "$GITHUB_ACTIONS" == "true" ]; then
33-
local new_version="ci-$short_hash"
44+
local new_version
45+
if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
46+
new_version="ci-$short_hash"
3447
else
35-
local new_version="dev-$short_hash"
48+
new_version="dev-$short_hash"
3649
fi
3750

3851
echo "$new_version"
3952
}
4053

41-
4254
cd netbird
4355

4456
# Get version using the function
45-
version=$(get_version "$1")
57+
version=$(get_version "${1:-}")
4658
echo "Using version: $version"
4759

4860
gomobile init
4961

5062
CGO_ENABLED=0 gomobile bind \
51-
-o $app_path/gomobile/netbird.aar \
63+
-o "$app_path/gomobile/netbird.aar" \
5264
-javapkg=io.netbird.gomobile \
5365
-ldflags="-checklinkname=0 -X golang.zx2c4.com/wireguard/ipc.socketDirectory=/data/data/io.netbird.client/cache/wireguard -X github.com/netbirdio/netbird/version.version=$version" \
54-
$(pwd)/client/android
66+
"$(pwd)/client/android"
5567

5668
cd - > /dev/null

0 commit comments

Comments
 (0)