Skip to content

Commit 5955b20

Browse files
authored
Merge pull request #168 from timvink/monorepo_compat
Fix monorepo compatibility
2 parents f341e03 + 1da3a96 commit 5955b20

29 files changed

+265
-20
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ dev-dependencies = [
119119
"mkdocs-gen-files>=0.5.0",
120120
"mkdocs-git-authors-plugin>=0.9.2",
121121
"mkdocs-material>=9.6.7",
122+
"mkdocs-monorepo-plugin>=1.1.0",
122123
"mkdocs-static-i18n>=1.3.0",
123124
"pytest>=8.3.5",
124125
"pytest-cov>=5.0.0",
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.4.0"
1+
__version__ = "1.4.1"

src/mkdocs_git_revision_date_localized_plugin/plugin.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from mkdocs_git_revision_date_localized_plugin.util import Util
2424
from mkdocs_git_revision_date_localized_plugin.exclude import exclude
2525

26-
from typing import Any, Dict
26+
from typing import Any, Dict, Optional
2727
from collections import OrderedDict
2828

2929
from packaging.version import Version
@@ -145,12 +145,15 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
145145
return config
146146

147147

148-
def parallel_compute_commit_timestamps(self, files, docs_dir, is_first_commit=False):
148+
def parallel_compute_commit_timestamps(self, files, original_source: Optional[Dict] = None, is_first_commit=False):
149149
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
150150
results = []
151151
for file in files:
152152
if file.is_documentation_page():
153-
abs_src_path = os.path.join(docs_dir, file.src_uri)
153+
abs_src_path = file.abs_src_path
154+
# Support plugins like monorep that might have moved the files from the original source that is under git
155+
if original_source and abs_src_path in original_source:
156+
abs_src_path = original_source[abs_src_path]
154157
result = pool.apply_async(
155158
self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit)
156159
)
@@ -170,14 +173,17 @@ def on_files(self, files: Files, config: MkDocsConfig):
170173
"""
171174
if not self.config.get("enabled") or not self.config.get("enable_parallel_processing"):
172175
return
173-
# Some plugins like TechDocs/monorepo copies docs_dir to a tmp dir and we need the real git path.
174-
real_docs_dir = os.path.join(
175-
os.path.dirname(config["config_file_path"]), "docs"
176-
)
176+
177+
# Support monorepo/techdocs, which copies the docs_dir to a temporary directory
178+
if "monorepo" in config.get('plugins', {}):
179+
original_source = config.get('plugins').get('monorepo').merger.files_source_dir
180+
else:
181+
original_source = None
182+
177183
if not self.last_revision_commits:
178-
self.parallel_compute_commit_timestamps(files=files, docs_dir=real_docs_dir, is_first_commit=False)
184+
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
179185
if not self.created_commits:
180-
self.parallel_compute_commit_timestamps(files=files, docs_dir=real_docs_dir, is_first_commit=True)
186+
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)
181187

182188

183189
def on_page_markdown(self, markdown: str, page: Page, config: config_options.Config, files, **kwargs) -> str:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Docs for A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
site_name: Component A
2+
3+
nav:
4+
- Home: "README.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Docs for B
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
site_name: Component B
2+
3+
nav:
4+
- Home: "README.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Docs for C
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
site_name: Component C
2+
3+
nav:
4+
- Home: "README.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Component D
2+
3+
This will not be included
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Docs for E
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
site_name: Component E
2+
3+
nav:
4+
- Home: "README.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File present to test that it is successfully ignored by wildcard include.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Authentication
2+
3+
This explains how to authenticate.
4+
5+
## Authorization Header
6+
7+
To authenticate, simply pass in your access token in the form of a `Authorization: Bearer {access_token}` header in your HTTP request to our API.
8+
9+
- Markdown source: `sample-docs/docs/authentication.md`
10+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/authentication/>

tests/fixtures/monorepo/docs/index.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Welcome to our Developer Platform
2+
3+
This contains the documentation for our Developer Platform.
4+
5+
## API
6+
7+
- [v1 API Reference](./versions/v1/reference.md)
8+
- [v2 API Reference](./versions/v2/reference.md)
9+
- [v3 API Reference](./versions/v3/reference.md)
10+
11+
- Markdown source: `sample-docs/docs/index.md`
12+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/>

tests/fixtures/monorepo/mkdocs.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
site_name: Cats API
2+
3+
nav:
4+
- Intro: 'index.md'
5+
- Authentication: 'authentication.md'
6+
- Components: '*include ./components/*/mkdocs.yml'
7+
- API:
8+
- v1: '!include ./v1/mkdocs.yml'
9+
- v2: '!include ./v2/mkdocs.yml'
10+
- v3: '!include ./v3/mkdocs.yml'
11+
12+
theme:
13+
name: material
14+
15+
plugins:
16+
- search
17+
- git-revision-date-localized
18+
- monorepo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
This contains the changelog for the v1 API.
4+
5+
- Markdown source: `sample-docs/v1/docs/changelog.md`
6+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v1/changelog/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# v1 API reference
2+
3+
This contains the v1 API reference.
4+
5+
## GET /v1/cats
6+
7+
Returns a list of cats.
8+
9+
## GET /v1/search
10+
11+
Parameters:
12+
13+
- `q` = A string containing the search query for a cat name.
14+
15+
Returns a list of cats that match the search query.
16+
17+
- Markdown source: `sample-docs/v1/docs/reference.md`
18+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v1/reference/>

tests/fixtures/monorepo/v1/mkdocs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
site_name: versions/v1
2+
3+
nav:
4+
- Reference: "reference.md"
5+
- Changelog: "changelog.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
This contains the changelog for the v2 API.
4+
5+
- Markdown source: `sample-docs/v2/docs/changelog.md`
6+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/changelog/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Migration Guide to v2
2+
3+
This contains the migration guide to the v2 API from the v1 API.
4+
5+
- Markdown source: `sample-docs/v2/docs/migrating.md`
6+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/migrating/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# v2 API reference
2+
3+
This contains the v2 API reference.
4+
5+
## GET /v2/cats
6+
7+
Returns a list of all cats.
8+
9+
## GET /v2/breeds
10+
11+
Returns a list of all breeds of cats.
12+
13+
## GET /v2/breeds/:breed_name/cats
14+
15+
Returns a list of all cats under the `:breed_name` breed.
16+
17+
## GET /v2/search
18+
19+
Parameters:
20+
21+
- `q` = A string containing the search query for a cat name or breed.
22+
23+
Returns a list of cats that match the search query.
24+
25+
- Markdown source: `sample-docs/v2/docs/reference.md`
26+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/reference/>

tests/fixtures/monorepo/v2/mkdocs.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
site_name: versions/v2
2+
3+
nav:
4+
- Migrating to v2: "migrating.md"
5+
- Reference: "reference.md"
6+
- Changelog: "changelog.md"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
This contains the changelog for the v3 API.
4+
5+
- Markdown source: `sample-docs/v3/docs/changelog.md`
6+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/changelog/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Migration Guide to v3
2+
3+
This contains the migration guide to the v3 API from the v2 API.
4+
5+
- Markdown source: `sample-docs/v3/docs/migrating.md`
6+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/migrating/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# v3 API reference
2+
3+
This contains the v3 API reference.
4+
5+
## GET /v3/cats
6+
7+
Returns a list of all cats.
8+
9+
## GET /v3/breeds
10+
11+
Returns a list of all breeds of cats.
12+
13+
## GET /v3/breeds/:breed_name/cats
14+
15+
Returns a list of all cats under the `:breed_name` breed.
16+
17+
## GET /v3/search
18+
19+
Parameters:
20+
21+
- `q` = A string containing the search query for a cat name or breed.
22+
23+
Returns a list of cats that match the search query.
24+
25+
- Markdown source: `sample-docs/v3/docs/reference.md`
26+
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/reference/>
27+
28+
## GET /v3/cat/:name/lives_left
29+
30+
Returns how many lives the cat named `:name` still has left.

tests/fixtures/monorepo/v3/mkdocs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
site_name: versions/v3
2+
3+
docs_dir: ./docs

tests/test_builds.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
127127
if "gen-files" in mkdocs_yml_path:
128128
shutil.copyfile(str(Path(mkdocs_yml_path).parent / "gen_pages.py"), str(testproject_path / "gen_pages.py"))
129129

130+
# Copy monorepo files
131+
if "monorepo" in mkdocs_yml_path:
132+
shutil.copytree("tests/fixtures/monorepo", str(testproject_path), dirs_exist_ok=True)
133+
130134
return testproject_path
131135

132136

@@ -313,27 +317,27 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
313317

314318

315319
MKDOCS_FILES = [
316-
'basic_project/mkdocs_theme_no_locale.yml',
317-
'basic_project/mkdocs.yml',
318-
'basic_project/mkdocs_theme_timeago_locale.yml',
320+
'basic_project/mkdocs_creation_date.yml',
321+
'basic_project/mkdocs_custom_type.yml',
319322
'basic_project/mkdocs_datetime.yml',
323+
'basic_project/mkdocs_exclude.yml',
324+
'basic_project/mkdocs_fallback_to_build_date.yml',
325+
'basic_project/mkdocs_locale.yml',
326+
'basic_project/mkdocs_meta.yml',
320327
'basic_project/mkdocs_plugin_locale.yml',
321-
'basic_project/mkdocs_with_override.yml',
328+
'basic_project/mkdocs.yml',
329+
'basic_project/mkdocs_theme_timeago_locale.yml',
322330
'basic_project/mkdocs_theme_language.yml',
323331
'basic_project/mkdocs_theme_locale_and_language.yml',
324332
'basic_project/mkdocs_theme_locale_disabled.yml',
325333
'basic_project/mkdocs_theme_timeago.yml',
326334
'basic_project/mkdocs_theme_locale.yml',
335+
'basic_project/mkdocs_theme_no_locale.yml',
327336
'basic_project/mkdocs_theme_timeago_override.yml',
328337
'basic_project/mkdocs_theme_timeago_instant.yml',
329-
'basic_project/mkdocs_creation_date.yml',
330338
'basic_project/mkdocs_timeago_locale.yml',
331339
'basic_project/mkdocs_timeago.yml',
332-
'basic_project/mkdocs_fallback_to_build_date.yml',
333-
'basic_project/mkdocs_locale.yml',
334-
'basic_project/mkdocs_exclude.yml',
335-
'basic_project/mkdocs_meta.yml',
336-
'basic_project/mkdocs_custom_type.yml',
340+
'basic_project/mkdocs_with_override.yml',
337341
# 'i18n/mkdocs.yml'
338342
]
339343

@@ -704,3 +708,19 @@ def test_ignored_commits(tmp_path):
704708
page_with_tag = testproject_path / "site/page_with_tag/index.html"
705709
contents = page_with_tag.read_text(encoding="utf8")
706710
assert "May 4, 2018" in contents
711+
712+
713+
714+
def test_monorepo_compat(tmp_path):
715+
testproject_path = setup_clean_mkdocs_folder(
716+
"tests/fixtures/monorepo/mkdocs.yml", tmp_path
717+
)
718+
repo = setup_commit_history(testproject_path)
719+
result = build_docs_setup(testproject_path)
720+
721+
# author = "Test Person <[email protected]>"
722+
# with working_directory(testproject_path):
723+
# repo.git.add(".")
724+
# repo.git.commit(message="add all", author=author, date="1500854705")
725+
726+
assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}"

0 commit comments

Comments
 (0)