Skip to content

Commit

Permalink
Add -WithSource to Publish-AzBicepModule
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Weatherford authored and StephenWeatherford committed Nov 28, 2023
1 parent 50b0385 commit ce6c1ae
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class PublishAzureBicepModuleCmdlet : AzureRMCmdlet
[Parameter(Mandatory = false, HelpMessage = "Documentation uri of the Bicep module.")]
public string DocumentationUri { get; set; }

[Parameter(Mandatory = false, HelpMessage="[Experimental] Publish source code with the module.")]
public SwitchParameter WithSource { get; set; }

[Parameter(Mandatory = false, HelpMessage="Overwrite existing published module.")]
public SwitchParameter Force { get; set; }

Expand All @@ -42,7 +45,7 @@ public class PublishAzureBicepModuleCmdlet : AzureRMCmdlet

public override void ExecuteCmdlet()
{
BicepUtility.Create().PublishFile(this.TryResolvePath(this.FilePath), this.Target, this.DocumentationUri, this.Force.IsPresent, this.WriteVerbose, this.WriteWarning);
BicepUtility.Create().PublishFile(this.TryResolvePath(this.FilePath), this.Target, this.DocumentationUri, this.WithSource.IsPresent, this.Force.IsPresent, this.WriteVerbose, this.WriteWarning);

if (this.PassThru.IsPresent)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Resources/ResourceManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ You can help us improve the accuracy of the result by opening an issue here: htt
<value>Invalid Bicep file path.</value>
</data>
<data name="BicepVersionRequirement" xml:space="preserve">
<value>Please use bicep '{0}' or higher verison.</value>
<value>Please use bicep version {0} or higher.</value>
</data>
<data name="BuildBicepFileToJsonFailed" xml:space="preserve">
<value>Build bicep file '{0}' to json failed.</value>
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/ResourceManager/Utilities/BicepUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static BicepUtility Create()

private const string MinimalVersionRequirementForBicepPublishWithOptionalForceParameter = "0.17.1";

private const string MinimalVersionRequirementForBicepPublishWithOptionalWithSourceParameter = "0.23.1";

private const string MinimalVersionRequirementForBicepparamFileBuild = "0.16.1";

private const string MinimalVersionRequirementForBicepparamFileBuildWithInlineOverrides = "0.22.6";
Expand Down Expand Up @@ -136,7 +138,7 @@ public BicepBuildParamsStdout BuildParams(string bicepParamFilePath, IReadOnlyDi
return JsonConvert.DeserializeObject<BicepBuildParamsStdout>(stdout);
}

public void PublishFile(string bicepFilePath, string target, string documentationUri = null, bool force = false, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)
public void PublishFile(string bicepFilePath, string target, string documentationUri = null, bool withSource = false, bool force = false, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)
{
if (!dataStore.FileExists(bicepFilePath))
{
Expand All @@ -150,10 +152,16 @@ public void PublishFile(string bicepFilePath, string target, string documentatio
bicepPublishCommand += $" --documentationUri {GetQuotedFilePath(documentationUri)}";
}

if (withSource)
{
CheckMinimalVersionRequirement(MinimalVersionRequirementForBicepPublishWithOptionalWithSourceParameter);
bicepPublishCommand += " --with-source";
}

if (force)
{
CheckMinimalVersionRequirement(MinimalVersionRequirementForBicepPublishWithOptionalForceParameter);
bicepPublishCommand += $" --force";
bicepPublishCommand += " --force";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void BuildParams_fails_for_old_bicep_version()
var bicepUtility = new BicepUtility(invokerMock.Object, dataStoreMock.Object);

FluentActions.Invoking(() => bicepUtility.BuildParams("foo.bicepparam", new Dictionary<string, object>()))
.Should().Throw<AzPSApplicationException>().WithMessage("Please use bicep '0.16.1' or higher verison.");
.Should().Throw<AzPSApplicationException>().WithMessage("Please use bicep version 0.16.1 or higher.");
}

[Fact]
Expand Down Expand Up @@ -141,5 +141,47 @@ public void BuildParams_fails_for_missing_param_file()
FluentActions.Invoking(() => bicepUtility.BuildParams("foo.bicepparam", new Dictionary<string, object>()))
.Should().Throw<AzPSArgumentException>().WithMessage("Invalid Bicepparam file path.");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void WithSource_adds_with_source_to_CLI()
{
var invokerMock = new Mock<IProcessInvoker>(MockBehavior.Strict);
invokerMock.Setup(x => x.Invoke(It.Is<ProcessInput>(p => p.Arguments == "-v")))
.Returns(new ProcessOutput { ExitCode = 0, Stderr = "", Stdout = "Bicep CLI version 0.23.1 (b02de2da48)" });
invokerMock.Setup(x => x.Invoke(It.Is<ProcessInput>(p => p.Arguments == "publish \"foo.bicep\" --target \"br:example.azurecr.io/hello/world:v1\" --with-source")))
.Returns(new ProcessOutput { ExitCode = 0, Stderr = "", Stdout = "" });

invokerMock.Setup(x => x.CheckExecutableExists("bicep"))
.Returns(true);

var dataStoreMock = new Mock<IDataStore>();
dataStoreMock.Setup(x => x.FileExists("foo.bicep"))
.Returns(true);

var bicepUtility = new BicepUtility(invokerMock.Object, dataStoreMock.Object);

bicepUtility.PublishFile("foo.bicep", "br:example.azurecr.io/hello/world:v1", null, withSource: true);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void WithSource_fails_for_old_bicep_version()
{
var invokerMock = new Mock<IProcessInvoker>(MockBehavior.Strict);
invokerMock.Setup(x => x.Invoke(It.Is<ProcessInput>(p => p.Arguments == "-v")))
.Returns(new ProcessOutput { ExitCode = 0, Stderr = "", Stdout = "Bicep CLI version 0.15.1 (d62b94db31)" });
invokerMock.Setup(x => x.CheckExecutableExists("bicep"))
.Returns(true);

var dataStoreMock = new Mock<IDataStore>();
dataStoreMock.Setup(x => x.FileExists("foo.bicep"))
.Returns(true);

var bicepUtility = new BicepUtility(invokerMock.Object, dataStoreMock.Object);

FluentActions.Invoking(() => bicepUtility.PublishFile("foo.bicep", "br:example.azurecr.io/hello/world:v1", null, withSource: true))
.Should().Throw<AzPSApplicationException>().WithMessage("Please use bicep version 0.23.1 or higher.");
}
}
}
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Fixed reporting duplicate warnings when compiling Bicep files.
* Updated New and Set Management Group cmdlets to allow DeploymentSubscription to be optional.
* Fixed inexplicable error message when subscription and scope are neither provided in RoleAssignment/RoleDefinition related commands. [#22716]
* Added `-WithSource` parameter to `Publish-AzBicepModule` for publishing source with a module (currently experimental)

## Version 6.11.2
* Fixed bug where `.bicepparam` values were not being correctly serialized in the correct format.
Expand Down
19 changes: 17 additions & 2 deletions src/Resources/Resources/help/Publish-AzBicepModule.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Publishes a Bicep file to a registry.
## SYNTAX

```
Publish-AzBicepModule -FilePath <String> -Target <String> [-DocumentationUri <String>] [-Force] [-PassThru]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
Publish-AzBicepModule -FilePath <String> -Target <String> [-DocumentationUri <String>] [-WithSource] [-Force]
[-PassThru] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -128,6 +128,21 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -WithSource
[Experimental] Publish source code with the module.
```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
Expand Down

0 comments on commit ce6c1ae

Please sign in to comment.