Files
claude-config/bin/project_config/main.py
T

116 lines
6.0 KiB
Python

"""Print the paths a project's config sets.
usage: project-config [<setting>]
Run inside a checkout, or a worktree of one. It finds the config that names the checkout,
~/.claude-projects/<key>/config.toml, and prints each setting and its absolute path, as in
`plan: /home/me/code/acme/todo.md`. Given a setting, it prints only that setting's path.
The config is TOML. `checkout` names the project's main checkout, and every setting in the example below is a path:
"checkout:<path>" inside the checkout, "folder:<path>" inside the config's own folder, or one that
starts with / or ~/. In a worktree, a "checkout:" path is the worktree's own file.
checkout = "~/code/acme-billing"
plan = "folder:todo.md"
working-rules = "folder:AGENTS.md"
decisions = "folder:docs/decisions.md"
score-anchors = "folder:docs/score-anchors.md"
comprehension-history = "folder:docs/comprehension-history.md"
reading-cost = "folder:docs/reading-cost.md"
changelog = "checkout:CHANGELOG.md"
goals-and-audience = "folder:goals.md"
principles = "folder:principles"
`env-file` is an optional setting: the file of KEY=value credentials the project's tools read,
a path that starts with / or ~/ and lies outside the checkout and the config's folder, such as
"~/.claude-projects/acme-billing.env". Where it is left out, the project has no credentials file, and
a tool that needs one fails naming the setting. It is printed with the others only where it is set.
`wait-for-approval` is an optional setting, true or false: whether `chunk-pr merge` waits for a human's
approval of the pull request, as its branch protection requires on GitHub, before it merges. Left out, it is
false. `project-config wait-for-approval` prints true or false; the full listing shows the setting only where
it is true.
Four optional settings say how the project writes, for a project whose owners don't write as
~/.claude/CLAUDE.md does:
language = "Swedish"
commit-title = "PAY-{id} - {summary}"
pull-request-title = "PAY-{id} - {summary}"
branch = "feature/PAY-{id}"
`language` names the language of the project's prose by its English name; left out, it is English.
The others are templates. {summary} is the one line that says what a commit or pull request does,
and {id} is the ID of the chunk's first plan item. A title template holds {summary} once and {id}
at most once; left out, it is "{summary}". `branch` holds {id} once, among letters, digits and
. _ / -; left out, it is "item-{id}". A template has no way to write a literal { or }.
`project-config <setting>` prints each one's value, and the full listing shows it only where the
config sets it to something else than it is when left out.
Exit 1, naming each fault on stderr, where no config names the checkout, or its config leaves out a
setting, names an unknown one, misspells a path, a language or a template, or sets `wait-for-approval` to anything but true or
false, and where `env-file` is asked for and not set. No path setting has a default.
"""
import pathlib
import sys
from project_config.settings import CONFIG, ENV_FILE, load, OPTIONAL, ProjectConfig, SETTINGS, WAIT_FOR_APPROVAL
from failure import Failure
from git_cli.git_repo import git, Git
from project_config import templates
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if len(argv) > 1 or (argv and argv[0] not in (*SETTINGS, *OPTIONAL)):
print(f"usage: project-config [<setting>], where <setting> is one of {', '.join((*SETTINGS, *OPTIONAL))}", file=sys.stderr)
return 1
config = located(None, pathlib.Path.home())
if isinstance(config, Failure):
print(config.reason, file=sys.stderr)
return 1
if argv == [WAIT_FOR_APPROVAL]:
print("true" if config.wait_for_approval else "false")
return 0
if argv and argv[0] in config.texts:
print(config.texts[argv[0]])
return 0
paths = {**config.paths, **({ENV_FILE: config.env_file} if config.env_file else {})}
if argv and argv[0] not in paths:
print(f"{config.folder / CONFIG} sets no `{ENV_FILE}`", file=sys.stderr)
return 1
defaults = ProjectConfig(config.key, config.folder, config.top, {}).texts
listed = [f"{name}: {path}" for name, path in paths.items()] + ([f"{WAIT_FOR_APPROVAL}: true"] if config.wait_for_approval else []) + [f"{name}: {text}" for name, text in config.texts.items() if text != defaults[name]]
print(paths[argv[0]] if argv else "\n".join(listed))
return 0
def located(directory: str | None, home: pathlib.Path, run: Git = git) -> ProjectConfig | Failure:
"""The config under home of the checkout that holds directory, else the current directory."""
found = run("rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir", cwd=directory)
if isinstance(found, Failure):
return found
if len(lines := found.splitlines()) != 2:
return Failure(f"git rev-parse printed {found!r}, not a checkout's top folder and git directory on two lines")
# The common dir is the main checkout's .git, whichever worktree runs this.
return load(pathlib.Path(lines[0]), pathlib.Path(lines[1]).parent, home)
def commit_title(directory: str | None, home: pathlib.Path, run: Git = git) -> str:
"""The `commit-title` template of the project whose checkout holds directory, else the current directory. A bare summary where no config names the checkout or its config cannot be read, as in a third party's clone, so every title there is read as it stands."""
config = located(directory, home, run)
return templates.TITLE if isinstance(config, Failure) else config.commit_title
def project() -> ProjectConfig | Failure:
"""The config of the project whose checkout holds the current directory, under the user's home."""
return located(None, pathlib.Path.home())
def goals_file(directory: str, home: pathlib.Path) -> pathlib.Path | Failure:
"""The file holding the premises, `## Goals` and `## Audience`, of the project whose checkout holds directory, as its config under home names it."""
config = located(directory, home)
return config if isinstance(config, Failure) else config.paths["goals-and-audience"]