Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A quick script to clean the hierarchy of .xib files as they related to .cs and .designer.cs files
import sys, os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element
namespace = ""
def find_files_with_ext(extension):
return [f for f in os.listdir('.') if os.path.isfile(f) and os.path.splitext(f)[1] == extension]
def find_ele_in_itemgroup(typestr):
return "{{{0}}}ItemGroup/{{{0}}}{1}".format(namespace, typestr)
def parse_filename_and_base(path):
fname = os.path.basename(path.replace("\\", "/"))
fbasename = fname[:fname.find('.')]
return (fname, fbasename)
if __name__ == "__main__":
csproj_path = find_files_with_ext(".csproj")[0]
print "Parsing %s" % (csproj_path)
ET.register_namespace("", namespace)
tree = ET.parse(csproj_path)
root = tree.getroot()
xibs = root.findall(find_ele_in_itemgroup("InterfaceDefinition"))
for i in xibs:
xib_path = i.attrib["Include"]
xib_name, xib_basename = parse_filename_and_base(xib_path)
code_behinds = root.findall(find_ele_in_itemgroup("Compile"))
for cb in code_behinds:
cs_path = cb.attrib["Include"]
cs_name, cs_basename = parse_filename_and_base(cs_path)
if cs_basename == xib_basename:
lencb = len(cb)
expected_file = "{0}.xib".format(xib_basename)
if lencb == 0:
ele = Element("DependentUpon")
ele.text = expected_file
print "Appending element for %s" % (cs_name)
elif lencb == 1:
if cb[0].text != expected_file:
cb[0].text = expected_file
print "Updating element for %s" % (cs_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment