Skip to content

Commit

Permalink
refactored variable config into VersionedDocs.configure_vars
Browse files Browse the repository at this point in the history
refactored building runtime into `VersionedDocs.run`.
  • Loading branch information
devanshshukla99 committed Apr 5, 2024
1 parent ff38547 commit 40a4969
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
28 changes: 11 additions & 17 deletions sphinx_versioned/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
79 changes: 50 additions & 29 deletions sphinx_versioned/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 = []
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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``.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 40a4969

Please sign in to comment.