forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBicepDecompileParamsCommandHandler.cs
70 lines (61 loc) · 2.59 KB
/
BicepDecompileParamsCommandHandler.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.FileSystem;
using Bicep.Decompiler;
using Bicep.LanguageServer.Telemetry;
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Bicep.LanguageServer.Handlers
{
public record BicepDecompileParamsCommandParams(
DocumentUri jsonUri,
string? bicepPath
);
public record DecompiledBicepparamFile(
string contents,
string absolutePath
);
public record BicepDecompileParamsCommandResult(
DecompiledBicepparamFile? decompiledBicepparamFile,
string? errorMessage
);
/// <summary>
/// Handles a request from the client to decompile a JSON file for given a file path, creating a bicepparam file
/// </summary>
public class BicepDecompileParamsCommandHandler : ExecuteTypedResponseCommandHandlerBase<BicepDecompileParamsCommandParams, BicepDecompileParamsCommandResult>
{
private readonly BicepparamDecompiler bicepparamDecompiler;
public BicepDecompileParamsCommandHandler(
ISerializer serializer,
BicepparamDecompiler bicepparamDecompiler)
: base(LangServerConstants.DecompileParamsCommand, serializer)
{
this.bicepparamDecompiler = bicepparamDecompiler;
}
public override Task<BicepDecompileParamsCommandResult> Handle(BicepDecompileParamsCommandParams parameters, CancellationToken cancellationToken)
{
try
{
Uri jsonUri = new Uri(parameters.jsonUri.GetFileSystemPath());
var (entryUri, filesToSave) = bicepparamDecompiler.Decompile(jsonUri, PathHelper.ChangeToBicepparamExtension(jsonUri), parameters.bicepPath);
return Task.FromResult(new BicepDecompileParamsCommandResult(new DecompiledBicepparamFile(filesToSave[entryUri], entryUri.AbsolutePath), null));
}
catch (Exception ex)
{
var message = string.Format(LangServerResources.Decompile_DecompilationFailed, ex.Message);
return Task.FromResult(new BicepDecompileParamsCommandResult(null, message));
}
}
}
}