Skip to content

Commit

Permalink
✨ Add / update nox sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgraaf committed Jun 24, 2024
1 parent 921ef05 commit 4397284
Showing 1 changed file with 84 additions and 4 deletions.
88 changes: 84 additions & 4 deletions {{ cookiecutter.project_name }}/noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from pathlib import Path
from shutil import rmtree

import nox

Expand Down Expand Up @@ -33,6 +34,13 @@
ROOT_DIR = Path(__file__).resolve().parent
VENV_DIR = ROOT_DIR / ".venv"

DOCS_DIR = ROOT_DIR / "docs"
DOCS_SOURCE_DIR = DOCS_DIR
DOCS_BUILD_DIR = DOCS_DIR / "_build" / "html"

PACKAGE_BUILD_DIR = ROOT_DIR / "build"
PACKAGE_DIST_DIR = ROOT_DIR / "dist"


@nox.session
def dev(session: nox.Session) -> None:
Expand Down Expand Up @@ -86,14 +94,24 @@ def dev(session: nox.Session) -> None:
def cog(session: nox.Session) -> None:
"""Run cog."""
session.install("cogapp", "PyYAML", ".")
session.run("cog", *session.posargs, "-r", "noxfile.py")

cog_input_files = ["noxfile.py"]
for cog_input_file in cog_input_files:
session.run("cog", *session.posargs, "-r", cog_input_file)

session.notify("pre_commit", ["trailing-whitespace", "--files", *cog_input_files])


@nox.session
def pre_commit(session: nox.Session) -> None:
"""Run pre-commit hooks."""
# fmt: off
args = session.posargs or [
"--all-files", # run on all the files in the repo.
]
# fmt: on
session.install("pre-commit")
session.run("pre-commit", "run", *session.posargs, "--all-files")
session.run("pre-commit", "run", *args)


@nox.session(python=ALL_PYTHON_VERSIONS, tags=["tests"])
Expand Down Expand Up @@ -121,8 +139,70 @@ def docs(session: nox.Session) -> None:
"-b", builder, # selects a builder
"-d", str(Path(tmp_dir) / "doctrees"), # directory to save doctree pickles
"-D", "language=en", # set language
"docs", # source directory
str(Path("docs") / "_build" / "html"), # output directory
str(DOCS_SOURCE_DIR), # source directory
str(DOCS_BUILD_DIR), # output directory
)
# fmt: on
session.run("python", "-m", "doctest", "README.md")


@nox.session(name="docs-live", python=DOCS_PYTHON_VERSION)
def docs_live(session: nox.Session) -> None:
"""Builds and serves the docs with hot reloading on file changes."""
# fmt: off
args = session.posargs or [
"--open-browser", # open the browser after building documentation
str(DOCS_SOURCE_DIR), # source directory
str(DOCS_BUILD_DIR), # output directory
]
# fmt: on

session.install(".[docs]")
session.install("sphinx-autobuild")

session.run("sphinx-autobuild", *args)


@nox.session(name="clear-packages", python=False)
def clear_packages(session: nox.Session) -> None: # noqa: ARG001
"""Remove build and dist directories."""
if PACKAGE_BUILD_DIR.is_dir():
rmtree(PACKAGE_BUILD_DIR)
if PACKAGE_DIST_DIR.is_dir():
rmtree(PACKAGE_DIST_DIR)


@nox.session(name="build-package")
def build_package(session: nox.Session) -> None:
"""Builds the package, both as a source distribution (sdist) and as a wheel."""
args = session.posargs or ["--outdir", PACKAGE_DIST_DIR, ROOT_DIR]
session.install("build")
session.run("python", "-m", "build", *args)


@nox.session(name="upload-package")
def upload_package(session: nox.Session) -> None:
"""Builds the package, both as a source distribution (sdist) and as a wheel."""
session.install("twine")

# check whether the package's long description will render correctly on PyPI
session.run("twine", "check", "--strict", f"{PACKAGE_DIST_DIR.as_posix()}/*")

# upload the package(s) to (Test)PyPI
session.run("twine", "upload", *session.posargs, f"{PACKAGE_DIST_DIR.as_posix()}/*")


@nox.session(name="release-to-testpypi", python=False)
def release_to_testpypi(session: nox.Session) -> None:
"""Clears, builds and uploads the package to TestPyPI."""
session.notify("clear-packages")
session.notify("build-package")
session.notify("upload-package", ["--repository", "testpypi"])


@nox.session(name="release-to-pypi", python=False)
def release_to_pypi(session: nox.Session) -> None:
"""Clears, builds and uploads the package to PyPI."""
session.notify("clear-packages")
session.notify("build-package")
session.notify("upload-package")

1 comment on commit 4397284

@github-actions
Copy link

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.