3
3
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4
4
//
5
5
6
+ using Microsoft . Azure . Functions . PowerShellWorker . Utility ;
6
7
using Microsoft . Azure . WebJobs . Script . Grpc . Messages ;
7
8
using Newtonsoft . Json ;
8
9
using System ;
9
10
using System . Collections . Generic ;
11
+ using System . Collections . ObjectModel ;
10
12
using System . IO ;
11
13
using System . Management . Automation ;
12
14
using System . Management . Automation . Runspaces ;
15
+ using LogLevel = Microsoft . Azure . WebJobs . Script . Grpc . Messages . RpcLog . Types . Level ;
16
+ using System . Text ;
17
+ using Microsoft . Azure . Functions . PowerShellWorker . PowerShell ;
13
18
14
19
namespace Microsoft . Azure . Functions . PowerShellWorker . WorkerIndexing
15
20
{
@@ -19,8 +24,10 @@ internal class WorkerIndexingHelper
19
24
//const string GetFunctionsMetadataCmdletName = "AzureFunctions.PowerShell.SDK\\Get-FunctionsMetadata";
20
25
const string GetFunctionsMetadataCmdletName = "Get-FunctionsMetadata" ;
21
26
const string AzureFunctionsPowerShellSDKModuleName = "AzureFunctions.PowerShell.SDK" ;
27
+ private static readonly ErrorRecordFormatter _errorRecordFormatter = new ErrorRecordFormatter ( ) ;
22
28
23
- internal static IEnumerable < RpcFunctionMetadata > IndexFunctions ( string baseDir )
29
+ //internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
30
+ internal static IEnumerable < RpcFunctionMetadata > IndexFunctions ( string baseDir , ILogger logger )
24
31
{
25
32
List < RpcFunctionMetadata > indexedFunctions = new List < RpcFunctionMetadata > ( ) ;
26
33
@@ -50,24 +57,63 @@ internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
50
57
System . Management . Automation . PowerShell _powershell = System . Management . Automation . PowerShell . Create ( ) ;
51
58
_powershell . Runspace = runspace ;
52
59
53
- var modulePath = Path . Join ( AppDomain . CurrentDomain . BaseDirectory , "Modules" , AzureFunctionsPowerShellSDKModuleName ) ;
54
- // Console.WriteLine("modulePath; {0}", modulePath);
55
- // Console.WriteLine("Importing module...");
56
- _powershell . AddCommand ( "Import-Module" ) . AddParameter ( "Name" , modulePath )
57
- . AddParameter ( "Force" , true ) ;
58
-
59
- // Console.WriteLine("Call Get-FunctionsMetadata");
60
- _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
61
60
string outputString = string . Empty ;
62
- foreach ( PSObject rawMetadata in _powershell . Invoke ( ) )
61
+ Exception exception = null ;
62
+ Collection < PSObject > results = null ;
63
+ var cmdletExecutionHadErrors = false ;
64
+ var exceptionThrown = false ;
65
+ string errorMsg = null ;
66
+
67
+ try
68
+ {
69
+ _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
70
+ results = _powershell . Invoke ( ) ;
71
+ cmdletExecutionHadErrors = _powershell . HadErrors ;
72
+ }
73
+ catch ( Exception ex )
74
+ {
75
+ exceptionThrown = true ;
76
+ exception = ex ;
77
+ throw ;
78
+ }
79
+ finally
80
+ {
81
+ if ( exceptionThrown )
82
+ {
83
+ errorMsg = string . Format ( PowerShellWorkerStrings . ErrorsWhileExecutingGetFunctionsMetadata , exception . ToString ( ) ) ;
84
+ }
85
+ else if ( cmdletExecutionHadErrors )
86
+ {
87
+ var errorCollection = _powershell . Streams . Error ;
88
+
89
+ var stringBuilder = new StringBuilder ( ) ;
90
+ foreach ( var errorRecord in errorCollection )
91
+ {
92
+ var message = _errorRecordFormatter . Format ( errorRecord ) ;
93
+ stringBuilder . AppendLine ( message ) ;
94
+ }
95
+
96
+ errorMsg = string . Format ( PowerShellWorkerStrings . ErrorsWhileExecutingGetFunctionsMetadata , stringBuilder . ToString ( ) ) ;
97
+ }
98
+
99
+ if ( errorMsg != null )
100
+ {
101
+ logger . Log ( isUserOnlyLog : true , LogLevel . Error , errorMsg , exception ) ;
102
+ throw new Exception ( errorMsg ) ;
103
+ }
104
+
105
+ _powershell . Commands . Clear ( ) ;
106
+ }
107
+
108
+ // TODO: The GetFunctionsMetadataCmdlet should never return more than one result. Make sure that this is the case and remove this code.
109
+ foreach ( PSObject rawMetadata in results )
63
110
{
64
111
if ( outputString != string . Empty )
65
112
{
66
113
throw new Exception ( PowerShellWorkerStrings . GetFunctionsMetadataMultipleResultsError ) ;
67
114
}
68
115
outputString = rawMetadata . ToString ( ) ;
69
116
}
70
- _powershell . Commands . Clear ( ) ;
71
117
72
118
List < FunctionInformation > functionInformations = JsonConvert . DeserializeObject < List < FunctionInformation > > ( outputString ) ;
73
119
0 commit comments