Skip to content

Commit 40a4969

Browse files
refactored variable config into VersionedDocs.configure_vars
refactored building runtime into `VersionedDocs.run`.
1 parent ff38547 commit 40a4969

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

sphinx_versioned/__main__.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from loguru import logger as log
55

66
from sphinx_versioned.build import VersionedDocs
7-
from sphinx_versioned.sphinx_ import EventHandlers
8-
from sphinx_versioned.lib import mp_sphinx_compatibility, parse_branch_selection
7+
from sphinx_versioned.lib import parse_branch_selection
98

109
app = typer.Typer(add_completion=False)
1110

@@ -123,32 +122,27 @@ def main(
123122

124123
select_branch, exclude_branch = parse_branch_selection(branches)
125124

126-
EventHandlers.RESET_INTERSPHINX_MAPPING = reset_intersphinx_mapping
127-
EventHandlers.FLYOUT_FLOATING_BADGE = floating_badge
128-
129-
if reset_intersphinx_mapping:
130-
log.warning("Forcing --no-prebuild")
131-
prebuild = False
132-
133-
if sphinx_compatibility:
134-
mp_sphinx_compatibility()
135-
136-
return VersionedDocs(
125+
DocsBuilder = VersionedDocs(
137126
chdir=chdir,
138127
local_conf=local_conf,
139128
output_dir=output_dir,
140129
git_root=git_root,
141130
config={
142-
"prebuild_branches": prebuild,
143-
"select_branch": select_branch,
131+
"reset_intersphinx_mapping": reset_intersphinx_mapping,
132+
"sphinx_compatibility": sphinx_compatibility,
133+
"force_branches": force_branches,
144134
"exclude_branch": exclude_branch,
135+
"floating_badge": floating_badge,
136+
"select_branch": select_branch,
137+
"prebuild": prebuild,
145138
"main_branch": main_branch,
146-
"quite": quite,
147139
"verbose": verbose,
148-
"force_branches": force_branches,
140+
"quite": quite,
149141
},
150142
)
151143

144+
return DocsBuilder.run()
145+
152146

153147
if __name__ == "__main__":
154148
app()

sphinx_versioned/build.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from loguru import logger as log
1010

1111
from sphinx_versioned.sphinx_ import EventHandlers
12-
from sphinx_versioned.lib import TempDir, ConfigInject
12+
from sphinx_versioned.lib import TempDir, ConfigInject, mp_sphinx_compatibility
1313
from sphinx_versioned.versions import GitVersions, BuiltVersions, PseudoBranch
1414

1515

@@ -55,6 +55,7 @@ def __init__(self, chdir: str, local_conf: str, output_dir: str, git_root: str,
5555

5656
# Read sphinx-conf.py variables
5757
self.read_conf()
58+
self.configure_conf()
5859

5960
self._versions_to_pre_build = []
6061
self._versions_to_build = []
@@ -72,18 +73,6 @@ def __init__(self, chdir: str, local_conf: str, output_dir: str, git_root: str,
7273
self.config["main_branch"] = self.versions.active_branch.name
7374
else:
7475
self.config["main_branch"] = "main"
75-
76-
self.prebuild()
77-
78-
# Adds our extension to the sphinx-config
79-
application.Config = ConfigInject
80-
81-
self.build()
82-
83-
# Adds a top-level `index.html` in `output_dir` which redirects to `output_dir`/`main-branch`/index.html
84-
self._generate_top_level_index()
85-
86-
print(f"\n\033[92m Successfully built {', '.join([x.name for x in self._built_version])} \033[0m")
8776
return
8877

8978
def read_conf(self) -> bool:
@@ -112,13 +101,38 @@ def read_conf(self) -> bool:
112101
continue
113102
self.config[x] = sv_conf_values.get(x)
114103

104+
log.debug(f"master config: {self.config}")
105+
return
106+
107+
def configure_conf(self) -> None:
108+
# Initialize GitVersions instance
109+
self.versions = GitVersions(self.git_root, self.output_dir, self.config.get("force_branches"))
110+
111+
if self.config.get("floating_badge"):
112+
EventHandlers.FLYOUT_FLOATING_BADGE = True
113+
114+
if self.config.get("reset_intersphinx_mapping"):
115+
EventHandlers.RESET_INTERSPHINX_MAPPING = True
116+
log.warning("Forcing --no-prebuild")
117+
self.config["prebuild"] = False
118+
119+
if self.config.get("sphinx_compatibility"):
120+
mp_sphinx_compatibility()
121+
115122
# Set additional config for sphinx
116123
self._additional_args = ()
117124
self._additional_args += ("-Q",) if self.config.get("quite") else ()
118125
self._additional_args += ("-vv",) if self.config.get("verbose") else ()
126+
return
119127

120-
# Initialize GitVersions instance
121-
self.versions = GitVersions(self.git_root, self.output_dir, self.config.get("force_branches"))
128+
def _select_exclude_branches(self) -> list:
129+
log.debug(f"Instructions to select: `{self.config.get('select_branch')}`")
130+
log.debug(f"Instructions to exclude: `{self.config.get('exclude_branch')}`")
131+
self._versions_to_pre_build = []
132+
133+
self._select_branch()
134+
135+
log.info(f"selected branches: `{[x.name for x in self._versions_to_pre_build]}`")
122136
return
123137

124138
def _select_branch(self) -> None:
@@ -149,16 +163,6 @@ def _exclude_branch(self) -> None:
149163

150164
return
151165

152-
def _select_exclude_branches(self) -> list:
153-
log.debug(f"Instructions to select: `{self.config.get('select_branch')}`")
154-
log.debug(f"Instructions to exclude: `{self.config.get('exclude_branch')}`")
155-
self._versions_to_pre_build = []
156-
157-
self._select_branch()
158-
159-
log.info(f"selected branches: `{[x.name for x in self._versions_to_pre_build]}`")
160-
return
161-
162166
def _generate_top_level_index(self) -> None:
163167
"""Generate a top-level ``index.html`` which redirects to the main-branch version specified
164168
via ``main_branch``.
@@ -236,7 +240,7 @@ def prebuild(self) -> None:
236240
The method carries out the transaction via the internal build method
237241
:meth:`~sphinx_versioned.build.VersionedDocs._build`.
238242
"""
239-
if not self.config.get("prebuild_branches"):
243+
if not self.config.get("prebuild"):
240244
log.info("No pre-builing...")
241245
self._versions_to_build = self._versions_to_pre_build
242246
return
@@ -260,7 +264,7 @@ def prebuild(self) -> None:
260264
log.success(f"Prebuilding successful for {', '.join([x.name for x in self._versions_to_build])}")
261265
return
262266

263-
def build(self) -> None:
267+
def build(self) -> bool:
264268
"""Build workflow.
265269
266270
Method to build the branch in a temporary directory with the modified
@@ -282,11 +286,28 @@ def build(self) -> None:
282286
self._build(tag.name)
283287
self._built_version.append(tag)
284288
except SphinxError:
285-
log.error(f"build failed for {tag}")
286-
exit(-1)
289+
log.error(f"Build failed for {tag}")
290+
return False
287291
finally:
288292
# restore to active branch
289293
self.versions.checkout(self._active_branch)
294+
return True
295+
296+
def run(self) -> bool:
297+
# Prebuild, but returns if `self.config["prebuild"]` is `False`
298+
self.prebuild()
299+
300+
# Adds our extension to the sphinx-config
301+
application.Config = ConfigInject
302+
303+
if self.build():
304+
# Adds a top-level `index.html` in `output_dir` which redirects to `output_dir`/`main-branch`/index.html
305+
self._generate_top_level_index()
306+
307+
print(f"\n\033[92m Successfully built {', '.join([x.name for x in self._built_version])} \033[0m")
308+
return
309+
310+
log.critical(f"Build failed.")
290311
return
291312

292313
pass

0 commit comments

Comments
 (0)