Skip to content

Commit 4704bab

Browse files
authored
[Az.Resources] Redirect bicep message to verbose stream (#14449)
* Redirect bicep message to verbose stream * update change log Co-authored-by: Beisi Zhou <[email protected]>
1 parent 9cacc8a commit 4704bab

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

src/Accounts/Authentication.ResourceManager/Properties/Resources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceWithParameterCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ protected string[] GetStaticParameterNames()
436436

437437
protected void BuildAndUseBicepTemplate()
438438
{
439-
TemplateFile = BicepUtility.BuildFile(this.ExecuteScript<Object>, this.ResolvePath(TemplateFile));
439+
TemplateFile = BicepUtility.BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose);
440440
}
441441
}
442442
}

src/Resources/ResourceManager/Utilities/BicepUtility.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,30 @@ internal static class BicepUtility
2727
{
2828
public static bool IsBicepExecutable { get; private set; } = false;
2929

30-
public static string MinimalVersionRequirement = "0.3.1";
30+
public static string MinimalVersionRequirement { get; private set; } = "0.3.1";
3131

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

37-
public delegate List<T> ScriptExecutor<T>(string script);
38-
39-
public static bool CheckBicepExecutable<T>(ScriptExecutor<T> executeScript)
37+
public static bool CheckBicepExecutable()
4038
{
41-
try
42-
{
43-
executeScript("get-command bicep");
44-
}
45-
catch
46-
{
47-
IsBicepExecutable = false;
48-
return IsBicepExecutable;
49-
}
50-
IsBicepExecutable = true;
39+
System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create();
40+
powershell.AddScript("Get-Command bicep");
41+
powershell.Invoke();
42+
IsBicepExecutable = powershell.HadErrors ? false : true;
5143
return IsBicepExecutable;
5244
}
5345

54-
private static bool CheckMinimalVersionRequirement(string checkMinumVersionRequirement)
46+
private static string CheckMinimalVersionRequirement(string minimalVersionRequirement)
5547
{
56-
57-
if (Version.Parse(checkMinumVersionRequirement).CompareTo(Version.Parse(GetBicepVesion())) > 0)
48+
string currentBicepVersion = GetBicepVesion();
49+
if (Version.Parse(minimalVersionRequirement).CompareTo(Version.Parse(currentBicepVersion)) > 0)
5850
{
59-
throw new AzPSApplicationException(string.Format(Properties.Resources.BicepVersionRequirement, checkMinumVersionRequirement));
51+
throw new AzPSApplicationException(string.Format(Properties.Resources.BicepVersionRequirement, minimalVersionRequirement));
6052
};
61-
return true;
53+
return currentBicepVersion;
6254
}
6355

6456
public static string GetBicepVesion()
@@ -71,14 +63,16 @@ public static string GetBicepVesion()
7163
return bicepVersion;
7264
}
7365

74-
public static string BuildFile<T>(ScriptExecutor<T> executeScript, string bicepTemplateFilePath)
66+
public delegate void OutputMethod(string msg);
67+
68+
public static string BuildFile(string bicepTemplateFilePath, OutputMethod outputMethod = null)
7569
{
76-
if (!IsBicepExecutable && !CheckBicepExecutable(executeScript))
70+
if (!IsBicepExecutable && !CheckBicepExecutable())
7771
{
7872
throw new AzPSApplicationException(Properties.Resources.BicepNotFound);
7973
}
8074

81-
CheckMinimalVersionRequirement(MinimalVersionRequirement);
75+
string currentBicepVersion = CheckMinimalVersionRequirement(MinimalVersionRequirement);
8276

8377
string tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
8478
Directory.CreateDirectory(tempDirectory);
@@ -87,7 +81,12 @@ public static string BuildFile<T>(ScriptExecutor<T> executeScript, string bicepT
8781
{
8882
System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create();
8983
powershell.AddScript($"bicep build '{bicepTemplateFilePath}' --outdir '{tempDirectory}'");
90-
powershell.Invoke();
84+
var result = powershell.Invoke();
85+
if (outputMethod != null)
86+
{
87+
outputMethod(string.Format("Using Bicep v{0}", currentBicepVersion));
88+
result.ForEach(r => outputMethod(r.ToString()));
89+
}
9190
if (powershell.HadErrors)
9291
{
9392
string errorMsg = string.Empty;
Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
22
using Microsoft.WindowsAzure.Commands.ScenarioTest;
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Text;
7-
84
using Xunit;
95

106
namespace Microsoft.Azure.Commands.Resources.Test.UnitTests.Utilities
@@ -18,23 +14,5 @@ public void TestIsBicepFile()
1814
Assert.True(BicepUtility.IsBicepFile("test.bicep"));
1915
Assert.False(BicepUtility.IsBicepFile("test.json"));
2016
}
21-
22-
[Fact]
23-
[Trait(Category.AcceptanceType, Category.CheckIn)]
24-
public void TestCheckBicepExecutable()
25-
{
26-
Assert.True(BicepUtility.CheckBicepExecutable(FakeTrueScriptExcutor<Object>));
27-
Assert.False(BicepUtility.CheckBicepExecutable(FakeFalseScriptExcutor<Object>));
28-
}
29-
30-
private List<T> FakeTrueScriptExcutor<T>(string script)
31-
{
32-
return null;
33-
}
34-
35-
private List<T> FakeFalseScriptExcutor<T>(string script)
36-
{
37-
throw new Exception("fake exception");
38-
}
3917
}
4018
}

src/Resources/Resources/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
-->
2020

2121
## Upcoming Release
22-
* Removed the logic of coping Bicep template file to temp folder.
22+
* Redirected bicep message to verbose stream
23+
* Removed the logic of copying Bicep template file to temp folder.
2324

2425
## Version 3.3.0
2526
* Added support for Azure resources deployment in Bicep language

0 commit comments

Comments
 (0)