Skip to content

Commit 1abbafc

Browse files
Use --stdout flag for bicep build (Azure#23997)
* Use --stdout flag for bicep build * Attempt to fix tests * Changelog entry * Address feedback
1 parent b2e224d commit 1abbafc

15 files changed

+107
-254
lines changed

src/Resources/ResourceManager/Components/TemplateSpecPackagingEngine.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
1616
{
1717
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
18+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
1819
using Microsoft.Azure.Management.Resources.Models;
1920
using Newtonsoft.Json.Linq;
2021
using System;
@@ -78,34 +79,32 @@ public PackingContext(string rootTemplateDirectory)
7879
/// <param name="rootTemplateFilePath">
7980
/// The path to the ARM Template .json file to pack
8081
/// </param>
81-
/// <param name="uiFormDefinitionFilePath">
82-
/// The path to the UI Form Definition .json to pack (if any)
83-
/// </param>
84-
public static PackagedTemplate Pack(string rootTemplateFilePath,
85-
string uiFormDefinitionFilePath)
82+
public static PackagedTemplate Pack(string rootTemplateFilePath)
8683
{
8784
rootTemplateFilePath = Path.GetFullPath(rootTemplateFilePath);
8885
PackingContext context = new PackingContext(
8986
Path.GetDirectoryName(rootTemplateFilePath)
9087
);
9188

9289
PackArtifacts(rootTemplateFilePath, context, out JObject templateObj);
93-
var packagedTemplate = new PackagedTemplate
90+
91+
return new PackagedTemplate
9492
{
9593
RootTemplate = templateObj,
9694
Artifacts = context.Artifacts.ToArray()
9795
};
96+
}
9897

99-
// If a UI Form Definition file path was provided to us, make sure we package the
100-
// UI Form Definition as well:
101-
102-
if (!string.IsNullOrWhiteSpace(uiFormDefinitionFilePath))
98+
public static PackagedTemplate PackBicep(string filePath, Action<string> writeVerbose, Action<string> writeWarning)
99+
{
100+
// Complex packaging is unneccessary for Bicep because it doesn't support usage of 'relativePath'.
101+
var templateJson = BicepUtility.Create().BuildBicepFile(filePath, msg => writeVerbose(msg), msg => writeWarning(msg));
102+
103+
return new PackagedTemplate
103104
{
104-
string uiFormDefinitionJson = FileUtilities.DataStore.ReadFileAsText(uiFormDefinitionFilePath);
105-
packagedTemplate.UIFormDefinition = JObject.Parse(uiFormDefinitionJson);
106-
}
107-
108-
return packagedTemplate;
105+
RootTemplate = JObject.Parse(templateJson),
106+
Artifacts = Array.Empty<LinkedTemplateArtifact>(),
107+
};
109108
}
110109

111110
/// <summary>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ protected string[] GetStaticParameterNames()
475475

476476
protected void BuildAndUseBicepTemplate()
477477
{
478-
TemplateFile = BicepUtility.Create().BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning);
478+
var templateJson = BicepUtility.Create().BuildBicepFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning);
479+
TemplateObject = JsonConvert.DeserializeObject<Hashtable>(templateJson);
480+
TemplateFile = null;
479481
}
480482

481483
private IReadOnlyDictionary<string, object> GetDynamicParametersDictionary()
@@ -490,7 +492,7 @@ private IReadOnlyDictionary<string, object> GetDynamicParametersDictionary()
490492
protected void BuildAndUseBicepParameters(bool emitWarnings)
491493
{
492494
BicepUtility.OutputCallback nullCallback = null;
493-
var output = BicepUtility.Create().BuildParams(this.ResolvePath(TemplateParameterFile), GetDynamicParametersDictionary(), this.WriteVerbose, emitWarnings ? this.WriteWarning : nullCallback);
495+
var output = BicepUtility.Create().BuildBicepParamFile(this.ResolvePath(TemplateParameterFile), GetDynamicParametersDictionary(), this.WriteVerbose, emitWarnings ? this.WriteWarning : nullCallback);
494496
bicepparamFileParameters = TemplateUtility.ParseTemplateParameterJson(output.parametersJson);
495497

496498
if (TemplateObject == null &&

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,23 @@ public DeploymentStacksSdkClient DeploymentStacksSdkClient
5656
}
5757
}
5858

59-
protected string ResolveBicepFile(string TemplateFile)
59+
protected BicepBuildParamsStdout ResolveBicepParameterFile(string TemplateParameterFile)
6060
{
61-
if (BicepUtility.IsBicepFile(TemplateFile))
61+
if (BicepUtility.IsBicepparamFile(TemplateParameterFile))
6262
{
63-
return BicepUtility.Create().BuildFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning);
63+
return BicepUtility.Create().BuildBicepParamFile(this.ResolvePath(TemplateParameterFile), new Dictionary<string, object>(), this.WriteVerbose, this.WriteWarning);
6464
}
65-
else
66-
return TemplateFile;
67-
65+
66+
return null;
6867
}
6968

70-
protected BicepBuildParamsStdout ResolveBicepParameterFile(string TemplateParameterFile)
69+
protected string ResolveBicepFile(string filePath)
7170
{
72-
if (BicepUtility.IsBicepparamFile(TemplateParameterFile))
71+
if (BicepUtility.IsBicepFile(filePath))
7372
{
74-
return BicepUtility.Create().BuildParams(this.ResolvePath(TemplateParameterFile), new Dictionary<string, object>(), this.WriteVerbose, this.WriteWarning);
73+
return BicepUtility.Create().BuildBicepFile(this.ResolvePath(filePath), this.WriteVerbose, this.WriteWarning);
7574
}
76-
75+
7776
return null;
7877
}
7978

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ protected DeploymentStacksCreateCmdletBase()
155155
return (parameters, templateJson, templateSpecId);
156156
}
157157

158+
protected void ResolveTemplate()
159+
{
160+
var filePath = this.TryResolvePath(TemplateFile);
161+
if (!File.Exists(filePath))
162+
{
163+
throw new PSInvalidOperationException(string.Format(ProjectResources.InvalidFilePath, TemplateFile));
164+
}
165+
166+
var templateJson = ResolveBicepFile(filePath);
167+
if (!string.IsNullOrEmpty(templateJson))
168+
{
169+
TemplateJson = templateJson;
170+
}
171+
else
172+
{
173+
TemplateUri = filePath;
174+
}
175+
}
176+
158177
protected Hashtable ResolveParameters()
159178
{
160179
var output = GetParametersAndMetadataFromFile();

src/Resources/ResourceManager/Implementation/DeploymentStacks/NewAzManagementGroupDeploymentStack.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,12 @@ protected override void OnProcessRecord()
9999
try
100100
{
101101
Hashtable parameters = new Hashtable();
102-
string filePath = "";
103102

104103
switch (ParameterSetName)
105104
{
106105
case ParameterlessTemplateFileParameterSetName:
107106
case ParameterUriTemplateFileParameterSetName:
108-
filePath = this.TryResolvePath(TemplateFile);
109-
if (!File.Exists(filePath))
110-
{
111-
throw new PSInvalidOperationException(
112-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
113-
}
114-
filePath = ResolveBicepFile(filePath);
115-
TemplateUri = filePath;
107+
ResolveTemplate();
116108
break;
117109
case ParameterFileTemplateSpecParameterSetName:
118110
case ParameterFileTemplateUriParameterSetName:
@@ -129,29 +121,13 @@ protected override void OnProcessRecord()
129121
break;
130122
case ParameterFileTemplateFileParameterSetName:
131123
parameters = ResolveParameters();
132-
133-
filePath = this.TryResolvePath(TemplateFile);
134-
if (!File.Exists(filePath))
135-
{
136-
throw new PSInvalidOperationException(
137-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
138-
}
139-
filePath = ResolveBicepFile(filePath);
140-
141-
TemplateUri = filePath;
124+
ResolveTemplate();
142125
break;
143126
case ByParameterFileWithNoTemplateParameterSetName:
144127
parameters = ResolveParameters();
145128
break;
146129
case ParameterObjectTemplateFileParameterSetName:
147-
filePath = this.TryResolvePath(TemplateFile);
148-
if (!File.Exists(filePath))
149-
{
150-
throw new PSInvalidOperationException(
151-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
152-
}
153-
filePath = ResolveBicepFile(filePath);
154-
TemplateUri = filePath;
130+
ResolveTemplate();
155131
parameters = GetTemplateParameterObject(TemplateParameterObject);
156132
break;
157133
case ParameterObjectTemplateSpecParameterSetName:

src/Resources/ResourceManager/Implementation/DeploymentStacks/NewAzResourceGroupDeploymentStack.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,12 @@ protected override void OnProcessRecord()
9191
try
9292
{
9393
Hashtable parameters = new Hashtable();
94-
string filePath = "";
9594

9695
switch (ParameterSetName)
9796
{
9897
case ParameterlessTemplateFileParameterSetName:
9998
case ParameterUriTemplateFileParameterSetName:
100-
filePath = this.TryResolvePath(TemplateFile);
101-
if(!File.Exists(filePath))
102-
{
103-
throw new PSInvalidOperationException(
104-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
105-
}
106-
filePath = ResolveBicepFile(filePath);
107-
TemplateUri = filePath;
99+
ResolveTemplate();
108100
break;
109101
case ParameterFileTemplateSpecParameterSetName:
110102
case ParameterFileTemplateUriParameterSetName:
@@ -121,29 +113,13 @@ protected override void OnProcessRecord()
121113
break;
122114
case ParameterFileTemplateFileParameterSetName:
123115
parameters = ResolveParameters();
124-
125-
filePath = this.TryResolvePath(TemplateFile);
126-
if (!File.Exists(filePath))
127-
{
128-
throw new PSInvalidOperationException(
129-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
130-
}
131-
filePath = ResolveBicepFile(filePath);
132-
133-
TemplateUri = filePath;
116+
ResolveTemplate();
134117
break;
135118
case ByParameterFileWithNoTemplateParameterSetName:
136119
parameters = ResolveParameters();
137120
break;
138121
case ParameterObjectTemplateFileParameterSetName:
139-
filePath = this.TryResolvePath(TemplateFile);
140-
if (!File.Exists(filePath))
141-
{
142-
throw new PSInvalidOperationException(
143-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
144-
}
145-
filePath = ResolveBicepFile(filePath);
146-
TemplateUri = filePath;
122+
ResolveTemplate();
147123
parameters = GetTemplateParameterObject(TemplateParameterObject);
148124
break;
149125
case ParameterObjectTemplateSpecParameterSetName:

src/Resources/ResourceManager/Implementation/DeploymentStacks/NewAzSubscriptionDeploymentStack.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,12 @@ protected override void OnProcessRecord()
9595
try
9696
{
9797
Hashtable parameters = new Hashtable();
98-
string filePath = "";
9998

10099
switch (ParameterSetName)
101100
{
102101
case ParameterlessTemplateFileParameterSetName:
103102
case ParameterUriTemplateFileParameterSetName:
104-
filePath = this.TryResolvePath(TemplateFile);
105-
if (!File.Exists(filePath))
106-
{
107-
throw new PSInvalidOperationException(
108-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
109-
}
110-
filePath = ResolveBicepFile(filePath);
111-
TemplateUri = filePath;
103+
ResolveTemplate();
112104
break;
113105
case ParameterFileTemplateSpecParameterSetName:
114106
case ParameterFileTemplateUriParameterSetName:
@@ -125,29 +117,13 @@ protected override void OnProcessRecord()
125117
break;
126118
case ParameterFileTemplateFileParameterSetName:
127119
parameters = ResolveParameters();
128-
129-
filePath = this.TryResolvePath(TemplateFile);
130-
if (!File.Exists(filePath))
131-
{
132-
throw new PSInvalidOperationException(
133-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
134-
}
135-
filePath = ResolveBicepFile(filePath);
136-
137-
TemplateUri = filePath;
120+
ResolveTemplate();
138121
break;
139122
case ByParameterFileWithNoTemplateParameterSetName:
140123
parameters = ResolveParameters();
141124
break;
142125
case ParameterObjectTemplateFileParameterSetName:
143-
filePath = this.TryResolvePath(TemplateFile);
144-
if (!File.Exists(filePath))
145-
{
146-
throw new PSInvalidOperationException(
147-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
148-
}
149-
filePath = ResolveBicepFile(filePath);
150-
TemplateUri = filePath;
126+
ResolveTemplate();
151127
parameters = GetTemplateParameterObject(TemplateParameterObject);
152128
break;
153129
case ParameterObjectTemplateSpecParameterSetName:

src/Resources/ResourceManager/Implementation/DeploymentStacks/SetAzManagmentGroupDeploymentStack.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,12 @@ protected override void OnProcessRecord()
9999
try
100100
{
101101
Hashtable parameters = new Hashtable();
102-
string filePath = "";
103102

104103
switch (ParameterSetName)
105104
{
106105
case ParameterlessTemplateFileParameterSetName:
107106
case ParameterUriTemplateFileParameterSetName:
108-
filePath = this.TryResolvePath(TemplateFile);
109-
if (!File.Exists(filePath))
110-
{
111-
throw new PSInvalidOperationException(
112-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
113-
}
114-
filePath = ResolveBicepFile(filePath);
115-
TemplateUri = filePath;
107+
ResolveTemplate();
116108
break;
117109
case ParameterFileTemplateSpecParameterSetName:
118110
case ParameterFileTemplateUriParameterSetName:
@@ -129,29 +121,13 @@ protected override void OnProcessRecord()
129121
break;
130122
case ParameterFileTemplateFileParameterSetName:
131123
parameters = ResolveParameters();
132-
133-
filePath = this.TryResolvePath(TemplateFile);
134-
if (!File.Exists(filePath))
135-
{
136-
throw new PSInvalidOperationException(
137-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
138-
}
139-
filePath = ResolveBicepFile(filePath);
140-
141-
TemplateUri = filePath;
124+
ResolveTemplate();
142125
break;
143126
case ByParameterFileWithNoTemplateParameterSetName:
144127
parameters = ResolveParameters();
145128
break;
146129
case ParameterObjectTemplateFileParameterSetName:
147-
filePath = this.TryResolvePath(TemplateFile);
148-
if (!File.Exists(filePath))
149-
{
150-
throw new PSInvalidOperationException(
151-
string.Format(ProjectResources.InvalidFilePath, TemplateFile));
152-
}
153-
filePath = ResolveBicepFile(filePath);
154-
TemplateUri = filePath;
130+
ResolveTemplate();
155131
parameters = GetTemplateParameterObject(TemplateParameterObject);
156132
break;
157133
case ParameterObjectTemplateSpecParameterSetName:

0 commit comments

Comments
 (0)