|
| 1 | +# /// script |
| 2 | +# dependencies = [ |
| 3 | +# ] |
| 4 | +# requires-python = ">=3.11" |
| 5 | +# /// |
| 6 | + |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import tempfile |
| 11 | + |
| 12 | + |
| 13 | +def clone_docs_from_repo(repo, destination_subpath, branch="main"): |
| 14 | + working_dir = os.getcwd() |
| 15 | + destination = os.path.join(working_dir, "src", "content", destination_subpath) |
| 16 | + |
| 17 | + print(f"Cloning docs from {repo} to {destination} on branch {branch}") |
| 18 | + |
| 19 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 20 | + os.chdir(tmpdirname) |
| 21 | + |
| 22 | + subprocess.run( |
| 23 | + ["git", "clone", "-n", "--depth=1", "--filter=tree:0", "-b", branch, repo], |
| 24 | + stdout=subprocess.DEVNULL, |
| 25 | + stderr=subprocess.DEVNULL, |
| 26 | + ) |
| 27 | + |
| 28 | + repo_name = os.path.basename(repo) |
| 29 | + os.chdir(repo_name) |
| 30 | + |
| 31 | + subprocess.run( |
| 32 | + ["git", "sparse-checkout", "set", "--no-cone", "docs"], |
| 33 | + stdout=subprocess.DEVNULL, |
| 34 | + stderr=subprocess.DEVNULL, |
| 35 | + ) |
| 36 | + subprocess.run( |
| 37 | + ["git", "checkout"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 38 | + ) |
| 39 | + |
| 40 | + os.makedirs(destination, exist_ok=True) |
| 41 | + |
| 42 | + for item in os.listdir("docs"): |
| 43 | + s = os.path.join("docs", item) |
| 44 | + d = os.path.join(destination, item) |
| 45 | + if os.path.isdir(s): |
| 46 | + shutil.copytree(s, d, dirs_exist_ok=True) |
| 47 | + else: |
| 48 | + shutil.copy2(s, d) |
| 49 | + |
| 50 | + os.chdir(working_dir) |
| 51 | + |
| 52 | + |
| 53 | +# Remove existing docs directory |
| 54 | +shutil.rmtree("src/content/docs", ignore_errors=True) |
| 55 | + |
| 56 | +# Clone docs from repository |
| 57 | +clone_docs_from_repo("https://github.com/strawberry-graphql/strawberry", "docs") |
| 58 | +# clone_docs_from_repo("https://github.com/strawberry-graphql/strawberry-django", "docs/django", "feature/new-docs") |
| 59 | + |
| 60 | +# Rename all .md files to .mdx in the docs folder |
| 61 | +for root, _, files in os.walk("src/content/docs"): |
| 62 | + for file in files: |
| 63 | + if file.endswith(".md"): |
| 64 | + file_path = os.path.join(root, file) |
| 65 | + new_file_path = os.path.splitext(file_path)[0] + ".mdx" |
| 66 | + os.rename(file_path, new_file_path) |
0 commit comments