Example #1
0
        /// <summary>
        /// Performs a C# build analysis.
        /// </summary>
        /// <param name="options">Analysis options from the command line.</param>
        /// <param name="progress">Display of analysis progress.</param>
        public BuildAnalysis(Options options, IProgressMonitor progress)
        {
            var startTime = DateTime.Now;

            progressMonitor = progress;
            var sourceDir = new DirectoryInfo(options.SrcDir);

            progressMonitor.FindingFiles(options.SrcDir);

            allSources = sourceDir.GetFiles("*.cs", SearchOption.AllDirectories)
                         .Select(d => d.FullName)
                         .Where(d => !options.ExcludesFile(d))
                         .ToArray();

            var dllDirNames = options.DllDirs.Select(Path.GetFullPath).ToList();

            packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName));

            if (options.UseNuGet)
            {
                try
                {
                    var nuget = new NugetPackages(sourceDir.FullName, packageDirectory);
                    nuget.InstallPackages(progressMonitor);
                }
                catch (FileNotFoundException)
                {
                    progressMonitor.MissingNuGet();
                }
            }

            // Find DLLs in the .Net Framework
            if (options.ScanNetFrameworkDlls)
            {
                var runtimeLocation = Runtime.GetRuntime(options.UseSelfContainedDotnet);
                progressMonitor.Log(Util.Logging.Severity.Debug, $"Runtime location selected: {runtimeLocation}");
                dllDirNames.Add(runtimeLocation);
            }

            // These files can sometimes prevent `dotnet restore` from working correctly.
            using (new FileRenamer(sourceDir.GetFiles("global.json", SearchOption.AllDirectories)))
                using (new FileRenamer(sourceDir.GetFiles("Directory.Build.props", SearchOption.AllDirectories)))
                {
                    var solutions = options.SolutionFile is not null ?
                                    new[] { options.SolutionFile } :
                    sourceDir.GetFiles("*.sln", SearchOption.AllDirectories).Select(d => d.FullName);

                    if (options.UseNuGet)
                    {
                        RestoreSolutions(solutions);
                    }
                    dllDirNames.Add(packageDirectory.DirInfo.FullName);
                    assemblyCache = new BuildAnalyser.AssemblyCache(dllDirNames, progress);
                    AnalyseSolutions(solutions);

                    foreach (var filename in assemblyCache.AllAssemblies.Select(a => a.Filename))
                    {
                        UseReference(filename);
                    }
                }

            ResolveConflicts();

            if (options.UseMscorlib)
            {
                UseReference(typeof(object).Assembly.Location);
            }

            // Output the findings
            foreach (var r in usedReferences.Keys)
            {
                progressMonitor.ResolvedReference(r);
            }

            foreach (var r in unresolvedReferences)
            {
                progressMonitor.UnresolvedReference(r.Key, r.Value);
            }

            progressMonitor.Summary(
                AllSourceFiles.Count(),
                ProjectSourceFiles.Count(),
                MissingSourceFiles.Count(),
                ReferenceFiles.Count(),
                UnresolvedReferences.Count(),
                conflictedReferences,
                succeededProjects + failedProjects,
                failedProjects,
                DateTime.Now - startTime);
        }