12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
16
- using Microsoft . Azure . Commands . Common . Exceptions ;
17
- using Microsoft . WindowsAzure . Commands . Utilities . Common ;
18
-
19
- using System ;
20
- using System . Collections . Generic ;
21
- using System . IO ;
22
- using System . Text . RegularExpressions ;
23
-
24
15
namespace Microsoft . Azure . Commands . ResourceManager . Cmdlets . Utilities
25
16
{
17
+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
18
+ using Microsoft . Azure . Commands . Common . Exceptions ;
19
+ using Microsoft . WindowsAzure . Commands . Utilities . Common ;
20
+ using System ;
21
+ using System . IO ;
22
+ using System . Management . Automation ;
23
+ using System . Text . RegularExpressions ;
24
+
26
25
internal static class BicepUtility
27
26
{
28
- public static bool IsBicepExecutable { get ; private set ; } = false ;
27
+ private static bool IsBicepExecutable = false ;
28
+
29
+ private const string MinimalVersionRequirement = "0.3.1" ;
30
+
31
+ private const string MinimalVersionRequirementForBicepPublish = "0.4.1008" ;
29
32
30
- public static string MinimalVersionRequirement { get ; private set ; } = "0.3.1" ;
33
+ public delegate void OutputCallback ( string msg ) ;
31
34
32
- public static bool IsBicepFile ( string templateFilePath )
35
+ public static bool IsBicepFile ( string templateFilePath ) =>
36
+ ".bicep" . Equals ( Path . GetExtension ( templateFilePath ) , StringComparison . OrdinalIgnoreCase ) ;
37
+
38
+ public static string BuildFile ( string bicepTemplateFilePath , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
33
39
{
34
- return ".bicep" . Equals ( Path . GetExtension ( templateFilePath ) , System . StringComparison . OrdinalIgnoreCase ) ;
40
+ if ( ! FileUtilities . DataStore . FileExists ( bicepTemplateFilePath ) )
41
+ {
42
+ throw new AzPSArgumentException ( Properties . Resources . InvalidBicepFilePath , "TemplateFile" ) ;
43
+ }
44
+
45
+ string tempDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
46
+ Directory . CreateDirectory ( tempDirectory ) ;
47
+
48
+ RunBicepCommand ( $ "bicep build '{ bicepTemplateFilePath } ' --outdir '{ tempDirectory } '", MinimalVersionRequirement , writeVerbose , writeWarning ) ;
49
+
50
+ string buildResultPath = Path . Combine ( tempDirectory , Path . GetFileName ( bicepTemplateFilePath ) ) . Replace ( ".bicep" , ".json" ) ;
51
+ if ( ! FileUtilities . DataStore . FileExists ( buildResultPath ) )
52
+ {
53
+ throw new AzPSApplicationException ( string . Format ( Properties . Resources . BuildBicepFileToJsonFailed , bicepTemplateFilePath ) ) ;
54
+ }
55
+
56
+ return buildResultPath ;
35
57
}
36
58
37
- public static bool CheckBicepExecutable ( )
59
+ public static void PublishFile ( string bicepFilePath , string target , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
38
60
{
39
- System . Management . Automation . PowerShell powershell = System . Management . Automation . PowerShell . Create ( ) ;
40
- powershell . AddScript ( "Get-Command bicep" ) ;
41
- powershell . Invoke ( ) ;
42
- powershell . AddScript ( "$?" ) ;
43
- var result = powershell . Invoke ( ) ;
44
- bool . TryParse ( result [ 0 ] . ToString ( ) , out bool res ) ;
45
- // Cache result
46
- IsBicepExecutable = res ;
47
- return IsBicepExecutable ;
61
+ if ( ! FileUtilities . DataStore . FileExists ( bicepFilePath ) )
62
+ {
63
+ throw new AzPSArgumentException ( Properties . Resources . InvalidBicepFilePath , "File" ) ;
64
+ }
65
+
66
+ RunBicepCommand ( $ "bicep publish '{ bicepFilePath } ' --target '{ target } '", MinimalVersionRequirementForBicepPublish , writeVerbose , writeWarning ) ;
67
+ }
68
+
69
+ private static void CheckBicepExecutable ( )
70
+ {
71
+ using ( var powerShell = PowerShell . Create ( ) )
72
+ {
73
+ if ( IsBicepExecutable )
74
+ {
75
+ return ;
76
+ }
77
+
78
+ powerShell . AddScript ( "Get-Command bicep" ) ;
79
+ powerShell . Invoke ( ) ;
80
+ powerShell . AddScript ( "$?" ) ;
81
+ var result = powerShell . Invoke ( ) ;
82
+ // Cache result
83
+ bool . TryParse ( result [ 0 ] . ToString ( ) , out IsBicepExecutable ) ;
84
+
85
+ if ( ! IsBicepExecutable )
86
+ {
87
+ throw new AzPSApplicationException ( Properties . Resources . BicepNotFound ) ;
88
+ }
89
+ }
48
90
}
49
91
50
92
private static string CheckMinimalVersionRequirement ( string minimalVersionRequirement )
51
93
{
52
- string currentBicepVersion = GetBicepVesion ( ) ;
94
+ string currentBicepVersion = GetBicepVersion ( ) ;
53
95
if ( Version . Parse ( minimalVersionRequirement ) . CompareTo ( Version . Parse ( currentBicepVersion ) ) > 0 )
54
96
{
55
97
throw new AzPSApplicationException ( string . Format ( Properties . Resources . BicepVersionRequirement , minimalVersionRequirement ) ) ;
56
98
} ;
57
99
return currentBicepVersion ;
58
100
}
59
101
60
- public static string GetBicepVesion ( )
102
+ private static string GetBicepVersion ( )
61
103
{
62
- using ( System . Management . Automation . PowerShell powershell = System . Management . Automation . PowerShell . Create ( ) )
104
+ using ( var powerShell = PowerShell . Create ( ) )
63
105
{
64
- powershell . AddScript ( "bicep -v" ) ;
65
- var result = powershell . Invoke ( ) [ 0 ] . ToString ( ) ;
106
+ powerShell . AddScript ( "bicep -v" ) ;
107
+ var result = powerShell . Invoke ( ) [ 0 ] . ToString ( ) ;
66
108
Regex pattern = new Regex ( "\\ d+(\\ .\\ d+)+" ) ;
67
109
string bicepVersion = pattern . Match ( result ) ? . Value ;
110
+
68
111
return bicepVersion ;
69
112
}
70
113
}
71
114
72
- public static int GetLastExitCode ( System . Management . Automation . PowerShell powershell )
115
+ private static int GetLastExitCode ( PowerShell powershell )
73
116
{
74
117
powershell . AddScript ( "$LASTEXITCODE" ) ;
75
118
var result = powershell . Invoke ( ) ;
76
119
int . TryParse ( result [ 0 ] ? . ToString ( ) , out int exitcode ) ;
77
120
return exitcode ;
78
121
}
79
122
80
- public delegate void VerboseOutputMethod ( string msg ) ;
81
- public delegate void WarningOutputMethod ( string msg ) ;
82
-
83
- public static string BuildFile ( string bicepTemplateFilePath , VerboseOutputMethod writeVerbose = null , WarningOutputMethod writeWarning = null )
123
+ private static void RunBicepCommand ( string command , string minimalVersionRequirement , OutputCallback writeVerbose = null , OutputCallback writeWarning = null )
84
124
{
85
- if ( ! IsBicepExecutable && ! CheckBicepExecutable ( ) )
86
- {
87
- throw new AzPSApplicationException ( Properties . Resources . BicepNotFound ) ;
88
- }
125
+ CheckBicepExecutable ( ) ;
89
126
90
- string currentBicepVersion = CheckMinimalVersionRequirement ( MinimalVersionRequirement ) ;
127
+ string currentBicepVersion = CheckMinimalVersionRequirement ( minimalVersionRequirement ) ;
91
128
92
- string tempDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
93
- Directory . CreateDirectory ( tempDirectory ) ;
94
-
95
- if ( FileUtilities . DataStore . FileExists ( bicepTemplateFilePath ) )
129
+ using ( var powerShell = PowerShell . Create ( ) )
96
130
{
97
- using ( System . Management . Automation . PowerShell powershell = System . Management . Automation . PowerShell . Create ( ) )
131
+ powerShell . AddScript ( command ) ;
132
+ var result = powerShell . Invoke ( ) ;
133
+
134
+ if ( writeVerbose != null )
98
135
{
99
- powershell . AddScript ( $ "bicep build '{ bicepTemplateFilePath } ' --outdir '{ tempDirectory } '") ;
100
- var result = powershell . Invoke ( ) ;
101
-
102
- if ( writeVerbose != null )
103
- {
104
- writeVerbose ( string . Format ( "Using Bicep v{0}" , currentBicepVersion ) ) ;
105
- result . ForEach ( r => writeVerbose ( r . ToString ( ) ) ) ;
106
- }
136
+ writeVerbose ( string . Format ( "Using Bicep v{0}" , currentBicepVersion ) ) ;
137
+ result . ForEach ( r => writeVerbose ( r . ToString ( ) ) ) ;
138
+ }
107
139
108
- // Bicep uses error stream to report warning message and error message, record it
109
- string warningOrErrorMsg = string . Empty ;
110
- if ( powershell . HadErrors )
111
- {
112
- powershell . Streams . Error . ForEach ( e => { warningOrErrorMsg += ( e + Environment . NewLine ) ; } ) ;
113
- warningOrErrorMsg = warningOrErrorMsg . Substring ( 0 , warningOrErrorMsg . Length - Environment . NewLine . Length ) ;
114
- }
140
+ // Bicep uses error stream to report warning message and error message, record it
141
+ string warningOrErrorMsg = string . Empty ;
142
+ if ( powerShell . HadErrors )
143
+ {
144
+ powerShell . Streams . Error . ForEach ( e => { warningOrErrorMsg += ( e + Environment . NewLine ) ; } ) ;
145
+ warningOrErrorMsg = warningOrErrorMsg . Substring ( 0 , warningOrErrorMsg . Length - Environment . NewLine . Length ) ;
146
+ }
115
147
116
- if ( 0 == GetLastExitCode ( powershell ) )
117
- {
118
- // print warning message
119
- if ( writeWarning != null && ! string . IsNullOrEmpty ( warningOrErrorMsg ) )
120
- {
121
- writeWarning ( warningOrErrorMsg ) ;
122
- }
123
- }
124
- else
148
+ if ( 0 == GetLastExitCode ( powerShell ) )
149
+ {
150
+ // print warning message
151
+ if ( writeWarning != null && ! string . IsNullOrEmpty ( warningOrErrorMsg ) )
125
152
{
126
- throw new AzPSApplicationException ( warningOrErrorMsg ) ;
153
+ writeWarning ( warningOrErrorMsg ) ;
127
154
}
128
155
}
156
+ else
157
+ {
158
+ throw new AzPSApplicationException ( warningOrErrorMsg ) ;
159
+ }
129
160
}
130
- else
131
- {
132
- throw new AzPSArgumentException ( Properties . Resources . InvalidBicepFilePath , "TemplateFile" ) ;
133
- }
134
-
135
- string buildResultPath = Path . Combine ( tempDirectory , Path . GetFileName ( bicepTemplateFilePath ) ) . Replace ( ".bicep" , ".json" ) ;
136
- if ( ! FileUtilities . DataStore . FileExists ( buildResultPath ) )
137
- {
138
- throw new AzPSApplicationException ( string . Format ( Properties . Resources . BuildBicepFileToJsonFailed , bicepTemplateFilePath ) ) ;
139
- }
140
- return buildResultPath ;
141
161
}
142
162
}
143
- }
163
+ }
0 commit comments