Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonycorletti committed Jun 22, 2023
1 parent ef1d04c commit 9022d72
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Check out the latest [issues](https://github.com/anthonycorletti/snok/issues) an

## ⚙️ Installation

After you've created your Python 3.11+ virtual environment, install Snok with:
After you've created your Python 3.11+ virtual environment in a new directory, install Snok:

```sh
pip install snok
Expand All @@ -75,7 +75,7 @@ pip install snok
Create a new package with:

```sh
snok new mypackage && cd mypackage
snok new mypackage
```

Snok uses `invoke` to manage tasks, like installing dependencies, running tests, and more.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ exclude =
[options.package_data]
snok =
py.typed
snok/templates/*
2 changes: 1 addition & 1 deletion snok/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""snok"""

__version__ = "0.0.2"
__version__ = "0.0.3"
21 changes: 9 additions & 12 deletions snok/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os

from typer import Argument, Option, Typer, echo

from snok import __version__
from snok.const import APP_DESC, APP_NAME, ProjectType
from snok.services.new import NewPackageService
from snok.utils import _get_default_output_dir

app = Typer(
name=APP_NAME,
Expand Down Expand Up @@ -43,26 +42,24 @@ def _new(
"--description",
help="The description of the project. Defaults to 'A new Python project.'",
),
output_dir: str = Option(
os.getcwd(),
"-o",
"--output-dir",
help="The output directory of the project. Defaults to the current directory.",
),
type: ProjectType = Option(
"package",
"-t",
"--type",
help="The type of project to create. Defaults to 'package'",
),
output_dir: str = Option(
_get_default_output_dir(),
"-o",
"--output-dir",
help="The output directory of the project. "
"Defaults to the same directory of the current virtual environment.",
),
) -> None:
new_project_service_dispatcher = {
ProjectType.package: NewPackageService,
}
new_project_service = new_project_service_dispatcher[type]()
new_project_service.create(
name=name,
desc=description,
type=type,
output_dir=output_dir,
name=name, desc=description, type=type, output_dir=output_dir
)
18 changes: 10 additions & 8 deletions snok/services/new.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.resources as resources
import os

from jinja2 import Template
Expand All @@ -7,7 +8,9 @@

class BaseNewService:
def __init__(self) -> None:
self.root_template_dir = "snok/templates"
import snok

self.root_template_dir = str(resources.files(snok)) + "/templates"

def create(
self,
Expand Down Expand Up @@ -76,16 +79,15 @@ def create(
shared_template_dir = f"{self.root_template_dir}/__shared"
package_template_dir = f"{self.root_template_dir}/__{type.value}"
tests_template_dir = f"{self.root_template_dir}/__{type.value}_tests"
root_output_dir = f"{output_dir}/{name}"
os.makedirs(root_output_dir, exist_ok=True)
os.makedirs(output_dir, exist_ok=True)
self._render_content_directory(
name=name,
desc=desc,
source_dir=shared_template_dir,
output_dir=root_output_dir,
output_dir=output_dir,
type=type,
)
package_root = f"{root_output_dir}/{name}"
package_root = f"{output_dir}/{name}"
os.makedirs(package_root, exist_ok=True)
self._render_content_directory(
name=name,
Expand All @@ -94,7 +96,7 @@ def create(
output_dir=package_root,
type=type,
)
package_test_root = f"{root_output_dir}/tests"
package_test_root = f"{output_dir}/tests"
os.makedirs(package_test_root, exist_ok=True)
self._render_content_directory(
name=name,
Expand All @@ -103,5 +105,5 @@ def create(
output_dir=package_test_root,
type=type,
)
if not os.path.exists(f"{root_output_dir}/.git"):
os.system(f"git init {root_output_dir} > /dev/null 2>&1")
if not os.path.exists(f"{output_dir}/.git"):
os.system(f"git init {output_dir} > /dev/null 2>&1")
6 changes: 6 additions & 0 deletions snok/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os


def _get_default_output_dir() -> str:
"""Returns the default output directory of the project."""
return os.path.dirname(os.getenv("VIRTUAL_ENV", os.getcwd()))
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def cli_runner() -> Generator:


@pytest.fixture(scope="function", autouse=True)
def setup_test_content_output_dir() -> Generator:
_dir = f"tmp/test_content_output_{os.getpid()}"
def setup_test_project_dir() -> Generator:
_dir = f"tmp/test_project_{os.getpid()}"
os.makedirs(_dir, exist_ok=True)
yield _dir
shutil.rmtree(_dir)
29 changes: 14 additions & 15 deletions tests/test_cli_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
from snok.cli import app


def test_new_package(cli_runner: CliRunner, setup_test_content_output_dir: str) -> None:
def test_new_package(cli_runner: CliRunner, setup_test_project_dir: str) -> None:
response = cli_runner.invoke(
app,
["new", "test_package", "-o", setup_test_content_output_dir],
["new", "test_package", "-o", setup_test_project_dir],
)
assert response.exit_code == 0
# assert the output directory exists
for path in [
f"{setup_test_content_output_dir}/test_package",
f"{setup_test_content_output_dir}/test_package/test_package",
f"{setup_test_content_output_dir}/test_package/tests",
f"{setup_test_content_output_dir}/test_package/.git",
f"{setup_test_content_output_dir}/test_package/README.md",
f"{setup_test_content_output_dir}/test_package/setup.cfg",
f"{setup_test_content_output_dir}/test_package/pyproject.toml",
f"{setup_test_content_output_dir}/test_package/test_package/__init__.py",
f"{setup_test_content_output_dir}/test_package/test_package/main.py",
f"{setup_test_content_output_dir}/test_package/tests/__init__.py",
f"{setup_test_content_output_dir}/test_package/tests/conftest.py",
f"{setup_test_content_output_dir}/test_package/tests/test_main.py",
f"{setup_test_content_output_dir}/test_package/tests/test_version.py",
f"{setup_test_project_dir}/test_package",
f"{setup_test_project_dir}/tests",
f"{setup_test_project_dir}/.git",
f"{setup_test_project_dir}/README.md",
f"{setup_test_project_dir}/setup.cfg",
f"{setup_test_project_dir}/pyproject.toml",
f"{setup_test_project_dir}/test_package/__init__.py",
f"{setup_test_project_dir}/test_package/main.py",
f"{setup_test_project_dir}/tests/__init__.py",
f"{setup_test_project_dir}/tests/conftest.py",
f"{setup_test_project_dir}/tests/test_main.py",
f"{setup_test_project_dir}/tests/test_version.py",
]:
assert os.path.exists(path)

Expand Down

1 comment on commit 9022d72

@vercel
Copy link

@vercel vercel bot commented on 9022d72 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.