@@ -178,6 +178,7 @@ class CSharpConfig:
178178 def __init__ (self , config : Dict [str , Any ]) -> None :
179179 root = config .get ('languages' , {}).get ('csharp' , {})
180180 self .static_embedding : Optional [str ] = root .get ('static_embedding' , None )
181+ self .csproj_template : Optional [str ] = root .get ('csproj_template' , None )
181182
182183
183184class CSharpLanguageEnvironment (LanguageEnvironment ):
@@ -186,16 +187,27 @@ def __init__(self, config: CSharpConfig) -> None:
186187 self .config = config
187188
188189 @staticmethod
189- def _create_runner_project (code : bytes , target_framework : str , output_dir ):
190- os .makedirs (str (output_dir ), exist_ok = True )
191- with open (output_dir / 'runner.csproj' , 'w' ) as f :
190+ def _write_default_project (output_file : pathlib .Path , target_framework : str ) -> None :
191+ with open (output_file , 'w' ) as f :
192192 f .write ('''<Project Sdk="Microsoft.NET.Sdk">
193193 <PropertyGroup>
194194 <OutputType>Exe</OutputType>
195195 <TargetFramework>{}</TargetFramework>
196196 </PropertyGroup>
197197</Project>''' .format (target_framework ))
198198
199+ def _write_csproj (self , output_file : pathlib .Path , csproj_template_path : Optional [pathlib .Path ], target_framework : str ) -> None :
200+ if csproj_template_path is None :
201+ self ._write_default_project (output_file , target_framework )
202+ elif not csproj_template_path .exists ():
203+ logger .warning ('%s is not found.' , str (csproj_template_path ))
204+ self ._write_default_project (output_file , target_framework )
205+ else :
206+ shutil .copy (str (csproj_template_path ), str (output_file ))
207+
208+ def _create_runner_project (self , code : bytes , target_framework : str , csproj_template_path : Optional [pathlib .Path ], output_dir : pathlib .Path ):
209+ os .makedirs (str (output_dir ), exist_ok = True )
210+ self ._write_csproj (output_dir / 'runner.csproj' , csproj_template_path , target_framework )
199211 with open (output_dir / 'main.cs' , 'wb' ) as f :
200212 f .write (code )
201213
@@ -205,7 +217,8 @@ def compile(self, path: pathlib.Path, *, basedir: pathlib.Path, tempdir: pathlib
205217 _check_env (path )
206218 target_framework = _get_target_framework (_resolve_csproj (path ))
207219 logger .info ('build: TargetFramework = %s' , target_framework )
208- self ._create_runner_project (self ._expand_code (path ), target_framework , output_dir )
220+ csproj_template_path = (basedir / pathlib .Path (self .config .csproj_template )) if self .config .csproj_template else None
221+ self ._create_runner_project (self ._expand_code (path ), target_framework , csproj_template_path , output_dir )
209222
210223 command = ['dotnet' , 'build' , str (output_dir / 'runner.csproj' ), '-c' , 'Release' , '-o' , str (output_dir / 'bin' )]
211224 logger .info ('$ %s' , ' ' .join (command ))
0 commit comments