diff --git a/src/Resources/ResourceManager/Properties/Resources.Designer.cs b/src/Resources/ResourceManager/Properties/Resources.Designer.cs index 01ef21bd0207..f96ac295b8f8 100644 --- a/src/Resources/ResourceManager/Properties/Resources.Designer.cs +++ b/src/Resources/ResourceManager/Properties/Resources.Designer.cs @@ -78,6 +78,15 @@ internal static string BicepNotFound { } } + /// + /// Looks up a localized string similar to Please use bicep '{0}' or higher verison.. + /// + internal static string BicepVersionRequirement { + get { + return ResourceManager.GetString("BicepVersionRequirement", resourceCulture); + } + } + /// /// Looks up a localized string similar to Cancelling active deployment .... /// @@ -458,11 +467,11 @@ internal static string InvalidAssignableScopes { } /// - /// Looks up a localized string similar to Invalid Bicep file path or URI.. + /// Looks up a localized string similar to Invalid Bicep file path.. /// - internal static string InvalidBicepFilePathOrUri { + internal static string InvalidBicepFilePath { get { - return ResourceManager.GetString("InvalidBicepFilePathOrUri", resourceCulture); + return ResourceManager.GetString("InvalidBicepFilePath", resourceCulture); } } diff --git a/src/Resources/ResourceManager/Properties/Resources.resx b/src/Resources/ResourceManager/Properties/Resources.resx index e337749321e2..fbcb0a3acab2 100644 --- a/src/Resources/ResourceManager/Properties/Resources.resx +++ b/src/Resources/ResourceManager/Properties/Resources.resx @@ -497,7 +497,10 @@ You can help us improve the accuracy of the result by opening an issue here: htt Cannot find Bicep. Please add Bicep to your PATH or visit https://github.com/Azure/bicep/blob/main/docs/installing.md to install Bicep. - - Invalid Bicep file path or URI. + + Invalid Bicep file path. + + + Please use bicep '{0}' or higher verison. \ No newline at end of file diff --git a/src/Resources/ResourceManager/Utilities/BicepUtility.cs b/src/Resources/ResourceManager/Utilities/BicepUtility.cs index da219ce3d41c..c0c4ae144d93 100644 --- a/src/Resources/ResourceManager/Utilities/BicepUtility.cs +++ b/src/Resources/ResourceManager/Utilities/BicepUtility.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.RegularExpressions; namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities { @@ -26,6 +27,8 @@ internal static class BicepUtility { public static bool IsBicepExecutable { get; private set; } = false; + public static string MinimalVersionRequirement = "0.3.1"; + public static bool IsBicepFile(string templateFilePath) { return ".bicep".Equals(Path.GetExtension(templateFilePath), System.StringComparison.OrdinalIgnoreCase); @@ -48,36 +51,56 @@ public static bool CheckBicepExecutable(ScriptExecutor executeScript) return IsBicepExecutable; } + private static bool CheckMinimalVersionRequirement(string checkMinumVersionRequirement) + { + + if (Version.Parse(checkMinumVersionRequirement).CompareTo(Version.Parse(GetBicepVesion())) > 0) + { + throw new AzPSApplicationException(string.Format(Properties.Resources.BicepVersionRequirement, checkMinumVersionRequirement)); + }; + return true; + } + + public static string GetBicepVesion() + { + System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create(); + powershell.AddScript("bicep -v"); + var result = powershell.Invoke()[0].ToString(); + Regex pattern = new Regex("\\d+(\\.\\d+)+"); + string bicepVersion = pattern.Match(result)?.Value; + return bicepVersion; + } + public static string BuildFile(ScriptExecutor executeScript, string bicepTemplateFilePath) { if (!IsBicepExecutable && !CheckBicepExecutable(executeScript)) { throw new AzPSApplicationException(Properties.Resources.BicepNotFound); } - - string tempPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(bicepTemplateFilePath)); - try{ - if (Uri.IsWellFormedUriString(bicepTemplateFilePath, UriKind.Absolute)) - { - FileUtilities.DataStore.WriteFile(tempPath, GeneralUtilities.DownloadFile(bicepTemplateFilePath)); - } - else if (FileUtilities.DataStore.FileExists(bicepTemplateFilePath)) - { - File.Copy(bicepTemplateFilePath, tempPath, true); - } - else + CheckMinimalVersionRequirement(MinimalVersionRequirement); + + string tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(tempDirectory); + + if (FileUtilities.DataStore.FileExists(bicepTemplateFilePath)) + { + System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create(); + powershell.AddScript($"bicep build '{bicepTemplateFilePath}' --outdir '{tempDirectory}'"); + powershell.Invoke(); + if (powershell.HadErrors) { - throw new AzPSArgumentException(Properties.Resources.InvalidBicepFilePathOrUri, "TemplateFile"); + string errorMsg = string.Empty; + powershell.Streams.Error.ForEach(e => { errorMsg += (e + Environment.NewLine); }); + throw new AzPSApplicationException(errorMsg); } - executeScript($"bicep build '{tempPath}'"); - return tempPath.Replace(".bicep", ".json"); } - finally + else { - File.Delete(tempPath); + throw new AzPSArgumentException(Properties.Resources.InvalidBicepFilePath, "TemplateFile"); } - + + return Path.Combine(tempDirectory, Path.GetFileName(bicepTemplateFilePath)).Replace(".bicep", ".json"); } } } diff --git a/src/Resources/Resources/ChangeLog.md b/src/Resources/Resources/ChangeLog.md index 2fc5f7245bef..fba0b5873383 100644 --- a/src/Resources/Resources/ChangeLog.md +++ b/src/Resources/Resources/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release +* Removed the logic of coping Bicep template file to temp folder. ## Version 3.3.0 * Added support for Azure resources deployment in Bicep language