Skip to content

Instantly share code, notes, and snippets.

@OmriSteiner
Created June 30, 2022 10:51
Show Gist options
  • Select an option

  • Save OmriSteiner/4738c7c41660273433f13af179090437 to your computer and use it in GitHub Desktop.

Select an option

Save OmriSteiner/4738c7c41660273433f13af179090437 to your computer and use it in GitHub Desktop.
Remove UNITY_EDITOR from Unity csproj files
#! /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