Esempio n. 1
0
        public static void Generate(string sourcePath, string outputPath, ConsoleLogger logger)
        {
            if (!Directory.Exists(sourcePath))
            {
                throw new DirectoryNotFoundException($"The path `{sourcePath}` does not exist. Unable to generate Azure Functions extensions metadata file.");
            }

            var extensionReferences = new List <ExtensionReference>();

            var targetAssemblies = Directory.EnumerateFiles(sourcePath, "*.dll")
                                   .Where(f => !AssemblyShouldBeSkipped(Path.GetFileName(f)))
                                   .ToArray();

            logger.LogMessage($"Found {targetAssemblies.Length} assemblies to evaluate in '{sourcePath}':");

            foreach (var path in targetAssemblies)
            {
                using (logger.Indent())
                {
                    logger.LogMessage($"{Path.GetFileName(path)}");

                    using (logger.Indent())
                    {
                        try
                        {
                            Assembly assembly = Assembly.LoadFrom(path);
                            var      currExtensionReferences = GenerateExtensionReferences(assembly);
                            extensionReferences.AddRange(currExtensionReferences);

                            foreach (var foundRef in currExtensionReferences)
                            {
                                logger.LogMessage($"Found extension: {foundRef.TypeName}");
                            }
                        }
                        catch (Exception ex) when(ex is FileNotFoundException || ex is BadImageFormatException)
                        {
                            // Don't log this as an error. This will almost always happen due to some publishing artifacts (i.e. Razor) existing in the
                            // functions bin folder without all of their dependencies present, or native package artifacts being copied to the bin folder
                            // These will almost never have Functions extensions, so we don't want to write out errors every time there is a build.
                            // This message can be seen with detailed logging enabled.
                            logger.LogMessage($"Could not evaluate '{Path.GetFileName(path)}' for extension metadata. If this assembly contains a Functions extension, ensure that all dependent assemblies exist in '{sourcePath}'. If this assembly does not contain any Functions extensions, this message can be ignored. Exception message: {ex.Message}");
                        }
                        catch (Exception ex)
                        {
                            logger.LogError($"Could not evaluate '{Path.GetFileName(path)}' for extension metadata. Exception message: {ex.Message}");
                        }
                    }
                }
            }

            string json = GenerateExtensionsJson(extensionReferences);

            File.WriteAllText(outputPath, json);
            logger.LogMessage($"'{outputPath}' successfully written.");
        }
Esempio n. 2
0
        public static void Generate(string sourcePath, string outputPath, ConsoleLogger logger)
        {
            if (!Directory.Exists(sourcePath))
            {
                throw new DirectoryNotFoundException($"The path `{sourcePath}` does not exist. Unable to generate Azure Functions extensions metadata file.");
            }

            var extensionReferences = new List <ExtensionReference>();

            var targetAssemblies = Directory.EnumerateFiles(sourcePath, "*.dll")
                                   .Where(f => !AssemblyShouldBeSkipped(Path.GetFileName(f)))
                                   .ToArray();

            logger.LogMessage($"Found {targetAssemblies.Length} assemblies to evaluate in '{sourcePath}':");

            foreach (var path in targetAssemblies)
            {
                using (logger.Indent())
                {
                    logger.LogMessage($"{Path.GetFileName(path)}");

                    using (logger.Indent())
                    {
                        try
                        {
                            var currExtensionReferences = GenerateExtensionReferences(path, logger);
                            extensionReferences.AddRange(currExtensionReferences);

                            foreach (var foundRef in currExtensionReferences)
                            {
                                logger.LogMessage($"Found extension: {foundRef.TypeName}");
                            }
                        }
                        catch (BadImageFormatException)
                        {
                            // No need to log here as we can ignore these as extensions.
                        }
                        catch (Exception ex)
                        {
                            logger.LogError($"Could not evaluate '{Path.GetFileName(path)}' for extension metadata. Exception message: {ex}");
                        }
                    }
                }
            }

            string json = GenerateExtensionsJson(extensionReferences);

            File.WriteAllText(outputPath, json);
            logger.LogMessage($"'{outputPath}' successfully written.");
        }
Esempio n. 3
0
        public static bool IsWebJobsStartupAttributeType(TypeReference attributeType, ConsoleLogger logger)
        {
            TypeReference currentAttributeType = attributeType;

            while (currentAttributeType != null)
            {
                if (string.Equals(currentAttributeType.FullName, WebJobsStartupAttributeType, StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                try
                {
                    currentAttributeType = currentAttributeType.Resolve()?.BaseType;
                }
                catch (FileNotFoundException ex)
                {
                    // Don't log this as an error. This will almost always happen due to some publishing artifacts (i.e. Razor) existing
                    // in the functions bin folder without all of their dependencies present. These will almost never have Functions extensions,
                    // so we don't want to write out errors every time there is a build. This message can be seen with detailed logging enabled.
                    string attributeTypeName = GetReflectionFullName(attributeType);
                    string fileName          = Path.GetFileName(attributeType.Module.FileName);
                    logger.LogMessage($"Could not determine whether the attribute type '{attributeTypeName}' used in the assembly '{fileName}' derives from '{WebJobsStartupAttributeType}' because the assembly defining its base type could not be found. Exception message: {ex.Message}");
                    return(false);
                }
            }

            return(false);
        }