Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Adding support for bicep v0.22.6 (#81)
Browse files Browse the repository at this point in the history
* implemented bicep v0.22.6, version bump of readme and manifest
---------

Co-authored-by: Simon Wåhlin <simon@simonw.se>
  • Loading branch information
PalmEmanuel and SimonWahlin authored Oct 29, 2023
1 parent 3bbc894 commit 1d509e5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
12 changes: 8 additions & 4 deletions BicepNet.Core/BicepWrapper.Build.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Bicep.Cli;
using Bicep.Core;
using Bicep.Core.Emit;
using Bicep.Core.FileSystem;
Expand All @@ -18,7 +19,6 @@ public partial class BicepWrapper
public async Task<IList<string>> BuildAsync(string bicepPath, string usingPath = "", bool noRestore = false)
{
var inputPath = PathHelper.ResolvePath(bicepPath);
var inputUri = PathHelper.FilePathToFileUrl(inputPath);

if (!IsBicepFile(inputPath) && !IsBicepparamsFile(inputPath))
{
Expand All @@ -33,7 +33,6 @@ public async Task<IList<string>> BuildAsync(string bicepPath, string usingPath =
}

var fileKind = compilation.SourceFileGrouping.EntryPoint.FileKind;
var semanticModel = compilation.GetEntrypointSemanticModel();

var stream = new MemoryStream();
EmitResult emitresult = fileKind switch
Expand Down Expand Up @@ -65,13 +64,18 @@ private static EmitResult EmitParamsFile(Compilation compilation, string usingPa
{
var bicepPath = PathHelper.ResolvePath(usingPath);
var paramsSemanticModel = compilation.GetEntrypointSemanticModel();
if (usingPath != "" && paramsSemanticModel.Root.TryGetBicepFileSemanticModelViaUsing(out var bicepSemanticModel, out _))
if (usingPath != "" && paramsSemanticModel.Root.TryGetBicepFileSemanticModelViaUsing().IsSuccess(out var usingModel))
{
if (usingModel is not SemanticModel bicepSemanticModel)
{
throw new InvalidOperationException($"Bicep file {bicepPath} provided can only be used if the Bicep parameters \"using\" declaration refers to a Bicep file on disk.");
}

var bicepFileUsingPathUri = bicepSemanticModel.Root.FileUri;

if (bicepPath is not null && !bicepFileUsingPathUri.Equals(PathHelper.FilePathToFileUrl(bicepPath)))
{
throw new InvalidOperationException($"Bicep file {bicepPath} provided with templatePath option doesn't match the Bicep file {bicepSemanticModel.Root.Name} referenced by the using declaration in the parameters file");
throw new InvalidOperationException($"Bicep file {bicepPath} provided with templatePath option doesn't match the Bicep file {bicepSemanticModel?.Root.Name} referenced by the using declaration in the parameters file");
}

}
Expand Down
25 changes: 21 additions & 4 deletions BicepNet.Core/BicepWrapper.ConvertResourceToBicep.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using Bicep.Core.Diagnostics;
using Bicep.Core.Navigation;
using Bicep.Core.PrettyPrint;
using Bicep.Core.PrettyPrint.Options;
using Bicep.Core.Registry;
using Bicep.Core.Rewriters;
using Bicep.Core.Semantics;
using Bicep.Core.Utils;
using Bicep.Core.Workspaces;
using BicepNet.Core.Azure;
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json;

namespace BicepNet.Core;
Expand All @@ -26,8 +33,18 @@ public string RewriteBicepTemplate(string template)
{
BicepFile virtualBicepFile = SourceFileFactory.CreateBicepFile(new Uri($"inmemory:///generated.bicep"), template);

var workspace = new Workspace();
var sourceFileGrouping = SourceFileGroupingBuilder.Build(fileResolver, moduleDispatcher, workspace, virtualBicepFile.FileUri, featureProviderFactory, false);
// SourceFileGroupingBuilder.Build doesn't work with fake files (because of some internal recursive shenanigans?)
// So we build the grouping manually à la hack
var sourceFileGrouping = new SourceFileGrouping(
fileResolver,
virtualBicepFile.FileUri,
new Dictionary<Uri, ResultWithDiagnostic<ISourceFile>>
{
new KeyValuePair<Uri, ResultWithDiagnostic<ISourceFile>>(virtualBicepFile.FileUri, new ResultWithDiagnostic<ISourceFile>(virtualBicepFile))
}.ToImmutableDictionary(),
new Dictionary<ISourceFile, ImmutableDictionary<IArtifactReferenceSyntax, Result<Uri, UriResolutionError>>>().ToImmutableDictionary(),
new Dictionary<ISourceFile, ImmutableHashSet<ISourceFile>>().ToImmutableDictionary()
);
var compilation = new Compilation(featureProviderFactory, namespaceProvider, sourceFileGrouping, configurationManager, bicepAnalyzer, moduleDispatcher);

var bicepFile = RewriterHelper.RewriteMultiple(
Expand All @@ -36,10 +53,10 @@ public string RewriteBicepTemplate(string template)
rewritePasses: 1,
model => new TypeCasingFixerRewriter(model),
model => new ReadOnlyPropertyRemovalRewriter(model));

var printOptions = new PrettyPrintOptions(NewlineOption.LF, IndentKindOption.Space, 2, false);
template = PrettyPrinter.PrintProgram(bicepFile.ProgramSyntax, printOptions, EmptyDiagnosticLookup.Instance, EmptyDiagnosticLookup.Instance);
template = PrettyPrinter.PrintValidProgram(bicepFile.ProgramSyntax, printOptions);

return template;
}

}
17 changes: 9 additions & 8 deletions BicepNet.Core/BicepWrapper.Decompile.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using Bicep.Core.FileSystem;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BicepNet.Core;

public partial class BicepWrapper
{
public IDictionary<string, string> Decompile(string templatePath, string? outputDir = null, string? outputFile = null) =>
joinableTaskFactory.Run(() => DecompileAsync(templatePath, outputDir, outputFile));
public IDictionary<string, string> Decompile(string templatePath) =>
joinableTaskFactory.Run(() => DecompileAsync(templatePath));

public async Task<IDictionary<string, string>> DecompileAsync(string templatePath, string? outputDir = null, string? outputFile = null)
public async Task<IDictionary<string, string>> DecompileAsync(string templatePath)
{
var inputPath = PathHelper.ResolvePath(templatePath);
var inputUri = PathHelper.FilePathToFileUrl(inputPath);

static string DefaultOutputPath(string path) => PathHelper.GetDefaultDecompileOutputPath(path);
var outputPath = PathHelper.ResolveDefaultOutputPath(inputPath, outputDir, outputFile, DefaultOutputPath);
var outputUri = PathHelper.FilePathToFileUrl(outputPath);

if (!fileResolver.TryRead(inputUri).IsSuccess(out var jsonContent))
{
throw new InvalidOperationException($"Failed to read {inputUri}");
}

var template = new Dictionary<string, string>();
var decompilation = await decompiler.Decompile(inputUri, outputUri);
var decompilation = await decompiler.Decompile(PathHelper.ChangeToBicepExtension(inputUri), jsonContent);

foreach (var (fileUri, bicepOutput) in decompilation.FilesToSave)
{
Expand Down
18 changes: 11 additions & 7 deletions BicepNet.Core/BicepWrapper.Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bicep.Core.Exceptions;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
using Bicep.Core.SourceCode;
using System;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -13,39 +14,42 @@ namespace BicepNet.Core;
public partial class BicepWrapper
{
public void Publish(string inputFilePath, string targetModuleReference, string? documentationUri, bool overwriteIfExists = false) =>
joinableTaskFactory.Run(() => PublishAsync(inputFilePath, targetModuleReference, documentationUri, overwriteIfExists = false));
joinableTaskFactory.Run(() => PublishAsync(inputFilePath, targetModuleReference, documentationUri, overwriteIfExists));

public async Task PublishAsync(string inputFilePath, string targetModuleReference, string? documentationUri, bool overwriteIfExists = false)
{
var inputPath = PathHelper.ResolvePath(inputFilePath);
var inputUri = PathHelper.FilePathToFileUrl(inputPath);
var features = featureProviderFactory.GetFeatureProvider(PathHelper.FilePathToFileUrl(inputPath));
ArtifactReference? moduleReference = ValidateReference(targetModuleReference, inputUri);

if (PathHelper.HasArmTemplateLikeExtension(inputUri))
{
// Publishing an ARM template file.
using var armTemplateStream = fileSystem.FileStream.New(inputPath, FileMode.Open, FileAccess.Read);
await this.PublishModuleAsync(moduleReference, armTemplateStream, documentationUri, overwriteIfExists);
await this.PublishModuleAsync(moduleReference, armTemplateStream, null, documentationUri, overwriteIfExists);
return;
}

var bicepCompiler = new BicepCompiler(featureProviderFactory, namespaceProvider, configurationManager, bicepAnalyzer, fileResolver, moduleDispatcher);
var compilation = await bicepCompiler.CreateCompilation(inputUri, workspace);

using var sourcesStream = features.PublishSourceEnabled ? SourceArchive.PackSourcesIntoStream(compilation.SourceFileGrouping) : null;

if (LogDiagnostics(compilation))
{
var stream = new MemoryStream();
new TemplateEmitter(compilation.GetEntrypointSemanticModel()).Emit(stream);

stream.Position = 0;
await PublishModuleAsync(moduleReference, stream, documentationUri, overwriteIfExists);
await PublishModuleAsync(moduleReference, stream, sourcesStream, documentationUri, overwriteIfExists);
}
}

// copied from PublishCommand.cs
private ArtifactReference ValidateReference(string targetModuleReference, Uri targetModuleUri)
{
if (!this.moduleDispatcher.TryGetModuleReference(targetModuleReference, targetModuleUri, out var moduleReference, out var failureBuilder))
if (!this.moduleDispatcher.TryGetModuleReference(targetModuleReference, targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder))
{
// TODO: We should probably clean up the dispatcher contract so this sort of thing isn't necessary (unless we change how target module is set in this command)
var message = failureBuilder(DiagnosticBuilder.ForDocumentStart()).Message;
Expand All @@ -62,16 +66,16 @@ private ArtifactReference ValidateReference(string targetModuleReference, Uri ta
}

// copied from PublishCommand.cs
private async Task PublishModuleAsync(ArtifactReference target, Stream stream, string? documentationUri, bool overwriteIfExists)
private async Task PublishModuleAsync(ArtifactReference target, Stream compiledArmTemplate, Stream? bicepSources, string? documentationUri, bool overwriteIfExists)
{
try
{
// If we don't want to overwrite, ensure module doesn't exist
if (!overwriteIfExists && await this.moduleDispatcher.CheckModuleExists(target))
{
throw new BicepException($"The module \"{target.FullyQualifiedReference}\" already exists in registry. Use -Force to overwrite the existing module.");
throw new BicepException($"The module \"{target.FullyQualifiedReference}\" already exists in registry. Use --force to overwrite the existing module.");
}
await this.moduleDispatcher.PublishModule(target, stream, documentationUri);
await this.moduleDispatcher.PublishModule(target, compiledArmTemplate, bicepSources, documentationUri);
}
catch (ExternalArtifactException exception)
{
Expand Down
8 changes: 2 additions & 6 deletions BicepNet.Core/BicepWrapper.Restore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ public async Task RestoreAsync(string inputFilePath, bool forceModulesRestore =
var inputPath = PathHelper.ResolvePath(inputFilePath);
var inputUri = PathHelper.FilePathToFileUrl(inputPath);

// Create separate configuration for the build, to account for custom rule changes
var buildConfiguration = configurationManager.GetConfiguration(inputUri);

var bicepCompiler = new BicepCompiler(featureProviderFactory, namespaceProvider, configurationManager, bicepAnalyzer, fileResolver, moduleDispatcher);
var compilation = await bicepCompiler.CreateCompilation(inputUri, workspace);
var compilation = await bicepCompiler.CreateCompilation(inputUri, workspace, true, forceModulesRestore);

var originalModulesToRestore = compilation.SourceFileGrouping.GetModulesToRestore().ToImmutableHashSet();

Expand All @@ -44,7 +41,6 @@ public async Task RestoreAsync(string inputFilePath, bool forceModulesRestore =

LogDiagnostics(GetModuleRestoreDiagnosticsByBicepFile(sourceFileGrouping, originalModulesToRestore, forceModulesRestore));


if (modulesToRestoreReferences.Any())
{
logger?.LogInformation("Successfully restored modules in {inputFilePath}", inputFilePath);
Expand All @@ -58,7 +54,7 @@ public async Task RestoreAsync(string inputFilePath, bool forceModulesRestore =
private static ImmutableDictionary<BicepSourceFile, ImmutableArray<IDiagnostic>> GetModuleRestoreDiagnosticsByBicepFile(SourceFileGrouping sourceFileGrouping, ImmutableHashSet<ArtifactResolutionInfo> originalModulesToRestore, bool forceModulesRestore)
{
static IDiagnostic? DiagnosticForModule(SourceFileGrouping grouping, IArtifactReferenceSyntax moduleDeclaration)
=> grouping.TryGetErrorDiagnostic(moduleDeclaration) is { } errorBuilder ? errorBuilder(DiagnosticBuilder.ForPosition(moduleDeclaration.SourceSyntax)) : null;
=> grouping.TryGetSourceFile(moduleDeclaration).IsSuccess(out _, out var errorBuilder) ? null : errorBuilder(DiagnosticBuilder.ForPosition(moduleDeclaration.SourceSyntax));

static IEnumerable<(BicepFile, IDiagnostic)> GetDiagnosticsForModulesToRestore(SourceFileGrouping grouping, ImmutableHashSet<ArtifactResolutionInfo> originalArtifactsToRestore)
{
Expand Down
2 changes: 1 addition & 1 deletion BicepNet.PS/BicepNet.PS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
NestedModules = 'BicepNet.PS/BicepNet.PS.dll'

# Version number of this module.
ModuleVersion = '2.2.0'
ModuleVersion = '2.3.0'

# Supported PSEditions
CompatiblePSEditions = @('Core')
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Using BicepNet is generally much faster than calling the CLI since the overhead

| BicepNet version | Bicep assembly version |
| --- | --- |
| `2.3.0` | `0.22.6` |
| `2.2.0` | `0.21.1` |
| `2.1.0` | `0.18.4` |
| `2.0.10` | `0.11.1` |
| `2.0.9` | `0.10.61` |
Expand Down
2 changes: 1 addition & 1 deletion RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
'DscResource.AnalyzerRules' = 'latest'
xDscResourceDesigner = 'latest'
'DscResource.DocGenerator' = 'latest'
'Azure/Bicep' = 'v0.21.1'
'Azure/Bicep' = 'v0.22.6'
}

0 comments on commit 1d509e5

Please sign in to comment.