in src/WorkerIndexingHelper.cs [74:126]
private static IEnumerable<FunctionInformation> IndexFunctionsInFile(FileInfo powerShellFile)
{
List<FunctionInformation> fileFunctions = new List<FunctionInformation>();
// Try to parse the PowerShell file.
// TODO: Need to follow up with the PowerShell team on this issue https://github.com/Azure/azure-functions-powershell-library/issues/43 before GA.
ScriptBlockAst? fileAst = Parser.ParseFile(powerShellFile.FullName, out _, out ParseError[] errors);
if (errors.Any())
{
var stringBuilder = new StringBuilder();
foreach (var error in errors)
{
stringBuilder.AppendLine(error.ToString());
}
var errorId = "FailedToParseFile";
var errorMessage = string.Format(AzPowerShellSdkStrings.FailedToParseFile, powerShellFile.FullName, stringBuilder.ToString());
errorRecords.Add(new ErrorRecord(new Exception(errorMessage), errorId, ErrorCategory.ParserError, powerShellFile));
return fileFunctions;
}
if (string.Equals(powerShellFile.Extension, Constants.Ps1FileExtension, StringComparison.OrdinalIgnoreCase))
{
// parse only the file param block, return one RpcFunctionMetadata assuming the file is the entry point
ParamBlockAst paramAsts = fileAst.ParamBlock;
if (paramAsts != null && paramAsts.Attributes.Where(x => x.TypeName.ToString() == Constants.AttributeNames.Function).Any())
{
// This is a function, return it
FunctionInformation functionInformation = CreateFunctionInformationFromFile(powerShellFile.FullName);
AddFunctionIfNameUnique(functionInformation, fileFunctions);
}
}
else if (string.Equals(powerShellFile.Extension, Constants.Psm1FileExtension, StringComparison.OrdinalIgnoreCase))
{
// parse all function definitions, return as many RpcFunctionMetadatas as exist in the file
IEnumerable<Ast> potentialFunctions = fileAst.FindAll(x => x is FunctionDefinitionAst, false);
foreach (Ast potentialFunction in potentialFunctions)
{
IEnumerable<Ast> matchingBlocks = potentialFunction.FindAll(x => x is ParamBlockAst &&
((ParamBlockAst)x).Attributes.Where(z => z.TypeName.ToString() == Constants.AttributeNames.Function).Any(), true);
if (matchingBlocks.Any()) {
//This function is one we need to register
FunctionInformation functionInformation = CreateFunctionInformationFromFunctionAst(powerShellFile.FullName, (FunctionDefinitionAst)potentialFunction);
AddFunctionIfNameUnique(functionInformation, fileFunctions);
}
// If there are no matching blocks, this is a helper function that will live in the file
// but shall not be indexed as an Azure Function
}
}
return fileFunctions;
}