Skip to content
Open
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
4 changes: 4 additions & 0 deletions app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from app.commands import check, download, progress, setup, verify
from app.commands.version import version
from app.configs.gitmastery_config import migrate_to_gitmastery_folder
from app.utils.click import ClickColor, CliContextKey, warn
from app.utils.version import Version
from app.version import __version__
Expand All @@ -28,6 +29,9 @@ def cli(ctx: click.Context, verbose: bool) -> None:
"""Git-Mastery app"""
ctx.ensure_object(dict)

if migrate_to_gitmastery_folder() is not None:
warn("Migrated .gitmastery.json and .gitmastery.log into .gitmastery/ folder.")

Comment on lines +32 to +34
ctx.obj[CliContextKey.VERBOSE] = verbose

current_version = Version.parse_version_string(__version__)
Expand Down
4 changes: 3 additions & 1 deletion app/commands/setup_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from app.commands.check.git import git
from app.commands.progress.constants import PROGRESS_LOCAL_FOLDER_NAME
from app.configs.gitmastery_config import GITMASTERY_FOLDER_NAME
from app.utils.click import error, info, invoke_command, prompt


Expand Down Expand Up @@ -35,8 +36,9 @@ def setup() -> None:
os.chdir(directory_path)

info("Setting up your local progress tracker...")
os.makedirs(GITMASTERY_FOLDER_NAME, exist_ok=True)
os.makedirs(PROGRESS_LOCAL_FOLDER_NAME, exist_ok=True)
with open(".gitmastery.json", "w") as gitmastery_file:
with open(f"{GITMASTERY_FOLDER_NAME}/config.json", "w") as gitmastery_file:
Comment on lines +39 to +41
gitmastery_file.write(
json.dumps(
{
Expand Down
34 changes: 29 additions & 5 deletions app/configs/gitmastery_config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Self, Type
from typing import Optional, Self, Type

from app.configs.utils import read_config
from app.configs.utils import find_root, read_config

GITMASTERY_CONFIG_NAME = ".gitmastery.json"
GITMASTERY_FOLDER_NAME = ".gitmastery"
GITMASTERY_CONFIG_NAME = "config.json"


@dataclass
Expand Down Expand Up @@ -36,12 +37,12 @@ def to_json(self) -> str:
)

def write(self) -> None:
with open(self.path / GITMASTERY_CONFIG_NAME, "w") as exercise_config_file:
with open(self.path / GITMASTERY_FOLDER_NAME / GITMASTERY_CONFIG_NAME, "w") as exercise_config_file:
exercise_config_file.write(self.to_json())

@classmethod
def read(cls: Type[Self], path: Path, cds: int) -> Self:
raw_config = read_config(path, GITMASTERY_CONFIG_NAME)
raw_config = read_config(path / GITMASTERY_FOLDER_NAME, GITMASTERY_CONFIG_NAME)

exercises_source_raw = raw_config.get("exercises_source", {})
return cls(
Expand All @@ -60,3 +61,26 @@ def read(cls: Type[Self], path: Path, cds: int) -> Self:
GIT_MASTERY_EXERCISES_SOURCE = GitMasteryConfig.ExercisesSource(
username="git-mastery", repository="exercises", branch="main"
)


def migrate_to_gitmastery_folder() -> Optional[Path]:
"""
If old-style layout is detected (.gitmastery.json at root, no .gitmastery/ folder),
migrate files into the new .gitmastery/ folder convention.
Returns the root path if migration occurred, otherwise None.
"""
old_root = find_root(".gitmastery.json")
if old_root is None:
return None

root, _ = old_root
new_dir = root / GITMASTERY_FOLDER_NAME
if new_dir.exists():
return None
Comment on lines +77 to +79

new_dir.mkdir()
(root / ".gitmastery.json").rename(new_dir / GITMASTERY_CONFIG_NAME)
old_log = root / ".gitmastery.log"
if old_log.exists():
old_log.rename(new_dir / "gitmastery.log")
return root
4 changes: 2 additions & 2 deletions app/hooks/in_gitmastery_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import click

from app.configs.gitmastery_config import GITMASTERY_CONFIG_NAME, GitMasteryConfig
from app.configs.gitmastery_config import GITMASTERY_CONFIG_NAME, GITMASTERY_FOLDER_NAME, GitMasteryConfig
from app.configs.utils import find_root
from app.hooks.utils import generate_cds_string
from app.utils.click import CliContextKey, error
Expand All @@ -18,7 +18,7 @@ def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
def wrapper(
ctx: click.Context, *args: Tuple[Any, ...], **kwargs: Dict[str, Any]
) -> Any:
root = find_root(GITMASTERY_CONFIG_NAME)
root = find_root(f"{GITMASTERY_FOLDER_NAME}/{GITMASTERY_CONFIG_NAME}")
if root is None:
error(
f"You are not in a Git-Mastery root folder. Navigate to an appropriate folder or use "
Expand Down
6 changes: 3 additions & 3 deletions app/logging/setup_logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import re

from app.configs.gitmastery_config import GITMASTERY_CONFIG_NAME
from app.configs.gitmastery_config import GITMASTERY_CONFIG_NAME, GITMASTERY_FOLDER_NAME
from app.configs.utils import find_root


Expand All @@ -10,11 +10,11 @@ def __init__(self) -> None:
super().__init__()

def emit(self, record: logging.LogRecord) -> None:
gitmastery_root = find_root(GITMASTERY_CONFIG_NAME)
gitmastery_root = find_root(f"{GITMASTERY_FOLDER_NAME}/{GITMASTERY_CONFIG_NAME}")
if gitmastery_root is None:
return

log_path = gitmastery_root[0] / ".gitmastery.log"
log_path = gitmastery_root[0] / GITMASTERY_FOLDER_NAME / "gitmastery.log"
handler = logging.FileHandler(log_path, mode="a")
# TODO: This feels inefficient for logging but I can't think of a good
# alternative
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/commands/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def test_setup(exercises_dir: Path) -> None:
"""
Test that setup creates the progress directory, progress.json, .gitmastery.json and .gitmastery.log
Test that setup creates the progress directory, progress.json, .gitmastery/config.json and .gitmastery/gitmastery.log
Setup command already called in conftest.py for test setup
"""
progress_dir = exercises_dir / "progress"
Expand All @@ -12,8 +12,8 @@ def test_setup(exercises_dir: Path) -> None:
progress_file = progress_dir / "progress.json"
assert progress_file.is_file(), f"Expected {progress_file} to exist"

config_file = exercises_dir / ".gitmastery.json"
config_file = exercises_dir / ".gitmastery" / "config.json"
assert config_file.is_file(), f"Expected {config_file} to exist"

log_file = exercises_dir / ".gitmastery.log"
log_file = exercises_dir / ".gitmastery" / "gitmastery.log"
assert log_file.is_file(), f"Expected {log_file} to exist"
Loading