diff --git a/zmk/commands/init.py b/zmk/commands/init.py index 7e04c17..c56d77c 100644 --- a/zmk/commands/init.py +++ b/zmk/commands/init.py @@ -17,7 +17,7 @@ from rich.table import Table from ..build import BuildMatrix -from ..config import Config, get_config +from ..config import Config, get_config, set_context_repo from ..exceptions import FatalError from ..prompt import UrlPrompt from ..repo import Repo, find_containing_repo, is_repo @@ -66,6 +66,9 @@ def init( repo = Repo(Path() / name) + # Make sure everything we do after this point applies to this new repo. + set_context_repo(ctx, repo) + if revision: try: repo.ensure_west_ready() @@ -98,6 +101,14 @@ def init( _add_first_keyboard(ctx, console, repo) + if repo.path != cfg.home_path: + console.print() + console.print("[yellow]The new repo is not your default repo.") + console.print('Run the following command before any other "zmk" commands:') + console.print() + console.print(f' [green]cd "{repo.path}"') + console.print() + # TODO: add some help for how to commit and push changes diff --git a/zmk/config.py b/zmk/config.py index e17c9a9..7cd61b3 100644 --- a/zmk/config.py +++ b/zmk/config.py @@ -33,6 +33,9 @@ class Config: path: Path force_home: bool + override_repo_path: Path | None = None + "Set this to override the path used for get_repo() without changing home_path." + def __init__(self, path: Path | None, force_home=False): self.path = path or _default_config_path() self.force_home = force_home @@ -109,7 +112,13 @@ def get_repo(self) -> Repo: Exits the program if neither the current directory nor the home path point to a valid directory. + + If override_repo_path is set, this takes priority over all other methods + of finding the repo. """ + if self.override_repo_path: + return Repo(self.override_repo_path) + if not self.force_home: if home := find_containing_repo(): return Repo(home) @@ -133,5 +142,19 @@ def get_config(ctx: typer.Context) -> Config: return cfg +def set_context_repo(ctx: typer.Context, repo: Repo) -> None: + """ + Set the override_repo_path on the Config object for the given context to + point to a given repo. All subsequent calls to get_config(ctx).get_repo() + will return a Repo instance with the same path as the given one. + + This should be used when a command wants to apply changes to a specific repo + that isn't necessarily what get_config(ctx).get_repo() would normally use + (e.g. when creating a new repo with "zmk init"), and when that command + calls another command that gets its repo from the context. + """ + get_config(ctx).override_repo_path = repo.path + + def _default_config_path(): return Path(typer.get_app_dir("zmk", roaming=False)) / "zmk.ini"