from pathlib import Path class VisualStudioContext: def __init__( self, version, year, prompt_path: Path, msbuild_path: Path, msbuild_arg_prefix, ): self.version = version self.year = year self.prompt_path = prompt_path self.msbuild_path = msbuild_path self._msbuild_arg_prefix = msbuild_arg_prefix def prompt_cmd(self): return f'{self.prompt_path} -arch=x64 -host_arch=x64' def msbuild_flag(self, flag): return f'{self._msbuild_arg_prefix}{flag}' def msbuild_clp(self, key, value=None): rv = f'{self._msbuild_arg_prefix}clp:{key}' if value is not None: rv = f'{rv}={value}' return rv def msbuild_target(self, target): return f'{self._msbuild_arg_prefix}t:{target}' def msbuild_prop(self, key, value): return f'{self._msbuild_arg_prefix}p:{key}={value}' def get_supported_vs_contexts(): '''Gets a list of supported Visual Studio contexts (versions)''' # vs 17 vs17_path = Path('C:/Program Files/Microsoft Visual Studio/2022/Community') vs17 = VisualStudioContext( 17, 2022, vs17_path.joinpath('Common7/Tools/VsDevCmd.bat'), vs17_path.joinpath('Msbuild/Current/Bin/amd64/MSBuild.exe'), '-', ) # vs 16 vs16_path = Path( 'C:/Program Files (x86)/Microsoft Visual Studio/2019/Community') vs16 = VisualStudioContext( 16, 2019, vs16_path.joinpath('Common7/Tools/VsDevCmd.bat'), vs16_path.joinpath('MSBuild/Current/Bin/MSBuild.exe'), '/', ) return [vs17, vs16] def get_preferred_vs_context(): '''Gets the most preferred Visual Studio context (version) on this computer''' vs_ctxs = get_supported_vs_contexts() for vs_context in vs_ctxs: if vs_context.prompt_path.exists(): return vs_context supported_versions = ', '.join([str(v.version) for v in vs_ctxs]) raise ValueError( f'Failed to find supported Visual Studio version (works with {supported_versions})' )