rewrite update script from scratch to use curl/jq

This commit is contained in:
Mitchell Hashimoto 2022-08-22 17:38:53 -07:00
parent e933abbcaf
commit 5fc48d57ca
No known key found for this signature in database
GPG key ID: 523D5DC389D273BC
4 changed files with 42 additions and 57 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
sources.json.bak sources.old.json
sources.new.json

View file

@ -1,7 +1,8 @@
{ pkgs ? import <nixpkgs> {}, { pkgs ? import <nixpkgs> {},
system ? builtins.currentSystem }: system ? builtins.currentSystem }:
let inherit (pkgs) lib; let
inherit (pkgs) lib;
releases = builtins.fromJSON (lib.strings.fileContents ./sources.json); releases = builtins.fromJSON (lib.strings.fileContents ./sources.json);
installPhase = '' installPhase = ''
mkdir -p $out/{doc,bin,lib} mkdir -p $out/{doc,bin,lib}
@ -10,8 +11,7 @@ let inherit (pkgs) lib;
cp -r lib/* $out/lib cp -r lib/* $out/lib
cp zig $out/bin/zig cp zig $out/bin/zig
''; '';
in lib.attrsets.mapAttrs (k: v:
in lib.attrsets.mapAttrs (k: v:
if k == "master" then if k == "master" then
lib.attrsets.mapAttrs (k: v: lib.attrsets.mapAttrs (k: v:
(pkgs.stdenv.mkDerivation { (pkgs.stdenv.mkDerivation {
@ -38,4 +38,4 @@ in lib.attrsets.mapAttrs (k: v:
dontFixup = true; dontFixup = true;
installPhase = installPhase; installPhase = installPhase;
}) })
releases releases

View file

@ -2502,4 +2502,4 @@
"sha256": "16b0e1defe4c1807f2e128f72863124bffdd906cefb21043c34b673bf85cd57f" "sha256": "16b0e1defe4c1807f2e128f72863124bffdd906cefb21043c34b673bf85cd57f"
} }
} }
} }

86
update
View file

@ -1,57 +1,41 @@
#!/usr/bin/env nix-shell #!/usr/bin/env nix-shell
#! nix-shell -p ruby -i ruby #! nix-shell -p curl jq -i sh
require 'net/http' # Build our new sources.json
require 'json' curl -s 'https://ziglang.org/download/index.json' | jq '
require 'pathname' ["aarch64-linux", "x86_64-linux", "aarch64-macos", "x86_64-macos"] as $targets |
require 'fileutils' def todarwin(x): x | gsub("macos"; "darwin");
def toentry(vsn; x):
[(vsn as $version |
.value |
to_entries[] |
select(.key as $key | any($targets[]; . == $key)) | {
(todarwin(.key)): {
"url": .value.tarball,
"sha256": .value.shasum,
"version": $version,
}
}
)] | add;
ZIG_URI = URI("https://ziglang.org/download/index.json") to_entries[] | {
DEFAULT_SYSTEMS = ["x86_64-linux", "aarch64-linux", "aarch64-darwin", "x86_64-darwin"] (.key): (
if (.key != "master") then
toentry(.key; .value)
else {
"latest": toentry(.value.version; .value),
(.value.date): toentry(.value.version; .value),
} end
)
}
' > sources.new.json
response = JSON.parse(Net::HTTP.get(ZIG_URI)) # For debugging
sources = if Pathname.new("sources.json").exist? # cat sources.new.json
JSON.parse(File.read("sources.json")) # exit
else
{ "master" => {} }
end
# Update versions # Copy the old file since jq can't modify in-place. This is also a backup.
response.each do |k, v| cp sources.json sources.old.json
unless k == "master"
sources[k] = DEFAULT_SYSTEMS.to_h do |system|
data = if system == "x86_64-darwin"
response[k]["x86_64-macos"] or {}
elsif system == "aarch64-darwin"
response[k]["aarch64-macos"] or {}
else
response[k][system] or {}
end
[system, { "url" => data["tarball"],
"version" => k,
"sha256" => data["shasum"] }]
end
end
end
# Update master # Recursive merge
sources["master"][response["master"]["date"]] = DEFAULT_SYSTEMS.to_h do |system| jq -s '.[0] * .[1]' sources.old.json sources.new.json > sources.json
data = if system == "x86_64-darwin"
response["master"]["x86_64-macos"]
elsif system == "aarch64-darwin"
response["master"]["aarch64-macos"]
else
response["master"][system]
end
[system, { "url" => data["tarball"],
"version" => response["master"]["version"],
"sha256" => data["shasum"] }]
end
sources["master"]["latest"] = sources["master"][response["master"]["date"]]
FileUtils.cp "sources.json", "sources.json.bak"
File.write "sources.json", JSON.pretty_generate(sources)
# Local Variables:
# mode: ruby
# End: