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

refac(back): #894 split function #895

Merged
merged 1 commit into from
Sep 6, 2022
Merged
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
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