Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support Adding explicit package entries using option in monas new #16

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
20 changes: 18 additions & 2 deletions src/monas/commands/new.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}"
"""
)