improve readability of eval expression

This commit is contained in:
Jörg Thalheim 2023-01-08 08:41:48 +01:00
parent 3f68ceed20
commit 7412eff904

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