@@ -207,14 +207,85 @@ public static string DeterminePublishLocation(string workingDirectory, string pr
207207
208208 public static string LookupTargetFrameworkFromProjectFile ( string projectLocation )
209209 {
210+
210211 var projectFile = FindProjectFileInDirectory ( projectLocation ) ;
212+ if ( string . IsNullOrEmpty ( projectFile ) )
213+ {
214+ throw new FileNotFoundException ( "Could not find a project file in the specified directory." ) ;
215+ }
211216
212- var xdoc = XDocument . Load ( projectFile ) ;
217+ var outputFile = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
218+ var targetsFile = FindTargetsFile ( ) ;
219+
220+ if ( ! File . Exists ( targetsFile ) )
221+ {
222+ throw new FileNotFoundException ( $ "Could not find the custom .targets file at { targetsFile } ") ;
223+ }
224+
225+ var arguments = new [ ]
226+ {
227+ "msbuild" ,
228+ projectFile ,
229+ "/nologo" ,
230+ "/t:_AmazonCommonToolsExtractTargetFrameworks" ,
231+ $ "/p:_AmazonCommonToolsTargetFrameworksFile={ outputFile } ",
232+ "/p:Configuration=Debug" ,
233+ $ "\" /p:CustomAfterMicrosoftCommonTargets={ targetsFile } \" ",
234+ $ "\" /p:CustomAfterMicrosoftCommonCrossTargetingTargets={ targetsFile } \" "
235+ } ;
236+
237+ var process = new Process
238+ {
239+ StartInfo = new ProcessStartInfo
240+ {
241+ FileName = "dotnet" ,
242+ Arguments = string . Join ( " " , arguments ) ,
243+ RedirectStandardOutput = true ,
244+ RedirectStandardError = true ,
245+ UseShellExecute = false ,
246+ CreateNoWindow = true
247+ }
248+ } ;
249+
250+ process . Start ( ) ;
251+ string output = process . StandardOutput . ReadToEnd ( ) ;
252+ process . WaitForExit ( ) ;
213253
214- var element = xdoc . XPathSelectElement ( "//PropertyGroup/TargetFramework" ) ;
215- return element ? . Value ;
254+ if ( process . ExitCode != 0 )
255+ {
256+ throw new Exception ( $ "MSBuild process exited with code { process . ExitCode } . See log for details.") ;
257+ }
258+
259+ if ( File . Exists ( outputFile ) )
260+ {
261+ var targetFrameworks = File . ReadAllLines ( outputFile ) ;
262+ File . Delete ( outputFile ) ; // Clean up the temporary file
263+
264+ if ( targetFrameworks . Length > 0 )
265+ {
266+ return targetFrameworks [ 0 ] ; // Return the first framework if there are multiple
267+ }
268+ }
269+
270+ return null ;
216271 }
217272
273+
274+ private static string FindTargetsFile ( )
275+ {
276+ var assemblyDir = Path . GetDirectoryName ( typeof ( DotNetCLIWrapper ) . Assembly . Location ) ;
277+ var searchPaths = new [ ]
278+ {
279+ Path . Combine ( AppContext . BaseDirectory , "Assets" ) ,
280+ Path . Combine ( assemblyDir , "Assets" ) ,
281+ AppContext . BaseDirectory ,
282+ assemblyDir ,
283+ } ;
284+
285+ return searchPaths . Select ( p => Path . Combine ( p , "AmazonCommonDotNetCliTools.targets" ) ) . FirstOrDefault ( File . Exists ) ;
286+ }
287+
288+
218289 /// <summary>
219290 /// Retrieve the `OutputType` property of a given project
220291 /// </summary>
0 commit comments