Skip to content

Commit a7fc3cc

Browse files
authored
Merge pull request #265 from microsphere-projects/copilot/refactor-generate-wiki-docs-script
Refactor generate-wiki-docs.py to derive config from project files instead of hardcoding
2 parents 1d5477d + 9d46e84 commit a7fc3cc

2 files changed

Lines changed: 87 additions & 33 deletions

File tree

.github/scripts/generate-wiki-docs.py

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,75 @@
2727

2828
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2929

30-
MODULES = [
31-
"microsphere-java-annotations",
32-
"microsphere-java-core",
33-
"microsphere-jdk-tools",
34-
"microsphere-lang-model",
35-
"microsphere-annotation-processor",
36-
"microsphere-java-test",
37-
]
38-
39-
JAVA_VERSIONS = ["8", "11", "17", "21", "25"]
40-
41-
PROJECT_VERSION = "0.1.10-SNAPSHOT"
42-
4330
# Source directory path suffix
4431
SRC_MAIN_JAVA = os.path.join("src", "main", "java")
4532

33+
34+
def _discover_modules(project_root):
35+
"""Discover module directories that contain Java sources."""
36+
modules = []
37+
for entry in sorted(os.listdir(project_root)):
38+
entry_path = os.path.join(project_root, entry)
39+
if os.path.isdir(entry_path) and os.path.isdir(os.path.join(entry_path, SRC_MAIN_JAVA)):
40+
modules.append(entry)
41+
return modules
42+
43+
44+
def _read_java_versions(project_root):
45+
"""Read Java versions from the CI workflow matrix configuration."""
46+
workflow_path = os.path.join(project_root, '.github', 'workflows', 'maven-build.yml')
47+
with open(workflow_path, 'r', encoding='utf-8') as f:
48+
content = f.read()
49+
match = re.search(r'matrix:\s*\n\s*java:\s*\[([^\]]+)\]', content)
50+
if match:
51+
return [v.strip().strip("'\"") for v in match.group(1).split(',')]
52+
print("WARNING: Could not parse Java versions from matrix in maven-build.yml", file=sys.stderr)
53+
return []
54+
55+
56+
def _read_pom_revision(project_root):
57+
"""Read the 'revision' property from the root pom.xml."""
58+
pom_path = os.path.join(project_root, 'pom.xml')
59+
with open(pom_path, 'r', encoding='utf-8') as f:
60+
content = f.read()
61+
match = re.search(r'<revision>([^<]+)</revision>', content)
62+
if match:
63+
return match.group(1).strip()
64+
print("WARNING: Could not find <revision> property in pom.xml", file=sys.stderr)
65+
return ""
66+
67+
68+
def _read_pom_artifact_id(project_root):
69+
"""Read the project artifactId from the root pom.xml (outside the <parent> block)."""
70+
pom_path = os.path.join(project_root, 'pom.xml')
71+
with open(pom_path, 'r', encoding='utf-8') as f:
72+
content = f.read()
73+
no_parent = re.sub(r'<parent>.*?</parent>', '', content, flags=re.DOTALL)
74+
match = re.search(r'<artifactId>([^<]+)</artifactId>', no_parent)
75+
if match:
76+
return match.group(1).strip()
77+
print("WARNING: Could not find <artifactId> in pom.xml", file=sys.stderr)
78+
return ""
79+
80+
81+
def _read_readme_title(project_root):
82+
"""Read the top-level heading from README.md."""
83+
readme_path = os.path.join(project_root, 'README.md')
84+
with open(readme_path, 'r', encoding='utf-8') as f:
85+
for line in f:
86+
line = line.strip()
87+
if line.startswith('# '):
88+
return line[2:].strip()
89+
print("WARNING: Could not find a title heading in README.md", file=sys.stderr)
90+
return ""
91+
92+
93+
MODULES = _discover_modules(PROJECT_ROOT)
94+
JAVA_VERSIONS = _read_java_versions(PROJECT_ROOT)
95+
PROJECT_VERSION = _read_pom_revision(PROJECT_ROOT)
96+
ARTIFACT_ID = _read_pom_artifact_id(PROJECT_ROOT)
97+
PROJECT_TITLE = _read_readme_title(PROJECT_ROOT)
98+
4699
# Regex patterns
47100
CLASS_DECL_RE = re.compile(
48101
r'^(?:public\s+)?(?:abstract\s+)?(?:final\s+)?'
@@ -417,7 +470,7 @@ def generate_wiki_page(component):
417470

418471
# Source link
419472
lines.append(f"> **Source:** [`{component.source_path}`]"
420-
f"(https://github.com/microsphere-projects/microsphere-java/blob/main/{component.source_path})")
473+
f"(https://github.com/microsphere-projects/{ARTIFACT_ID}/blob/main/{component.source_path})")
421474
lines.append("")
422475

423476
# ── Overview ──
@@ -515,12 +568,12 @@ def generate_wiki_page(component):
515568
lines.append("<dependency>")
516569
lines.append(" <groupId>io.github.microsphere-projects</groupId>")
517570
lines.append(f" <artifactId>{component.module}</artifactId>")
518-
lines.append(f" <version>${{microsphere-java.version}}</version>")
571+
lines.append(f" <version>${{{ARTIFACT_ID}.version}}</version>")
519572
lines.append("</dependency>")
520573
lines.append("```")
521574
lines.append("")
522-
lines.append("> **Tip:** Use the BOM (`microsphere-java-dependencies`) for consistent version management. "
523-
"See the [Getting Started](https://github.com/microsphere-projects/microsphere-java#getting-started) guide.")
575+
lines.append(f"> **Tip:** Use the BOM (`{ARTIFACT_ID}-dependencies`) for consistent version management. "
576+
f"See the [Getting Started](https://github.com/microsphere-projects/{ARTIFACT_ID}#getting-started) guide.")
524577
lines.append("")
525578

526579
# ── Import ──
@@ -585,7 +638,7 @@ def generate_wiki_page(component):
585638
lines.append("---")
586639
lines.append("")
587640
lines.append(f"*This documentation was auto-generated from the source code of "
588-
f"[microsphere-java](https://github.com/microsphere-projects/microsphere-java).*")
641+
f"[{ARTIFACT_ID}](https://github.com/microsphere-projects/{ARTIFACT_ID}).*")
589642
lines.append("")
590643

591644
return '\n'.join(lines)
@@ -594,18 +647,18 @@ def generate_wiki_page(component):
594647
def generate_home_page(components_by_module):
595648
"""Generate the Home (index) wiki page."""
596649
lines = []
597-
lines.append("# Microsphere Java - API Documentation")
650+
lines.append(f"# {PROJECT_TITLE} - API Documentation")
598651
lines.append("")
599-
lines.append("Welcome to the **Microsphere Java** wiki! This documentation is auto-generated "
600-
"from the project source code and provides detailed information about each Java component.")
652+
lines.append(f"Welcome to the **{PROJECT_TITLE}** wiki! This documentation is auto-generated "
653+
f"from the project source code and provides detailed information about each Java component.")
601654
lines.append("")
602655
lines.append("## Project Information")
603656
lines.append("")
604657
lines.append(f"- **Current Version:** `{PROJECT_VERSION}`")
605658
lines.append(f"- **Java Compatibility:** {', '.join('Java ' + v for v in JAVA_VERSIONS)}")
606659
lines.append("- **License:** Apache License 2.0")
607-
lines.append(f"- **Repository:** [microsphere-projects/microsphere-java]"
608-
f"(https://github.com/microsphere-projects/microsphere-java)")
660+
lines.append(f"- **Repository:** [microsphere-projects/{ARTIFACT_ID}]"
661+
f"(https://github.com/microsphere-projects/{ARTIFACT_ID})")
609662
lines.append("")
610663

611664
# Table of Contents by module
@@ -643,16 +696,16 @@ def generate_home_page(components_by_module):
643696
# Quick links
644697
lines.append("## Quick Links")
645698
lines.append("")
646-
lines.append("- [Getting Started](https://github.com/microsphere-projects/microsphere-java#getting-started)")
647-
lines.append("- [Building from Source](https://github.com/microsphere-projects/microsphere-java#building-from-source)")
648-
lines.append("- [Contributing](https://github.com/microsphere-projects/microsphere-java#contributing)")
699+
lines.append(f"- [Getting Started](https://github.com/microsphere-projects/{ARTIFACT_ID}#getting-started)")
700+
lines.append(f"- [Building from Source](https://github.com/microsphere-projects/{ARTIFACT_ID}#building-from-source)")
701+
lines.append(f"- [Contributing](https://github.com/microsphere-projects/{ARTIFACT_ID}#contributing)")
649702
lines.append("- [JavaDoc](https://javadoc.io/doc/io.github.microsphere-projects)")
650703
lines.append("")
651704
lines.append("---")
652705
lines.append("")
653-
lines.append("*This wiki is auto-generated from the source code of "
654-
"[microsphere-java](https://github.com/microsphere-projects/microsphere-java). "
655-
"To update, trigger the `wiki-publish` workflow.*")
706+
lines.append(f"*This wiki is auto-generated from the source code of "
707+
f"[{ARTIFACT_ID}](https://github.com/microsphere-projects/{ARTIFACT_ID}). "
708+
f"To update, trigger the `wiki-publish` workflow.*")
656709
lines.append("")
657710

658711
return '\n'.join(lines)
@@ -696,7 +749,7 @@ def discover_java_files(project_root, modules):
696749

697750

698751
def main():
699-
parser = argparse.ArgumentParser(description="Generate wiki documentation for microsphere-java")
752+
parser = argparse.ArgumentParser(description=f"Generate wiki documentation for {ARTIFACT_ID}")
700753
parser.add_argument(
701754
"--output", "-o",
702755
default=os.path.join(PROJECT_ROOT, "wiki"),
@@ -705,14 +758,14 @@ def main():
705758
parser.add_argument(
706759
"--project-root",
707760
default=PROJECT_ROOT,
708-
help="Root directory of the microsphere-java project",
761+
help=f"Root directory of the {ARTIFACT_ID} project",
709762
)
710763
args = parser.parse_args()
711764

712765
project_root = args.project_root
713766
output_dir = args.output
714767

715-
print(f"Microsphere Java Wiki Documentation Generator")
768+
print(f"{PROJECT_TITLE} Wiki Documentation Generator")
716769
print(f" Project root: {project_root}")
717770
print(f" Output dir: {output_dir}")
718771
print()

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ build.txt
7171

7272
# Wiki generated output
7373
wiki/
74-
wiki-output/
74+
wiki-output/
75+
__pycache__/

0 commit comments

Comments
 (0)