1#!/usr/bin/env nix-shell
2#! nix-shell -i bash -p bundix coreutils diffutils nix-prefetch-github gnused jq yarn-berry_4.yarn-berry-fetcher
3set -e
4
5OWNER=mastodon
6REPO=mastodon
7
8POSITIONAL=()
9while [[ $# -gt 0 ]]; do
10 key="$1"
11
12 case $key in
13 --owner)
14 OWNER="$2"
15 shift # past argument
16 shift # past value
17 ;;
18 --repo)
19 REPO="$2"
20 shift # past argument
21 shift # past value
22 ;;
23 --ver)
24 VERSION="$2"
25 shift # past argument
26 shift # past value
27 ;;
28 --rev)
29 REVISION="$2"
30 shift # past argument
31 shift # past value
32 ;;
33 --patches)
34 PATCHES="$2"
35 shift # past argument
36 shift # past value
37 ;;
38 *) # unknown option
39 POSITIONAL+=("$1")
40 shift # past argument
41 ;;
42 esac
43done
44
45if [[ -n "$POSITIONAL" ]]; then
46 echo "Usage: update.sh [--owner OWNER] [--repo REPO] [--ver VERSION] [--rev REVISION] [--patches PATCHES]"
47 echo "OWNER and REPO must be paths on github."
48 echo "If REVISION is not provided, the latest tag from github.com/mastodon/mastodon is fetched and VERSION is calculated from it."
49 echo "If OWNER and REPO are not provided, it defaults they default to mastodon and mastodon."
50 echo "PATCHES, if provided, should be one or more Nix expressions separated by spaces."
51 exit 1
52fi
53
54if [[ -z "$REVISION" ]]; then
55 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')"
56fi
57
58VERSION="$(echo "$REVISION" | cut -c2-)"
59
60rm -f gemset.nix source.nix
61cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
62
63WORK_DIR=$(mktemp -d)
64
65# Check that working directory was created.
66if [[ -z "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
67 echo "Could not create temporary directory"
68 exit 1
69fi
70
71# Delete the working directory on exit.
72function cleanup {
73 # Report errors, if any, from nix-prefetch-git
74 grep "fatal" $WORK_DIR/nix-prefetch-git.out >/dev/stderr || true
75 rm -rf "$WORK_DIR"
76}
77trap cleanup EXIT
78
79echo "Fetching source code $REVISION"
80JSON=$(nix-prefetch-github "$OWNER" "$REPO" --rev "$REVISION" 2> $WORK_DIR/nix-prefetch-git.out)
81HASH=$(echo "$JSON" | jq -r .hash)
82
83cat > source.nix << EOF
84# This file was generated by pkgs.mastodon.updateScript.
85{
86 fetchFromGitHub,
87 applyPatches,
88 patches ? [ ],
89}:
90let
91 version = "$VERSION";
92in
93applyPatches {
94 src = fetchFromGitHub {
95 owner = "$OWNER";
96 repo = "$REPO";
97 rev = "v\${version}";
98 hash = "$HASH";
99 passthru = {
100 inherit version;
101 yarnHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
102 yarnMissingHashes = ./missing-hashes.json;
103 };
104 };
105 patches = patches ++ [ $PATCHES];
106}
107EOF
108SOURCE_DIR="$(nix-build --no-out-link -E '(import <nixpkgs> {}).callPackage ./source.nix {}')"
109
110echo "Creating gemset.nix"
111bundix --lockfile="$SOURCE_DIR/Gemfile.lock" --gemfile="$SOURCE_DIR/Gemfile"
112echo "" >> gemset.nix # Create trailing newline to please EditorConfig checks
113
114echo "Updating yarnHash"
115yarn-berry-fetcher missing-hashes "$SOURCE_DIR/yarn.lock" > missing-hashes.json
116YARN_HASH="$(yarn-berry-fetcher prefetch "$SOURCE_DIR/yarn.lock" 2>/dev/null)"
117sed -i "s;sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=;$YARN_HASH;g" source.nix
118