From b7ed9d7223a35b31693cb2019259fd92fcf260ef Mon Sep 17 00:00:00 2001 From: jdoiro3 Date: Mon, 10 Jul 2023 11:43:09 -0400 Subject: [PATCH 1/2] added monorepo sub mkdocs.yml docs gen hook option --- mkdocs_monorepo_plugin/merger.py | 27 ++++++++++++++++++++++----- mkdocs_monorepo_plugin/plugin.py | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/mkdocs_monorepo_plugin/merger.py b/mkdocs_monorepo_plugin/merger.py index 5a1d318..06e37ef 100644 --- a/mkdocs_monorepo_plugin/merger.py +++ b/mkdocs_monorepo_plugin/merger.py @@ -19,12 +19,16 @@ import os from os.path import join from pathlib import Path +import subprocess -from mkdocs.utils import warning_filter +from mkdocs.utils import warning_filter, yaml_load log = logging.getLogger(__name__) log.addFilter(warning_filter) + +TEMP_DOCS_DIR = "TEMP_DOCS_DIR" + # This collects the multiple docs/ folders and merges them together. @@ -33,11 +37,11 @@ def __init__(self, config): self.config = config self.root_docs_dir = config['docs_dir'] self.docs_dirs = list() - self.append('', self.root_docs_dir) + self.append('', self.root_docs_dir, config.config_file_path) self.files_source_dir = dict() - def append(self, alias, docs_dir): - self.docs_dirs.append([alias, docs_dir]) + def append(self, alias, docs_dir, yaml_file): + self.docs_dirs.append([alias, docs_dir, yaml_file]) def merge(self): self.temp_docs_dir = TemporaryDirectory('', 'docs_') @@ -50,7 +54,9 @@ def merge(self): "Current registered site names in the monorepository: {}".format(', '.join(aliases))) raise SystemExit(1) - for alias, docs_dir in self.docs_dirs: + for alias, docs_dir, yaml_file in self.docs_dirs: + with open(yaml_file) as f: + config = yaml_load(f) source_dir = docs_dir if len(alias) == 0: dest_dir = self.temp_docs_dir.name @@ -60,6 +66,17 @@ def merge(self): if os.path.exists(source_dir): copy_tree(source_dir, dest_dir) + gen_docs_hook = config.get("mono_gen_docs_hook", {}) + hook, python_path = gen_docs_hook.get("hook"), gen_docs_hook.get("python_path") + if gen_docs_hook and hook: + cmd = f"{python_path or 'python'} {' '.join(hook)}" + log.info(f"[mkdocs-monorepo] Running {cmd}...") + subprocess.run( + cmd, + cwd=Path(docs_dir).parent, + env={TEMP_DOCS_DIR: dest_dir, **os.environ.copy()}, + shell=True + ) for file_abs_path in Path(source_dir).rglob('*.md'): file_abs_path = str(file_abs_path) # python 3.5 compatibility if os.path.isfile(file_abs_path): diff --git a/mkdocs_monorepo_plugin/plugin.py b/mkdocs_monorepo_plugin/plugin.py index 5774805..df085fe 100644 --- a/mkdocs_monorepo_plugin/plugin.py +++ b/mkdocs_monorepo_plugin/plugin.py @@ -48,7 +48,7 @@ def on_config(self, config): self.aliases = {} for alias, docs_dir, yaml_file in resolvedPaths: self.aliases[alias] = { 'docs_dir': docs_dir, 'yaml_file': yaml_file } - self.merger.append(alias, docs_dir) + self.merger.append(alias, docs_dir, yaml_file) new_docs_dir = self.merger.merge() # Update the docs_dir with our temporary one! From f1e977ad99a8189629fa40dc7a32d3ee99a3a8d7 Mon Sep 17 00:00:00 2001 From: jdoiro3 Date: Tue, 11 Jul 2023 09:49:39 -0400 Subject: [PATCH 2/2] adding tests and bumping version --- .../fixtures/ok-with-hook/docs/index.md | 53 +++++++++++++++++++ .../fixtures/ok-with-hook/mkdocs.yml | 10 ++++ .../ok-with-hook/project-1/mkdocs.yml | 11 ++++ .../ok-with-hook/project-1/my_hook.py | 16 ++++++ .../ok-with-hook/project-2/mkdocs.yml | 12 +++++ __tests__/integration/test.bats | 9 ++++ __tests__/test-local.sh | 4 +- mkdocs_monorepo_plugin/merger.py | 21 ++++---- setup.py | 2 +- 9 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 __tests__/integration/fixtures/ok-with-hook/docs/index.md create mode 100644 __tests__/integration/fixtures/ok-with-hook/mkdocs.yml create mode 100644 __tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml create mode 100644 __tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py create mode 100644 __tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml diff --git a/__tests__/integration/fixtures/ok-with-hook/docs/index.md b/__tests__/integration/fixtures/ok-with-hook/docs/index.md new file mode 100644 index 0000000..8ead955 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/docs/index.md @@ -0,0 +1,53 @@ +# Neptunia vapor + +## Has mente et + +Lorem markdownum aequora Famemque, a ramos regna Ulixem verba, posito qui +nubilus membra. Pendet dixit canisve, hanc quoque animosa **veni**, inducere. +Fer quem, mihi vallem; reposcunt aequoreae Haec, inposita. Eras dicere sic! Ore +ad at nec pius rivi pectora Pandione amari pietas Ulixem. + +> Argenteus sinit. Corpore non Booten Uranie, in hac has dixi herbas. *Oculos +> omnes Dixerat* suae coloribus et antris spernitque silva, dixit. + +Mihi [quamvis](http://caput-latebris.com/), ardua venit nam, de mox in et inquit +incisa relevare reseminet Cycnus forma sororis. In mater artus utque iustis me +vestrae magno datque, quaque multumque oscula iubemur. + +## Aditumque ubi + +Brevibus cervice inmunibus sunt peragit, [sua tanto +insuper](http://ampycuslyncides.org/), arva ubi: torto mixta. Sanguis +conscendunt sumit, utilis illo nec quaecumque ad urbis inpositaque. Alto sic +esse resumere albet, pharetras sola, erat, [non longo +paviunt](http://verba.org/) dives aurem. Nomina genus nulli insignia, carpere +dare quo vident, *nox flemus sed* Telamon auras, erant illuc, tantum. Regia +[duroque opto](http://www.flectathiberi.io/estredeunt), segetes paterna de +crimen! + + var edutainment_php = plain_ring_scan(adfUgcImap * delPanel); + var click_meta_dv = 3 + + systemScrollingDocument.snippetCdAnimated.memoryInstallHost(service( + bezel_trojan_plagiarism, 1, base_resources), intelligence, + umlWiSkin.software_olap.on(quadHocData)); + var bare = jumper_server_solid - rupE + 3; + var timeRegistryStandby = disk_ppc_menu + gigahertzCifsRss; + if (im(correction_desktop, disk_integer_soft(serviceLogic, data_zone, + daw_ssid_web)) > graphicsExpansionBug + active) { + apiSpam = storageVisual + 3; + } + +Mors cum cum proturbat, gente nasci Semiramis sonum, toto est eris facto dapibus +propulit; a! Rogantis ira canat, [in nec +sanguine](http://acceptiordefensus.io/accepto) probro inmunesque molliter +sustineat quem quamquam parentis non. Per **quod nec** rapit ipsa nec, +territaque fallacis fluviis progenies aratro. Colla puer regesta si Haec +silentia omen Paeonia, harenis puer Marmaridae pectora ingens miratur Thisbes +veri. Plaga profugi, iram, praestans, pro hanc vehit, vites. + +Illa per acerris vivit difficile pulveris, faciebat pontus populabile utque? In +flagrant umbrae marito, coniunx parari, **quoque sanguine Nisi**, ego +[saxo](http://cervice-fessusque.com/), fovet, ait unda contigit. Gaudet in, +herba quibus? Ore ne ambo mecumque pectoraque alta: viri illi in puer corpore +expersque pharetra solutum proximitas. Gorgonis adempto, in montes terga quae +nec remoratur nives perque insidias exsiluit tribuitque mille. diff --git a/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml new file mode 100644 index 0000000..c88c552 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml @@ -0,0 +1,10 @@ +site_name: "Example" + +docs_dir: ./docs + +plugins: + - monorepo + +nav: + - Home: index.md + - project-1: "!include project-2/mkdocs.yml" \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml new file mode 100644 index 0000000..67bfa1a --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml @@ -0,0 +1,11 @@ + + +nav: + - Home: index.md + - About: about.md + + +mono_docs_hook: + hook: + - my_hook.py + - $TEMP_DOCS_DIR \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py b/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py new file mode 100644 index 0000000..292fbb2 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py @@ -0,0 +1,16 @@ +import sys +from pathlib import Path + +temp_docs_dir = Path(sys.argv[1]) + +with open(temp_docs_dir / "index.md", "w") as md_f: + md_f.writelines([ + "# Hello", + "Welcome to project 1's docs!" + ]) + +with open(temp_docs_dir / "about.md", "w") as md_f: + md_f.writelines([ + "# About", + "This is the about page." + ]) \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml new file mode 100644 index 0000000..4326fe0 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml @@ -0,0 +1,12 @@ + + +nav: + - Home: index.md + - About: about.md + + +mono_docs_hook: + cwd: ../project-1 + hook: + - my_hook.py + - $TEMP_DOCS_DIR \ No newline at end of file diff --git a/__tests__/integration/test.bats b/__tests__/integration/test.bats index c7f133c..94237e2 100644 --- a/__tests__/integration/test.bats +++ b/__tests__/integration/test.bats @@ -198,6 +198,15 @@ teardown() { [[ "$output" == *"This contains a sentence which only exists in the ok-include-wildcard/project-b fixture."* ]] } +@test "builds a mkdocs site with hooks" { + cd ${fixturesDir}/ok-with-hook + assertSuccessMkdocs build + assertFileExists site/project-1/index.html + assertFileExists site/project-1/about.html + assertFileExists site/project-2/index.html + assertFileExists site/project-2/about.html +} + @test "fails if !include path is above current folder" { cd ${fixturesDir}/error-include-path-is-parent assertFailedMkdocs build diff --git a/__tests__/test-local.sh b/__tests__/test-local.sh index d09e759..e99dad1 100755 --- a/__tests__/test-local.sh +++ b/__tests__/test-local.sh @@ -2,8 +2,8 @@ # Lint via flake8 echo "Running flake8 linter -------->" -flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=setup.py -flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=setup.py +flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=setup.py,venv,env,.venv,.env +flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=setup.py,venv,env,.venv,.env # Running unit-tests python3 -m unittest diff --git a/mkdocs_monorepo_plugin/merger.py b/mkdocs_monorepo_plugin/merger.py index 06e37ef..89d3a8f 100644 --- a/mkdocs_monorepo_plugin/merger.py +++ b/mkdocs_monorepo_plugin/merger.py @@ -37,7 +37,7 @@ def __init__(self, config): self.config = config self.root_docs_dir = config['docs_dir'] self.docs_dirs = list() - self.append('', self.root_docs_dir, config.config_file_path) + self.append('', self.root_docs_dir, getattr(config, "config_file_path", Path("mkdocs.yml"))) self.files_source_dir = dict() def append(self, alias, docs_dir, yaml_file): @@ -66,17 +66,20 @@ def merge(self): if os.path.exists(source_dir): copy_tree(source_dir, dest_dir) - gen_docs_hook = config.get("mono_gen_docs_hook", {}) - hook, python_path = gen_docs_hook.get("hook"), gen_docs_hook.get("python_path") - if gen_docs_hook and hook: - cmd = f"{python_path or 'python'} {' '.join(hook)}" - log.info(f"[mkdocs-monorepo] Running {cmd}...") - subprocess.run( - cmd, - cwd=Path(docs_dir).parent, + mono_docs_hook = config.get("mono_docs_hook", {}) + hook, python_path = mono_docs_hook.get("hook"), mono_docs_hook.get("python_path") + cwd = mono_docs_hook.get("cwd") + project_dir = Path(docs_dir).parent + if cwd and not cwd.is_absolute(): + cwd = project_dir / cwd + if mono_docs_hook and hook: + comp_proc = subprocess.run( + f"{python_path or 'python'} {' '.join(hook)}", + cwd=Path(cwd).resolve() if cwd else project_dir, env={TEMP_DOCS_DIR: dest_dir, **os.environ.copy()}, shell=True ) + comp_proc.check_returncode() for file_abs_path in Path(source_dir).rglob('*.md'): file_abs_path = str(file_abs_path) # python 3.5 compatibility if os.path.isfile(file_abs_path): diff --git a/setup.py b/setup.py index 06dc8dc..0244752 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setuptools.setup( name='mkdocs-monorepo-plugin', - version='1.0.5', + version='1.0.6', description='Plugin for adding monorepository support in Mkdocs.', long_description=""" This introduces support for the !include syntax in mkdocs.yml, allowing you to import additional Mkdocs navigation.