From 40a49694ef8e6dde6e515d1a2b04af1836a0e068 Mon Sep 17 00:00:00 2001 From: Devansh Shukla Date: Fri, 5 Apr 2024 16:04:26 +0530 Subject: [PATCH] refactored variable config into `VersionedDocs.configure_vars` refactored building runtime into `VersionedDocs.run`. --- sphinx_versioned/__main__.py | 28 +++++-------- sphinx_versioned/build.py | 79 +++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/sphinx_versioned/__main__.py b/sphinx_versioned/__main__.py index 55353dc..5aa9d92 100755 --- a/sphinx_versioned/__main__.py +++ b/sphinx_versioned/__main__.py @@ -4,8 +4,7 @@ from loguru import logger as log from sphinx_versioned.build import VersionedDocs -from sphinx_versioned.sphinx_ import EventHandlers -from sphinx_versioned.lib import mp_sphinx_compatibility, parse_branch_selection +from sphinx_versioned.lib import parse_branch_selection app = typer.Typer(add_completion=False) @@ -123,32 +122,27 @@ def main( select_branch, exclude_branch = parse_branch_selection(branches) - EventHandlers.RESET_INTERSPHINX_MAPPING = reset_intersphinx_mapping - EventHandlers.FLYOUT_FLOATING_BADGE = floating_badge - - if reset_intersphinx_mapping: - log.warning("Forcing --no-prebuild") - prebuild = False - - if sphinx_compatibility: - mp_sphinx_compatibility() - - return VersionedDocs( + DocsBuilder = VersionedDocs( chdir=chdir, local_conf=local_conf, output_dir=output_dir, git_root=git_root, config={ - "prebuild_branches": prebuild, - "select_branch": select_branch, + "reset_intersphinx_mapping": reset_intersphinx_mapping, + "sphinx_compatibility": sphinx_compatibility, + "force_branches": force_branches, "exclude_branch": exclude_branch, + "floating_badge": floating_badge, + "select_branch": select_branch, + "prebuild": prebuild, "main_branch": main_branch, - "quite": quite, "verbose": verbose, - "force_branches": force_branches, + "quite": quite, }, ) + return DocsBuilder.run() + if __name__ == "__main__": app() diff --git a/sphinx_versioned/build.py b/sphinx_versioned/build.py index 1ac76c9..d92540f 100644 --- a/sphinx_versioned/build.py +++ b/sphinx_versioned/build.py @@ -9,7 +9,7 @@ from loguru import logger as log from sphinx_versioned.sphinx_ import EventHandlers -from sphinx_versioned.lib import TempDir, ConfigInject +from sphinx_versioned.lib import TempDir, ConfigInject, mp_sphinx_compatibility from sphinx_versioned.versions import GitVersions, BuiltVersions, PseudoBranch @@ -55,6 +55,7 @@ def __init__(self, chdir: str, local_conf: str, output_dir: str, git_root: str, # Read sphinx-conf.py variables self.read_conf() + self.configure_conf() self._versions_to_pre_build = [] self._versions_to_build = [] @@ -72,18 +73,6 @@ def __init__(self, chdir: str, local_conf: str, output_dir: str, git_root: str, self.config["main_branch"] = self.versions.active_branch.name else: self.config["main_branch"] = "main" - - self.prebuild() - - # Adds our extension to the sphinx-config - application.Config = ConfigInject - - self.build() - - # Adds a top-level `index.html` in `output_dir` which redirects to `output_dir`/`main-branch`/index.html - self._generate_top_level_index() - - print(f"\n\033[92m Successfully built {', '.join([x.name for x in self._built_version])} \033[0m") return def read_conf(self) -> bool: @@ -112,13 +101,38 @@ def read_conf(self) -> bool: continue self.config[x] = sv_conf_values.get(x) + log.debug(f"master config: {self.config}") + return + + def configure_conf(self) -> None: + # Initialize GitVersions instance + self.versions = GitVersions(self.git_root, self.output_dir, self.config.get("force_branches")) + + if self.config.get("floating_badge"): + EventHandlers.FLYOUT_FLOATING_BADGE = True + + if self.config.get("reset_intersphinx_mapping"): + EventHandlers.RESET_INTERSPHINX_MAPPING = True + log.warning("Forcing --no-prebuild") + self.config["prebuild"] = False + + if self.config.get("sphinx_compatibility"): + mp_sphinx_compatibility() + # Set additional config for sphinx self._additional_args = () self._additional_args += ("-Q",) if self.config.get("quite") else () self._additional_args += ("-vv",) if self.config.get("verbose") else () + return - # Initialize GitVersions instance - self.versions = GitVersions(self.git_root, self.output_dir, self.config.get("force_branches")) + def _select_exclude_branches(self) -> list: + log.debug(f"Instructions to select: `{self.config.get('select_branch')}`") + log.debug(f"Instructions to exclude: `{self.config.get('exclude_branch')}`") + self._versions_to_pre_build = [] + + self._select_branch() + + log.info(f"selected branches: `{[x.name for x in self._versions_to_pre_build]}`") return def _select_branch(self) -> None: @@ -149,16 +163,6 @@ def _exclude_branch(self) -> None: return - def _select_exclude_branches(self) -> list: - log.debug(f"Instructions to select: `{self.config.get('select_branch')}`") - log.debug(f"Instructions to exclude: `{self.config.get('exclude_branch')}`") - self._versions_to_pre_build = [] - - self._select_branch() - - log.info(f"selected branches: `{[x.name for x in self._versions_to_pre_build]}`") - return - def _generate_top_level_index(self) -> None: """Generate a top-level ``index.html`` which redirects to the main-branch version specified via ``main_branch``. @@ -236,7 +240,7 @@ def prebuild(self) -> None: The method carries out the transaction via the internal build method :meth:`~sphinx_versioned.build.VersionedDocs._build`. """ - if not self.config.get("prebuild_branches"): + if not self.config.get("prebuild"): log.info("No pre-builing...") self._versions_to_build = self._versions_to_pre_build return @@ -260,7 +264,7 @@ def prebuild(self) -> None: log.success(f"Prebuilding successful for {', '.join([x.name for x in self._versions_to_build])}") return - def build(self) -> None: + def build(self) -> bool: """Build workflow. Method to build the branch in a temporary directory with the modified @@ -282,11 +286,28 @@ def build(self) -> None: self._build(tag.name) self._built_version.append(tag) except SphinxError: - log.error(f"build failed for {tag}") - exit(-1) + log.error(f"Build failed for {tag}") + return False finally: # restore to active branch self.versions.checkout(self._active_branch) + return True + + def run(self) -> bool: + # Prebuild, but returns if `self.config["prebuild"]` is `False` + self.prebuild() + + # Adds our extension to the sphinx-config + application.Config = ConfigInject + + if self.build(): + # Adds a top-level `index.html` in `output_dir` which redirects to `output_dir`/`main-branch`/index.html + self._generate_top_level_index() + + print(f"\n\033[92m Successfully built {', '.join([x.name for x in self._built_version])} \033[0m") + return + + log.critical(f"Build failed.") return pass