Example #1
0
        static ICollection <string> GetAssemblyFiles(string basePath, string xmlDocFileNamePatterns, Action <LogLevel, string> logger)
        {
            // Find all candidate xml files that meet filter
            List <string> filteredXmlFiles = new List <string>();
            var           allXmlFiles      = Directory.GetFiles(basePath, "*.xml", SearchOption.AllDirectories);
            var           filterRegexes    = string.IsNullOrEmpty(xmlDocFileNamePatterns) ? null : xmlDocFileNamePatterns.Split(',').Select(x => StringX.WildCardToRegex(x.Trim()));

            foreach (var xmlFile in allXmlFiles)
            {
                string xmlFileName = Path.GetFileName(xmlFile);
                if (filterRegexes == null || filterRegexes.Any(x => x.IsMatch(xmlFileName)))
                {
                    filteredXmlFiles.Add(xmlFile);
                }
            }

            // Get assembly for each xml file
            List <string> assemblyFiles = new List <string>();

            foreach (var xmlFile in filteredXmlFiles)
            {
                var directoryName            = Path.GetDirectoryName(xmlFile);
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(xmlFile);

                var matchingAssemblyFiles = new List <string>();
                matchingAssemblyFiles.AddRange(Directory.GetFiles(directoryName, $"{fileNameWithoutExtension}.exe"));
                matchingAssemblyFiles.AddRange(Directory.GetFiles(directoryName, $"{fileNameWithoutExtension}.dll"));
                if (matchingAssemblyFiles.Count() == 0)
                {
                    if (logger != null)
                    {
                        logger(LogLevel.Trace, $"GetAssemblyFiles():Could not find assembly for xml file {xmlFile} ({string.Join(",", matchingAssemblyFiles)})");
                    }
                }
                else if (matchingAssemblyFiles.Count() == 1)
                {
                    assemblyFiles.Add(matchingAssemblyFiles.First());
                }
                else if (matchingAssemblyFiles.Count() == 2 &&
                         matchingAssemblyFiles.Any(f => f.EndsWith(".exe")) &&
                         matchingAssemblyFiles.Any(f => f.EndsWith(".dll")))
                {
                    //.NET Core win-* publishing target creates 2 assemblies with the same name but different extensions:
                    // MyAssembly.dll (contains actual code)
                    // MyAssembly.exe (actually just a stub)
                    // In this situation we take the .dll version

                    assemblyFiles.Add(matchingAssemblyFiles.First(f => f.EndsWith(".dll")));
                }
                else if (matchingAssemblyFiles.Count() > 1)
                {
                    if (logger != null)
                    {
                        logger(LogLevel.Warn, $"GetAssemblyFiles():Found too many assemblies for xml file {xmlFile} ({string.Join(",", matchingAssemblyFiles)})");
                    }
                }
            }

            return(assemblyFiles);
        }
        static ICollection <string> GetAssemblyFiles(string basePath, string xmlDocFileNamePatterns, Action <LogLevel, string> logger)
        {
            // Find all candidate xml files that meet filter
            List <string> filteredXmlFiles = new List <string>();
            var           allXmlFiles      = Directory.GetFiles(basePath, "*.xml", SearchOption.AllDirectories);
            var           filterRegexes    = string.IsNullOrEmpty(xmlDocFileNamePatterns) ? null : xmlDocFileNamePatterns.Split(',').Select(x => StringX.WildCardToRegex(x.Trim()));

            foreach (var xmlFile in allXmlFiles)
            {
                string xmlFileName = Path.GetFileName(xmlFile);
                if (filterRegexes == null || filterRegexes.Any(x => x.IsMatch(xmlFileName)))
                {
                    filteredXmlFiles.Add(xmlFile);
                }
            }

            // Get assembly for each xml file
            List <string> assemblyFiles = new List <string>();

            foreach (var xmlFile in filteredXmlFiles)
            {
                var directoryName            = Path.GetDirectoryName(xmlFile);
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(xmlFile);

                var matchingAssemblyFiles = new List <string>();
                matchingAssemblyFiles.AddRange(Directory.GetFiles(directoryName, $"{fileNameWithoutExtension}.exe"));
                matchingAssemblyFiles.AddRange(Directory.GetFiles(directoryName, $"{fileNameWithoutExtension}.dll"));
                if (matchingAssemblyFiles.Count() == 0)
                {
                    if (logger != null)
                    {
                        logger(LogLevel.Trace, $"GetAssemblyFiles():Could not find assembly for xml file {xmlFile} ({string.Join(",", matchingAssemblyFiles)})");
                    }
                }
                else if (matchingAssemblyFiles.Count() == 1)
                {
                    assemblyFiles.Add(matchingAssemblyFiles.First());
                }
                else if (matchingAssemblyFiles.Count() > 1)
                {
                    if (logger != null)
                    {
                        logger(LogLevel.Warn, $"GetAssemblyFiles():Found too many assemblies for xml file {xmlFile} ({string.Join(",", matchingAssemblyFiles)})");
                    }
                }
            }

            return(assemblyFiles);
        }