Skip to content

Instantly share code, notes, and snippets.

@drojf
Last active May 27, 2018 08:00
Show Gist options
  • Save drojf/c630d4c82107b115e00173ca9ac529d0 to your computer and use it in GitHub Desktop.
Save drojf/c630d4c82107b115e00173ca9ac529d0 to your computer and use it in GitHub Desktop.
# This program does the following
# 1. Collect all the using statements of all the files
# 2. Collect the bodies of all the files (everything after the using statement)
# 3. Create the final merged file, which is all the using statements, followed by all the bodies.
import os
import sys
if len(sys.argv) != 2:
print('This program takes exactly one arguments: the folder containing cs files to merge')
exit(-1)
merge_folder = sys.argv[1]
using_statements = [] # contains the using statements of each file
file_bodies = [] # contains everything after the using statements of each file
for subdir, dirs, files in os.walk(merge_folder):
for filename in files:
filepath = os.path.join(subdir, filename)
_, ext = os.path.splitext(filename)
if ext != '.cs':
continue
with open(filepath, encoding='utf-8') as fileToMerge:
entire_file = fileToMerge.read()
# find something like 'namespace Mono.Xml.Xsl.Operations' and then split the file just before 'namespace'
splitLocation = entire_file.find('namespace')
if splitLocation == -1:
print("ERROR: Couldn't find namespace keyword in file '{}'".format(filepath))
exit(-1)
using_statements.append(entire_file[:splitLocation])
file_bodies.append(entire_file[splitLocation:])
# output to STDOUT
print(''.join(using_statements))
print(''.join(file_bodies))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment