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

Enable //:mike to work without direnv #133

Merged
merged 1 commit into from
May 24, 2023
Merged
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
46 changes: 36 additions & 10 deletions mike_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import subprocess
import sys

from unittest import mock
import mike.driver
import mike.mkdocs_utils
from mike.mkdocs_utils import docs_version_var


# We need to monkey patch any function in mike which calls `mkdocs` via its
# command line interface. Fortunately, there are only two: `version` and
# `build`.


def override_version():
"""Retrieve the version in a way that's agnostic to the name of the command.

Since the `mkdocs` executable isn't installed on our machine, but instead is
run through bazel, it turns out that _our_ mkdocs executable thinks it has a
different name (since we use a wrapper script). But mike assumes both that
we can call `mkdocs`, _and_ that it thinks its own name is `mkdocs` when we
do.
"""
"""Override mike's native logic for retrieving the version."""
output = subprocess.run(
["mkdocs", "--version"],
["update_docs", "--version"],
check=True,
stdout=subprocess.PIPE,
universal_newlines=True,
Expand All @@ -47,6 +47,32 @@ def override_version():
return m.group(1)


def override_build(config_file, version, verbose=True):
"""Override mike's native logic for building the docs."""
# Original line:
#
# command = (
# ['mkdocs', 'build', '--clean'] +
# (['--config-file', config_file] if config_file else [])
# )
#
# Changed version (reformatted with Black):
command = ["update_docs", "build", "--clean"] + (
["--config-file", config_file] if config_file else []
)

env = os.environ.copy()
env[docs_version_var] = version

output = None if verbose else subprocess.DEVNULL
subprocess.run(command, check=True, env=env, stdout=output, stderr=output)


if __name__ == "__main__":
# `update_docs` is our name for `mkdocs`. It will be found in the same folder as this script.
# We need to add this folder to the PATH.
os.environ["PATH"] = f"{os.path.join(os.getcwd())}:{os.environ['PATH']}"

with mock.patch("mike.mkdocs_utils.version", override_version):
sys.exit(mike.driver.main())
with mock.patch("mike.mkdocs_utils.build", override_build):
sys.exit(mike.driver.main())
25 changes: 0 additions & 25 deletions tools/bin/mkdocs

This file was deleted.