|
| 1 | +""" |
| 2 | +This script creates an unstable documentation version at the time of branch-off for a new Haystack release. |
| 3 | +
|
| 4 | +Between branch-off and the actual release, two unstable doc versions coexist. |
| 5 | +If we branch off for 2.20, we have: |
| 6 | +1. the target unstable version, 2.20-unstable (lives in docs-website/versioned_docs/version-2.20-unstable) |
| 7 | +2. the next unstable version, 2.21-unstable (lives in docs-website/docs) |
| 8 | +
|
| 9 | +This script takes care of all the necessary updates to the documentation website. |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import os |
| 15 | +import re |
| 16 | +import shutil |
| 17 | +import sys |
| 18 | + |
| 19 | +VERSION_VALIDATOR = re.compile(r"^[0-9]+\.[0-9]+$") |
| 20 | + |
| 21 | + |
| 22 | +if __name__ == "__main__": |
| 23 | + parser = argparse.ArgumentParser() |
| 24 | + parser.add_argument( |
| 25 | + "-v", "--new-version", help="The new unstable version that is being created (e.g. 2.20).", required=True |
| 26 | + ) |
| 27 | + args = parser.parse_args() |
| 28 | + |
| 29 | + if VERSION_VALIDATOR.match(args.new_version) is None: |
| 30 | + sys.exit("Version must be formatted like so <major>.<minor>") |
| 31 | + |
| 32 | + target_version = f"{args.new_version}" # e.g., "2.20" - the target release version |
| 33 | + major, minor = args.new_version.split(".") |
| 34 | + |
| 35 | + target_unstable = f"{target_version}-unstable" # e.g., "2.20-unstable" |
| 36 | + next_unstable = f"{major}.{int(minor) + 1}-unstable" # e.g., "2.21-unstable" - next cycle |
| 37 | + previous_stable = f"{major}.{int(minor) - 1}" # e.g., "2.19" - previous stable release |
| 38 | + |
| 39 | + versions = [ |
| 40 | + folder.replace("version-", "") |
| 41 | + for folder in os.listdir("docs-website/versioned_docs") |
| 42 | + if os.path.isdir(os.path.join("docs-website/versioned_docs", folder)) |
| 43 | + ] |
| 44 | + |
| 45 | + # Check if the versions we're about to create already exist in versioned_docs |
| 46 | + if target_version in versions: |
| 47 | + sys.exit(f"{target_version} already exists (already released). Aborting.") |
| 48 | + if target_unstable in versions: |
| 49 | + print(f"{target_unstable} already exists. Nothing to do.") |
| 50 | + sys.exit(0) |
| 51 | + |
| 52 | + # Create new unstable from the currently existing one. |
| 53 | + # The new unstable will be made stable at a later time by another workflow |
| 54 | + print(f"Creating new unstable version {target_unstable} from main") |
| 55 | + |
| 56 | + ### Docusaurus updates |
| 57 | + |
| 58 | + # copy docs to versioned_docs/version-target_unstable |
| 59 | + shutil.copytree("docs-website/docs", f"docs-website/versioned_docs/version-{target_unstable}") |
| 60 | + |
| 61 | + # copy reference to reference_versioned_docs/version-target_unstable |
| 62 | + shutil.copytree("docs-website/reference", f"docs-website/reference_versioned_docs/version-{target_unstable}") |
| 63 | + |
| 64 | + # copy versioned_sidebars/version-previous_stable-sidebars.json |
| 65 | + # to versioned_sidebars/version-target_unstable-sidebars.json |
| 66 | + shutil.copy( |
| 67 | + f"docs-website/versioned_sidebars/version-{previous_stable}-sidebars.json", |
| 68 | + f"docs-website/versioned_sidebars/version-{target_unstable}-sidebars.json", |
| 69 | + ) |
| 70 | + |
| 71 | + # copy reference_versioned_sidebars/version-previous_stable-sidebars.json |
| 72 | + # to reference_versioned_sidebars/version-target_unstable-sidebars.json |
| 73 | + shutil.copy( |
| 74 | + f"docs-website/reference_versioned_sidebars/version-{previous_stable}-sidebars.json", |
| 75 | + f"docs-website/reference_versioned_sidebars/version-{target_unstable}-sidebars.json", |
| 76 | + ) |
| 77 | + |
| 78 | + # add unstable version to versions.json |
| 79 | + with open("docs-website/versions.json", "r") as f: |
| 80 | + versions_list = json.load(f) |
| 81 | + versions_list.insert(0, target_unstable) |
| 82 | + with open("docs-website/versions.json", "w") as f: |
| 83 | + json.dump(versions_list, f) |
| 84 | + |
| 85 | + # add unstable version to reference_versions.json |
| 86 | + with open("docs-website/reference_versions.json", "r") as f: |
| 87 | + reference_versions_list = json.load(f) |
| 88 | + reference_versions_list.insert(0, target_unstable) |
| 89 | + with open("docs-website/reference_versions.json", "w") as f: |
| 90 | + json.dump(reference_versions_list, f) |
| 91 | + |
| 92 | + # in docusaurus.config.js, replace the target unstable version with the next unstable version |
| 93 | + with open("docs-website/docusaurus.config.js", "r") as f: |
| 94 | + config = f.read() |
| 95 | + config = config.replace(f"label: '{target_unstable}'", f"label: '{next_unstable}'") |
| 96 | + with open("docs-website/docusaurus.config.js", "w") as f: |
| 97 | + f.write(config) |
0 commit comments