@@ -23,6 +23,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities
23
23
using System . Collections . Generic ;
24
24
using System . IO ;
25
25
using System . Text . RegularExpressions ;
26
+ using Microsoft . Azure . Commands . Common . Authentication ;
26
27
27
28
public class BicepBuildParamsStdout
28
29
{
@@ -33,22 +34,10 @@ public class BicepBuildParamsStdout
33
34
public string templateSpecId { get ; set ; }
34
35
}
35
36
36
- internal static class BicepUtility
37
+ internal class BicepUtility
37
38
{
38
- private static Lazy < string > BicepVersionLazy = new Lazy < string > ( ( ) => {
39
- var processInvoker = ProcessInvoker . Create ( ) ;
40
- if ( ! processInvoker . CheckExecutableExists ( BicepExecutable ) )
41
- {
42
- return null ;
43
- }
44
-
45
- var output = processInvoker . Invoke ( new ProcessInput { Executable = BicepExecutable , Arguments = "-v" } ) ;
46
-
47
- var pattern = new Regex ( "\\ d+(\\ .\\ d+)+" ) ;
48
- return pattern . Match ( output . Stdout ) ? . Value ;
49
- } ) ;
50
-
51
- private static string BicepVersion => BicepVersionLazy . Value ;
39
+ public static BicepUtility Create ( )
40
+ => new BicepUtility ( ProcessInvoker . Create ( ) , FileUtilities . DataStore ) ;
52
41
53
42
/// <summary>
54
43
/// The Bicep executable to use. By default, this'll be resolved from the system PATH.
@@ -72,15 +61,38 @@ internal static class BicepUtility
72
61
73
62
public delegate void OutputCallback ( string msg ) ;
74
63
64
+ private readonly IProcessInvoker processInvoker ;
65
+ private readonly IDataStore dataStore ;
66
+ private readonly Lazy < string > bicepVersionLazy ;
67
+
68
+ public BicepUtility ( IProcessInvoker processInvoker , IDataStore dataStore )
69
+ {
70
+ this . processInvoker = processInvoker ;
71
+ this . dataStore = dataStore ;
72
+ this . bicepVersionLazy = new Lazy < string > ( ( ) => {
73
+ if ( ! processInvoker . CheckExecutableExists ( BicepExecutable ) )
74
+ {
75
+ return null ;
76
+ }
77
+
78
+ var output = processInvoker . Invoke ( new ProcessInput { Executable = BicepExecutable , Arguments = "-v" } ) ;
79
+
80
+ var pattern = new Regex ( "\\ d+(\\ .\\ d+)+" ) ;
81
+ return pattern . Match ( output . Stdout ) ? . Value ;
82
+ } ) ;
83
+ }
84
+
85
+ private string BicepVersion => bicepVersionLazy . Value ;
86
+
75
87
public static bool IsBicepFile ( string templateFilePath ) =>
76
88
".bicep" . Equals ( Path . GetExtension ( templateFilePath ) , StringComparison . OrdinalIgnoreCase ) ;
77
89
78
90
public static bool IsBicepparamFile ( string parametersFilePath ) =>
79
91
".bicepparam" . Equals ( Path . GetExtension ( parametersFilePath ) , StringComparison . OrdinalIgnoreCase ) ;
80
92
81
- public static string BuildFile ( string bicepTemplateFilePath , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
93
+ public string BuildFile ( string bicepTemplateFilePath , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
82
94
{
83
- if ( ! FileUtilities . DataStore . FileExists ( bicepTemplateFilePath ) )
95
+ if ( ! dataStore . FileExists ( bicepTemplateFilePath ) )
84
96
{
85
97
throw new AzPSArgumentException ( Properties . Resources . InvalidBicepFilePath , "TemplateFile" ) ;
86
98
}
@@ -91,17 +103,17 @@ public static string BuildFile(string bicepTemplateFilePath, OutputCallback writ
91
103
RunBicepCommand ( $ "build { GetQuotedFilePath ( bicepTemplateFilePath ) } --outdir { GetQuotedFilePath ( tempDirectory ) } ", MinimalVersionRequirement , writeVerbose , writeWarning ) ;
92
104
93
105
string buildResultPath = Path . Combine ( tempDirectory , Path . GetFileName ( bicepTemplateFilePath ) ) . Replace ( ".bicep" , ".json" ) ;
94
- if ( ! FileUtilities . DataStore . FileExists ( buildResultPath ) )
106
+ if ( ! dataStore . FileExists ( buildResultPath ) )
95
107
{
96
108
throw new AzPSApplicationException ( string . Format ( Properties . Resources . BuildBicepFileToJsonFailed , bicepTemplateFilePath ) ) ;
97
109
}
98
110
99
111
return buildResultPath ;
100
112
}
101
113
102
- public static BicepBuildParamsStdout BuildParams ( string bicepParamFilePath , IReadOnlyDictionary < string , object > overrideParams , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
114
+ public BicepBuildParamsStdout BuildParams ( string bicepParamFilePath , IReadOnlyDictionary < string , object > overrideParams , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
103
115
{
104
- if ( ! FileUtilities . DataStore . FileExists ( bicepParamFilePath ) )
116
+ if ( ! dataStore . FileExists ( bicepParamFilePath ) )
105
117
{
106
118
throw new AzPSArgumentException ( Properties . Resources . InvalidBicepparamFilePath , "TemplateParameterFile" ) ;
107
119
}
@@ -124,9 +136,9 @@ public static BicepBuildParamsStdout BuildParams(string bicepParamFilePath, IRea
124
136
return JsonConvert . DeserializeObject < BicepBuildParamsStdout > ( stdout ) ;
125
137
}
126
138
127
- public static void PublishFile ( string bicepFilePath , string target , string documentationUri = null , bool force = false , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
139
+ public void PublishFile ( string bicepFilePath , string target , string documentationUri = null , bool force = false , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
128
140
{
129
- if ( ! FileUtilities . DataStore . FileExists ( bicepFilePath ) )
141
+ if ( ! dataStore . FileExists ( bicepFilePath ) )
130
142
{
131
143
throw new AzPSArgumentException ( Properties . Resources . InvalidBicepFilePath , "File" ) ;
132
144
}
@@ -148,15 +160,15 @@ public static void PublishFile(string bicepFilePath, string target, string docum
148
160
RunBicepCommand ( bicepPublishCommand , MinimalVersionRequirementForBicepPublish , writeVerbose , writeWarning ) ;
149
161
}
150
162
151
- private static void CheckBicepExecutable ( )
163
+ private void CheckBicepExecutable ( )
152
164
{
153
165
if ( BicepVersion == null )
154
166
{
155
167
throw new AzPSApplicationException ( Properties . Resources . BicepNotFound ) ;
156
168
}
157
169
}
158
170
159
- private static string CheckMinimalVersionRequirement ( string minimalVersionRequirement )
171
+ private string CheckMinimalVersionRequirement ( string minimalVersionRequirement )
160
172
{
161
173
CheckBicepExecutable ( ) ;
162
174
@@ -168,14 +180,13 @@ private static string CheckMinimalVersionRequirement(string minimalVersionRequir
168
180
return BicepVersion ;
169
181
}
170
182
171
- private static string RunBicepCommandWithStdoutCapture ( string arguments , string minimalVersionRequirement , Dictionary < string , string > envVars = null , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
183
+ private string RunBicepCommandWithStdoutCapture ( string arguments , string minimalVersionRequirement , Dictionary < string , string > envVars = null , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
172
184
{
173
185
string currentBicepVersion = CheckMinimalVersionRequirement ( minimalVersionRequirement ) ;
174
186
writeVerbose ? . Invoke ( $ "Using Bicep v{ currentBicepVersion } ") ;
175
187
176
188
writeVerbose ? . Invoke ( $ "Calling Bicep with arguments: { arguments } ") ;
177
189
178
- var processInvoker = ProcessInvoker . Create ( ) ;
179
190
var output = processInvoker . Invoke ( new ProcessInput { Executable = BicepExecutable , Arguments = arguments , EnvVars = envVars } ) ;
180
191
181
192
if ( output . ExitCode != 0 )
@@ -192,14 +203,13 @@ private static string RunBicepCommandWithStdoutCapture(string arguments, string
192
203
return output . Stdout ;
193
204
}
194
205
195
- private static void RunBicepCommand ( string arguments , string minimalVersionRequirement , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
206
+ private void RunBicepCommand ( string arguments , string minimalVersionRequirement , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
196
207
{
197
208
string currentBicepVersion = CheckMinimalVersionRequirement ( minimalVersionRequirement ) ;
198
209
writeVerbose ? . Invoke ( $ "Using Bicep v{ currentBicepVersion } ") ;
199
210
200
211
writeVerbose ? . Invoke ( $ "Calling Bicep with arguments: { arguments } ") ;
201
212
202
- var processInvoker = ProcessInvoker . Create ( ) ;
203
213
var output = processInvoker . Invoke ( new ProcessInput { Executable = BicepExecutable , Arguments = arguments } ) ;
204
214
205
215
writeVerbose ? . Invoke ( output . Stdout ) ;
0 commit comments