-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualstudio-remove_bloated_configurations.py
161 lines (129 loc) · 4.44 KB
/
visualstudio-remove_bloated_configurations.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import stat
import argparse
IGNORED_FILE_NAMES = [
'.git',
'.vs',
'.vscode',
]
def find_visual_studio_config_files(
inpath: str,
solution_file_list: list,
vcxproj_file_list: list,
):
_, filename = os.path.split(inpath)
lowered_filename = filename.lower()
if lowered_filename in IGNORED_FILE_NAMES:
return
file_stat = os.stat(inpath)
if stat.S_ISREG(file_stat.st_mode):
_, ext = os.path.splitext(lowered_filename)
if ext == '.sln':
solution_file_list.append(inpath)
elif ext == '.vcxproj':
vcxproj_file_list.append(inpath)
elif stat.S_ISDIR(file_stat.st_mode):
child_filename_list = os.listdir(inpath)
for child_filename in child_filename_list:
child_filepath = os.path.join(inpath, child_filename)
find_visual_studio_config_files(
child_filepath,
solution_file_list,
vcxproj_file_list,
)
SOLUTION_FILE_BLACKLIST_CONFIG = [
'Debug|x86',
'Release|x86',
'Release|x64',
]
def remove_visual_studio_config_from_solution_file(inpath: str):
content_bs = open(inpath, 'rb').read()
content_str = content_bs.decode('utf-8')
lines = content_str.splitlines()
output_lines = []
for line in lines:
detected = False
for s in SOLUTION_FILE_BLACKLIST_CONFIG:
if s in line:
detected = True
break
if detected:
continue
output_lines.append(line)
output_str = '\n'.join(output_lines)
output_str += '\n'
output_bs = output_str.encode('utf-8')
is_diff = (content_bs != output_bs)
return is_diff, output_bs
VXCPROJ_FILE_BLACKLIST_STRING_LIST = [
'Debug|Win32',
'Release|Win32',
'Release|x64',
]
VCXPROJ_FILE_BLACKLIST_CONFIG_INFO_LIST = [
# (tag_name, attribute_name, attribute_value_list)
('ProjectConfiguration', 'Include', VXCPROJ_FILE_BLACKLIST_STRING_LIST),
('ImportGroup', 'Condition', VXCPROJ_FILE_BLACKLIST_STRING_LIST),
('ItemDefinitionGroup', 'Condition', VXCPROJ_FILE_BLACKLIST_STRING_LIST),
('PropertyGroup', 'Condition', VXCPROJ_FILE_BLACKLIST_STRING_LIST),
]
def remove_visual_studio_config_from_vcxproj_file(inpath: str):
content_bs = open(inpath, 'rb').read()
content_str = content_bs.decode('utf-8')
lines = content_str.splitlines()
output_lines = []
ignoring_tag_name = None
for line in lines:
if ignoring_tag_name is not None:
if f'</{ignoring_tag_name}>' in line:
ignoring_tag_name = None
continue
else:
detected = False
for tag_name, attribute_name, attribute_value_list in VCXPROJ_FILE_BLACKLIST_CONFIG_INFO_LIST:
if (tag_name in line) and (attribute_name in line):
for attribute_value in attribute_value_list:
if attribute_value in line:
ignoring_tag_name = tag_name
detected = True
break
if detected:
break
if detected:
continue
else:
output_lines.append(line)
output_str = '\n'.join(output_lines)
output_str += '\n'
output_bs = output_str.encode('utf-8')
is_diff = (content_bs != output_bs)
return is_diff, output_bs
def main():
parser = argparse.ArgumentParser()
parser.add_argument('inpath', nargs='?', default='.')
parser.add_argument('-r', '--r', '-run', '--run', dest='run', action='store_true')
args = parser.parse_args()
print('args', args)
inpath = args.inpath
run = args.run
solution_file_list = []
vcxproj_file_list = []
find_visual_studio_config_files(
inpath,
solution_file_list,
vcxproj_file_list,
)
for solution_file in solution_file_list:
is_diff, output_bs = remove_visual_studio_config_from_solution_file(solution_file)
if is_diff:
print(f'x {solution_file}')
if run:
open(solution_file, 'wb').write(output_bs)
for vcxproj_file in vcxproj_file_list:
is_diff, output_bs = remove_visual_studio_config_from_vcxproj_file(vcxproj_file)
if is_diff:
print(f'x {vcxproj_file}')
if run:
open(vcxproj_file, 'wb').write(output_bs)
if __name__ == '__main__':
main()