66from .types import Runtime , RuntimeInfo , StrOrPath
77from .util import check_result
88
9- __all__ = ["DotnetCoreRuntime" ]
9+ __all__ = ["DotnetCoreRuntime" , "DotnetCoreCommandRuntime" ]
1010
1111_IS_SHUTDOWN = False
1212
1313
14- class DotnetCoreRuntime (Runtime ):
15- def __init__ (self , runtime_config : Path , dotnet_root : Path , ** params : str ):
14+ class DotnetCoreRuntimeBase (Runtime ):
15+ _version : str
16+
17+ def __init__ (self , dotnet_root : Path ):
1618 self ._handle = None
1719
1820 if _IS_SHUTDOWN :
1921 raise RuntimeError ("Runtime can not be reinitialized" )
2022
2123 self ._dotnet_root = Path (dotnet_root )
2224 self ._dll = load_hostfxr (self ._dotnet_root )
23- self ._handle = _get_handle (self ._dll , self ._dotnet_root , runtime_config )
2425 self ._load_func = None
2526
26- for key , value in params .items ():
27- self [key ] = value
28-
29- # TODO: Get version
30- self ._version = "<undefined>"
31-
3227 @property
3328 def dotnet_root (self ) -> Path :
3429 return self ._dotnet_root
@@ -122,7 +117,31 @@ def info(self):
122117 )
123118
124119
125- def _get_handle (dll , dotnet_root : StrOrPath , runtime_config : StrOrPath ):
120+ class DotnetCoreRuntime (DotnetCoreRuntimeBase ):
121+ def __init__ (self , runtime_config : Path , dotnet_root : Path , ** params : str ):
122+ super ().__init__ (dotnet_root )
123+ self ._handle = _get_handle_for_runtime_config (self ._dll , self ._dotnet_root , runtime_config )
124+
125+ for key , value in params .items ():
126+ self [key ] = value
127+
128+ # TODO: Get version
129+ self ._version = "<undefined>"
130+
131+
132+ class DotnetCoreCommandRuntime (DotnetCoreRuntimeBase ):
133+ def __init__ (self , entry_dll : Path , dotnet_root : Path , ** params : str ):
134+ super ().__init__ (dotnet_root )
135+ self ._handle = _get_handle_for_dotnet_command_line (self ._dll , self ._dotnet_root , entry_dll )
136+
137+ for key , value in params .items ():
138+ self [key ] = value
139+
140+ # TODO: Get version
141+ self ._version = "<undefined>"
142+
143+
144+ def _get_handle_for_runtime_config (dll , dotnet_root : StrOrPath , runtime_config : StrOrPath ):
126145 params = ffi .new ("hostfxr_initialize_parameters*" )
127146 params .size = ffi .sizeof ("hostfxr_initialize_parameters" )
128147 # params.host_path = ffi.new("char_t[]", encode(sys.executable))
@@ -140,6 +159,29 @@ def _get_handle(dll, dotnet_root: StrOrPath, runtime_config: StrOrPath):
140159 return handle_ptr [0 ]
141160
142161
162+ def _get_handle_for_dotnet_command_line (dll , dotnet_root : StrOrPath , entry_dll : StrOrPath ):
163+ params = ffi .new ("hostfxr_initialize_parameters*" )
164+ params .size = ffi .sizeof ("hostfxr_initialize_parameters" )
165+ params .host_path = ffi .NULL
166+ dotnet_root_p = ffi .new ("char_t[]" , encode (str (Path (dotnet_root ))))
167+ params .dotnet_root = dotnet_root_p
168+
169+ handle_ptr = ffi .new ("hostfxr_handle*" )
170+
171+ args_ptr = ffi .new ("char_t*[1]" )
172+ arg_ptr = ffi .new ("char_t[]" , encode (str (Path (entry_dll ))))
173+ args_ptr [0 ] = arg_ptr
174+ res = dll .hostfxr_initialize_for_dotnet_command_line (
175+ 1 ,
176+ args_ptr ,
177+ params , handle_ptr
178+ )
179+
180+ check_result (res )
181+
182+ return handle_ptr [0 ]
183+
184+
143185def _get_load_func (dll , handle ):
144186 delegate_ptr = ffi .new ("void**" )
145187
0 commit comments