Skip to content

Commit

Permalink
refac(back): #894 split function
Browse files Browse the repository at this point in the history
- Create a Config composite type
- Split main function into some steps
- The reason for this change is that
  we are just at the limit of the length
  of the `cli` function and I cannot add
  any more code without the linter yelling at me
  • Loading branch information
kamadorueda committed Sep 6, 2022
1 parent 4c3a326 commit d9ff396
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions makes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
bin = [inputs.nixpkgs.hello];
};
makes = {
bin = [inputs.nixpkgs.just];
source = [outputs."/cli/pypi"];
};
};
Expand Down
56 changes: 43 additions & 13 deletions src/cli/main/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Callable,
Dict,
List,
NamedTuple,
Optional,
Set,
Tuple,
Expand Down Expand Up @@ -419,7 +420,12 @@ def _get_head(src: str) -> str:
return head


def _get_config(head: str) -> Dict[str, Any]:
class Config(NamedTuple):
attrs: List[str]
cache: List[Dict[str, str]]


def _get_config(head: str) -> Config:
CON.out()
CON.rule("Building project configuration")
CON.out()
Expand All @@ -440,7 +446,9 @@ def _get_config(head: str) -> Dict[str, Any]:

if code == 0:
with open(out, encoding="utf-8") as file:
return json.load(file)
config: Dict[str, Any] = json.load(file)

return Config(attrs=config["outputs"], cache=config["cache"])

raise SystemExit(code)

Expand Down Expand Up @@ -762,10 +770,25 @@ def cli(args: List[str]) -> None:
_help_and_exit_base()

head: str = _get_head(src)
config: Dict[str, Any] = _get_config(head)
attrs: List[str] = config["outputs"]
cache: List[Dict[str, str]] = config["cache"]
config: Config = _get_config(head)

args, attr = _cli_get_args_and_attr(args, config.attrs, src)

out: str = join(MAKES_DIR, f"out{attr.replace('/', '-')}")
code = _cli_build(attr, config, head, out, src)

if code == 0:
cache_push(config.cache, out)
execute_action(args[3:], head, out)

raise SystemExit(code)


def _cli_get_args_and_attr(
args: List[str],
attrs: List[str],
src: str,
) -> Tuple[List[str], str]:
if args[2:]:
attr: str = args[2]
elif CON.is_terminal:
Expand All @@ -774,34 +797,41 @@ def cli(args: List[str]) -> None:
else:
_help_and_exit_with_src_no_tty(src, attrs)

return args, attr


def _cli_build(
attr: str,
config: Config,
head: str,
out: str,
src: str,
) -> int:
CON.out()
CON.rule(f"Building {attr}")
CON.out()
if attr not in attrs:
if attr not in config.attrs:
CON.print(f"We can't proceed with OUTPUT: {attr}", justify="center")
CON.print("It is not a valid project output", justify="center")
CON.print()
CON.print("Please see the correct usage below", justify="center")
_help_and_exit_with_src_no_tty(src, attrs)
_help_and_exit_with_src_no_tty(src, config.attrs)

out: str = join(MAKES_DIR, f"out{attr.replace('/', '-')}")
code = _run(
args=_nix_build(
attr=f'config.outputs."{attr}"'
if NIX_STABLE
else f'{head}#__makes__."config:outputs:{attr}"',
cache=cache,
cache=config.cache,
head=head,
out=out,
),
env=None if NIX_STABLE else dict(HOME=environ["HOME_IMPURE"]),
stderr=None,
stdout=None,
)
if code == 0:
cache_push(cache, out)
execute_action(args[3:], head, out)
raise SystemExit(code)

return code


def execute_action(args: List[str], head: str, out: str) -> None:
Expand Down

0 comments on commit d9ff396

Please sign in to comment.