Esempio n. 1
0
 public IEnumerable <(FunctionJsonSchema schema, FileInfo outputFile)?> GenerateFunctions(IEnumerable <Type> types)
 {
     foreach (var type in types)
     {
         foreach (var method in type.GetMethods())
         {
             if (method.HasFunctionNameAttribute())
             {
                 if (method.HasUnsuportedAttributes(out string error))
                 {
                     _logger.LogError(error);
                     yield return(null);
                 }
                 else if (method.IsWebJobsSdkMethod())
                 {
                     var functionName         = method.GetSdkFunctionName();
                     var artifactName         = Path.Combine(functionName, "function.json");
                     var path                 = Path.Combine(_outputPath, artifactName);
                     var relativeAssemblyPath = PathUtility.MakeRelativePath(Path.Combine(_outputPath, "dummyFunctionName"), type.Assembly.Location);
                     var functionJson         = method.ToFunctionJson(relativeAssemblyPath);
                     if (CheckAppSettingsAndFunctionName(functionJson, method) &&
                         _buildArtifactsLog.TryAddBuildArtifact(artifactName))
                     {
                         yield return(functionJson, new FileInfo(path));
                     }
                     else
                     {
                         yield return(null);
                     }
                 }
                 else if (method.HasFunctionNameAttribute())
                 {
                     if (method.HasNoAutomaticTriggerAttribute() && method.HasTriggerAttribute())
                     {
                         _logger.LogWarning($"Method {method.ReflectedType?.FullName}.{method.Name} has both a 'NoAutomaticTrigger' attribute and a trigger attribute. Both can't be used together for an Azure function definition.");
                     }
                     else
                     {
                         _logger.LogWarning($"Method {method.ReflectedType?.FullName}.{method.Name} is missing a trigger attribute. Both a trigger attribute and FunctionName attribute are required for an Azure function definition.");
                     }
                 }
                 else if (method.HasValidWebJobSdkTriggerAttribute())
                 {
                     _logger.LogWarning($"Method {method.ReflectedType?.FullName}.{method.Name} is missing the 'FunctionName' attribute. Both a trigger attribute and 'FunctionName' are required for an Azure function definition.");
                 }
             }
         }
     }
 }
Esempio n. 2
0
        private bool TryGenerateFunctionJsons()
        {
            var assembly = Assembly.LoadFrom(_assemblyPath);

            var relativeAssemblyPath = PathUtility.MakeRelativePath(Path.Combine(_outputPath, "dummyFunctionName"), assembly.Location);

            foreach (var type in assembly.GetExportedTypes())
            {
                foreach (var method in type.GetMethods())
                {
                    if (method.HasUnsuportedAttributes(out string error))
                    {
                        Logger.LogError(error);
                        return(false);
                    }
                    else if (method.IsWebJobsSdkMethod())
                    {
                        var functionName = method.GetSdkFunctionName();
                        var artifactName = Path.Combine(functionName, "function.json");
                        var path         = Path.Combine(_outputPath, artifactName);
                        if (File.Exists(path))
                        {
                            continue;
                        }

                        var functionJson = method.ToFunctionJson(relativeAssemblyPath);
                        if (CheckAppSettingsAndFunctionName(functionJson, method) &&
                            _buildArtifactsLog.TryAddBuildArtifact(artifactName))
                        {
                            functionJson.Serialize(path);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else if (method.HasFunctionNameAttribute())
                    {
                        Logger.LogWarning($"Method {method.Name} is missing a trigger attribute. Both a trigger attribute and FunctionName attribute are required for an Azure function definition.");
                    }
                    else if (method.HasWebJobSdkAttribute())
                    {
                        Logger.LogWarning($"Method {method.Name} is missing the 'FunctionName' attribute. Both a trigger attribute and 'FunctionName' are required for an Azure function definition.");
                    }
                }
            }
            return(true);
        }