-
Notifications
You must be signed in to change notification settings - Fork 821
Add context menu option to generate snapshots for .bicepparam files #19024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
verschaevesiebe
wants to merge
1
commit into
Azure:main
Choose a base branch
from
verschaevesiebe:feature/bicep-snapshot-vsc-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/Bicep.LangServer/Handlers/BicepSnapshotCommandHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Bicep.Core; | ||
| using Bicep.Core.Diagnostics; | ||
| using Bicep.Core.Emit; | ||
| using Bicep.Core.Extensions; | ||
| using Bicep.Core.Utils.Snapshots; | ||
| using Bicep.IO.Abstraction; | ||
| using Bicep.LanguageServer.CompilationManager; | ||
| using Bicep.LanguageServer.Utils; | ||
| using OmniSharp.Extensions.JsonRpc; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Workspace; | ||
|
|
||
| namespace Bicep.LanguageServer.Handlers | ||
| { | ||
| // This handler is used to generate a snapshot (.snapshot.json) file for a given bicep parameters file. | ||
| // It returns snapshot generation succeeded/failed message, which can be displayed appropriately in IDE output window | ||
| public class BicepSnapshotCommandHandler : ExecuteTypedResponseCommandHandlerBase<DocumentUri, string> | ||
| { | ||
| private readonly ICompilationManager compilationManager; | ||
| private readonly IFileExplorer fileExplorer; | ||
| private readonly BicepCompiler bicepCompiler; | ||
|
|
||
| public BicepSnapshotCommandHandler(ICompilationManager compilationManager, IFileExplorer fileExplorer, BicepCompiler bicepCompiler, ISerializer serializer) | ||
| : base(LangServerConstants.SnapshotCommand, serializer) | ||
| { | ||
| this.compilationManager = compilationManager; | ||
| this.fileExplorer = fileExplorer; | ||
| this.bicepCompiler = bicepCompiler; | ||
| } | ||
|
|
||
| public override async Task<string> Handle(DocumentUri documentUri, CancellationToken cancellationToken) | ||
| { | ||
| string output = await GenerateSnapshotFileAndReturnOutputMessage(documentUri, cancellationToken); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| private async Task<string> GenerateSnapshotFileAndReturnOutputMessage(DocumentUri documentUri, CancellationToken cancellationToken) | ||
| { | ||
| var bicepParamFileUri = documentUri.ToIOUri(); | ||
| var snapshotFileUri = bicepParamFileUri.WithExtension(".snapshot.json"); | ||
| var snapshotFile = this.fileExplorer.GetFile(snapshotFileUri); | ||
|
|
||
| var compilation = await new CompilationHelper(bicepCompiler, compilationManager).GetRefreshedCompilation(documentUri); | ||
| var paramsResult = compilation.Emitter.Parameters(); | ||
|
|
||
| if (paramsResult.Success != true || paramsResult.Template?.Template is not { } templateContent || paramsResult.Parameters is not { } parametersContent) | ||
| { | ||
| var diagnosticsByFile = compilation.GetAllDiagnosticsByBicepFile(); | ||
|
|
||
| return "Generating snapshot file failed. Please fix below errors:\n" + DiagnosticsHelper.GetDiagnosticsMessage(diagnosticsByFile); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var snapshot = await SnapshotHelper.GetSnapshot( | ||
| targetScope: compilation.GetEntrypointSemanticModel().TargetScope, | ||
| templateContent: templateContent, | ||
| parametersContent: parametersContent, | ||
| tenantId: null, | ||
| subscriptionId: null, | ||
| resourceGroup: null, | ||
| location: null, | ||
| deploymentName: null, | ||
| cancellationToken: cancellationToken, | ||
| externalInputs: []); | ||
|
|
||
| if (snapshot.Diagnostics.Length > 0) | ||
| { | ||
| var diagnosticsMessage = string.Join("\n", snapshot.Diagnostics.Select(d => $" {d}")); | ||
| return $"Snapshot generation completed with warnings:\n{diagnosticsMessage}\n\nSnapshot file created at {snapshotFileUri}"; | ||
| } | ||
|
|
||
| var contents = SnapshotHelper.Serialize(snapshot); | ||
| await snapshotFile.WriteAllTextAsync(contents, cancellationToken); | ||
|
|
||
| return $"Snapshot generation succeeded. Created file {snapshotFileUri}"; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return $"Snapshot generation failed: {ex.Message}"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| import { IActionContext, parseError } from "@microsoft/vscode-azext-utils"; | ||
| import vscode from "vscode"; | ||
| import { LanguageClient } from "vscode-languageclient/node"; | ||
| import { OutputChannelManager } from "../utils/OutputChannelManager"; | ||
| import { findOrCreateActiveBicepParamFile } from "./findOrCreateActiveBicepFile"; | ||
| import { Command } from "./types"; | ||
|
|
||
| export class SnapshotCommand implements Command { | ||
| public readonly id = "bicep.snapshot"; | ||
| public constructor( | ||
| private readonly client: LanguageClient, | ||
| private readonly outputChannelManager: OutputChannelManager, | ||
| ) {} | ||
|
|
||
| public async execute(context: IActionContext, documentUri?: vscode.Uri | undefined): Promise<void> { | ||
| documentUri = await findOrCreateActiveBicepParamFile( | ||
| context, | ||
| documentUri, | ||
| "Choose which Bicep Parameters file to generate a snapshot from", | ||
| ); | ||
|
|
||
| if (documentUri.scheme.toLowerCase() !== "file") { | ||
| this.client.error( | ||
| "Snapshot generation failed. The active file must be saved to your local filesystem.", | ||
| undefined, | ||
| true, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const snapshotOutput: string = await this.client.sendRequest("workspace/executeCommand", { | ||
| command: "snapshot", | ||
| arguments: [documentUri.toString()], | ||
| }); | ||
| this.outputChannelManager.appendToOutputChannel(snapshotOutput); | ||
| } catch (err) { | ||
| this.client.error("Snapshot generation failed", parseError(err).message, true); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think realistically we'd need a way for someone to supply these inputs, but this would result in a complex UI experience, unless we're able to save the values somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can build a tree view for selecting scope later. It would be great if both deploy pane and the snapshot command can share the same UI.