aboutsummaryrefslogtreecommitdiff
path: root/tools/cpm/package/update.sh
blob: 77d408bee0b0b3d833421119e1adef9a05571ebc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh -e

# SPDX-FileCopyrightText: Copyright 2026 crueter
# SPDX-License-Identifier: LGPL-3.0-or-later

filter_out() {
	TAGS=$(echo "$TAGS" | jq "[.[] | select(.name | test(\"$1\"; \"i\") | not)]")
}

filter_in() {
	TAGS=$(echo "$TAGS" | jq "[.[] | select(.name | test(\"$1\"; \"i\"))]")
}

usage() {
	cat <<EOF
Usage: cpmutil.sh package update [-n|--dry-run] [-a|--all] [PACKAGE]...

Check a specific package or packages for updates.

Options:
    -n, --dry-run 	Do not update the package if it has an update available
    -a, --all    	Operate on all packages in this project.
    -c, --commit   	Automatically generate a commit message

EOF

	exit 0
}

while :; do
	case "$1" in
	-[a-z]*)
		opt=$(printf '%s' "$1" | sed 's/^-//')
		while [ -n "$opt" ]; do
			# cut out first char from the optstring
			char=$(echo "$opt" | cut -c1)
			opt=$(echo "$opt" | cut -c2-)

			case "$char" in
			a) ALL=1 ;;
			n) UPDATE=false ;;
			c) COMMIT=true ;;
			h) usage ;;
			*) die "Invalid option -$char" ;;
			esac
		done
		;;
	--dry-run) UPDATE=false ;;
	--all) ALL=1 ;;
	--help) usage ;;
	--commit) COMMIT=true ;;
	"$0") break ;;
	"") break ;;
	*) packages="$packages $1" ;;
	esac

	shift
done

[ "$ALL" != 1 ] || packages="${LIBS:-$packages}"
: "${UPDATE:=true}"
: "${COMMIT:=false}"
[ -n "$packages" ] || usage

for pkg in $packages; do
	PACKAGE="$pkg"
	export PACKAGE
	# shellcheck disable=SC1091
	. "$SCRIPTS"/vars.sh

	SKIP=$(value "skip_updates")

	[ "$SKIP" != "true" ] || continue

	[ "$REPO" != null ] || continue
	[ "$GIT_HOST" = "github.com" ] || continue # TODO

	[ "$CI" != "true" ] || continue

	# shellcheck disable=SC2153
	[ "$TAG" != null ] || continue

	echo "-- Package $PACKAGE"

	# TODO(crueter): Support for Forgejo updates w/ forgejo_token
	# Use gh-cli to avoid ratelimits, if available
	endpoint="/repos/$REPO/tags"
	if command -v gh >/dev/null 2>&1; then
		TAGS=$(gh api --method GET "$endpoint")
	else
		TAGS=$(curl -sfL "https://api.github.com$endpoint")
	fi

	# filter out some commonly known annoyances
	# TODO add more

	if [ "$PACKAGE" = "vulkan-validation-layers" ]; then
		filter_in vulkan-sdk
	else
		filter_out vulkan-sdk
	fi

	filter_out yotta # mbedtls

	# ????????????????????????????????
	filter_out vksc

	# ignore betas/alphas (remove if needed)
	filter_out alpha
	filter_out beta
	filter_out rc

	# openssl
	if [ "$PACKAGE" = openssl ]; then
		filter_out rsaref
		filter_in "openssl-"
	fi

	# Add package-specific overrides here, e.g. here for fmt:
	[ "$PACKAGE" != fmt ] || filter_out v0.11

	LATEST=$(echo "$TAGS" | jq -r '.[0].name')

	if [ "$LATEST" = "null" ] ||
		{ [ "$LATEST" = "$TAG" ] && [ "$FORCE" != "true" ]; }; then
		echo "-- * Up-to-date"
		continue
	fi

	if [ "$HAS_REPLACE" = "true" ]; then
		# this just extracts the tag prefix
		VERSION_PREFIX=$(echo "$ORIGINAL_TAG" | cut -d"%" -f1)

		# then we strip out the prefix from the new tag, and make that our new git_version
		if [ -z "$VERSION_PREFIX" ]; then
			NEW_GIT_VERSION="$LATEST"
		else
			NEW_GIT_VERSION=$(echo "$LATEST" | sed "s/$VERSION_PREFIX//g")
		fi
	else
		NEW_GIT_VERSION="$LATEST"
	fi

	_commit="$_commit
* $PACKAGE: $GIT_VERSION -> $NEW_GIT_VERSION"

	echo "-- * Version $LATEST available, current is $TAG"

	if [ "$UPDATE" = "true" ]; then
		if [ "$HAS_REPLACE" = "true" ]; then
			NEW_JSON=$(echo "$JSON" | jq ".git_version = \"$NEW_GIT_VERSION\"")
		else
			NEW_JSON=$(echo "$JSON" | jq ".tag = \"$NEW_GIT_VERSION\"")
		fi

		"$SCRIPTS"/util/replace.sh "$PACKAGE" "$NEW_JSON"

		echo "-- * -- Updating hash"

		export UPDATE
		QUIET=true "$SCRIPTS"/util/fix-hash.sh "$PACKAGE"
	fi
done

if [ "$UPDATE" = "true" ] && [ "$COMMIT" = "true" ] && [ -n "$_commit" ]; then
	for file in $CPMFILES; do
		git add "$file"
	done
	git commit -m "Update dependencies
$_commit"
fi