Skip to content

Instantly share code, notes, and snippets.

@avakar
Last active April 28, 2018 13:14
Show Gist options
  • Save avakar/5075992 to your computer and use it in GitHub Desktop.
Save avakar/5075992 to your computer and use it in GitHub Desktop.
The script will recursively walk a directory and modify all .vcxproj.filter files so that the filters reflect the filesystem structure. It will also update .sln files so that they would open in msvc11, and update .vcxproj files so that Debug configurations would have optimizations disabled. I use the script after generating msvc projects from Qt…
import sys, os.path
from xml.etree import ElementTree
import xml.dom.minidom
def process_vcxproj_file(fname):
with open(fname, 'rb') as fin:
proj = fin.read()
add_optimize_after = ' <ItemDefinitionGroup Condition="&apos;$(Configuration)|$(Platform)&apos;==&apos;Debug|Win32&apos;">\r\n <ClCompile>'
proj = proj.replace(add_optimize_after, add_optimize_after + '\r\n <Optimization>Disabled</Optimization>')
with open(fname, 'wb') as fout:
fout.write(proj)
def process_file(fname):
print fname
doc = xml.dom.minidom.parse(fname)
base_path = os.path.split(fname)[0]
filters = set()
new_filters = set()
for item_group in doc.getElementsByTagName("ItemGroup"):
for item in item_group.childNodes:
if item.nodeType != item.ELEMENT_NODE:
continue
if item.tagName != 'Filter':
inc = item.getAttribute('Include')
if not inc:
continue
filterList = item.getElementsByTagName('Filter')
if filterList.length:
inc = os.path.split(inc)[0]
inc = os.path.join(base_path, inc)
inc = os.path.relpath(inc, base_path)
if inc == '.':
inc = ''
new_filter = inc
if filterList[0].childNodes.length != 0:
filterList[0].firstChild.data = new_filter
while new_filter:
new_filters.add(new_filter)
old_filter = new_filter
new_filter = os.path.split(new_filter)[0]
if new_filter == old_filter:
break
else:
filters.add((item_group, item))
for parent, filter in filters:
parent.removeChild(filter)
item_groups_to_clean = set()
for item_group in doc.getElementsByTagName('ItemGroup'):
if all((child.nodeType == child.TEXT_NODE for child in item_group.childNodes)):
item_groups_to_clean.add(item_group)
for item_group in item_groups_to_clean:
item_group.parentNode.removeChild(item_group)
elem = doc.createElement('ItemGroup')
doc.documentElement.appendChild(elem)
for nf in sorted(new_filters):
if not nf:
continue
filelem = doc.createElement('Filter')
filelem.setAttribute('Include', nf)
elem.appendChild(filelem)
with open(fname, 'w') as fout:
doc.writexml(fout)
def fixup_sln(fname):
with open(fname, 'r') as fin:
lines = list(fin)
for i, line in enumerate(lines):
lines[i] = line.replace('# Visual Studio 2010', '# Visual Studio 2012')
with open(fname, 'w') as fout:
fout.writelines(lines)
def _main(argv):
for dirpath, dirnames, filenames in os.walk(os.path.split(argv[1])[0]):
# Do not walk down the subrepositories
if '.git' in filenames:
del dirnames[:]
continue
for filename in filenames:
if filename.endswith('.vcxproj'):
process_vcxproj_file(os.path.join(dirpath, filename))
if filename.endswith('.vcxproj.filters'):
process_file(os.path.join(dirpath, filename))
if filename.endswith('.sln'):
fixup_sln(os.path.join(dirpath, filename))
if __name__ == '__main__':
print sys.argv
sys.exit(_main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment