Skip to content

Commit

Permalink
use temp directories for building (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst authored Dec 21, 2020
1 parent b81c0ba commit 7023c29
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ RUN apt-get update \
# Ruby for html-proofer
&& apt-get install -y --no-install-recommends ruby ruby-dev \
&& gem install html-proofer \
# git settings
&& apt-get install -y --no-install-recommends wget \
&& wget -O /etc/profile.d/git_aliases.sh https://github.com/athackst/workstation_setup/raw/main/user/.aliases/git_aliases.sh \
&& echo "source /etc/profile.d/git_aliases.sh" >> /home/vscode/.bashrc \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_draft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
# Drafts your next Release notes as Pull Requests are merged into "main"
- uses: release-drafter/release-drafter@v5
- uses: release-drafter/release-drafter@v5.12.1
with:
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
config-name: release-drafter.yml
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
site_name: athackst/mkdocs-simple-plugin
docs_dir: docs
docs_dir: /tmp/mkdocs-simple/mkdocs-simple-plugin
plugins:
- search
- simple:
Expand Down
11 changes: 10 additions & 1 deletion mkdocs_simple_plugin/gen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
import tempfile
import os
import yaml

Expand All @@ -12,7 +13,14 @@ def default_config():
config['site_url'] = os.environ["SITE_URL"]
if "REPO_URL" in os.environ.keys():
config['repo_url'] = os.environ["REPO_URL"]
config['docs_dir'] = "docs"
# Set the docs dir to temporary directory, or docs if the folder exists
config['docs_dir'] = os.path.join(
tempfile.gettempdir(),
'mkdocs-simple',
os.path.basename(os.getcwd()),
"docs")
if os.path.exists(os.path.join(os.getcwd(), "docs")):
config['docs_dir'] = "docs"
config['plugins'] = ["simple", "search"]
return config

Expand Down Expand Up @@ -55,6 +63,7 @@ def setup_config():
local_plugins = local_config["plugins"]
[i for i in local_plugins if i not in config_plugins
or config_plugins.remove(i)]
# Overwrite default config values with local mkdocs.yml
config.update(local_config)
print(config)
if not os.path.exists(config["docs_dir"]):
Expand Down
46 changes: 22 additions & 24 deletions mkdocs_simple_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import os
import shutil
import sys
import tempfile


# TODO(athackst): Use TemporaryDirectory for docs_dir
# from tempfile import TemporaryDirectory

def common_extensions():
return [".bmp", ".tif", ".tiff", ".gif", ".svg", ".jpeg", ".jpg", ".jif", ".jfif",
".jp2", ".jpx", ".j2k", ".j2c", ".fpx", ".pcd", ".png", ".pdf", "CNAME"]
Expand Down Expand Up @@ -46,31 +44,31 @@ class SimplePlugin(BasePlugin):
)

def on_pre_build(self, config, **kwargs):
self.config_dir = os.path.dirname(
config.config_file_path) if config.config_file_path else "."
self.docs_dir = os.path.join(self.config_dir, "docs_")
self.include_folders = self.config['include_folders']
self.ignore_folders = self.config['ignore_folders']
self.ignore_paths = [config['docs_dir'],
config['site_dir'],
self.docs_dir]
self.ignore_hidden = self.config['ignore_hidden']
self.include_extensions = utils.markdown_extensions + \
self.config['include_extensions']
self.merge_docs_dir = self.config['merge_docs_dir']
# Update the docs_dir with our temporary one!
# The temp folder to dump all the documentation
self.build_docs_dir = os.path.join(
tempfile.gettempdir(),
'mkdocs-simple',
os.path.basename(os.getcwd()),
"docs_")
# Always ignore the output paths
self.ignore_paths = [config['site_dir'],
self.build_docs_dir]
# Save original docs directory location
self.orig_docs_dir = config['docs_dir']
config['docs_dir'] = self.docs_dir
# Add all md files from directory, keeping folder structure
self.paths = self.get_doc_files()
# Add any files in the original docs directory
docs_dir_dest = self.docs_dir
# If not merging docs directory, create a new docs directory
if not self.merge_docs_dir and len(self.paths) > 0:
docs_dir_dest = self.docs_dir + \
self.orig_docs_dir.replace(self.config_dir, "")
if os.path.exists(self.orig_docs_dir):
self.copy_docs_dir(self.orig_docs_dir, docs_dir_dest)
# Copy contents of docs directory if merging
if self.merge_docs_dir and os.path.exists(self.orig_docs_dir):
self.copy_docs_dir(self.orig_docs_dir, self.build_docs_dir)
self.ignore_paths += [self.orig_docs_dir]
# Copy all of the valid doc files into build_docs_dir
self.paths = self.copy_doc_files(self.build_docs_dir)
# Update the docs_dir with our temporary one
config['docs_dir'] = self.build_docs_dir

def on_serve(self, server, config, **kwargs):
builder = list(server.watcher._tasks.values())[0]['func']
Expand All @@ -86,7 +84,7 @@ def on_serve(self, server, config, **kwargs):
return server

def on_post_build(self, config, **kwargs):
shutil.rmtree(self.docs_dir)
shutil.rmtree(self.build_docs_dir)

def in_search_dir(self, dir, root):
if self.ignore_hidden and dir[0] == ".":
Expand All @@ -103,13 +101,13 @@ def in_include_dir(self, dir):
def in_extensions(self, file):
return any(extension in file for extension in self.include_extensions)

def get_doc_files(self):
def copy_doc_files(self, dest_dir):
paths = []
for root, dirs, files in os.walk("."):
if self.in_include_dir(root):
for f in files:
if self.in_extensions(f):
doc_root = self.docs_dir + root[1:]
doc_root = dest_dir + root[1:]
orig = "{}/{}".format(root, f)
new = "{}/{}".format(doc_root, f)
try:
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ debugger() {
echo "--- OUTPUT ---"
echo $output
echo "--------------"
echo "--- CONFIG ---"
cat mkdocs.yml
echo "--------------"
echo "--- FILES ----"
ls -R
echo "--------------"
}

assertGen() {
Expand Down

0 comments on commit 7023c29

Please sign in to comment.