aboutsummaryrefslogtreecommitdiff
path: root/tools/cpm/check-updates.sh
blob: 7fa1904e4e9c2e8d3940d75bf060ca2ee3a08319 (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
#!/bin/sh -e

# SPDX-FileCopyrightText: 2025 crueter
# SPDX-License-Identifier: GPL-3.0-or-later

# shellcheck disable=SC1091
. tools/cpm/common.sh

RETURN=0

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

usage() {
	cat << EOF
Usage: $0 [uf] [PACKAGE]...
Check a specific package or packages for updates.

Options:
    -u, --update	Update the package if a new version is available.
                	This will also update the hash if provided.

    -f, --force 	Forcefully update the package version (implies -u)
                	This is seldom useful, and should only be used in cases of
                	severe corruption.

This project has defined the following as valid cpmfiles:
EOF

	for file in $CPMFILES; do
		echo "- $file"
	done

	exit $RETURN
}

while true; do
	case "$1" in
		(-uf|--force) UPDATE=true; FORCE=true; continue ;;
		(-u|--update) UPDATE=true; continue ;;
		(-h) usage ;;
		("$0") break ;;
		("") break ;;
	esac

	PACKAGE="$1"

	shift

	export PACKAGE
	# shellcheck disable=SC1091
	. tools/cpm/package.sh

    SKIP=$(value "skip_updates")

    [ "$SKIP" = "true" ] && continue

    [ "$REPO" = null ] && continue
    [ "$GIT_HOST" != "github.com" ] && continue # TODO
    # shellcheck disable=SC2153
    [ "$TAG" = null ] && continue

    echo "-- Package $PACKAGE"

	# TODO(crueter): Support for Forgejo updates w/ forgejo_token
    # Use gh-cli to avoid ratelimits lmao
    TAGS=$(gh api --method GET "/repos/$REPO/tags")

    # filter out some commonly known annoyances
	# TODO add more

    filter vulkan-sdk # vulkan
    filter yotta # mbedtls

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

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

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

    [ "$LATEST" = "$TAG" ] && [ "$FORCE" != "true" ] && echo "-- * Up-to-date" && continue

	RETURN=1

    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
        NEW_GIT_VERSION=$(echo "$LATEST" | sed "s/$VERSION_PREFIX//g")
    fi

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

    HASH=$(tools/cpm/hash.sh "$REPO" "$LATEST")

    echo "-- * New hash: $HASH"

    if [ "$UPDATE" = "true" ]; then
		RETURN=0

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

		export NEW_JSON

		tools/cpm/replace.sh
    fi
done

exit $RETURN