Skip to content

Commit 4925d20

Browse files
committed
Add GetAllTfms for Nongraph path
1 parent df0ea78 commit 4925d20

File tree

4 files changed

+86
-66
lines changed

4 files changed

+86
-66
lines changed

src/Ionide.ProjInfo/ProjectLoader2.fs

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,22 @@ module Map =
5454
let union loses wins =
5555
Map.fold (fun acc key value -> Map.add key value acc) loses wins
5656

57-
let inline ofDict (dic) =
58-
dic
57+
let inline ofDict dictionary =
58+
dictionary
5959
|> Seq.map (|KeyValue|)
6060
|> Map.ofSeq
6161

62+
63+
let inline copyToDict (map: Map<_, _>) =
64+
// Have to use a mutable dictionary here because the F# Map doesn't have an Add method
65+
let dictionary = Dictionary<_, _>()
66+
67+
for KeyValue(k, v) in map do
68+
dictionary.Add(k, v)
69+
70+
dictionary :> IDictionary<_, _>
71+
72+
6273
module BuildErrorEventArgs =
6374

6475
let messages (e: BuildErrorEventArgs seq) =
@@ -244,35 +255,32 @@ module ProjectPropertyInstance =
244255

245256
module ProjectLoading =
246257

247-
let selectFirstTfm (projectPath: string) =
258+
let getAllTfms (projectPath: string) =
248259
let pi = ProjectInstance(projectPath)
249260

250-
match
261+
pi.Properties
262+
|> (ProjectPropertyInstance.tryFind "TargetFramework"
263+
>> Option.map Array.singleton)
264+
|> Option.orElseWith (fun () ->
251265
pi.Properties
252-
|> ProjectPropertyInstance.tryFind "TargetFramework"
253-
with
254-
| Some v -> Some v
255-
| None ->
256-
match
257-
pi.Properties
258-
|> ProjectPropertyInstance.tryFind "TargetFrameworks"
259-
with
260-
| None -> None
261-
| Some tfms ->
262-
match tfms.Split(';') with
263-
| [||] -> None
264-
| tfms -> Array.tryHead tfms
265-
266-
let defaultProjectInstanceFactory tfmSelector (projectPath: string) (xml: Dictionary<string, string>) (collection: ProjectCollection) =
267-
268-
let tfm = tfmSelector projectPath
269-
270-
let props = Map.union (Map.ofDict xml) (Map.ofDict collection.GlobalProperties)
271-
// |> Map.mapAddSome "TargetFramework" tfm
266+
|> ProjectPropertyInstance.tryFind "TargetFrameworks"
267+
|> Option.bind (fun tfms ->
268+
tfms.Split(
269+
';',
270+
StringSplitOptions.TrimEntries
271+
||| StringSplitOptions.RemoveEmptyEntries
272+
)
273+
|> Option.ofObj
274+
)
275+
)
272276

273-
let pi = ProjectInstance(projectPath, props, toolsVersion = null, projectCollection = collection)
277+
let selectFirstTfm (projectPath: string) =
278+
getAllTfms projectPath
279+
|> Option.bind Array.tryHead
274280

275-
pi
281+
let defaultProjectInstanceFactory (projectPath: string) (xml: Dictionary<string, string>) (collection: ProjectCollection) =
282+
let props = Map.union (Map.ofDict xml) (Map.ofDict collection.GlobalProperties)
283+
ProjectInstance(projectPath, props, toolsVersion = null, projectCollection = collection)
276284

277285

278286
type ProjectLoader2 =
@@ -293,6 +301,30 @@ type ProjectLoader2 =
293301
entryProjectFiles
294302
|> Seq.map (fun file -> ProjectLoader2.EvaluateAsProject(file, ?globalProperties = globalProperties, ?projectCollection = projectCollection))
295303

304+
static member EvaluateAsProjectsAllTfms(entryProjectFiles: string seq, ?globalProperties: IDictionary<string, string>, ?projectCollection: ProjectCollection) =
305+
let globalProperties =
306+
globalProperties
307+
|> Option.map Map.ofDict
308+
|> Option.defaultValue Map.empty
309+
310+
entryProjectFiles
311+
|> Seq.collect (fun path ->
312+
ProjectLoading.getAllTfms path
313+
|> Option.toArray
314+
|> Array.collect (
315+
Array.map (fun tfm ->
316+
ProjectLoader2.EvaluateAsProject(
317+
path,
318+
globalProperties =
319+
(globalProperties
320+
|> Map.add "TargetFramework" tfm
321+
|> Map.copyToDict),
322+
?projectCollection = projectCollection
323+
)
324+
)
325+
)
326+
)
327+
296328
static member EvaluateAsGraph(entryProjectFile: string, ?globalProperties: IDictionary<string, string>, ?projectCollection: ProjectCollection, ?projectInstanceFactory, ?ct: CancellationToken) =
297329
let globalProperties = defaultArg globalProperties null
298330
ProjectLoader2.EvaluateAsGraph([ ProjectGraphEntryPoint(entryProjectFile, globalProperties = globalProperties) ], ?projectCollection = projectCollection, ?projectInstanceFactory = projectInstanceFactory, ?ct = ct)
@@ -301,23 +333,14 @@ type ProjectLoader2 =
301333
let pc = defaultArg projectCollection ProjectCollection.GlobalProjectCollection
302334
let ct = defaultArg ct CancellationToken.None
303335

304-
let projectInstanceFactory =
305-
defaultArg projectInstanceFactory (ProjectLoading.defaultProjectInstanceFactory ProjectLoading.selectFirstTfm)
336+
let projectInstanceFactory = defaultArg projectInstanceFactory ProjectLoading.defaultProjectInstanceFactory
306337

307338
ProjectGraph(entryProjectFile, pc, projectInstanceFactory, ct)
308339

309340

310341
static member EvaluateAsGraphAllTfms(entryProjectFile: ProjectGraphEntryPoint seq, ?projectCollection: ProjectCollection, ?projectInstanceFactory) =
311-
312-
let pc = defaultArg projectCollection ProjectCollection.GlobalProjectCollection
313-
314-
let projectInstanceFactory =
315-
defaultArg projectInstanceFactory (ProjectLoading.defaultProjectInstanceFactory ProjectLoading.selectFirstTfm)
316-
317342
let graph =
318-
ProjectLoader2.EvaluateAsGraph(entryProjectFile, projectCollection = pc, projectInstanceFactory = projectInstanceFactory)
319-
320-
let targets = graph.ProjectNodes
343+
ProjectLoader2.EvaluateAsGraph(entryProjectFile, ?projectCollection = projectCollection, ?projectInstanceFactory = projectInstanceFactory)
321344

322345
let inline tryGetTfmFromProps (node: ProjectGraphNode) =
323346
match node.ProjectInstance.GlobalProperties.TryGetValue "TargetFramework" with
@@ -326,7 +349,7 @@ type ProjectLoader2 =
326349

327350
// Then we only care about those with a TargetFramework
328351
let projects =
329-
targets
352+
graph.ProjectNodes
330353
|> Seq.choose (fun node ->
331354
tryGetTfmFromProps node
332355
|> Option.orElseWith (fun () ->
@@ -336,7 +359,7 @@ type ProjectLoader2 =
336359
|> Option.map (fun _ -> ProjectGraphEntryPoint(node.ProjectInstance.FullPath, globalProperties = node.ProjectInstance.GlobalProperties))
337360
)
338361

339-
ProjectLoader2.EvaluateAsGraph(projects, projectCollection = pc, projectInstanceFactory = projectInstanceFactory)
362+
ProjectLoader2.EvaluateAsGraph(projects, ?projectCollection = projectCollection, ?projectInstanceFactory = projectInstanceFactory)
340363

341364
static member Execution(session: BuildManagerSession, graph: ProjectGraph, ?targetsToBuild: string array, ?flags: BuildRequestDataFlags, ?ct: CancellationToken) =
342365
task {

test/Ionide.ProjInfo.Tests/TestAssets.fs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,27 @@ let ``loader2-solution-with-2-projects`` = {
345345
EntryPoints = [ "loader2-solution-with-2-projects.sln" ]
346346
Expects =
347347
fun projectsAfterBuild ->
348-
Expect.equal (Seq.length projectsAfterBuild) 2 "projects count"
348+
Expect.equal (Seq.length projectsAfterBuild) 3 "projects count"
349349

350-
let classlibf1 =
350+
let classlibf1s =
351351
projectsAfterBuild
352-
|> Seq.find (fun x -> x.ProjectFileName.EndsWith("classlibf1.fsproj"))
352+
|> Seq.filter (fun x -> x.ProjectFileName.EndsWith("classlibf1.fsproj"))
353+
354+
Expect.hasLength classlibf1s 2 ""
355+
356+
let classlibf1net80 =
357+
classlibf1s
358+
|> Seq.find (fun x -> x.TargetFramework = "net8.0")
359+
360+
Expect.equal classlibf1net80.SourceFiles.Length 3 "classlibf1 source files"
361+
362+
363+
let classlibf1ns21 =
364+
classlibf1s
365+
|> Seq.find (fun x -> x.TargetFramework = "netstandard2.1")
366+
367+
Expect.equal classlibf1ns21.SourceFiles.Length 3 "classlibf1 source files"
353368

354-
Expect.equal classlibf1.SourceFiles.Length 3 "classlibf1 source files"
355-
Expect.equal classlibf1.TargetFramework "net8.0" "classlibf1 target framework"
356369

357370
let classlibf2 =
358371
projectsAfterBuild

test/Ionide.ProjInfo.Tests/Tests.fs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,16 +1558,15 @@ let buildManagerSessionTests toolsPath =
15581558
``loader2-solution-with-2-projects``
15591559
(fun env ->
15601560
task {
1561-
1562-
let path =
1561+
let entrypoints =
15631562
env.Entrypoints
1564-
|> Seq.map ProjectGraphEntryPoint
1563+
|> Seq.map ProjectGraphEntryPoint
15651564

15661565
let loggers = env.Binlog.Loggers
15671566

15681567
// Evaluation
15691568
use pc = projectCollection ()
1570-
let graph = ProjectLoader2.EvaluateAsGraphAllTfms(path, pc)
1569+
let graph = ProjectLoader2.EvaluateAsGraphAllTfms(entrypoints, pc)
15711570

15721571
// Execution
15731572
let bp = BuildParameters(Loggers = loggers)
@@ -1615,7 +1614,7 @@ let buildManagerSessionTests toolsPath =
16151614

16161615
// Evaluation
16171616
use pc = projectCollection ()
1618-
let graph = ProjectLoader2.EvaluateAsProjects(entrypoints, projectCollection = pc)
1617+
let graph = ProjectLoader2.EvaluateAsProjectsAllTfms(entrypoints, projectCollection = pc)
16191618

16201619
// Execution
16211620
let bp = BuildParameters(Loggers = loggers)
@@ -1634,22 +1633,6 @@ let buildManagerSessionTests toolsPath =
16341633
| _ -> None
16351634
)
16361635

1637-
// Parse
1638-
// let projectsAfterBuild =
1639-
// match result with
1640-
// | Ok result ->
1641-
// ProjectLoader2.Parse result
1642-
// |> Seq.choose (
1643-
// function
1644-
// | Ok(LoadedProjectInfo.StandardProjectInfo x) -> Some x
1645-
// | _ -> None
1646-
// )
1647-
// | Result.Error(GraphBuildErrors.BuildErr(result, errorLogs)) ->
1648-
// let results: Dictionary<ProjectGraphNode, Result<BuildResult, BuildErrors>> =
1649-
// GraphBuildResult.resultsByNode (result, errorLogs)
1650-
1651-
// failwith "Build failed"
1652-
16531636
env.Data.Expects projectsAfterBuild
16541637
}
16551638
)
@@ -2740,6 +2723,7 @@ let traversalProjectTest toolsPath loaderType workspaceFactory =
27402723
$"can crack traversal projects - {loaderType}"
27412724
(fun () ->
27422725
let logger = Log.create "Test 'can crack traversal projects'"
2726+
27432727
let fs = FileUtils(logger)
27442728
let projPath = pathForProject ``traversal project``
27452729
// // need to build the projects first so that there's something to latch on to

test/examples/loader2-solution-with-2-projects/src/classlibf1/classlibf1.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
</PropertyGroup>
77

0 commit comments

Comments
 (0)