Skip to content

Commit

Permalink
[MISC] Fix build-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aedial committed Apr 27, 2023
1 parent 6d62b50 commit 3436fef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This module is intended to be used by developers as a helper for using NovelAI's
| Pypi | [![PyPI](https://img.shields.io/pypi/v/novelai-api)](https://pypi.org/project/novelai-api) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/novelai-api)](https://pypi.org/project/novelai-api) [![PyPI - License](https://img.shields.io/pypi/l/novelai-api)](https://pypi.org/project/novelai-api/) [![PyPI - Format](https://img.shields.io/pypi/format/novelai-api)](https://pypi.org/project/novelai-api/) |
| Quality checking | [![Python package](https://github.com/Aedial/novelai-api/actions/workflows/python-package.yml/badge.svg)](https://github.com/Aedial/novelai-api/actions/workflows/python-package.yml) [![Python package](https://github.com/Aedial/novelai-api/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Aedial/novelai-api/actions/workflows/codeql-analysis.yml) [![linting: pylint](https://img.shields.io/badge/linting-pylint-yellowgreen)](https://github.com/PyCQA/pylint) [![security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) |
| Stats | [![GitHub top language](https://img.shields.io/github/languages/top/Aedial/novelai-api)](https://github.com/Aedial/novelai-api/search?l=python) ![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/Aedial/novelai-api) ![GitHub repo size](https://img.shields.io/github/repo-size/Aedial/novelai-api) ![GitHub issues](https://img.shields.io/github/issues-raw/Aedial/novelai-api) ![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/Aedial/novelai-api) |
| Activity | ![GitHub last commit](https://img.shields.io/github/last-commit/Aedial/novelai-api) ![GitHub commits since tagged version](https://img.shields.io/github/commits-since/Aedial/novelai-api/v0.10.5) ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/Aedial/novelai-api) |
| Activity | ![GitHub last commit](https://img.shields.io/github/last-commit/Aedial/novelai-api) ![GitHub commits since tagged version](https://img.shields.io/github/commits-since/Aedial/novelai-api/v0.11.6) ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/Aedial/novelai-api) |


# Usage
Expand Down
64 changes: 45 additions & 19 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pathlib
import re
import shutil

import nox
Expand Down Expand Up @@ -75,9 +76,9 @@ def test_api(session: nox.Session):
session.run("npm", "install", "fflate", external=True)

if session.posargs:
session.run("pytest", "--tb=short", "-n", "auto", *(f"tests/api/{e}" for e in session.posargs))
session.run("pytest", "--tb=short", *(f"tests/api/{e}" for e in session.posargs))
else:
session.run("pytest", "--tb=short", "-n", "auto", "tests/api/")
session.run("pytest", "--tb=short", "tests/api/")


@nox.session()
Expand All @@ -97,28 +98,53 @@ def run(session: nox.Session):
@nox.session(name="build-docs")
def build_docs(session: nox.Session):
docs_path = pathlib.Path(__file__).parent / "docs"
source_path = docs_path / "source"

install_package(session)
session.install("-r", str(docs_path / "requirements.txt"))

paths = [pathlib.Path(path) for path in session.posargs]
if not paths:
raise ValueError("No path provided (put the path(s) after the --)")
with session.chdir(docs_path):
session.run("make", "html", external=True)

for path in paths:
if not path.exists():
raise ValueError(f"Path {path.resolve()} does not exist")

old_files_in_source = set(sorted(source_path.iterdir()))
for path in paths:
session.run("sphinx-apidoc", "-o", str(source_path.resolve()), "-Te", "-d", "2", str(path.resolve()))
new_files_in_source = set(sorted(source_path.iterdir()))
@nox.session(name="bump-version")
def bump_version(session: nox.Session):
if len(session.posargs) < 1:
raise ValueError("Expected `nox -s bump-version major|minor|patch`")

source_diff = new_files_in_source - old_files_in_source
if source_diff:
print("New files generated:", ", ".join(f"'{f}'" for f in source_diff))
print("Update the docs accordingly")
bump = session.posargs[0]

with session.chdir(docs_path):
session.run("make", "html", external=True)
if bump not in ("major", "minor", "patch"):
raise ValueError(f"Expected bump rule to be 'major', 'minor', or 'patch', got '{bump}'")

# Check for staged files (you don't want to accidentally commit them)
staged_files = session.run("git", "diff", "--name-only", "--cached", external=True)
if staged_files:
raise RuntimeError(f"Staged files, commit them before bumping version:\n{staged_files}")

# Bump the pyproject.toml and get the versions
session.install("poetry")
current_version = session.run("poetry", "version", "-s", silent=True).strip()
session.run("poetry", "version", bump)
bumped_version = session.run("poetry", "version", "-s", silent=True).strip()

# Modify the version in README's badges
rgx_badge = re.compile(r"(https://img.shields.io[^)]+?)(v\d+(?:\.\d+(?:\.\d+)?)?)")
with open("README.md", "w+", encoding="utf-8") as f:
readme = f.read()
readme = rgx_badge.sub(f"\\1v{bumped_version}", readme)

f.truncate(0)
f.seek(0)
f.write(readme)

# Commit the bump
commit_message = f"[MISC] Bump version - {bump}: {current_version} -> {bumped_version}"
session.run("git", "commit", commit_message, external=True)

# Commit the tag
session.run("git", "tag", "-a", f"v{bumped_version}", external=True)

session.log(
f"You can now push the commit and the tag with `git push origin v{bumped_version}`.\n"
"Ensure you're really ready to push with `git status` and `git log`."
)

0 comments on commit 3436fef

Please sign in to comment.