refactor: move updaters to use modules

This commit is contained in:
Yusuf Bera Ertan 2022-11-20 09:57:33 +03:00
parent f44016a2d4
commit 3d400cc706
No known key found for this signature in database
GPG Key ID: 1D8F8FAF2294D6EA
12 changed files with 111 additions and 66 deletions

View File

@ -0,0 +1,6 @@
{
imports = [
./implementation.nix
./interface.nix
];
}

View File

@ -1,21 +1,8 @@
{
curl,
gnugrep,
jq,
lib,
python3,
writeText,
# dream2nix inputs
callPackageDream,
framework,
...
}: let
inherit (framework) utils fetchers;
{config, ...}: let
inherit (config) utils fetchers updaters;
lockUtils = utils.dream-lock;
updaters = callPackageDream ./updaters.nix {};
getUpdaterName = {dreamLock}: let
lock = (utils.dream-lock.readDreamLock {inherit dreamLock;}).lock;
source = lockUtils.getMainPackageSource lock;
@ -34,5 +21,7 @@
in
updater' source;
in {
inherit getUpdaterName makeUpdateScript updaters;
config.functions.updaters = {
inherit getUpdaterName makeUpdateScript;
};
}

View File

@ -0,0 +1,14 @@
{config, ...}: let
inherit (config.dlib) mkFunction;
l = config.lib // builtins;
t = l.types;
in {
options.functions.updaters = {
getUpdaterName = mkFunction {
type = t.either t.str t.null;
};
makeUpdateScript = mkFunction {
type = t.path;
};
};
}

View File

@ -45,6 +45,7 @@ in {
./functions.default-fetcher
./functions.combined-fetcher
./functions.translators
./functions.updaters
./apps
./builders
./discoverers
@ -62,6 +63,7 @@ in {
./dlib.parsing
./dlib.construct
./dlib.simpleTranslate2
./updaters
./externals
./dream2nix-interface
];

View File

@ -0,0 +1,6 @@
{
imports = [
./implementation.nix
./interface.nix
];
}

View File

@ -0,0 +1,17 @@
{config, ...}: let
l = config.lib // builtins;
updatersDir = ../../updaters;
updaterNames = l.attrNames (
l.filterAttrs
(_: type: type == "directory")
(l.readDir updatersDir)
);
updaterModules =
l.genAttrs
updaterNames
(name: import "${updatersDir}/${name}" config);
in {
config = {
updaters = updaterModules;
};
}

View File

@ -0,0 +1,10 @@
{config, ...}: let
l = config.lib // builtins;
t = l.types;
in {
options = {
updaters = l.mkOption {
type = t.lazyAttrsOf (t.functionTo t.path);
};
};
}

View File

@ -0,0 +1,12 @@
{
pkgs,
utils,
...
}: {
owner,
repo,
...
}:
utils.writePureShellScript (with pkgs; [curl jq]) ''
curl -s "https://api.github.com/repos/${owner}/${repo}/releases?per_page=1" | jq -r '.[0].tag_name'
''

View File

@ -0,0 +1,9 @@
{
pkgs,
utils,
...
}: {pname, ...}:
# api docs: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get
utils.writePureShellScript (with pkgs; [curl jq]) ''
curl -s https://registry.npmjs.com/${pname} | jq -r '."dist-tags".latest'
''

View File

@ -0,0 +1,8 @@
{
pkgs,
utils,
...
}: {pname, ...}:
utils.writePureShellScript (with pkgs; [curl jq]) ''
curl -s https://pypi.org/pypi/${pname}/json | jq -r '.info.version'
''

View File

@ -1,50 +0,0 @@
{
curl,
gnugrep,
jq,
lib,
python3,
writeText,
# dream2nix inputs
utils,
...
}: {
githubNewestReleaseTag = {
owner,
repo,
...
}:
utils.writePureShellScript [curl jq] ''
curl -s "https://api.github.com/repos/${owner}/${repo}/releases?per_page=1" | jq -r '.[0].tag_name'
'';
pypiNewestReleaseVersion = {pname, ...}:
utils.writePureShellScript [curl jq] ''
curl -s https://pypi.org/pypi/${pname}/json | jq -r '.info.version'
'';
npmNewestReleaseVersion = {pname, ...}:
# api docs: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#get
utils.writePureShellScript [curl jq] ''
curl -s https://registry.npmjs.com/${pname} | jq -r '."dist-tags".latest'
'';
urlRegexPython =
# Don't forget to use double quoted strings
# or double escape ('\\' instead of '\').
# Expects named group 'rev' to be defined.
# Example regex:
# ''[Pp]ython-(?P<ver>[\d\.]+)\.tgz''
{
url,
regex,
...
}: let
reFile = writeText "regex" regex;
in
utils.writePureShellScript [curl gnugrep python3] ''
curl -s ${url} \
| python3 -c \
'import re, sys; print(re.search(open("${reFile}").read(), sys.stdin.read()).group("ver"), end="")'
'';
}

View File

@ -0,0 +1,22 @@
{
pkgs,
utils,
...
}:
# Don't forget to use double quoted strings
# or double escape ('\\' instead of '\').
# Expects named group 'rev' to be defined.
# Example regex:
# ''[Pp]ython-(?P<ver>[\d\.]+)\.tgz''
{
url,
regex,
...
}: let
reFile = pkgs.writeText "regex" regex;
in
utils.writePureShellScript (with pkgs; [curl gnugrep python3]) ''
curl -s ${url} \
| python3 -c \
'import re, sys; print(re.search(open("${reFile}").read(), sys.stdin.read()).group("ver"), end="")'
''