Skip to content

Commit d9b0b7b

Browse files
dsymeDon Symebaronfel
authored
Allow global properties to be provided to project loaders (#107)
Co-authored-by: Don Syme <[email protected]> Co-authored-by: Chet Husk <[email protected]>
1 parent 0db8ac2 commit d9b0b7b

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

CHANGELOG.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.52.0] - 2021-04-13
8+
9+
### Changed
10+
11+
- [Allow global properties to be provided to project loaders](https://github.com/ionide/proj-info/pull/107)
12+
713
## [0.51.0] - 2021-03-15
814

9-
* Change the order of calls so that FSAC doesn't have a deadlock with its current usage of this library
15+
### Changed
16+
17+
- Change the order of calls so that FSAC doesn't have a deadlock with its current usage of this library
18+
1019
## [0.50.0] - 2021-03-13
1120

12-
* Introduce a pluggable abstraction for creating workspaces to allow for independent experimentation
13-
* Introduce a workspace implementation for MsBuild Graph Build mode, which should be a large performance boost for consumers
14-
* introduce debouncing to prevent rebuilds when invoked multiple times in a short timeframe
21+
### Changed
22+
23+
- Introduce a pluggable abstraction for creating workspaces to allow for independent experimentation
24+
- Introduce a workspace implementation for MsBuild Graph Build mode, which should be a large performance boost for consumers
25+
- introduce debouncing to prevent rebuilds when invoked multiple times in a short timeframe
1526

1627
## [0.49.0] - 2021-03-03
1728

src/Ionide.ProjInfo/Library.fs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module ProjectLoader =
7878
else
7979
[ logger ]
8080

81-
let getGlobalProps (path: string) (tfm: string option) =
81+
let getGlobalProps (path: string) (tfm: string option) (globalProperties: (string * string) list)=
8282
dict [ "ProvideCommandLineArgs", "true"
8383
"DesignTimeBuild", "true"
8484
"SkipCompilerExecution", "true"
@@ -91,19 +91,20 @@ module ProjectLoader =
9191
"TargetFramework", tfm.Value
9292
if path.EndsWith ".csproj" then
9393
"NonExistentFile", Path.Combine("__NonExistentSubDir__", "__NonExistentFile__")
94-
"DotnetProjInfo", "true" ]
94+
"DotnetProjInfo", "true"
95+
yield! globalProperties ]
9596

9697

9798
let buildArgs =
9899
[| "ResolvePackageDependenciesDesignTime"
99100
"_GenerateCompileDependencyCache"
100101
"CoreCompile" |]
101102

102-
let loadProject (path: string) (generateBinlog: bool) (ToolsPath toolsPath) =
103+
let loadProject (path: string) (generateBinlog: bool) (ToolsPath toolsPath) globalProperties =
103104
try
104105
let tfm = getTfm path
105106

106-
let globalProperties = getGlobalProps path tfm
107+
let globalProperties = getGlobalProps path tfm globalProperties
107108

108109
match System.Environment.GetEnvironmentVariable "DOTNET_HOST_PATH" with
109110
| null
@@ -112,7 +113,7 @@ module ProjectLoader =
112113

113114
use pc = new ProjectCollection(globalProperties)
114115

115-
let pi = pc.LoadProject(path)
116+
let pi = pc.LoadProject(path, globalProperties, toolsVersion=null)
116117

117118
use sw = new StringWriter()
118119

@@ -343,10 +344,11 @@ module ProjectLoader =
343344
/// <param name="path">Full path to the `.fsproj` file</param>
344345
/// <param name="toolsPath">Path to MsBuild obtained from `ProjectLoader.init ()`</param>
345346
/// <param name="generateBinlog">Enable Binary Log generation</param>
347+
/// <param name="globalProperties">The global properties to use (e.g. Configuration=Release). Some additional global properties are pre-set by the tool</param>
346348
/// <param name="customProperties">List of additional MsBuild properties that you want to obtain.</param>
347349
/// <returns>Returns the record instance representing the loaded project or string containing error message</returns>
348-
let getProjectInfo (path: string) (toolsPath: ToolsPath) (generateBinlog: bool) (customProperties: string list) : Result<Types.ProjectOptions, string> =
349-
let loadedProject = loadProject path generateBinlog toolsPath
350+
let getProjectInfo (path: string) (toolsPath: ToolsPath) (globalProperties: (string*string) list) (generateBinlog: bool) (customProperties: string list) : Result<Types.ProjectOptions, string> =
351+
let loadedProject = loadProject path generateBinlog toolsPath globalProperties
350352

351353
match loadedProject with
352354
| Success project -> getLoadedProjectInfo path customProperties project
@@ -369,7 +371,8 @@ type IWorkspaceLoader =
369371
[<CLIEvent>]
370372
abstract Notifications : IEvent<WorkspaceProjectState>
371373

372-
type WorkspaceLoaderViaProjectGraph private (toolsPath: ToolsPath) =
374+
type WorkspaceLoaderViaProjectGraph private (toolsPath: ToolsPath, ?globalProperties: (string*string) list) =
375+
let globalProperties = defaultArg globalProperties []
373376
let logger = LogProvider.getLoggerFor<WorkspaceLoaderViaProjectGraph> ()
374377
let loadingNotification = new Event<Types.WorkspaceProjectState>()
375378

@@ -383,17 +386,19 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath: ToolsPath) =
383386
loadingNotification.Trigger(WorkspaceProjectState.Failed(p, ProjectNotFound(p)))
384387
None
385388

386-
let projectInstanceFactory projectPath globalProperties (projectCollection: ProjectCollection) =
389+
let projectInstanceFactory projectPath (_globalProperties: IDictionary<string,string>) (projectCollection: ProjectCollection) =
387390
let tfm = ProjectLoader.getTfm projectPath
388-
ProjectInstance(projectPath, ProjectLoader.getGlobalProps projectPath tfm, null, projectCollection)
391+
//let globalProperties = globalProperties |> Seq.toList |> List.map (fun (KeyValue(k,v)) -> (k,v))
392+
let globalProperties = ProjectLoader.getGlobalProps projectPath tfm globalProperties
393+
ProjectInstance(projectPath, globalProperties, toolsVersion=null, projectCollection=projectCollection)
389394

390395
let projectGraphProjs (paths: string seq) =
391396

392397
handleProjectGraphFailures
393398
<| fun () ->
394399
paths |> Seq.iter (fun p -> loadingNotification.Trigger(WorkspaceProjectState.Loading p))
395400
let entryPoints = paths |> Seq.map ProjectGraphEntryPoint
396-
ProjectGraph(entryPoints, ProjectCollection.GlobalProjectCollection, projectInstanceFactory)
401+
ProjectGraph(entryPoints, projectCollection=ProjectCollection.GlobalProjectCollection, projectInstanceFactory=projectInstanceFactory)
397402

398403
let projectGraphSln (path: string) =
399404
handleProjectGraphFailures
@@ -541,10 +546,11 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath: ToolsPath) =
541546
this.LoadSln(sln, customProperties, false)
542547

543548

544-
static member Create(toolsPath: ToolsPath) =
545-
WorkspaceLoaderViaProjectGraph(toolsPath) :> IWorkspaceLoader
549+
static member Create(toolsPath: ToolsPath, ?globalProperties) =
550+
WorkspaceLoaderViaProjectGraph(toolsPath, ?globalProperties=globalProperties) :> IWorkspaceLoader
546551

547-
type WorkspaceLoader private (toolsPath: ToolsPath) =
552+
type WorkspaceLoader private (toolsPath: ToolsPath, ?globalProperties: (string * string) list) =
553+
let globalProperties = defaultArg globalProperties []
548554
let loadingNotification = new Event<Types.WorkspaceProjectState>()
549555

550556

@@ -561,7 +567,7 @@ type WorkspaceLoader private (toolsPath: ToolsPath) =
561567
cache |> Seq.map (fun n -> n.Value) |> Seq.toList
562568

563569
let rec loadProject p =
564-
let res = ProjectLoader.getProjectInfo p toolsPath generateBinlog customProperties
570+
let res = ProjectLoader.getProjectInfo p toolsPath globalProperties generateBinlog customProperties
565571

566572
match res with
567573
| Ok project ->
@@ -644,8 +650,8 @@ type WorkspaceLoader private (toolsPath: ToolsPath) =
644650

645651

646652

647-
static member Create(toolsPath: ToolsPath) =
648-
WorkspaceLoader(toolsPath) :> IWorkspaceLoader
653+
static member Create(toolsPath: ToolsPath, ?globalProperties) =
654+
WorkspaceLoader(toolsPath, ?globalProperties=globalProperties) :> IWorkspaceLoader
649655

650656
type ProjectViewerTree =
651657
{ Name: string

test/Ionide.ProjInfo.Tests/Tests.fs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ open Ionide.ProjInfo
1313
open Expecto.Logging.Message
1414
open FSharp.Compiler.SourceCodeServices
1515

16+
#nowarn "25"
17+
1618
let RepoDir = (__SOURCE_DIRECTORY__ / ".." / "..") |> Path.GetFullPath
1719
let ExamplesDir = RepoDir / "test" / "examples"
1820
let TestRunDir = RepoDir / "test" / "testrun_ws"
@@ -140,10 +142,10 @@ module ExpectNotification =
140142
let watchNotifications logger loader =
141143
NotificationWatcher(loader, logNotification logger)
142144

143-
let testSample2 toolsPath workspaceLoader (workspaceFactory: ToolsPath -> IWorkspaceLoader) =
145+
let testSample2 toolsPath workspaceLoader isRelease (workspaceFactory: ToolsPath * (string * string) list -> IWorkspaceLoader) =
144146
testCase
145147
|> withLog
146-
(sprintf "can load sample2 - %s" workspaceLoader)
148+
(sprintf "can load sample2 - %s - isRelease is %b" workspaceLoader isRelease)
147149
(fun logger fs ->
148150
let testDir = inDir fs "load_sample2"
149151
copyDirFromAssets fs ``sample2 NetSdk library``.ProjDir testDir
@@ -153,7 +155,9 @@ let testSample2 toolsPath workspaceLoader (workspaceFactory: ToolsPath -> IWorks
153155

154156
dotnet fs [ "restore"; projPath ] |> checkExitCodeZero
155157

156-
let loader = workspaceFactory toolsPath
158+
let config = if isRelease then "Release" else "Debug"
159+
let props = [("Configuration", config)]
160+
let loader = workspaceFactory (toolsPath, props)
157161

158162
let watcher = watchNotifications logger loader
159163

@@ -168,8 +172,10 @@ let testSample2 toolsPath workspaceLoader (workspaceFactory: ToolsPath -> IWorks
168172
let n1Parsed = parsed |> expectFind projPath "first is a lib"
169173

170174
let expectedSources =
171-
[ projDir / "obj/Debug/netstandard2.0/n1.AssemblyInfo.fs"
172-
projDir / "Library.fs" ]
175+
[ projDir / ("obj/" + config + "/netstandard2.0/n1.AssemblyInfo.fs")
176+
projDir / "Library.fs"
177+
if isRelease then
178+
projDir / "Other.fs" ]
173179
|> List.map Path.GetFullPath
174180

175181
Expect.equal parsed.Length 1 "console and lib"
@@ -981,8 +987,10 @@ let tests toolsPath =
981987
testSequenced
982988
<| testList
983989
"Main tests"
984-
[ testSample2 toolsPath "WorkspaceLoader" WorkspaceLoader.Create
985-
testSample2 toolsPath "WorkspaceLoaderViaProjectGraph" WorkspaceLoaderViaProjectGraph.Create
990+
[ testSample2 toolsPath "WorkspaceLoader" false (fun (tools,props) -> WorkspaceLoader.Create(tools, globalProperties=props))
991+
testSample2 toolsPath "WorkspaceLoader" true (fun (tools,props) -> WorkspaceLoader.Create(tools, globalProperties=props))
992+
testSample2 toolsPath "WorkspaceLoaderViaProjectGraph" false (fun (tools,props) -> WorkspaceLoaderViaProjectGraph.Create(tools, globalProperties=props))
993+
testSample2 toolsPath "WorkspaceLoaderViaProjectGraph" true (fun (tools,props) -> WorkspaceLoaderViaProjectGraph.Create(tools, globalProperties=props))
986994
testSample3 toolsPath "WorkspaceLoader" WorkspaceLoader.Create testSample3WorkspaceLoaderExpected //- Sample 3 having issues, was also marked pending on old test suite
987995
// testSample3 toolsPath "WorkspaceLoaderViaProjectGraph" WorkspaceLoaderViaProjectGraph.Create testSample3GraphExpected //- Sample 3 having issues, was also marked pending on old test suite
988996
testSample4 toolsPath "WorkspaceLoader" WorkspaceLoader.Create

test/examples/sample2-netsdk-lib/n1/n1.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Compile Include="Library.fs" />
8+
<Compile Include="Library.fs" />
9+
<Compile Include="Other.fs" Condition="'$(Configuration)' == 'Release'"/>
910
</ItemGroup>
1011

1112
</Project>

0 commit comments

Comments
 (0)