Exemplo n.º 1
0
 public InspectionResultJsonWriter(InspectionResult result)
 {
     Result                = result;
     Inspection            = new Model.Inspection();
     Inspection.Containers = result.Containers;
 }
        public Model.Container GetContainer()
        {
            Console.WriteLine("Processing Solution: " + Options.TargetPath);
            var stopwatch = Stopwatch.StartNew();

            Model.Container solution = new Model.Container();
            solution.Name       = Options.SolutionName;
            solution.SourcePath = Options.TargetPath;
            solution.Type       = "Solution";
            try
            {
                List <ProjectFile> projectFiles = FindProjectFilesFromSolutionFile(Options.TargetPath, ExcludedProjectTypeGUIDs);
                Console.WriteLine("Parsed Solution File");
                if (projectFiles.Count > 0)
                {
                    string solutionDirectory = Path.GetDirectoryName(Options.TargetPath);
                    Console.WriteLine("Solution directory: {0}", solutionDirectory);

                    var duplicateNames = projectFiles
                                         .GroupBy(project => project.Name)
                                         .Where(group => group.Count() > 1)
                                         .Select(group => group.Key);

                    var duplicatePaths = projectFiles
                                         .GroupBy(project => project.Path)
                                         .Where(group => group.Count() > 1)
                                         .Select(group => group.Key);

                    foreach (ProjectFile project in projectFiles)
                    {
                        try
                        {
                            string        projectRelativePath = project.Path;
                            List <string> projectPathSegments = new List <string>();
                            projectPathSegments.Add(solutionDirectory);
                            projectPathSegments.Add(projectRelativePath);

                            string projectPath = InspectorUtil.CreatePath(projectPathSegments);
                            string projectName = project.Name;
                            string projectId   = projectName;
                            if (duplicateNames.Contains(projectId))
                            {
                                Console.WriteLine($"Duplicate project name '{projectName}' found. Using GUID instead.");
                                projectId = project.GUID;
                            }

                            Boolean projectFileExists = false;
                            try
                            {
                                projectFileExists = File.Exists(projectPath);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Skipping unknown project path: " + projectPath);
                                continue;
                            }

                            if (!projectFileExists)
                            {
                                Console.WriteLine("Skipping non-existent project path: " + projectPath);
                                continue;
                            }


                            ProjectInspector projectInspector = new ProjectInspector(new ProjectInspectionOptions(Options)
                            {
                                ProjectName     = projectName,
                                ProjectUniqueId = projectId,
                                TargetPath      = projectPath
                            }, NugetService);

                            InspectionResult projectResult = projectInspector.Inspect();
                            if (projectResult != null && projectResult.Containers != null)
                            {
                                solution.Children.AddRange(projectResult.Containers);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            if (Options.IgnoreFailure)
                            {
                                Console.WriteLine("Error inspecting project: {0}", project.Path);
                                Console.WriteLine("Error inspecting project. Cause: {0}", ex);
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No project data found for solution {0}", Options.TargetPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                if (Options.IgnoreFailure)
                {
                    Console.WriteLine("Error executing Build BOM task. Cause: {0}", ex);
                }
                else
                {
                    throw ex;
                }
            }

            if (solution != null && solution.Children != null)
            {
                Console.WriteLine("Found " + solution.Children.Count + " children.");
            }
            Console.WriteLine("Finished processing solution: " + Options.TargetPath);
            Console.WriteLine("Took " + stopwatch.ElapsedMilliseconds + " ms to process.");
            return(solution);
        }