Skip to content
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

Add support for building bicepparam files #62

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions BicepNet.Core/BicepWrapper.Build.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bicep.Core.Emit;
using Bicep.Core.FileSystem;
using Bicep.Core.Semantics;
using Bicep.Core.Workspaces;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -11,9 +12,9 @@ namespace BicepNet.Core;

public partial class BicepWrapper
{
public IList<string> Build(string bicepPath, bool noRestore = false) => joinableTaskFactory.Run(() => BuildAsync(bicepPath, noRestore));
public IList<string> Build(string bicepPath, string usingPath = "", bool noRestore = false) => joinableTaskFactory.Run(() => BuildAsync(bicepPath, usingPath, noRestore));

public async Task<IList<string>> BuildAsync(string bicepPath, bool noRestore = false)
public async Task<IList<string>> BuildAsync(string bicepPath, string usingPath = "", bool noRestore = false)
{
var inputPath = PathHelper.ResolvePath(bicepPath);
var features = featureProviderFactory.GetFeatureProvider(PathHelper.FilePathToFileUrl(inputPath));
Expand All @@ -32,7 +33,7 @@ public async Task<IList<string>> BuildAsync(string bicepPath, bool noRestore = f
var compilation = await compilationService.CompileAsync(inputPath, noRestore);
if (diagnosticLogger is not null && diagnosticLogger.ErrorCount > 0)
{
throw new InvalidOperationException($"Failed to compile template: {inputPath}");
throw new InvalidOperationException($"Failed to compile file: {inputPath}");
}

var stream = new MemoryStream();
Expand All @@ -41,7 +42,7 @@ public async Task<IList<string>> BuildAsync(string bicepPath, bool noRestore = f
EmitResult emitresult = fileKind switch
{
BicepSourceFileKind.BicepFile => new TemplateEmitter(compilation.GetEntrypointSemanticModel()).Emit(stream),
BicepSourceFileKind.ParamsFile => new ParametersEmitter(compilation.GetEntrypointSemanticModel()).Emit(stream),
BicepSourceFileKind.ParamsFile => EmitParamsFile(compilation, usingPath, stream),
_ => throw new NotImplementedException($"Unexpected file kind '{fileKind}'"),
};

Expand All @@ -57,4 +58,22 @@ public async Task<IList<string>> BuildAsync(string bicepPath, bool noRestore = f

return template;
}

private static EmitResult EmitParamsFile(Compilation compilation, string usingPath, Stream stream)
{
var bicepPath = PathHelper.ResolvePath(usingPath);
var paramsSemanticModel = compilation.GetEntrypointSemanticModel();
if (usingPath != "" && paramsSemanticModel.Root.TryGetBicepFileSemanticModelViaUsing(out var bicepSemanticModel, out _))
{
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");
}

}
var emitter = new ParametersEmitter(paramsSemanticModel);
return emitter.Emit(stream);
}
}
1 change: 0 additions & 1 deletion BicepNet.Core/BicepWrapper.Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public async Task PublishAsync(string inputFilePath, string targetModuleReferenc

stream.Position = 0;
await PublishModuleAsync(moduleReference, stream);
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion BicepNet.PS/Commands/BuildBicepNetFileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BuildBicepNetFileCommand : BicepNetBaseCommand

protected override void ProcessRecord()
{
var result = bicepWrapper.Build(Path, NoRestore.IsPresent);
var result = bicepWrapper.Build(Path, noRestore: NoRestore.IsPresent);
WriteObject(result);
}
}
24 changes: 24 additions & 0 deletions BicepNet.PS/Commands/BuildBicepNetParamFileCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Management.Automation;

namespace BicepNet.PS.Commands;

[Cmdlet(VerbsLifecycle.Build, "BicepNetParamFile")]
public class BuildBicepNetParamFileCommand : BicepNetBaseCommand
{
[Parameter(Mandatory = true, ValueFromPipeline = true)]
[ValidateNotNullOrEmpty]
public string Path { get; set; }

[Parameter(Mandatory = false)]
[ValidateNotNullOrEmpty]
public string TemplatePath { get; set; } = "";

[Parameter()]
public SwitchParameter NoRestore { get; set; }

protected override void ProcessRecord()
{
var result = bicepWrapper.Build(Path, TemplatePath, NoRestore.IsPresent);
WriteObject(result);
}
}
1 change: 1 addition & 0 deletions BicepNet.PS/Manifest/BicepNet.PS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BicepNet is developed for the Bicep PowerShell module but could be used for any
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @(
'Build-BicepNetFile'
'Build-BicepNetParamFile'
'Clear-BicepNetCredential'
'Convert-BicepNetResourceToBicep'
'ConvertTo-BicepNetFile'
Expand Down