Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pre-build command #46

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ Here's an example for the `exhale extension <exhale_>`_:

To see a list of available placeholder names and their values for each version you can use the ``--dump-metadata`` flag.

Running Commands Before Building
================================

You can run commands before each version of the documentation is built with the option ``--pre-build``.

This could be useful to prepare the docs repository before running ``sphinx-build``, debug the execution, or even generate versioned documentation using other builders.

For example, imagine that you want to build versioned docs written in Sphinx, but the API reference is generated with JavaDoc. This option enables the generation of both versioned docs to host them under the same folder using GitHub Pages.

Here's an example showing the directory where the build command is running:

.. code-block:: python

sphinx-multiversion docs build/html --pre-build pwd


You can pass multiple commands by adding extra ``--pre-build`` tags. The commands run in order, from left to right:

.. code-block:: python

sphinx-multiversion docs build/html --pre-build pwd --pre-build ls

.. _python_regex: https://docs.python.org/3/howto/regex.html
.. _python_format: https://pyformat.info/
.. _exhale: https://exhale.readthedocs.io/en/latest/
16 changes: 16 additions & 0 deletions sphinx_multiversion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import subprocess
import sys
import tempfile
import shlex

from sphinx import config as sphinx_config
from sphinx import project as sphinx_project
Expand Down Expand Up @@ -159,6 +160,12 @@ def main(argv=None):
action="store_true",
help="dump generated metadata and exit",
)
parser.add_argument(
"--pre-build",
action="append",
default=[],
help="a list of commands to run before running sphinx-build",
)
args, argv = parser.parse_known_args(argv)
if args.noconfig:
return 1
Expand All @@ -183,6 +190,9 @@ def main(argv=None):
confdir_absolute, confoverrides, add_defaults=True
)

# Parse pre build commands
pre_build_commands = [shlex.split(command) for command in args.pre_build]

# Get relative paths to root of git repository
gitroot = pathlib.Path(
git.get_toplevel_path(cwd=sourcedir_absolute)
Expand Down Expand Up @@ -346,6 +356,12 @@ def main(argv=None):
"SPHINX_MULTIVERSION_CONFDIR": data["confdir"],
}
)

if pre_build_commands:
logger.debug("Running pre build commands:")
for command in pre_build_commands:
subprocess.check_call(command, cwd=current_cwd, env=env)

subprocess.check_call(cmd, cwd=current_cwd, env=env)

return 0