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

[Az.Resources] Redirect bicep message to verbose stream #14449

Merged
merged 2 commits into from
Mar 8, 2021
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

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

Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ protected string[] GetStaticParameterNames()

protected void BuildAndUseBicepTemplate()
{
TemplateFile = BicepUtility.BuildFile(this.ExecuteScript<Object>, this.ResolvePath(TemplateFile));
TemplateFile = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose);
}
}
}
45 changes: 22 additions & 23 deletions src/Resources/ResourceManager/Utilities/BicepUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,30 @@ internal static class BicepUtility
{
public static bool IsBicepExecutable { get; private set; } = false;

public static string MinimalVersionRequirement = "0.3.1";
public static string MinimalVersionRequirement { get; private set; } = "0.3.1";

public static bool IsBicepFile(string templateFilePath)
{
return ".bicep".Equals(Path.GetExtension(templateFilePath), System.StringComparison.OrdinalIgnoreCase);
}

public delegate List<T> ScriptExecutor<T>(string script);

public static bool CheckBicepExecutable<T>(ScriptExecutor<T> executeScript)
public static bool CheckBicepExecutable()
{
try
{
executeScript("get-command bicep");
}
catch
{
IsBicepExecutable = false;
return IsBicepExecutable;
}
IsBicepExecutable = true;
System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create();
powershell.AddScript("Get-Command bicep");
powershell.Invoke();
IsBicepExecutable = powershell.HadErrors ? false : true;
return IsBicepExecutable;
}

private static bool CheckMinimalVersionRequirement(string checkMinumVersionRequirement)
private static string CheckMinimalVersionRequirement(string minimalVersionRequirement)
{

if (Version.Parse(checkMinumVersionRequirement).CompareTo(Version.Parse(GetBicepVesion())) > 0)
string currentBicepVersion = GetBicepVesion();
if (Version.Parse(minimalVersionRequirement).CompareTo(Version.Parse(currentBicepVersion)) > 0)
{
throw new AzPSApplicationException(string.Format(Properties.Resources.BicepVersionRequirement, checkMinumVersionRequirement));
throw new AzPSApplicationException(string.Format(Properties.Resources.BicepVersionRequirement, minimalVersionRequirement));
};
return true;
return currentBicepVersion;
}

public static string GetBicepVesion()
Expand All @@ -71,14 +63,16 @@ public static string GetBicepVesion()
return bicepVersion;
}

public static string BuildFile<T>(ScriptExecutor<T> executeScript, string bicepTemplateFilePath)
public delegate void OutputMethod(string msg);

public static string BuildFile(string bicepTemplateFilePath, OutputMethod outputMethod = null)
{
if (!IsBicepExecutable && !CheckBicepExecutable(executeScript))
if (!IsBicepExecutable && !CheckBicepExecutable())
{
throw new AzPSApplicationException(Properties.Resources.BicepNotFound);
}

CheckMinimalVersionRequirement(MinimalVersionRequirement);
string currentBicepVersion = CheckMinimalVersionRequirement(MinimalVersionRequirement);

string tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempDirectory);
Expand All @@ -87,7 +81,12 @@ public static string BuildFile<T>(ScriptExecutor<T> executeScript, string bicepT
{
System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create();
powershell.AddScript($"bicep build '{bicepTemplateFilePath}' --outdir '{tempDirectory}'");
powershell.Invoke();
var result = powershell.Invoke();
if (outputMethod != null)
{
outputMethod(string.Format("Using Bicep v{0}", currentBicepVersion));
result.ForEach(r => outputMethod(r.ToString()));
}
if (powershell.HadErrors)
{
string errorMsg = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

using System;
using System.Collections.Generic;
using System.Text;

using Xunit;

namespace Microsoft.Azure.Commands.Resources.Test.UnitTests.Utilities
Expand All @@ -18,23 +14,5 @@ public void TestIsBicepFile()
Assert.True(BicepUtility.IsBicepFile("test.bicep"));
Assert.False(BicepUtility.IsBicepFile("test.json"));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestCheckBicepExecutable()
{
Assert.True(BicepUtility.CheckBicepExecutable(FakeTrueScriptExcutor<Object>));
Assert.False(BicepUtility.CheckBicepExecutable(FakeFalseScriptExcutor<Object>));
}

private List<T> FakeTrueScriptExcutor<T>(string script)
{
return null;
}

private List<T> FakeFalseScriptExcutor<T>(string script)
{
throw new Exception("fake exception");
}
}
}
3 changes: 2 additions & 1 deletion src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
-->

## Upcoming Release
* Removed the logic of coping Bicep template file to temp folder.
* Redirected bicep message to verbose stream
* Removed the logic of copying Bicep template file to temp folder.

## Version 3.3.0
* Added support for Azure resources deployment in Bicep language
Expand Down