Merge pull request #132 from Mic92/readability

improve readability of eval expression
This commit is contained in:
Jörg Thalheim 2023-01-08 18:17:51 +01:00 committed by GitHub
commit 4db8142775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
import json
from dataclasses import InitVar, dataclass, field
from textwrap import dedent, indent
from typing import Any, Dict, List, Optional
from urllib.parse import ParseResult, urlparse
@ -54,21 +55,23 @@ class Package:
def eval_expression(import_path: str, attr: str, flake: bool) -> str:
let_bindings = (
f"""inherit (builtins) currentSystem getFlake stringLength substring;
flake = getFlake "{import_path}";
pkg = flake.packages.${{currentSystem}}.{attr} or flake.{attr};
inherit (flake) outPath;
outPathLen = stringLength outPath;
sanitizePosition = {{ file, ... }}@pos:
assert substring 0 outPathLen file == outPath;
pos // {{ file = "{import_path}" + substring outPathLen (stringLength file - outPathLen) file; }};"""
if flake
else f"""
inputs = if (builtins.functionArgs (import {import_path})) ? overlays then {{ overlays = [ ]; }} else {{ }};
pkg = (import {import_path} inputs).{attr};
sanitizePosition = x: x;"""
)
if flake:
let_bindings = f"""
inherit (builtins) currentSystem getFlake stringLength substring;
flake = getFlake "{import_path}";
pkg = flake.packages.${{currentSystem}}.{attr} or flake.{attr};
inherit (flake) outPath;
outPathLen = stringLength outPath;
sanitizePosition = {{ file, ... }}@pos:
assert substring 0 outPathLen file == outPath;
pos // {{ file = "{import_path}" + substring outPathLen (stringLength file - outPathLen) file; }};
"""
else:
let_bindings = f"""
inputs = if (builtins.functionArgs (import {import_path})) ? overlays then {{ overlays = [ ]; }} else {{ }};
pkg = (import {import_path} inputs).{attr};
sanitizePosition = x: x;
"""
has_update_script = (
"false" if flake else "pkg.passthru.updateScript or null != null"
@ -76,7 +79,7 @@ def eval_expression(import_path: str, attr: str, flake: bool) -> str:
return f"""
let
{let_bindings}
{indent(dedent(let_bindings), " ")}
raw_version_position = sanitizePosition (builtins.unsafeGetAttrPos "version" pkg);
position = if pkg ? isRubyGem then