Esempio n. 1
0
        public DependencyAnalyzerResult Analyze(ILogger logger)
        {
            var result = new DependencyAnalyzerResult();

            result.AnalyzedFiles = GetLibrariesAndExecutables().ToArray();

            if (result.AnalyzedFiles.Length <= 0)
            {
                return result;
            }

            result.Assemblies = new Dictionary<string, AssemblyReferenceInfo>(StringComparer.OrdinalIgnoreCase);
            foreach (var fileInfo in result.AnalyzedFiles.OrderBy(asm => asm.Name))
            {
                logger.LogMessage(string.Format("Checking file {0}", fileInfo.Name));
                Assembly assembly;
                try
                {
                    if (!fileInfo.IsAssembly())
                    {
                        continue;
                    }
                    assembly = Assembly.ReflectionOnlyLoadFrom(fileInfo.FullName);
                }
                catch (Exception ex)
                {
                    logger.LogWarning(string.Format("Failed to load assembly '{0}': {1}", fileInfo.FullName, ex.Message));
                    continue;
                }
                var assemblyReferenceInfo = GetAssemblyReferenceInfo(result.Assemblies, assembly.GetName());
                assemblyReferenceInfo.ReflectionOnlyAssembly = assembly;
                assemblyReferenceInfo.AssemblySource = AssemblySource.Local;
                foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
                {
                    var referencedAssemblyReferenceInfo = GetAssemblyReferenceInfo(result.Assemblies, referencedAssembly); ;
                    assemblyReferenceInfo.AddReference(referencedAssemblyReferenceInfo);
                    referencedAssemblyReferenceInfo.AddReferencedBy(assemblyReferenceInfo);
                }
            }

            foreach (var assembly in result.Assemblies.Values)
            {
                if (assembly.ReflectionOnlyAssembly != null)
                {
                    continue;
                }
                logger.LogMessage(string.Format("Checking reference {0}", assembly.AssemblyName.Name));
                try
                {
                    assembly.ReflectionOnlyAssembly = Assembly.ReflectionOnlyLoad(assembly.AssemblyName.FullName);
                    assembly.AssemblySource = assembly.ReflectionOnlyAssembly.GlobalAssemblyCache ? AssemblySource.GAC : AssemblySource.Unknown;
                }
                catch (Exception ex)
                {
                    // TODO: Show message?
                }
            }
            return result;
        }
Esempio n. 2
0
        public DependencyAnalyzerResult Analyze(ILogger logger)
        {
            var result = new DependencyAnalyzerResult();

            result.AnalyzedFiles = GetLibrariesAndExecutables().ToArray();

            if (result.AnalyzedFiles.Length <= 0)
            {
                return(result);
            }

            result.Assemblies = new Dictionary <string, AssemblyReferenceInfo>(StringComparer.OrdinalIgnoreCase);
            foreach (var fileInfo in result.AnalyzedFiles.OrderBy(asm => asm.Name))
            {
                logger.LogMessage(string.Format("Checking file {0}", fileInfo.Name));
                Assembly assembly;
                try
                {
                    if (!fileInfo.IsAssembly())
                    {
                        continue;
                    }
                    assembly = Assembly.ReflectionOnlyLoadFrom(fileInfo.FullName);
                }
                catch (Exception ex)
                {
                    logger.LogWarning(string.Format("Failed to load assembly '{0}': {1}", fileInfo.FullName, ex.Message));
                    continue;
                }
                var assemblyReferenceInfo = GetAssemblyReferenceInfo(result.Assemblies, assembly.GetName());
                assemblyReferenceInfo.ReflectionOnlyAssembly = assembly;
                assemblyReferenceInfo.AssemblySource         = AssemblySource.Local;
                foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
                {
                    var referencedAssemblyReferenceInfo = GetAssemblyReferenceInfo(result.Assemblies, referencedAssembly);;
                    assemblyReferenceInfo.AddReference(referencedAssemblyReferenceInfo);
                    referencedAssemblyReferenceInfo.AddReferencedBy(assemblyReferenceInfo);
                }
            }

            foreach (var assembly in result.Assemblies.Values)
            {
                if (assembly.ReflectionOnlyAssembly != null)
                {
                    continue;
                }
                logger.LogMessage(string.Format("Checking reference {0}", assembly.AssemblyName.Name));
                try
                {
                    assembly.ReflectionOnlyAssembly = Assembly.ReflectionOnlyLoad(assembly.AssemblyName.FullName);
                    assembly.AssemblySource         = assembly.ReflectionOnlyAssembly.GlobalAssemblyCache ? AssemblySource.GAC : AssemblySource.Unknown;
                }
                catch (Exception ex)
                {
                    // TODO: Show message?
                }
            }
            return(result);
        }
Esempio n. 3
0
 public DgmlExport(DependencyAnalyzerResult result, string exportFilename, ILogger logger)
 {
     _Result         = result;
     _ExportFilename = exportFilename;
     _Logger         = logger;
 }
Esempio n. 4
0
 public ConsoleVisualizer(DependencyAnalyzerResult result)
 {
     _AnalyzerResult = result;
 }
Esempio n. 5
0
 public DgmlExport(DependencyAnalyzerResult result, string exportFilename, ILogger logger)
 {
     _Result = result;
     _ExportFilename = exportFilename;
     _Logger = logger;
 }
Esempio n. 6
0
        static void OtherMain(string[] args)
        {
            CommandLineApplication commandLineApplication = new CommandLineApplication(throwOnUnexpectedArg: false);
            CommandArgument        directory  = commandLineApplication.Argument("directory", "The directory to search for assemblies");
            CommandOption          dgmlExport = commandLineApplication.Option("-dg|--dgml <filename>", "Export to a dgml file", CommandOptionType.SingleValue);
            CommandOption          nonsystem  = commandLineApplication.Option("-n|--nonsystem", "List system assemblies", CommandOptionType.NoValue);
            CommandOption          all        = commandLineApplication.Option("-a|--all", "List all assemblies and references.", CommandOptionType.NoValue);
            CommandOption          noconsole  = commandLineApplication.Option("-nc|--noconsole", "Do not show references on console.", CommandOptionType.NoValue);
            CommandOption          silent     = commandLineApplication.Option("-s|--silent", "Do not show any message, only warnings and errors will be shown.", CommandOptionType.NoValue);

            commandLineApplication.HelpOption("-? | -h | --help");
            commandLineApplication.OnExecute(() =>
            {
                ConsoleLogger consoleLogger = new ConsoleLogger(!silent.HasValue());

                string directoryPath = directory.Value;
                if (!Directory.Exists(directoryPath))
                {
                    consoleLogger.LogMessage(string.Format("Directory: '{0}' does not exist.", directoryPath));
                    return(-1);
                }

                bool onlyConflicts = !all.HasValue();
                bool skipSystem    = nonsystem.HasValue();

                IDependencyAnalyzer analyzer = new DependencyAnalyzer()
                {
                    DirectoryInfo = new DirectoryInfo(directoryPath)
                };

                consoleLogger.LogMessage(string.Format("Check assemblies in: {0}", analyzer.DirectoryInfo));

                DependencyAnalyzerResult result = analyzer.Analyze(consoleLogger);

                if (!noconsole.HasValue())
                {
                    IDependencyVisualizer visualizer = new ConsoleVisualizer(result)
                    {
                        SkipSystem = skipSystem, OnlyConflicts = onlyConflicts
                    };
                    visualizer.Visualize();
                }

                if (dgmlExport.HasValue())
                {
                    IDependencyVisualizer export = new DgmlExport(result, string.IsNullOrWhiteSpace(dgmlExport.Value()) ? Path.Combine(analyzer.DirectoryInfo.FullName, "references.dgml") : dgmlExport.Value(), consoleLogger);
                    export.Visualize();
                }

                return(0);
            });
            try
            {
                commandLineApplication.Execute(args);
            }
            catch (CommandParsingException cpe)
            {
                Console.WriteLine(cpe.Message);
                commandLineApplication.ShowHelp();
            }
        }
Esempio n. 7
0
 public ConsoleVisualizer(DependencyAnalyzerResult result)
 {
     _AnalyzerResult = result;
 }