summarylogtreecommitdiffstats
path: root/update.sh
blob: 4664fb5414d96b21435e1a090f257d4b61d04b2b (plain)
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
#!/bin/bash

# - Fetch the sha of the current aur version via http api.
# - Fetch the sha of the head of jojo's MuseScore 3.x branch via http api.
# - Take no further action if the shas are equal.
# - Update the _commit variable of PKGBUILD.
# - Build the package (updates pkgver and maybe pkgrel of PKGBUILD).
# - Update .SRCINFO.
# - Take no further action unless $COMMIT is set to 1.
# - The commit message is simply "${pkgver}-${pkgrel}".
# - If a commit for the current pkgver/pkgrel already exists, then
#   exit with error status.
# - Commit (but do not push) the updated PKGBUILD and .SRCINFO.

AUR_DATA_FILE="aur_data.json" # for testing
AUR_DATA_URL="https://aur.archlinux.org/rpc/v5/info?arg[]=musescore3-git"

GH_DATA_FILE="gh_data.json" # for testing
GH_DATA_URL="https://api.github.com/repos/Jojo-Schmitz/MuseScore/commits/3.x"

set_from_file_or_url() {
    local VAR="$1"
    local FILE="$2"
    local URL="$3"

    if [ -f "$FILE" ]; then
        declare -g $VAR="$(cat "$FILE")"
    else
        declare -g $VAR="$(curl -s "$URL")"
    fi
}

set_aur_data() {
    set_from_file_or_url "AUR_DATA" "$AUR_DATA_FILE" "$AUR_DATA_URL"
}

set_gh_data() {
    set_from_file_or_url "GH_DATA" "$GH_DATA_FILE" "$GH_DATA_URL"
}

aur_version() {
    jq -r '.["results"][0]["Version"]' <<< "$AUR_DATA"
}

aur_sha() {
    aur_version | sed -E 's|.*\.r[0-9]+\.([0-9a-z]+)-.*|\1|'
}

gh_sha_long() {
    jq -r '.["sha"]' <<< "$GH_DATA"
}

gh_sha() {
    local SHA=$(gh_sha_long)

    echo "${SHA:0:7}"
}

pkg_ver() {
    grep ^pkgver= PKGBUILD | cut -d = -f 2
}

pkg_rel() {
    grep ^pkgrel= PKGBUILD | cut -d = -f 2
}

pkg_filename() {
    local VER="$(pkg_ver)"
    local REL="$(pkg_rel)"
    local MACHINE="$(uname -m)"
    local EXT="zst"

    echo "musescore3-git-${VER}-${REL}-${MACHINE}.pkg.tar.${EXT}"
}

up_to_date() {
    local AUR_SHA="$(aur_sha)"
    local GH_SHA="$(gh_sha)"
    local INFO="aur=$AUR_SHA git=$GH_SHA"

    if [ "$FORCE" = "1" ]; then
        echo "build forced: $INFO"
        return 1
    elif [ "$AUR_SHA" = "$GH_SHA" ]; then
        echo "aur is up to date: $INFO" 
        return 0
    else
        echo "aur is stale: $INFO"
        return 1
    fi
}

patch() {
    local SHA="$(gh_sha_long)"

    sed -i "s|^_commit=.*\$|_commit=\"${SHA}\"|" PKGBUILD
}

build() {
    rm -rf pkg src
    makepkg -s

    local PKG="$(pkg_filename)"

    if ! [ -f "$PKG" ]; then
        echo "error: build failed: $PKG"
        exit 1
    fi

    makepkg --printsrcinfo > .SRCINFO
}

commit() {
    if [ "$COMMIT" != "1" ]; then
        echo "note: COMMIT is not set to 1"
        echo "      PKGBUILD and .SRCINFO changes were not committed"
        return 0
    fi

    local VER="$(pkg_ver)-$(pkg_rel)"
    
    if git log | grep -m 1 -E "^[[:space:]]+${VER}\$" >/dev/null; then
        echo "error: a commit for $VER already exists"
        exit 1
    fi

    git commit -m "$VER" PKGBUILD .SRCINFO
    git log -1
}

main() {
    set -e

    set_aur_data
    set_gh_data

    if ! up_to_date; then
        patch
        build
        commit
    fi
}

main