diff --git a/scripts/instruments.py b/scripts/instruments.py index 1fe2247..632de4c 100644 --- a/scripts/instruments.py +++ b/scripts/instruments.py @@ -2,6 +2,7 @@ from subprocess import call import shutil import os import re +import sln_toolkit as sln def MapToBackupName(origin_path): @@ -54,19 +55,8 @@ def ResetDirectory(base_dir, relative_path_to_files): return None def RemoveProjectFromSolution(abs_path_to_sln, project_names): - - sln_file = open(abs_path_to_sln, 'r+') - content = sln_file.read() - - for pn in project_names: - reg_pattern = r'\n*Project.*?"{0}".*?\n*EndProject'.format(pn) - content = re.sub(reg_pattern, "", content) - - # override file - sln_file.seek(0) - sln_file.write(content) - sln_file.truncate() - sln_file.close() + toolkit = sln.SolutionToolkit(abs_path_to_sln) + toolkit.RemoveProjects(project_names) def CleanSolution(mdtool, abs_path_to_sln, config): diff --git a/scripts/sln_toolkit.py b/scripts/sln_toolkit.py new file mode 100644 index 0000000..9aa121f --- /dev/null +++ b/scripts/sln_toolkit.py @@ -0,0 +1,38 @@ +import re + +class SolutionToolkitBase: + def RemoveProjectSectionsFrom(self, sln_file_content, project_names): + for pn in project_names: + reg_pattern = r'\n*Project.*?"{0}".*?\n*EndProject'.format(pn) + sln_file_content = re.sub(reg_pattern, "", sln_file_content) + + return sln_file_content + +class SolutionToolkit(SolutionToolkitBase): + def __init__(self, pathToSlnFile): + self._sln_path = pathToSlnFile + self._sln_file = None + + def RemoveProjects(self, project_names): + self.OpenSlnFile() + content = self.ReadSlnFileContent() + + new_content = self.RemoveProjectSectionsFrom(content, project_names) + + self.RewriteSlnFile(new_content) + self.CloseSlnFile() + + def OpenSlnFile(self): + self._sln_file = open(self._sln_path, 'r+') + + def CloseSlnFile(self): + self._sln_file.close() + + def ReadSlnFileContent(self): + content = self._sln_file.read() + return content + + def RewriteSlnFile(self, content): + self._sln_file.seek(0) + self._sln_file.write(content) + self._sln_file.truncate() \ No newline at end of file