forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBicepDeploymentScopeRequestHandler.cs
104 lines (88 loc) · 4.35 KB
/
BicepDeploymentScopeRequestHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Bicep.Core;
using Bicep.Core.Diagnostics;
using Bicep.Core.Emit;
using Bicep.Core.Semantics;
using Bicep.Core.TypeSystem;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Deploy;
using Bicep.LanguageServer.Utils;
using MediatR;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
namespace Bicep.LanguageServer.Handlers
{
public record BicepDeploymentScopeParams(TextDocumentIdentifier TextDocument) : ITextDocumentIdentifierParams, IRequest<BicepDeploymentScopeResponse>;
public record BicepDeploymentScopeResponse(string scope, string? template, string? errorMessage);
/// <summary>
/// Handles getDeploymentScope LSP request.
/// The BicepDeploymentScopeRequestHandler returns targetScope, template and error message.
/// Error message would be null if provided bicep file was error free.
/// </summary>
public class BicepDeploymentScopeRequestHandler : ExecuteTypedResponseCommandHandlerBase<BicepDeploymentScopeParams, BicepDeploymentScopeResponse>
{
private readonly BicepCompiler bicepCompiler;
private readonly ICompilationManager compilationManager;
private readonly IDeploymentFileCompilationCache deploymentFileCompilationCache;
public BicepDeploymentScopeRequestHandler(
BicepCompiler bicepCompiler,
ICompilationManager compilationManager,
IDeploymentFileCompilationCache deploymentFileCompilationCache,
ISerializer serializer)
: base(LangServerConstants.GetDeploymentScopeCommand, serializer)
{
this.bicepCompiler = bicepCompiler;
this.compilationManager = compilationManager;
this.deploymentFileCompilationCache = deploymentFileCompilationCache;
}
public override async Task<BicepDeploymentScopeResponse> Handle(BicepDeploymentScopeParams request, CancellationToken cancellationToken)
{
try
{
var documentUri = request.TextDocument.Uri;
var compilation = await new CompilationHelper(bicepCompiler, compilationManager).GetRefreshedCompilation(documentUri);
// Cache the compilation so that it can be reused by BicepDeploymentParametersHandler
deploymentFileCompilationCache.CacheCompilation(documentUri, compilation);
var deploymentScope = GetDeploymentScope(compilation.GetEntrypointSemanticModel().TargetScope);
return new(deploymentScope, GetCompiledFile(compilation, documentUri), null);
}
catch (Exception exception)
{
return new(ResourceScope.None.ToString(), null, exception.Message);
}
}
private static string GetDeploymentScope(ResourceScope resourceScope) => resourceScope switch
{
ResourceScope.ResourceGroup => LanguageConstants.TargetScopeTypeResourceGroup,
ResourceScope.Subscription => LanguageConstants.TargetScopeTypeSubscription,
ResourceScope.ManagementGroup => LanguageConstants.TargetScopeTypeManagementGroup,
ResourceScope.Tenant => LanguageConstants.TargetScopeTypeTenant,
_ => resourceScope.ToString()
};
private string GetCompiledFile(Compilation compilation, DocumentUri documentUri)
{
var fileUri = documentUri.ToUri();
var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile()
.FirstOrDefault(x => x.Key.FileUri == fileUri);
if (diagnosticsByFile.Value.Any(x => x.Level == DiagnosticLevel.Error))
{
throw new Exception(DiagnosticsHelper.GetDiagnosticsMessage(diagnosticsByFile));
}
var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder);
var model = compilation.GetEntrypointSemanticModel();
var emitter = new TemplateEmitter(model);
emitter.Emit(stringWriter);
return stringBuilder.ToString();
}
}
}