about summary refs log tree commit diff
path: root/nixpkgs/pkgs/servers/mastodon/update.sh
blob: b79e8d30631000e37bc458890c3cf63055542c50 (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
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bundix coreutils diffutils nix-prefetch-github gnused jq prefetch-yarn-deps
set -e

OWNER=mastodon
REPO=mastodon

POSITIONAL=()
while [[ $# -gt 0 ]]; do
    key="$1"

    case $key in
        --owner)
            OWNER="$2"
            shift # past argument
            shift # past value
            ;;
        --repo)
            REPO="$2"
            shift # past argument
            shift # past value
            ;;
        --ver)
            VERSION="$2"
            shift # past argument
            shift # past value
            ;;
        --rev)
            REVISION="$2"
            shift # past argument
            shift # past value
            ;;
        --patches)
            PATCHES="$2"
            shift # past argument
            shift # past value
            ;;
        *)  # unknown option
            POSITIONAL+=("$1")
            shift # past argument
            ;;
    esac
done

if [[ -n "$POSITIONAL" ]]; then
    echo "Usage: update.sh [--owner OWNER] [--repo REPO] [--ver VERSION] [--rev REVISION] [--patches PATCHES]"
    echo "OWNER and REPO must be paths on github."
    echo "If REVISION is not provided, the latest tag from github.com/mastodon/mastodon is fetched and VERSION is calculated from it."
    echo "If OWNER and REPO are not provided, it defaults they default to mastodon and mastodon."
    echo "PATCHES, if provided, should be one or more Nix expressions separated by spaces."
    exit 1
fi

if [[ -z "$REVISION" ]]; then
    REVISION="$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s "https://api.github.com/repos/$OWNER/$REPO/releases" | jq -r  'map(select(.prerelease == false)) | .[0].tag_name')"
    VERSION="$(echo "$REVISION" | cut -c2-)"
fi

rm -f gemset.nix source.nix
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

WORK_DIR=$(mktemp -d)

# Check that working directory was created.
if [[ -z "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
    echo "Could not create temporary directory"
    exit 1
fi

# Delete the working directory on exit.
function cleanup {
    # Report errors, if any, from nix-prefetch-git
    grep "fatal" $WORK_DIR/nix-prefetch-git.out >/dev/stderr || true
    rm -rf "$WORK_DIR"
}
trap cleanup EXIT

echo "Fetching source code $REVISION"
JSON=$(nix-prefetch-github "$OWNER" "$REPO" --rev "$REVISION" 2> $WORK_DIR/nix-prefetch-git.out)
HASH=$(echo "$JSON" | jq -r .hash)

cat > source.nix << EOF
# This file was generated by pkgs.mastodon.updateScript.
{ fetchFromGitHub, applyPatches }:
let
  version = "$VERSION";
in
applyPatches {
  inherit version;
  src = fetchFromGitHub {
    owner = "$OWNER";
    repo = "$REPO";
    rev = "v\${version}";
    hash = "$HASH";
  };
  patches = [$PATCHES];
  yarnHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}
EOF
SOURCE_DIR="$(nix-build --no-out-link -E '(import <nixpkgs> {}).callPackage ./source.nix {}')"

echo "Creating gemset.nix"
bundix --lockfile="$SOURCE_DIR/Gemfile.lock" --gemfile="$SOURCE_DIR/Gemfile"
echo "" >> gemset.nix  # Create trailing newline to please EditorConfig checks

echo "Creating yarn-hash.nix"
YARN_HASH="$(prefetch-yarn-deps "$SOURCE_DIR/yarn.lock")"
YARN_HASH="$(nix hash to-sri --type sha256 "$YARN_HASH")"
sed -i "s/sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=/$YARN_HASH/g" source.nix