Skip to content

Commit

Permalink
BREAKING CHANGE: use glob patterns for package identification
Browse files Browse the repository at this point in the history
  • Loading branch information
AGiantSquid committed Aug 15, 2023
1 parent 947b32a commit f87adfa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/monas/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def init(*, version: str, python_version: str) -> None:
Args:
path: The path to the `pyproject.toml` file.
"""
default_package_dirs = ["packages"]
mono_settings = {
"packages": ["packages/"],
"packages": [f"{pb}/*" for pb in default_package_dirs],
"version": version,
"python-version": python_version,
}
Expand All @@ -39,7 +40,7 @@ def init(*, version: str, python_version: str) -> None:
info(f"{verb} {pyproject_toml.name}")
doc.setdefault("tool", {}).setdefault("monas", {}).update(mono_settings)
pyproject_toml.write_text(tomlkit.dumps(doc))
for p in mono_settings["packages"]:
for p in default_package_dirs:
p = Path(p)
if not p.exists():
info(f"Creating {p.name}")
Expand Down
13 changes: 8 additions & 5 deletions src/monas/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def new(
If location isn't given, it defaults to the first location of `packages` config.
"""
if location is None:
location = config.package_paths[0]
parent_dir = Path(config.default_package_dir)
if location is not None:
parent_dir = Path(location)

if any(package == pkg.name for pkg in config.iter_packages()):
raise click.BadParameter(f"{package} already exists")
package_path = Path(location, package).absolute()

package_path = (parent_dir / package).absolute()
repo = config.get_repo()

default_values = {
Expand Down Expand Up @@ -63,5 +66,5 @@ def new(
f.write(metadata_contents)
info("Creating project files")
PyPackage.create(config, package_path, inputs)
if location not in config.package_paths:
config.add_package_path(location)

config.add_package_path(parent_dir)
28 changes: 20 additions & 8 deletions src/monas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,35 @@ def python_version(self) -> str:
"python-version", ".".join(map(str, sys.version_info[:2]))
)

def add_package_path(self, path: str) -> None:
"""Add a package path to the configuration"""
self._tool.setdefault("packages", []).append(path.rstrip("/") + "/")
@property
def default_package_dir(self) -> Path:
"""Default directory to find or add mono-repo packages."""
first_pkg_path = self.package_paths[0]
if "*" or "?" in first_pkg_path.name:
return first_pkg_path.parent
return first_pkg_path

def add_package_path(self, path: Path) -> None:
"""Add a package-containing directory path to the configuration"""
relative_path = path.relative_to(self.path) if path.is_absolute() else path
if relative_path.name != "*":
relative_path = relative_path / "*"
if (self.path / relative_path) in self.package_paths:
return
self._tool.setdefault("packages", []).append(relative_path.as_posix())
TOMLFile(self.path / "pyproject.toml").write(self._pyproject)

def iter_packages(self) -> Iterable[PyPackage]:
"""Iterate over the packages in the monorepo"""
from monas.project import PyPackage

for p in self.package_paths:
for package in p.iterdir():
child_pkgs = list(p.parent.glob(p.name))
for package in child_pkgs:
if package.is_dir():
pypackage = PyPackage(self, package)
if not pypackage.metadata.path.is_file():
# directory has no python metadata file, ignore
continue
yield pypackage
if pypackage.metadata.path.is_file():
yield pypackage


pass_config = click.make_pass_decorator(Config, ensure=True)
4 changes: 2 additions & 2 deletions tests/cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def test_init_with_default_python_version(cli_run, tmp_path, python_version):
tmp_path.joinpath("pyproject.toml").read_text()
== f"""\
[tool.monas]
packages = ["packages/"]
packages = ["packages/*"]
version = "0.0.0"
python-version = "{python_version}"
"""
Expand All @@ -17,7 +17,7 @@ def test_init_with_given_python_version(cli_run, tmp_path):
tmp_path.joinpath("pyproject.toml").read_text()
== """\
[tool.monas]
packages = ["packages/"]
packages = ["packages/*"]
version = "0.0.0"
python-version = "3.9"
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_new_package_under_non_default_location(project, cli_run, python_version
project.joinpath("pyproject.toml").read_text()
== f"""\
[tool.monas]
packages = ["packages/", "others/"]
packages = ["packages/*", "others/*"]
version = "0.0.0"
python-version = "{python_version}"
"""
Expand Down

0 comments on commit f87adfa

Please sign in to comment.