summary refs log tree commit diff
diff options
context:
space:
mode:
authorJakub Kozłowski <kubukoz@gmail.com>2022-05-30 16:12:43 +0200
committerJakub Kozłowski <kubukoz@gmail.com>2022-05-30 19:23:16 +0200
commit567b83ebbd9961717a83233970209918b6430867 (patch)
tree894099f890709e918b6a0752deecc1c8ca909add
parent437c8e6554911095f0557d524e9d2ffe1c26e33a (diff)
downloadnixpkgs-567b83ebbd9961717a83233970209918b6430867.tar
nixpkgs-567b83ebbd9961717a83233970209918b6430867.tar.gz
nixpkgs-567b83ebbd9961717a83233970209918b6430867.tar.bz2
nixpkgs-567b83ebbd9961717a83233970209918b6430867.tar.lz
nixpkgs-567b83ebbd9961717a83233970209918b6430867.tar.xz
nixpkgs-567b83ebbd9961717a83233970209918b6430867.tar.zst
nixpkgs-567b83ebbd9961717a83233970209918b6430867.zip
scala-cli: add updater
-rw-r--r--pkgs/development/tools/build-managers/scala-cli/default.nix25
-rw-r--r--pkgs/development/tools/build-managers/scala-cli/sources.json13
-rw-r--r--pkgs/development/tools/build-managers/scala-cli/update.nix37
3 files changed, 59 insertions, 16 deletions
diff --git a/pkgs/development/tools/build-managers/scala-cli/default.nix b/pkgs/development/tools/build-managers/scala-cli/default.nix
index 40b230a72c3..1eb6b92cbd4 100644
--- a/pkgs/development/tools/build-managers/scala-cli/default.nix
+++ b/pkgs/development/tools/build-managers/scala-cli/default.nix
@@ -1,21 +1,14 @@
-{ stdenv, coreutils, lib, installShellFiles, zlib, autoPatchelfHook, fetchurl }:
+{ stdenv, coreutils, lib, installShellFiles, zlib, autoPatchelfHook, fetchurl, callPackage }:
 
 let
-  version = "0.1.5";
-  assets = {
-    x86_64-darwin = {
-      asset = "scala-cli-x86_64-apple-darwin.gz";
-      sha256 = "1sczbvvhh1ff8mdb6zj4q3fnrz1qsqmr58jlb1s3fy1wv2p5ywxl";
-    };
-    x86_64-linux = {
-      asset = "scala-cli-x86_64-pc-linux.gz";
-      sha256 = "1ahvjgcghh1pmgfsajg0b8bf5aaqxdx0f6b6qgljs0xfqm7zs05v";
-    };
-  };
+  pname = "scala-cli";
+  sources = builtins.fromJSON (builtins.readFile ./sources.json);
+  inherit (sources) version assets;
+
+  platforms = builtins.attrNames assets;
 in
 stdenv.mkDerivation {
-  pname = "scala-cli";
-  inherit version;
+  inherit pname version;
   nativeBuildInputs = [ installShellFiles ]
     ++ lib.optional stdenv.isLinux autoPatchelfHook;
   buildInputs = [ coreutils zlib stdenv.cc.cc ];
@@ -27,7 +20,6 @@ stdenv.mkDerivation {
       url = "https://github.com/Virtuslab/scala-cli/releases/download/v${version}/${asset.asset}";
       sha256 = asset.sha256;
     };
-
   unpackPhase = ''
     runHook preUnpack
     gzip -d < $src > scala-cli
@@ -61,6 +53,7 @@ stdenv.mkDerivation {
     license = licenses.asl20;
     description = "Command-line tool to interact with the Scala language";
     maintainers = [ maintainers.kubukoz ];
-    platforms = builtins.attrNames assets;
   };
+
+  passthru.updateScript = callPackage ./update.nix { } { inherit platforms pname version; };
 }
diff --git a/pkgs/development/tools/build-managers/scala-cli/sources.json b/pkgs/development/tools/build-managers/scala-cli/sources.json
new file mode 100644
index 00000000000..a69e67d3bf8
--- /dev/null
+++ b/pkgs/development/tools/build-managers/scala-cli/sources.json
@@ -0,0 +1,13 @@
+{
+  "version": "0.1.5",
+  "assets": {
+    "x86_64-darwin": {
+      "asset": "scala-cli-x86_64-apple-darwin.gz",
+      "sha256": "1sczbvvhh1ff8mdb6zj4q3fnrz1qsqmr58jlb1s3fy1wv2p5ywxl"
+    },
+    "x86_64-linux": {
+      "asset": "scala-cli-x86_64-pc-linux.gz",
+      "sha256": "1ahvjgcghh1pmgfsajg0b8bf5aaqxdx0f6b6qgljs0xfqm7zs05v"
+    }
+  }
+}
diff --git a/pkgs/development/tools/build-managers/scala-cli/update.nix b/pkgs/development/tools/build-managers/scala-cli/update.nix
new file mode 100644
index 00000000000..fd79a1cf3a8
--- /dev/null
+++ b/pkgs/development/tools/build-managers/scala-cli/update.nix
@@ -0,0 +1,37 @@
+{ lib, curl, writeShellScript, jq, gnused, git, nix, coreutils }: { platforms, pname, version }:
+
+writeShellScript "${pname}-update-script" ''
+  set -o errexit
+  PATH=${lib.makeBinPath [ curl jq gnused git nix coreutils ]}
+
+  latest_version=$(curl -s "https://api.github.com/repos/VirtusLab/scala-cli/releases?per_page=1" | jq ".[0].tag_name" --raw-output | sed 's/^v//')
+
+  if [[ "${version}" = "$latest_version" ]]; then
+      echo "The new version same as the old version."
+      exit 0
+  fi
+
+  nixpkgs=$(git rev-parse --show-toplevel)
+  sources_json="$nixpkgs/pkgs/development/tools/build-managers/scala-cli/sources.json"
+
+  platform_assets=()
+
+  for platform in ${lib.concatStringsSep " " platforms}; do
+    asset=$(jq ".assets.\"$platform\".asset" --raw-output < $sources_json)
+    release_asset_url="https://github.com/Virtuslab/scala-cli/releases/download/v$latest_version/$asset"
+
+    asset_hash=$(nix-prefetch-url "$release_asset_url")
+
+    asset_object=$(jq --compact-output --null-input \
+      --arg asset "$asset" \
+      --arg sha256 "$asset_hash" \
+      --arg platform "$platform" \
+      '{asset: $asset, sha256: $sha256, platform: $platform}')
+    platform_assets+=($asset_object)
+  done
+
+  printf '%s\n' "''${platform_assets[@]}" | \
+    jq -s "map ( { (.platform): . | del(.platform) }) | add" | \
+    jq --arg version $latest_version \
+      '{ version: $version, assets: . }' > $sources_json
+''