Skip to content

Remove the logic of coping file to temp folder #14444

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

Merged
merged 3 commits into from
Mar 5, 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
15 changes: 12 additions & 3 deletions src/Resources/ResourceManager/Properties/Resources.Designer.cs

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

7 changes: 5 additions & 2 deletions src/Resources/ResourceManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,10 @@ You can help us improve the accuracy of the result by opening an issue here: htt
<data name="BicepNotFound" xml:space="preserve">
<value>Cannot find Bicep. Please add Bicep to your PATH or visit https://github.com/Azure/bicep/blob/main/docs/installing.md to install Bicep.</value>
</data>
<data name="InvalidBicepFilePathOrUri" xml:space="preserve">
<value>Invalid Bicep file path or URI.</value>
<data name="InvalidBicepFilePath" xml:space="preserve">
<value>Invalid Bicep file path.</value>
</data>
<data name="BicepVersionRequirement" xml:space="preserve">
<value>Please use bicep '{0}' or higher verison.</value>
</data>
</root>
59 changes: 41 additions & 18 deletions src/Resources/ResourceManager/Utilities/BicepUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities
{
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);
Expand All @@ -48,36 +51,56 @@ public static bool CheckBicepExecutable<T>(ScriptExecutor<T> 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<T>(ScriptExecutor<T> 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");
}
}
}
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down