20
20
from typing import Dict , Set
21
21
22
22
from distro import distro
23
+ from enum import Enum
23
24
25
+ class VsWhereLocation (Enum ):
26
+ CHOCO = 1
27
+ DEFAULT = 2
24
28
25
29
def _query_yes_no (question : str , no_confirm : bool ) -> bool :
26
30
valid = {"yes" : True , "y" : True , "ye" : True , "no" : False , "n" : False }
@@ -52,6 +56,9 @@ def install(self, dependencies: Dict[str, Set[str]]) -> bool:
52
56
53
57
def install_compiler (self ) -> str :
54
58
raise Exception ("NotImplementedException" )
59
+
60
+ def ensureEnvironment (self ):
61
+ pass
55
62
56
63
def _install (self , dependencies : Dict [str , Set [str ]], replace_dict : Dict [str , Set [str ]], install_cmd : str ) -> bool :
57
64
dependencies .update ({k : v for k , v in replace_dict .items () if k in dependencies })
@@ -76,6 +83,7 @@ def run_cmd(self, cmd: str) -> bool:
76
83
return result .returncode == 0
77
84
78
85
86
+
79
87
class BrewPackageManager (PackageManager ):
80
88
def __init__ (self , no_confirm ):
81
89
PackageManager .__init__ (self , no_confirm )
@@ -174,40 +182,56 @@ def install_compiler(self) -> str:
174
182
return ""
175
183
176
184
177
- def _get_vs_dev_cmd_path () -> str :
185
+ def _get_vs_dev_cmd_path (vs_where_location : VsWhereLocation ) -> str :
186
+ if vs_where_location == VsWhereLocation .CHOCO :
187
+ vs_where_path = "vswhere"
188
+ else :
189
+ vs_where_path = "%ProgramFiles(x86)%\\ Microsoft Visual Studio\\ Installer\\ vswhere.exe"
190
+
178
191
vswhere_results = subprocess .run (
179
- "vswhere -products * -property installationPath -requires Microsoft.VisualStudio.Component.VC.ATL" ,
192
+ f" { vs_where_path } -products * -property installationPath -requires Microsoft.VisualStudio.Component.VC.ATL -version 17 " ,
180
193
capture_output = True )
181
194
182
195
for vswhere_result in vswhere_results .stdout .splitlines ():
183
196
possible_path = f"{ vswhere_result .decode ()} \\ Common7\\ Tools\\ VsDevCmd.bat"
184
197
if os .path .exists (possible_path ):
185
198
return f'"{ possible_path } "'
186
- raise Exception ( "Could not find valid Visual Studio installation" )
199
+ return None
187
200
188
201
189
- def _get_vs_dev_cmd () -> str :
190
- vs_dev_path = _get_vs_dev_cmd_path ()
191
- return f"{ vs_dev_path } -arch=x64 -host_arch=x64 "
202
+ def _get_vs_dev_cmd (vs_where_location : VsWhereLocation ) -> str :
203
+ vs_dev_path = _get_vs_dev_cmd_path (vs_where_location )
204
+ return f"{ vs_dev_path } "
192
205
193
206
194
- def _get_venv_path ():
195
- return pathlib .Path (__file__ ).parent .parent . resolve () / "build "
207
+ def _get_activate_venv_path ():
208
+ return pathlib .Path (__file__ ).parent .resolve () / "venv" / "Scripts" / "activate.bat "
196
209
197
210
198
- def _minifi_setup_env_str () -> str :
199
- return f"""@echo off
211
+ def _minifi_setup_env_str (vs_where_location : VsWhereLocation ) -> str :
212
+ return f"""
200
213
call refreshenv
201
- call " { _get_vs_dev_cmd () } "
214
+ call { _get_vs_dev_cmd (vs_where_location ) }
202
215
setlocal EnableDelayedExpansion
203
- set PATH=!PATH:C:\\ Strawberry\\ c\\ bin;=!;C:\Program Files\\ NASM;
216
+ set PATH=!PATH:C:\\ Strawberry\\ c\\ bin;=!;C:\\ Program Files\\ NASM;
204
217
endlocal & set PATH=%PATH%
205
218
set build_platform=x64
206
- { _get_venv_path ()}
219
+ IF "%VIRTUALENV%"=="" (
220
+ echo already in venv
221
+ ) ELSE (
222
+ { _get_activate_venv_path ()}
223
+ )
224
+
207
225
"""
208
226
227
+ def _create_minifi_setup_env_batch (vs_where_location : VsWhereLocation ) -> str :
228
+ with open ("build_environment.bat" ,"w" ) as f :
229
+ f .write (_minifi_setup_env_str (vs_where_location ))
230
+
209
231
210
232
class ChocolateyPackageManager (PackageManager ):
233
+
234
+
211
235
def __init__ (self , no_confirm ):
212
236
PackageManager .__init__ (self , no_confirm )
213
237
@@ -240,20 +264,35 @@ def _get_installed_packages(self) -> Set[str]:
240
264
lines .append ("NASM" ) # choco doesnt remember NASM
241
265
return set (lines )
242
266
267
+ def _acquire_vswhere (self ):
268
+ installed_packages = self ._get_installed_packages ()
269
+ if "vswhere" in installed_packages :
270
+ return VsWhereLocation .CHOCO
271
+ vswhere_default_path = "%ProgramFiles(x86)%\\ Microsoft Visual Studio\\ Installer\\ vswhere.exe"
272
+ if os .path .exists (vswhere_default_path ):
273
+ return VsWhereLocation .DEFAULT
274
+ self .install ({"vswhere" :{"vswhere" }})
275
+ return VsWhereLocation .CHOCO
276
+
277
+
243
278
def install_compiler (self ) -> str :
244
- self .install ({"visualstudio2022buildtools" : {'visualstudio2022buildtools --package-parameters "--wait --quiet '
245
- '--add Microsoft.VisualStudio.Workload.VCTools '
246
- '--add Microsoft.VisualStudio.Component.VC.ATL '
247
- '--includeRecommended"' },
248
- "vswhere" : {"vswhere" }})
279
+ vs_where_loc = self ._acquire_vswhere ()
280
+ vs_dev_path = _get_vs_dev_cmd_path (vs_where_loc )
281
+ if not vs_dev_path :
282
+ self .install ({"visualstudio2022buildtools" : {'visualstudio2022buildtools --package-parameters "--wait --quiet '
283
+ '--add Microsoft.VisualStudio.Workload.VCTools '
284
+ '--add Microsoft.VisualStudio.Component.VC.ATL '
285
+ '--includeRecommended"' }})
249
286
return ""
250
287
251
288
def run_cmd (self , cmd : str ) -> bool :
252
- cmd_command = f"refreshenv & { _get_vs_dev_cmd ()} & set PATH=!PATH:C:\\ Strawberry\\ c\\ bin;=!;C:\\ Program Files\\ NASM; & { cmd } "
253
- cmd_command_list = f'cmd /V:ON /C { cmd_command } '
254
- res = subprocess .run (cmd_command_list , check = True , text = True )
289
+ env_bat_path = pathlib .Path (__file__ ).parent .resolve () / "build_environment.bat"
290
+ res = subprocess .run (f"{ env_bat_path } & { cmd } " , check = True , text = True )
255
291
256
292
return res .returncode == 0
293
+
294
+ def ensureEnvironment (self ):
295
+ _create_minifi_setup_env_batch (self ._acquire_vswhere ())
257
296
258
297
259
298
def get_package_manager (no_confirm : bool ) -> PackageManager :
0 commit comments