summary refs log tree commit diff
path: root/pkgs/applications/emulators/yuzu/update.sh
blob: 7234adf977bb25fe0d1b4b524f56a63ffbe5a995 (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
#! /usr/bin/env nix-shell
#! nix-shell -I nixpkgs=./. -i bash -p nix nix-prefetch-git coreutils curl jq gnused

set -euo pipefail

# Will be replaced with the actual branch when running this from passthru.updateScript
BRANCH="@branch@"
DEFAULT_NIX="$(dirname "${BASH_SOURCE[@]}")/default.nix"

if [[ "$(basename "$PWD")" = "yuzu" ]]; then
    echo "error: Script must be ran from nixpkgs's root directory for compatibility with the maintainer script"
    exit 1
fi

updateBranch() {
    local branch attribute oldVersion oldHash newVersion newHash
    branch="$1"
    attribute="yuzu-$branch"
    [[ "$branch" = "early-access" ]] && attribute="yuzu-ea" # Attribute path doesnt match the branch name
    oldVersion="$(nix eval --raw -f "./default.nix" "$attribute".version)"
    oldHash="$(nix eval --raw -f "./default.nix" "$attribute".src.drvAttrs.outputHash)"

    if [[ "$branch" = "mainline" ]]; then
        newVersion="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "https://api.github.com/repos/yuzu-emu/yuzu-mainline/releases?per_page=1" \
            | jq -r '.[0].name' | cut -d" " -f2)"
    elif [[ "$branch" = "early-access" ]]; then
        newVersion="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "https://api.github.com/repos/pineappleEA/pineapple-src/releases?per_page=2" \
            | jq -r '.[].tag_name' | grep '^EA-[0-9]*' | head -n1 | cut -d"-" -f2 | cut -d" " -f1)"
    fi

    if [[ "${oldVersion}" = "${newVersion}" ]]; then
        echo "$attribute is already up to date."
        return
    else
        echo "$attribute: ${oldVersion} -> ${newVersion}"
    fi

    echo "  fetching source code to generate hash..."
    if [[ "$branch" = "mainline" ]]; then
        newHash="$(nix-prefetch-git --quiet --fetch-submodules --rev "mainline-0-${newVersion}" "https://github.com/yuzu-emu/yuzu-mainline" | jq -r '.sha256')"
    elif [[ "$branch" = "early-access" ]]; then
        newHash="$(nix-prefetch-git --quiet --fetch-submodules --rev "EA-${newVersion}" "https://github.com/pineappleEA/pineapple-src" | jq -r '.sha256')"
    fi
    newHash="$(nix hash to-sri --type sha256 "${newHash}")"

    sed -i "s,${oldVersion},${newVersion}," "$DEFAULT_NIX"
    sed -i "s,${oldHash},${newHash},g" "$DEFAULT_NIX"
    echo "  successfully updated $attribute. new hash: $newHash"
}

updateCompatibilityList() {
    local latestRevision oldUrl newUrl oldHash newHash oldDate newDate
    latestRevision="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "https://api.github.com/repos/flathub/org.yuzu_emu.yuzu/commits/master" | jq -r '.sha')"

    oldUrl="$(sed -n '/yuzu-compat-list/,/url/p' "$DEFAULT_NIX" | tail -n1 | cut -d'"' -f2)"
    newUrl="https://raw.githubusercontent.com/flathub/org.yuzu_emu.yuzu/${latestRevision}/compatibility_list.json"

    oldDate="$(sed -n '/last updated.*/p' "$DEFAULT_NIX" | rev | cut -d' ' -f1 | rev)"
    newDate="$(curl -s ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} "https://api.github.com/repos/flathub/org.yuzu_emu.yuzu/commits/${latestRevision}" \
        | jq -r '.commit.committer.date' | cut -d'T' -f1)"

    oldHash="$(sed -n '/yuzu-compat-list/,/sha256/p' "$DEFAULT_NIX" | tail -n1 | cut -d'"' -f2)"
    newHash="$(nix hash to-sri --type sha256 "$(nix-prefetch-url --quiet "$newUrl")")"

    if [[ "$oldHash" = "$newHash" ]]; then
        echo "compatibility_list is already up to date."
        return
    else
        echo "compatibility_list: $oldDate -> $newDate"
    fi

    sed -i "s,${oldUrl},${newUrl},g" "$DEFAULT_NIX"
    sed -i "s,${oldHash},${newHash},g" "$DEFAULT_NIX"
    sed -i "s,${oldDate},${newDate},g" "$DEFAULT_NIX"
    echo "  successfully updated compatibility_list. new hash: $newHash"
}

if [[ "$BRANCH" = "mainline" ]] || [[ "$BRANCH" = "early-access" ]]; then
    updateBranch "$BRANCH"
    updateCompatibilityList
else # Script is not ran from passthru.updateScript
    if (( $# == 0 )); then
        updateBranch "mainline"
        updateBranch "early-access"
    fi

    while (( "$#" > 0 )); do
        case "$1" in
            mainline|yuzu-mainline)
                updateBranch "mainline"
                ;;
            early-access|yuzu-early-access|ea|yuzu-ea)
                updateBranch "early-access"
                ;;
            *)
                echo "error: invalid branch: $1."
                echo "usage: $(basename "$0") [mainline|early-access]"
                exit 1
                ;;
        esac
        shift
    done

    updateCompatibilityList
fi