From 8057991ffbced7097ebfe85988da3693a7ab9f2d Mon Sep 17 00:00:00 2001 From: Petar Kirov Date: Fri, 29 Sep 2023 18:07:22 +0300 Subject: [PATCH] wip: refactor(script): Factor-out common code --- scripts/fetch_binary.d | 24 ++---------------------- scripts/update.d | 1 + scripts/utils.d | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 scripts/utils.d diff --git a/scripts/fetch_binary.d b/scripts/fetch_binary.d index 3bedeefb..2410572f 100755 --- a/scripts/fetch_binary.d +++ b/scripts/fetch_binary.d @@ -12,11 +12,9 @@ import std.stdio : stdout, stderr; import std.string : strip; import std.typecons : tuple; +import utils; + enum Compiler { dmd, ldc }; -alias Version = string; -alias Platform = string; -alias Hash = string; -alias Url = string; alias UrlFormatter = Url function(Platform platform, Version compilerVersion); @@ -123,21 +121,3 @@ void main(string[] args) { hashes.JSONValue.toPrettyString(JSONOptions.doNotEscapeSlashes) ); } - -Hash prefech(bool dryRun, Url url) => - executeCommand( - dryRun, - `nix store prefetch-file --json "%s" | jq -r '.hash'` - .format(url) - ).strip; - -string executeCommand(bool dryRun, string command) { - stderr.writefln(`> %s`, command); - if (dryRun) return null; - const result = executeShell( - command, - null, - Config.stderrPassThrough, - ); - return result.status == 0 ? result.output : null; -} diff --git a/scripts/update.d b/scripts/update.d index f1a6b356..8e8b9c2b 100755 --- a/scripts/update.d +++ b/scripts/update.d @@ -1,4 +1,5 @@ #!/usr/bin/env dub + /+ dub.sdl: name "update-nix-inputs" dependency "semver" version="~>0.3.4" diff --git a/scripts/utils.d b/scripts/utils.d new file mode 100644 index 00000000..00f2d28f --- /dev/null +++ b/scripts/utils.d @@ -0,0 +1,28 @@ +import std.format : format; +import std.process : executeShell, Config; +import std.string : strip; +import std.stdio : stderr; + +alias Version = string; +alias Platform = string; +alias Hash = string; +alias Url = string; + +Hash prefech(bool dryRun, Url url) => + executeCommand( + dryRun, + `nix store prefetch-file --json "%s" | jq -r '.hash'` + .format(url) + ).strip; + + +string executeCommand(bool dryRun, string command) { + stderr.writefln(`> %s`, command); + if (dryRun) return null; + const result = executeShell( + command, + null, + Config.stderrPassThrough, + ); + return result.status == 0 ? result.output : null; +}