Created
June 30, 2022 10:51
-
-
Save OmriSteiner/4738c7c41660273433f13af179090437 to your computer and use it in GitHub Desktop.
Remove UNITY_EDITOR from Unity csproj files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python3 | |
| import xml.etree.ElementTree as ET | |
| import argparse | |
| import glob | |
| import os.path | |
| def extract_namespace(xml_root): | |
| match xml_root.tag.split("}"): | |
| case [tag]: | |
| return "" | |
| case [namespace, *tag]: | |
| return namespace.lstrip("{") | |
| def remove_constants(root, ns): | |
| for pg in root.iterfind("{" + ns + "}PropertyGroup"): | |
| for dc in pg.iterfind("{" + ns + "}DefineConstants"): | |
| constants = dc.text.split(";") | |
| for constant in ("UNITY_EDITOR", "UNITY_EDITOR_WIN", "UNITY_EDITOR_64"): | |
| try: | |
| constants.remove(constant) | |
| except ValueError: | |
| pass | |
| dc.text = ";".join(constants) | |
| def fix_csproj(filepath): | |
| with open(filepath, "rb") as f: | |
| tree = ET.parse(f) | |
| root = tree.getroot() | |
| ns = extract_namespace(root) | |
| ET.register_namespace("", ns) | |
| remove_constants(root, ns) | |
| tree.write(filepath) | |
| def main(args): | |
| for csproj in glob.glob(os.path.join(args.directory, "*.csproj")): | |
| fix_csproj(csproj) | |
| def parse_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("directory") | |
| return parser.parse_args() | |
| if __name__ == "__main__": | |
| args = parse_args() | |
| main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment