Skip to content

Commit

Permalink
feat: Support Adding explicit package entries using option in `monas …
Browse files Browse the repository at this point in the history
…new` (#16)

* Add explicit package entry implementation for `monas new`

* Add backend fixtures for test_new_package_with_explicit_path
  • Loading branch information
HuM4NoiD committed Oct 25, 2023
1 parent a504c05 commit f67c48f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/monas/commands/new.py
Expand Up @@ -15,10 +15,23 @@
@click.command()
@click.argument("package", required=True)
@click.argument("location", required=False)
@click.option(
"-e",
"--explicit-package-entry",
is_flag=True,
default=False,
help="If specified, an explicit package entry "
+ "will be added to the list of packages",
)
@pass_config
@pass_context
def new(
ctx: click.Context, config: Config, *, package: str, location: str | None = None
ctx: click.Context,
config: Config,
*,
package: str,
location: str | None = None,
explicit_package_entry: bool,
):
"""Create a new <package> under <location>.
Expand Down Expand Up @@ -67,4 +80,7 @@ def new(
info("Creating project files")
PyPackage.create(config, package_path, inputs)

config.add_package_path(parent_dir)
if explicit_package_entry:
config.add_explicit_package_path(package_path)
else:
config.add_package_path(parent_dir)
18 changes: 17 additions & 1 deletion src/monas/config.py
Expand Up @@ -80,7 +80,12 @@ def python_version(self) -> str:

@property
def default_package_dir(self) -> Path:
"""Default directory to find or add mono-repo packages."""
"""
Default directory to find or add mono-repo packages.
If no directory is specified, will fallback to project root path
"""
if len(self.package_paths) == 0:
return self.path
first_pkg_path = self.package_paths[0]
if "*" or "?" in first_pkg_path.name:
return first_pkg_path.parent
Expand All @@ -96,6 +101,17 @@ def add_package_path(self, path: Path) -> None:
self._tool.setdefault("packages", []).append(relative_path.as_posix())
TOMLFile(self.path / "pyproject.toml").write(self._pyproject)

def add_explicit_package_path(self, package_path: Path) -> None:
relative_path = (
package_path.relative_to(self.path)
if package_path.is_absolute()
else package_path
)
if 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
Expand Down
27 changes: 27 additions & 0 deletions tests/cli/test_new.py
Expand Up @@ -149,3 +149,30 @@ def test_new_package_under_non_default_location(project, cli_run, python_version
python-version = "{python_version}"
"""
)


@pytest.mark.usefixtures("mock_input")
@pytest.mark.parametrize("backend", ["setuptools(pyproject.toml)"])
def test_new_package_with_explicit_path(project, cli_run, python_version):
"""
Create a new project with explicit path entry
"""
cli_run(["new", "-e", "first", "."], cwd=project, input="\n")
cli_run(["new", "-e", "second", "."], cwd=project, input="\n")
cli_run(["new", "-e", "third", "nested"], cwd=project, input="\n")
packages = [
("first", project.joinpath("first")),
("second", project.joinpath("second")),
("third", project.joinpath("nested/third")),
]
for _package_name, package_path in packages:
assert package_path.is_dir()
assert (
project.joinpath("pyproject.toml").read_text()
== f"""\
[tool.monas]
packages = ["packages/*", "first", "second", "nested/third"]
version = "0.0.0"
python-version = "{python_version}"
"""
)

0 comments on commit f67c48f

Please sign in to comment.