Skip to content

Commit be0ac23

Browse files
authored
refactor: use BuildManager (#19)
1 parent 1e24d5f commit be0ac23

File tree

4 files changed

+61
-56
lines changed

4 files changed

+61
-56
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Remove the line below if you want to inherit .editorconfig settings from higher directories
22
root = true
33

4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
[*.{json,xml}]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[nuget.config]
14+
indent_style = space
15+
indent_size = 2
16+
417
[*{Test,Tests}.cs]
518
# Anonymous function can be made static
619
dotnet_diagnostic.IDE0320.severity = none

src/bsp-server/BuildInitializeManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BaseProtocol;
22
using bsp4csharp.Protocol;
3+
using Microsoft.Build.Execution;
34

45
namespace dotnet_bsp;
56

@@ -34,6 +35,11 @@ public InitializeBuildResult GetInitializeResult()
3435
return initializeResult;
3536
}
3637

38+
public BuildManager GetBuildManager()
39+
{
40+
return BuildManager.DefaultBuildManager;
41+
}
42+
3743
public InitializeBuildParams GetInitializeParams()
3844
{
3945
EnsureInitialize();

src/bsp-server/Handlers/BuildTargetCleanCacheHandler.cs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using BaseProtocol;
22
using bsp4csharp.Protocol;
3-
using Microsoft.Build.Construction;
43
using Microsoft.Build.Evaluation;
4+
using Microsoft.Build.Execution;
5+
using Microsoft.Build.Graph;
56
using dotnet_bsp.Logging;
67

78
namespace dotnet_bsp.Handlers;
@@ -21,44 +22,31 @@ public Task<CleanCacheResult> HandleRequestAsync(CleanCacheParams cleanCachePara
2122
{
2223
_initializeManager.EnsureInitialized();
2324

25+
var projects = new ProjectCollection();
2426
var cleanResult = false;
27+
var targetFiles = BuildHelper.ExtractProjectsFromSolutions(cleanCacheParams.Targets);
28+
var graph = new ProjectGraph(targetFiles, projects);
2529
var initParams = _initializeManager.GetInitializeParams();
2630
if (initParams.RootUri.IsFile)
2731
{
28-
var projects = new ProjectCollection();
29-
foreach (var target in cleanCacheParams.Targets)
30-
{
31-
var fileExtension = Path.GetExtension(target.ToString());
32-
context.Logger.LogInformation("Target file extension {}", fileExtension);
33-
if (fileExtension == ".sln")
34-
{
35-
var slnFile = SolutionFile.Parse(target.ToString());
36-
37-
var projectFilesInSln = slnFile.ProjectsInOrder
38-
.Where(x =>
39-
x.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat ||
40-
x.ProjectType == SolutionProjectType.WebProject)
41-
.Select(x => x.AbsolutePath);
42-
43-
foreach (var projectFile in projectFilesInSln)
44-
{
45-
projects.LoadProject(projectFile);
46-
}
47-
}
48-
else if (fileExtension == ".csproj")
49-
{
50-
projects.LoadProject(target.ToString());
51-
}
52-
}
53-
5432
var workspacePath = initParams.RootUri.LocalPath;
5533
context.Logger.LogInformation("GetLoadedProjects from {}", workspacePath);
56-
foreach (var proj in projects.LoadedProjects)
57-
{
58-
var msBuildLogger = new MSBuildLogger(_baseProtocolClientManager, null, workspacePath);
59-
context.Logger.LogInformation("Start clean for target: {}", proj.FullPath);
60-
cleanResult |= proj.Build("Clean", new [] { msBuildLogger });
61-
}
34+
_baseProtocolClientManager.SendClearDiagnosticsMessage();
35+
36+
var buildManager = _initializeManager.GetBuildManager();
37+
buildManager.BeginBuild(new BuildParameters
38+
{
39+
Loggers = [
40+
new MSBuildLogger(_baseProtocolClientManager, null, workspacePath)
41+
],
42+
},
43+
[]);
44+
45+
context.Logger.LogInformation("Start clean for target: {}", targetFiles);
46+
var graphCleanResult = buildManager.BuildRequest(new GraphBuildRequestData(graph, ["Clean"]));
47+
context.Logger.LogInformation("Clean result: {}", graphCleanResult.OverallResult);
48+
cleanResult = (graphCleanResult.OverallResult == BuildResultCode.Success);
49+
buildManager.EndBuild();
6250
}
6351

6452
var message = cleanResult ? "Cleaned" : "Not cleaned";

src/bsp-server/Handlers/BuildTargetCompileHandler.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using BaseProtocol;
22
using bsp4csharp.Protocol;
33
using Microsoft.Build.Evaluation;
4-
using dotnet_bsp.Logging;
4+
using Microsoft.Build.Execution;
55
using Microsoft.Build.Graph;
6+
using dotnet_bsp.Logging;
67

78
namespace dotnet_bsp.Handlers;
89

@@ -23,7 +24,7 @@ public Task<CompileResult> HandleRequestAsync(CompileParams compileParams, Reque
2324

2425
var projects = new ProjectCollection();
2526
var buildResult = true;
26-
var targetFiles = compileParams.Targets.Select(x => x.ToString());
27+
var targetFiles = BuildHelper.ExtractProjectsFromSolutions(compileParams.Targets);
2728
var graph = new ProjectGraph(targetFiles, projects);
2829
var initParams = _initializeManager.GetInitializeParams();
2930
if (initParams.RootUri.IsFile)
@@ -32,27 +33,24 @@ public Task<CompileResult> HandleRequestAsync(CompileParams compileParams, Reque
3233
context.Logger.LogInformation("GetLoadedProjects from {}", workspacePath);
3334
_baseProtocolClientManager.SendClearDiagnosticsMessage();
3435

35-
foreach (var proj in graph.ProjectNodesTopologicallySorted)
36-
{
37-
var globalProps = proj.ProjectInstance.GlobalProperties
38-
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
39-
.ToArray();
40-
context.Logger.LogInformation("Global Properties: {}", string.Join("\n", globalProps));
41-
context.Logger.LogInformation("Start restore target: {}", proj.ProjectInstance.FullPath);
42-
var msBuildLogger = new MSBuildLogger(_baseProtocolClientManager, compileParams.OriginId, workspacePath);
43-
var result = proj.ProjectInstance.Build(["Restore"], [msBuildLogger]);
44-
context.Logger.LogInformation($"{proj.ProjectInstance.FullPath} restore result: {result}");
45-
buildResult &= result;
46-
}
47-
48-
foreach (var proj in graph.ProjectNodesTopologicallySorted)
49-
{
50-
context.Logger.LogInformation("Start building target: {}", proj.ProjectInstance.FullPath);
51-
var msBuildLogger = new MSBuildLogger(_baseProtocolClientManager, compileParams.OriginId, workspacePath);
52-
var result = proj.ProjectInstance.Build(["Build"], [msBuildLogger]);
53-
context.Logger.LogInformation($"{proj.ProjectInstance.FullPath} build result: {result}");
54-
buildResult &= result;
55-
}
36+
var buildManager = _initializeManager.GetBuildManager();
37+
buildManager.BeginBuild(new BuildParameters
38+
{
39+
Loggers = [
40+
new MSBuildLogger(_baseProtocolClientManager, compileParams.OriginId, workspacePath)
41+
],
42+
},
43+
[]);
44+
45+
context.Logger.LogInformation("Start restore targets: {}", targetFiles);
46+
var graphRestoreResult = buildManager.BuildRequest(new GraphBuildRequestData(graph, ["Restore"]));
47+
context.Logger.LogInformation("Restore result: {}", graphRestoreResult.OverallResult);
48+
buildResult &= (graphRestoreResult.OverallResult == BuildResultCode.Success);
49+
context.Logger.LogInformation("Start building targets: {}", targetFiles);
50+
var graphBuildResult = buildManager.BuildRequest(new GraphBuildRequestData(graph, ["Build"]));
51+
context.Logger.LogInformation("Build result: {}", graphBuildResult.OverallResult);
52+
buildResult &= (graphBuildResult.OverallResult == BuildResultCode.Success);
53+
buildManager.EndBuild();
5654
}
5755

5856
return Task.FromResult(new CompileResult

0 commit comments

Comments
 (0)