Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for version_key #214

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion nix_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def die(msg: str) -> NoReturn:


def parse_args(args: list[str]) -> Options:
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
help = "File to import rather than default.nix. Examples, ./release.nix"
parser.add_argument("-f", "--file", default="./.", help=help)
parser.add_argument(
Expand Down Expand Up @@ -56,6 +58,12 @@ def parse_args(args: list[str]) -> Options:
help="Regex to extract version with, i.e. 'jq-(.*)'",
default="(.*)",
)
parser.add_argument(
"-vk",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need a short-hand for that:

Suggested change
"-vk",

I see this more being used in scripts where the long-form is more descriptive.

"--version-key",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think version-attr fits better with how nix language names things:

Suggested change
"--version-key",
"--version-attr",

help="Key of attribute that holds the version",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="Key of attribute that holds the version",
help="Name of attribute that holds the version",

default="version",
)
parser.add_argument(
"--run",
action="store_true",
Expand Down Expand Up @@ -107,6 +115,7 @@ def parse_args(args: list[str]) -> Options:
attribute=a.attribute,
test=a.test,
version_regex=a.version_regex,
version_key=a.version_key,
review=a.review,
format=a.format,
override_filename=a.override_filename,
Expand Down
14 changes: 11 additions & 3 deletions nix_update/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def __post_init__(


def eval_expression(
escaped_import_path: str, attr: str, flake: bool, system: str | None
escaped_import_path: str,
attr: str,
flake: bool,
version_key: str,
system: str | None,
) -> str:
system = f'"{system}"' if system else "builtins.currentSystem"

Expand Down Expand Up @@ -135,7 +139,7 @@ def eval_expression(
sanitizePosition (builtins.unsafeGetAttrPos "src" pkg);
in {{
name = pkg.name;
old_version = pkg.version or (builtins.parseDrvName pkg.name).version;
old_version = pkg.{version_key} or (builtins.parseDrvName pkg.name).{version_key};
inherit raw_version_position;
filename = position.file;
line = position.line;
Expand Down Expand Up @@ -168,7 +172,11 @@ def eval_expression(

def eval_attr(opts: Options) -> Package:
expr = eval_expression(
opts.escaped_import_path, opts.escaped_attribute, opts.flake, opts.system
opts.escaped_import_path,
opts.escaped_attribute,
opts.flake,
opts.version_key,
opts.system,
)
cmd = [
"nix",
Expand Down
1 change: 1 addition & 0 deletions nix_update/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Options:
version: str = "stable"
version_preference: VersionPreference = VersionPreference.STABLE
version_regex: str = "(.*)"
version_key: str = "version"
import_path: str = os.getcwd()
override_filename: str | None = None
url: str | None = None
Expand Down
29 changes: 29 additions & 0 deletions tests/test_versionkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import subprocess

import conftest

from nix_update.options import Options
from nix_update.update import update


def test_update(helpers: conftest.Helpers) -> None:
with helpers.testpkgs() as path:
opts = Options(
attribute="versionkey", import_path=str(path), version_key="immich_version"
)
update(opts)
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"versionkey.immich_version",
],
text=True,
stdout=subprocess.PIPE,
).stdout.strip()
assert tuple(map(int, version.split("."))) >= (1, 91, 0)
1 change: 1 addition & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix { };
savanna = pkgs.python3.pkgs.callPackage ./savanna.nix { };
npm = pkgs.callPackage ./npm.nix { };
versionkey = pkgs.callPackage ./versionkey.nix { };
}
22 changes: 22 additions & 0 deletions tests/testpkgs/versionkey.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ buildNpmPackage
, fetchFromGitHub
}:
buildNpmPackage rec {
pname = "immich-cli";
# version of immich and immich cli differes
version = (builtins.fromJSON (builtins.readFile "${src}/cli/package.json")).version;

immich_version = "1.91.0";
src = fetchFromGitHub {
owner = "immich-app";
repo = "immich";
rev = "v${immich_version}";
hash = "sha256-tFaa2rN4iGMlrPjHqSbMOE2xbyJh7Ro+Fm8j0+wa1MM=";
};

npmDepsHash = "sha256-NvU+v8MrwPK6q8RdVEHhzi5g6qRRmdTtInf7o2E5y6Y=";

postPatch = ''
cd cli
'';
}
Loading