@@ -27,38 +27,30 @@ internal static class BicepUtility
27
27
{
28
28
public static bool IsBicepExecutable { get ; private set ; } = false ;
29
29
30
- public static string MinimalVersionRequirement = "0.3.1" ;
30
+ public static string MinimalVersionRequirement { get ; private set ; } = "0.3.1" ;
31
31
32
32
public static bool IsBicepFile ( string templateFilePath )
33
33
{
34
34
return ".bicep" . Equals ( Path . GetExtension ( templateFilePath ) , System . StringComparison . OrdinalIgnoreCase ) ;
35
35
}
36
36
37
- public delegate List < T > ScriptExecutor < T > ( string script ) ;
38
-
39
- public static bool CheckBicepExecutable < T > ( ScriptExecutor < T > executeScript )
37
+ public static bool CheckBicepExecutable ( )
40
38
{
41
- try
42
- {
43
- executeScript ( "get-command bicep" ) ;
44
- }
45
- catch
46
- {
47
- IsBicepExecutable = false ;
48
- return IsBicepExecutable ;
49
- }
50
- IsBicepExecutable = true ;
39
+ System . Management . Automation . PowerShell powershell = System . Management . Automation . PowerShell . Create ( ) ;
40
+ powershell . AddScript ( "Get-Command bicep" ) ;
41
+ powershell . Invoke ( ) ;
42
+ IsBicepExecutable = powershell . HadErrors ? false : true ;
51
43
return IsBicepExecutable ;
52
44
}
53
45
54
- private static bool CheckMinimalVersionRequirement ( string checkMinumVersionRequirement )
46
+ private static string CheckMinimalVersionRequirement ( string minimalVersionRequirement )
55
47
{
56
-
57
- if ( Version . Parse ( checkMinumVersionRequirement ) . CompareTo ( Version . Parse ( GetBicepVesion ( ) ) ) > 0 )
48
+ string currentBicepVersion = GetBicepVesion ( ) ;
49
+ if ( Version . Parse ( minimalVersionRequirement ) . CompareTo ( Version . Parse ( currentBicepVersion ) ) > 0 )
58
50
{
59
- throw new AzPSApplicationException ( string . Format ( Properties . Resources . BicepVersionRequirement , checkMinumVersionRequirement ) ) ;
51
+ throw new AzPSApplicationException ( string . Format ( Properties . Resources . BicepVersionRequirement , minimalVersionRequirement ) ) ;
60
52
} ;
61
- return true ;
53
+ return currentBicepVersion ;
62
54
}
63
55
64
56
public static string GetBicepVesion ( )
@@ -71,14 +63,16 @@ public static string GetBicepVesion()
71
63
return bicepVersion ;
72
64
}
73
65
74
- public static string BuildFile < T > ( ScriptExecutor < T > executeScript , string bicepTemplateFilePath )
66
+ public delegate void OutputMethod ( string msg ) ;
67
+
68
+ public static string BuildFile ( string bicepTemplateFilePath , OutputMethod outputMethod = null )
75
69
{
76
- if ( ! IsBicepExecutable && ! CheckBicepExecutable ( executeScript ) )
70
+ if ( ! IsBicepExecutable && ! CheckBicepExecutable ( ) )
77
71
{
78
72
throw new AzPSApplicationException ( Properties . Resources . BicepNotFound ) ;
79
73
}
80
74
81
- CheckMinimalVersionRequirement ( MinimalVersionRequirement ) ;
75
+ string currentBicepVersion = CheckMinimalVersionRequirement ( MinimalVersionRequirement ) ;
82
76
83
77
string tempDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
84
78
Directory . CreateDirectory ( tempDirectory ) ;
@@ -87,7 +81,12 @@ public static string BuildFile<T>(ScriptExecutor<T> executeScript, string bicepT
87
81
{
88
82
System . Management . Automation . PowerShell powershell = System . Management . Automation . PowerShell . Create ( ) ;
89
83
powershell . AddScript ( $ "bicep build '{ bicepTemplateFilePath } ' --outdir '{ tempDirectory } '") ;
90
- powershell . Invoke ( ) ;
84
+ var result = powershell . Invoke ( ) ;
85
+ if ( outputMethod != null )
86
+ {
87
+ outputMethod ( string . Format ( "Using Bicep v{0}" , currentBicepVersion ) ) ;
88
+ result . ForEach ( r => outputMethod ( r . ToString ( ) ) ) ;
89
+ }
91
90
if ( powershell . HadErrors )
92
91
{
93
92
string errorMsg = string . Empty ;
0 commit comments