jnv: 0.4.2 -> 0.5.0 (#371655)
[NixPkgs.git] / pkgs / os-specific / darwin / apple-source-releases / update-source-releases.sh
blobaa852d125a0d342888b3aaf72aa2ff6460b6b112
1 #!/usr/bin/env nix-shell
2 #!nix-shell -i bash -p coreutils curl git gnutar jq moreutils nix
4 set -eu -o pipefail
6 if [ ! -v 1 ]; then
7 echo "usage: lock-sdk-deps.sh <macOS version> <Packages>" >&2
8 echo " <macOS version> Decimal-separated version number." >&2
9 echo " Must correspond to a tag in https://github.com/apple-oss-distributions/distribution-macOS" >&2
10 exit 1
13 pkgdir=$(dirname "$(realpath "$0")")
15 lockfile=$pkgdir/versions.json
16 if [ -e "$lockfile" ]; then
17 echo '{}' > "$lockfile"
20 workdir=$(mktemp -d)
21 trap 'rm -rf -- "$workdir"' EXIT
23 sdkVersion=$1; shift
24 tag="macos-${sdkVersion//.}"
26 declare -A ignoredPackages=(
27 [libsbuf]=1
28 [locale]=1
29 [mkAppleDerivation]=1
30 [update-source-releases.sh]=1
31 [versions.json]=1
34 readarray -t packages < <(
35 for file in "$pkgdir"/*; do
36 pkg=$(basename "$file" ".nix")
37 test ! "${ignoredPackages[$pkg]-}" && echo "$pkg"
38 done
41 echo "Locking versions for macOS $sdkVersion using tag '$tag'..."
43 pushd "$workdir" > /dev/null
45 git clone --branch "$tag" https://github.com/apple-oss-distributions/distribution-macOS.git &> /dev/null
46 cd distribution-macOS
48 for package in "${packages[@]}"; do
49 # If the tag exists in `release.json`, use that as an optimization to avoid downloading unnecessarily from Github.
50 packageTag=$(jq -r --arg package "$package" '.projects[] | select(.project == $package) | .tag' release.json)
51 packageCommit=$(git ls-tree -d HEAD "$package" | awk '{print $3}')
53 if [ ! -d "$package" ]; then
54 packageCommit=HEAD
57 # However, sometimes it doesn’t exist. In that case, fall back to cloning the repo and check manually
58 # which tag corresponds to the commit from the submodule.
59 if [ -z "$packageTag" ]; then
60 git clone --no-checkout "https://github.com/apple-oss-distributions/$package.git" ../source &> /dev/null
61 pushd ../source > /dev/null
62 packageTag=$(git tag --points-at "$packageCommit")
63 popd > /dev/null
64 rm -rf ../source
67 packageVersion=${packageTag##"$package"-}
69 curl -OL "https://github.com/apple-oss-distributions/$package/archive/$packageTag.tar.gz" &> /dev/null
70 tar axf "$packageTag.tar.gz"
72 packageHash=$(nix --extra-experimental-features nix-command hash path "$package-$packageTag")
74 pkgsjson="{\"$package\": {\"version\": \"$packageVersion\", \"hash\": \"$packageHash\"}}"
76 echo " - Locking $package to version $packageVersion with hash '$packageHash'"
77 jq --argjson pkg "$pkgsjson" -S '. * $pkg' "$lockfile" | sponge "$lockfile"
78 done
80 popd > /dev/null