Skip to content

Commit

Permalink
Prebuild documentation (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored May 6, 2024
1 parent 0f2fa02 commit 49b5f68
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 68 deletions.
10 changes: 10 additions & 0 deletions betty/_package/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from betty.documentation import _prebuild_documentation
from betty.extension.webpack import _prebuild_webpack_assets


async def prebuild() -> None:
"""
Prebuild assets for inclusion in package builds.
"""
await _prebuild_webpack_assets()
await _prebuild_documentation()
28 changes: 2 additions & 26 deletions betty/_package/pyinstaller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,9 @@
from PyInstaller.building.build_main import Analysis
from setuptools import find_packages

from betty._package import prebuild
from betty._package.pyinstaller.hooks import HOOKS_DIRECTORY_PATH
from betty.app import App
from betty.app.extension import discover_extension_types
from betty.extension.webpack import (
Webpack,
WebpackEntrypointProvider,
)
from betty.fs import ROOT_DIRECTORY_PATH
from betty.job import Context


async def prebuild_webpack_assets() -> None:
"""
Prebuild Webpack assets for inclusion in package builds.
"""
job_context = Context()
async with App.new_temporary() as app, app:
app.project.configuration.extensions.enable(Webpack)
webpack = app.extensions[Webpack]
app.project.configuration.extensions.enable(
*{
extension_type
for extension_type in discover_extension_types()
if issubclass(extension_type, WebpackEntrypointProvider)
}
)
await webpack.prebuild(job_context=job_context)


async def a_pyz_exe_coll() -> tuple[Analysis, PYZ, EXE, COLLECT]:
Expand All @@ -50,7 +26,7 @@ async def a_pyz_exe_coll() -> tuple[Analysis, PYZ, EXE, COLLECT]:
else:
raise RuntimeError(f"Unsupported platform {sys.platform}.")

await prebuild_webpack_assets()
await prebuild()
block_cipher = None
datas = []
data_file_path_patterns = [
Expand Down
79 changes: 38 additions & 41 deletions betty/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@
"""

import asyncio
import logging
import shutil
from contextlib import suppress, AsyncExitStack
from contextlib import AsyncExitStack
from pathlib import Path
from subprocess import CalledProcessError
from tempfile import TemporaryDirectory

from aiofiles.os import makedirs

from betty import serve
from betty import serve, fs
from betty.fs import ROOT_DIRECTORY_PATH
from betty.locale import Str, Localizer
from betty.serve import Server, NoPublicUrlBecauseServerNotStartedError
from betty.subprocess import run_process


async def _build_cache(cache_directory_path: Path) -> Path:
cache_directory_path /= "docs"
async def _prebuild_documentation() -> None:
await _build(fs.PREBUILT_ASSETS_DIRECTORY_PATH / "documentation")


async def _ensure_documentation_directory(cache_directory_path: Path) -> Path:
if (fs.PREBUILT_ASSETS_DIRECTORY_PATH / "documentation").exists():
return fs.PREBUILT_ASSETS_DIRECTORY_PATH / "documentation"
cache_directory_path /= "documentation"
if not cache_directory_path.exists():
await _build(cache_directory_path)
return cache_directory_path


async def _build(output_directory_path: Path) -> None:
with suppress(FileExistsError):
await makedirs(output_directory_path)
await makedirs(output_directory_path, exist_ok=True)
with TemporaryDirectory() as working_directory_path_str:
working_directory_path = Path(working_directory_path_str)
# sphinx-apidoc must output to the documentation directory, but because we do not want
Expand All @@ -40,38 +43,30 @@ async def _build(output_directory_path: Path) -> None:
ROOT_DIRECTORY_PATH / "documentation",
source_directory_path,
)
try:

await run_process(
[
"sphinx-apidoc",
"--force",
"--separate",
"-d",
"999",
"-o",
str(source_directory_path),
str(ROOT_DIRECTORY_PATH / "betty"),
str(ROOT_DIRECTORY_PATH / "betty" / "tests"),
],
cwd=working_directory_path,
)
await run_process(
[
"sphinx-build",
"-j",
"auto",
"-b",
"dirhtml",
str(source_directory_path),
str(output_directory_path),
],
cwd=working_directory_path,
)
except CalledProcessError as e:
if e.stderr is not None:
logging.getLogger().error(e.stderr)
raise
await run_process(
[
"sphinx-apidoc",
"--force",
"--separate",
"-d",
"999",
"-o",
str(source_directory_path),
str(ROOT_DIRECTORY_PATH / "betty"),
str(ROOT_DIRECTORY_PATH / "betty" / "tests"),
],
cwd=working_directory_path,
)
await run_process(
[
"sphinx-build",
"-b",
"dirhtml",
str(source_directory_path),
str(output_directory_path),
],
cwd=working_directory_path,
)


class DocumentationServer(Server):
Expand All @@ -98,7 +93,9 @@ def public_url(self) -> str:

async def start(self) -> None:
await super().start()
www_directory_path = await _build_cache(self._cache_directory_path)
www_directory_path = await _ensure_documentation_directory(
self._cache_directory_path
)
self._server = serve.BuiltinServer(
www_directory_path, localizer=self._localizer
)
Expand Down
21 changes: 20 additions & 1 deletion betty/extension/webpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

from betty import fs
from betty._npm import NpmRequirement, NpmUnavailable
from betty.app.extension import Extension
from betty.app import App
from betty.app.extension import Extension, discover_extension_types
from betty.extension.webpack import build
from betty.extension.webpack.build import webpack_build_id
from betty.extension.webpack.jinja2.filter import FILTERS
Expand All @@ -43,6 +44,24 @@ def _prebuilt_webpack_build_directory_path(
)


async def _prebuild_webpack_assets() -> None:
"""
Prebuild Webpack assets for inclusion in package builds.
"""
job_context = Context()
async with App.new_temporary() as app, app:
app.project.configuration.extensions.enable(Webpack)
webpack = app.extensions[Webpack]
app.project.configuration.extensions.enable(
*{
extension_type
for extension_type in discover_extension_types()
if issubclass(extension_type, WebpackEntrypointProvider)
}
)
await webpack.prebuild(job_context=job_context)


class WebpackEntrypointProvider:
@classmethod
def webpack_entrypoint_directory_path(cls) -> Path:
Expand Down
2 changes: 2 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -Eeuo pipefail

cd "$(dirname "$0")/.."

./bin/clean-build

if [ -z "${BETTY_TEST_SKIP_SHELLCHECK-}" ]; then
./bin/test-shellcheck
fi
Expand Down

0 comments on commit 49b5f68

Please sign in to comment.