|
1 | 1 | namespace fsdocs |
2 | 2 |
|
3 | 3 | open System |
| 4 | +open System.Diagnostics |
4 | 5 | open System.IO |
5 | 6 | open System.Runtime.InteropServices |
6 | 7 | open System.Runtime.Serialization |
@@ -142,6 +143,22 @@ module Utils = |
142 | 143 | else |
143 | 144 | s + "/" |
144 | 145 |
|
| 146 | +module DotNetCli = |
| 147 | + |
| 148 | + /// Run `dotnet msbuild <args>` and receive the trimmed standard output. |
| 149 | + let msbuild (pwd: string) (args: string) : string = |
| 150 | + let psi = ProcessStartInfo "dotnet" |
| 151 | + psi.WorkingDirectory <- pwd |
| 152 | + psi.Arguments <- $"msbuild %s{args}" |
| 153 | + psi.RedirectStandardOutput <- true |
| 154 | + psi.UseShellExecute <- false |
| 155 | + use ps = new Process() |
| 156 | + ps.StartInfo <- psi |
| 157 | + ps.Start() |> ignore |
| 158 | + let output = ps.StandardOutput.ReadToEnd() |
| 159 | + ps.WaitForExit() |
| 160 | + output.Trim() |
| 161 | + |
145 | 162 | module Crack = |
146 | 163 |
|
147 | 164 | let (|ConditionEquals|_|) (str: string) (arg: string) = |
@@ -337,14 +354,26 @@ module Crack = |
337 | 354 | Ok(targetFrameworks, projOptions2) |
338 | 355 | | Error err -> GetProjectOptionsErrors(string err, msgs) |> Result.Error |
339 | 356 |
|
340 | | - let crackProjectFile slnDir extraMsbuildProperties (file: string) : CrackedProjectInfo = |
341 | | - |
| 357 | + let private ensureProjectWasRestored (file: string) = |
342 | 358 | let projDir = Path.GetDirectoryName(file) |
343 | | - |
344 | 359 | let projectAssetsJsonPath = Path.Combine(projDir, "obj", "project.assets.json") |
345 | 360 |
|
346 | | - if not (File.Exists(projectAssetsJsonPath)) then |
347 | | - failwithf "project '%s' not restored" file |
| 361 | + if File.Exists projectAssetsJsonPath then |
| 362 | + () |
| 363 | + else |
| 364 | + // In dotnet 8 <UseArtifactsOutput> was introduced, see https://learn.microsoft.com/en-us/dotnet/core/sdk/artifacts-output |
| 365 | + // We will try and use CLI-based project evaluation to determine the location of project.assets.json file |
| 366 | + // See https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#cli-based-project-evaluation |
| 367 | + try |
| 368 | + let path = DotNetCli.msbuild projDir "--getProperty:ProjectAssetsFile" |
| 369 | + |
| 370 | + if not (File.Exists path) then |
| 371 | + failwithf $"project '%s{file}' not restored" |
| 372 | + with ex -> |
| 373 | + failwithf $"Failed to detect if the project '%s{file}' was restored" |
| 374 | + |
| 375 | + let crackProjectFile slnDir extraMsbuildProperties (file: string) : CrackedProjectInfo = |
| 376 | + ensureProjectWasRestored file |
348 | 377 |
|
349 | 378 | let result = crackProjectFileAndIncludeTargetFrameworks slnDir extraMsbuildProperties file |
350 | 379 | //printfn "msgs = %A" msgs |
|
0 commit comments