|
| 1 | +""" |
| 2 | +This python script makes the NGO package release ready. What it does is: |
| 3 | +1) Update changelogs |
| 4 | +2) Update validation exception file based on manifest version |
| 5 | +
|
| 6 | +Note that this script NEEDS TO BE RUN FROM THE ROOT of the project. |
| 7 | +""" |
| 8 | +#!/usr/bin/env python3 |
| 9 | +import datetime |
| 10 | +import json |
| 11 | +import os |
| 12 | +import re |
| 13 | +import subprocess |
| 14 | +import platform |
| 15 | + |
| 16 | +package_name = 'com.unity.netcode.gameobjects' |
| 17 | + |
| 18 | +def update_changelog(new_version): |
| 19 | + """ |
| 20 | + Cleans the [Unreleased] section of the changelog by removing empty subsections, |
| 21 | + then replaces the '[Unreleased]' tag with the new version and release date. |
| 22 | + """ |
| 23 | + |
| 24 | + changelog_entry = f'## [{new_version}] - {datetime.date.today().isoformat()}' |
| 25 | + changelog_path = f'{package_name}/CHANGELOG.md' |
| 26 | + print("Latest CHANGELOG entry will be modified to: " + changelog_entry) |
| 27 | + |
| 28 | + with open(changelog_path, 'r', encoding='UTF-8') as f: |
| 29 | + changelog_text = f.read() |
| 30 | + |
| 31 | + # This pattern finds a line starting with '###', followed by its newline, |
| 32 | + # and then two more lines that contain only whitespace. |
| 33 | + # The re.MULTILINE flag allows '^' to match the start of each line. |
| 34 | + pattern = re.compile(r"^###.*\n\n\n", re.MULTILINE) |
| 35 | + |
| 36 | + # Replace every match with an empty string. The goal is to remove empty CHANGELOG subsections. |
| 37 | + cleaned_content = pattern.sub('', changelog_text) |
| 38 | + |
| 39 | + # Replace the [Unreleased] section with the new version + cleaned subsections |
| 40 | + changelog_text = re.sub(r'## \[Unreleased\]', changelog_entry, cleaned_content) |
| 41 | + |
| 42 | + # Write the changes |
| 43 | + with open(changelog_path, 'w', encoding='UTF-8', newline='\n') as file: |
| 44 | + file.write(changelog_text) |
| 45 | + |
| 46 | + |
| 47 | +def update_validation_exceptions(new_version): |
| 48 | + """ |
| 49 | + Updates the ValidationExceptions.json file with the new package version. |
| 50 | + """ |
| 51 | + |
| 52 | + validation_file = f'{package_name}/ValidationExceptions.json' |
| 53 | + |
| 54 | + # If files do not exist, exit |
| 55 | + if not os.path.exists(validation_file): |
| 56 | + return |
| 57 | + |
| 58 | + # Update the PackageVersion in the exceptions |
| 59 | + with open(validation_file, 'rb') as f: |
| 60 | + json_text = f.read() |
| 61 | + data = json.loads(json_text) |
| 62 | + updated = False |
| 63 | + for exceptionElements in ["WarningExceptions", "ErrorExceptions"]: |
| 64 | + exceptions = data.get(exceptionElements) |
| 65 | + |
| 66 | + if exceptions is not None: |
| 67 | + for exception in exceptions: |
| 68 | + if 'PackageVersion' in exception: |
| 69 | + exception['PackageVersion'] = new_version |
| 70 | + updated = True |
| 71 | + |
| 72 | + # If no exceptions were updated, we do not need to write the file |
| 73 | + if not updated: |
| 74 | + return |
| 75 | + |
| 76 | + with open(validation_file, 'w', encoding='UTF-8', newline='\n') as json_file: |
| 77 | + json.dump(data, json_file, ensure_ascii=False, indent=2) |
| 78 | + json_file.write("\n") # Add newline cause Py JSON does not |
| 79 | + print(f" updated `{validation_file}`") |
| 80 | + |
| 81 | + |
| 82 | +def get_manifest_json_version(filename): |
| 83 | + """ |
| 84 | + Reads the package.json file and returns the version specified in it. |
| 85 | + """ |
| 86 | + with open(filename, 'rb') as f: |
| 87 | + json_text = f.read() |
| 88 | + data = json.loads(json_text) |
| 89 | + |
| 90 | + return data['version'] |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + manifest_path = f'{package_name}/package.json' |
| 95 | + package_version = get_manifest_json_version(manifest_path) |
| 96 | + |
| 97 | + # Update the ValidationExceptions.json file |
| 98 | + # with the new package version OR remove it if not a release branch |
| 99 | + update_validation_exceptions(package_version) |
| 100 | + # Clean the CHANGELOG and add latest entry |
| 101 | + update_changelog(package_version) |
0 commit comments