Beispiel #1
0
        public bool Add(AnalyzerFile analyzerFile)
        {
            if (!_analyzerFiles.ContainsKey(analyzerFile.Assembly.FullName))
            {
                AddImpl(analyzerFile);
                return(true);
            }

            return(false);
        }
Beispiel #2
0
        public AnalyzerFile GetOrAdd(Assembly assembly)
        {
            if (!_analyzerFiles.TryGetValue(assembly.FullName, out AnalyzerFile analyzerFile))
            {
                analyzerFile = AnalyzerFile.Create(assembly);
                AddImpl(analyzerFile);
                return(analyzerFile);
            }

            return(analyzerFile);
        }
Beispiel #3
0
        internal void LoadFrom(string path)
        {
            if (File.Exists(path))
            {
                AddAnalyzerFileIfNotEmpty(path);
            }
            else if (Directory.Exists(path))
            {
                using (IEnumerator <string> en = Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories).GetEnumerator())
                {
                    while (true)
                    {
                        try
                        {
                            if (en.MoveNext())
                            {
                                AddAnalyzerFileIfNotEmpty(en.Current);
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch (IOException)
                        {
                        }
                        catch (SecurityException)
                        {
                        }
                        catch (UnauthorizedAccessException)
                        {
                        }
                    }
                }
            }

            void AddAnalyzerFileIfNotEmpty(string filePath)
            {
                Assembly assembly = null;

                try
                {
                    assembly = Assembly.LoadFrom(filePath);
                }
                catch (Exception ex)
                {
                    if (ex is FileLoadException ||
                        ex is BadImageFormatException ||
                        ex is SecurityException)
                    {
#if DEBUG
                        WriteLine($"Cannot load assembly '{filePath}'", ConsoleColor.Yellow);
#endif
                        return;
                    }
                    else
                    {
                        throw;
                    }
                }

                AnalyzerFile analyzerFile = AnalyzerFile.Create(assembly);

                if (analyzerFile.Analyzers.Count > 0 ||
                    analyzerFile.Fixers.Count > 0)
                {
                    Add(analyzerFile);
                }
            }
        }
Beispiel #4
0
        private void AddImpl(AnalyzerFile analyzerFile)
        {
            WriteLine($"Add analyzer assembly '{analyzerFile.Assembly.FullName}'");

            _analyzerFiles.Add(analyzerFile.Assembly.FullName, analyzerFile);
        }