This repository has been archived by the owner on May 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
new_refs.py
99 lines (94 loc) · 3.77 KB
/
new_refs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import shutil
from optparse import OptionParser,OptionError
def main():
usage = "Usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-i","--inputFile",dest="inputFile",
help="solution file")
try:
(options,args) = parser.parse_args()
except OptionError,e:
logger.error("Unable to parse command line options: %s" % e.msg)
sys.exit(1)
#map project paths to GUIDs
f = open(options.inputFile, 'rU')
ii = 1
projects = dict()
for line in f:
line = line.strip()
if line.startswith("Project("):
projectDefinition = line.split("=")
projectComponents = projectDefinition[1].split(",")
projectPath = projectComponents[1]
projectPath = projectPath.strip()
projectGuid = projectComponents[2]
projectGuid = projectGuid.strip()
projectPath = projectPath.replace("\"", "")
projectGuid = projectGuid.replace("\"", "")
projects[projectGuid] = projectPath
#for key in projects.iterkeys():
# print key, projects[key]
cwd = os.getcwd() + "\\"
#make a second pass looking for references
f = open(options.inputFile, 'rU')
ii = 1
referencingProjectName = ""
referencedProjects = list()
state = 0 #0 means simply looping, 1 means collecting references, 2 means writing out references
for line in f:
line = line.strip()
if line.startswith("Project("):
projectDefinition = line.split("=")
projectComponents = projectDefinition[1].split(",")
referencingProjectName = projectComponents[2]
referencingProjectName = referencingProjectName.strip()
referencingProjectName = referencingProjectName.replace("\"", "")
elif line.startswith("ProjectSection(ProjectDependencies)"):
state = 1
elif line.startswith("EndProjectSection"):
state = 2
elif 1 == state:
projectDefinition = line.split("=")
projectDefinition = projectDefinition[0].strip()
referencedProjects.append(projectDefinition)
elif 2 == state:
output = "\t<ItemGroup>\n"
projectFileName = projects[referencingProjectName]
projectFileNameFullPath = cwd + projectFileName
#print "Full path " + projectFileNameFullPath
for project in referencedProjects:
output += "\t\t<ProjectReference Include=\""
referencedProjectFullPath = cwd + projects[project]
relProjectFolder = os.path.dirname(projectFileNameFullPath)
referencedProjectRelPath = os.path.relpath(referencedProjectFullPath, relProjectFolder)
output += referencedProjectRelPath
output += "\">\n"
output += "\t\t\t<Project>" + project + "</Project>\n"
output += "\t\t</ProjectReference>\n"
output += "\t</ItemGroup>\n"
#print output
#reset state
referencedProjects = list()
state = 0
#write output to project file
f2 = open(projectFileName, 'rU')
f3 = open(projectFileName + ".tmp", 'w')
lastLine = ""
firstPass = True
for line in f2:
if firstPass:
lastLine = line
firstPass = False
else:
f3.write(lastLine)
lastLine = line
f3.write(output)
f3.write(lastLine)
f2.close()
f3.close()
shutil.copyfile(projectFileName, projectFileName + ".orig")
shutil.copyfile(projectFileName + ".tmp", projectFileName)
print "Updating " + projectFileName
if __name__ == "__main__":
main()