Merge pull request #201658 from lovesegfault/tabnine-auto-update

This commit is contained in:
Bernardo Meurer 2022-11-17 13:18:30 -05:00 committed by GitHub
commit e2c91016fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 52 deletions

View file

@ -1,34 +1,18 @@
{ stdenv, lib, fetchurl, unzip }: { stdenv, lib, fetchurl, unzip }:
let let
# You can check the latest version with `curl -sS https://update.tabnine.com/bundles/version` sources = builtins.fromJSON (builtins.readFile ./sources.json);
# There's a handy prefetch script in ./fetch-latest.sh
version = "4.4.139";
supportedPlatforms = {
"x86_64-linux" = {
name = "x86_64-unknown-linux-musl";
hash = "sha256-CXm9WR77SMvv+9w+8QUBNHKPhe4otquLyHAwzd+jbNk=";
};
"x86_64-darwin" = {
name = "x86_64-apple-darwin";
hash = "sha256-01lotn9DzgwIj1n9GCUuGmwgtS4DtK+XOl/wduI+QyI=";
};
"aarch64-darwin" = {
name = "aarch64-apple-darwin";
hash = "sha256-RQBBsp48Xhxi3WQKsYzSiiSEW8W7UikKAyFf4sJ2JqQ=";
};
};
platform = platform =
if (builtins.hasAttr stdenv.hostPlatform.system supportedPlatforms) then if (builtins.hasAttr stdenv.hostPlatform.system sources.platforms) then
builtins.getAttr (stdenv.hostPlatform.system) supportedPlatforms builtins.getAttr (stdenv.hostPlatform.system) sources.platforms
else else
throw "Not supported on ${stdenv.hostPlatform.system}"; throw "Not supported on ${stdenv.hostPlatform.system}";
in in
stdenv.mkDerivation { stdenv.mkDerivation {
pname = "tabnine"; pname = "tabnine";
inherit version; inherit (sources) version;
src = fetchurl { src = fetchurl {
url = "https://update.tabnine.com/bundles/${version}/${platform.name}/TabNine.zip"; url = "https://update.tabnine.com/bundles/${sources.version}/${platform.name}/TabNine.zip";
inherit (platform) hash; inherit (platform) hash;
}; };
@ -49,13 +33,16 @@ stdenv.mkDerivation {
runHook postInstall runHook postInstall
''; '';
passthru.platform = platform.name; passthru = {
platform = platform.name;
updateScript = ./update.sh;
};
meta = with lib; { meta = with lib; {
homepage = "https://tabnine.com"; homepage = "https://tabnine.com";
description = "Smart Compose for code that uses deep learning to help you write code faster"; description = "Smart Compose for code that uses deep learning to help you write code faster";
license = licenses.unfree; license = licenses.unfree;
platforms = attrNames supportedPlatforms; platforms = attrNames sources.platforms;
maintainers = with maintainers; [ lovesegfault ]; maintainers = with maintainers; [ lovesegfault ];
}; };
} }

View file

@ -1,29 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
function prefetch-sri() {
nix-prefetch-url "$1" 2>/dev/null | xargs nix hash to-sri --type sha256
}
declare -a PLATFORMS=(
x86_64-unknown-linux-musl
x86_64-apple-darwin
aarch64-apple-darwin
)
LATEST="$(curl -sS https://update.tabnine.com/bundles/version)"
cat <<-EOF
version = "${LATEST}";
EOF
for platform in "${PLATFORMS[@]}"; do
url="https://update.tabnine.com/bundles/${LATEST}/${platform}/TabNine.zip"
sha="$(prefetch-sri "$url")"
cat <<-EOF
name = "${platform}";
hash = "${sha}";
EOF
done

View file

@ -0,0 +1,17 @@
{
"version": "4.4.169",
"platforms": {
"x86_64-linux": {
"name": "x86_64-unknown-linux-musl",
"hash": "sha256-gLhFSsOcMElm6a4oHM0yLL6ZGtSk+YvAsUTU/UgGwWg="
},
"aarch64-darwin": {
"name": "aarch64-apple-darwin",
"hash": "sha256-nEUPSj1BFkZ1VyeA7X3Y0zt6eeD4AKAIcJ51QF24JVA="
},
"x86_64-darwin": {
"name": "x86_64-apple-darwin",
"hash": "sha256-GePhGKPvvhYmOSN5NEaqcsFvyuSJi8eTfYTOtufmDiI="
}
}
}

View file

@ -0,0 +1,50 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq git
#shellcheck shell=bash
set -euo pipefail
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
function prefetch-sri() {
nix-prefetch-url "$1" 2>/dev/null |
xargs nix --experimental-features nix-command hash to-sri --type sha256
}
declare -A platforms=(
[x86_64-unknown-linux-musl]="x86_64-linux"
[x86_64-apple-darwin]="x86_64-darwin"
[aarch64-apple-darwin]="aarch64-darwin"
)
old_version="$(jq -r '.version' "$SCRIPT_DIR/sources.json")"
new_version="$(curl -sS https://update.tabnine.com/bundles/version)"
echo "Updating $old_version -> $new_version"
sources_tmp="$(mktemp)"
trap 'rm -f "$sources_tmp"' EXIT
cat <<EOF >"$sources_tmp"
{
"version": "$new_version",
"platforms": {}
}
EOF
for platform in "${!platforms[@]}"; do
url="https://update.tabnine.com/bundles/${new_version}/${platform}/TabNine.zip"
hash="$(prefetch-sri "$url")"
nix_platform="${platforms[$platform]}"
cat <<<"$(jq --arg nix_platform "$nix_platform" --arg platform "$platform" --arg hash "$hash" '.platforms += {($nix_platform): {name: $platform, hash: $hash}}' "$sources_tmp")" >"$sources_tmp"
done
cp "$sources_tmp" "$SCRIPT_DIR/sources.json"
if [[ `git status --porcelain "$SCRIPT_DIR/sources.json"` ]]; then
git add "$SCRIPT_DIR/sources.json"
git commit -m "tabnine: $old_version -> $new_version"
else
echo "No changes made to $SCRIPT_DIR/sources.json, skipping commit"
fi