public async Task GetSolutionProjectReferences_ReturnsListOfProjects_IncludingSecondaryProjectReferences()
        {
            var mockFileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\SolutionPath\SolutionFile.sln"), new MockFileData(@"
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""CycloneDX"", ""Project1\Project1.csproj"", ""{88DFA76C-1C0A-4A83-AA48-EA1D28A9ABED}""
                        ") },
                { XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), Helpers.GetProjectFileWithProjectReferences(
                      new string[] {
                        @"..\Project2\Project2.csproj",
                    }) },
                { XFS.Path(@"c:\SolutionPath\Project2\Project2.csproj"), new MockFileData(@"<Project></Project>") }
            });
            var solutionFileService = new SolutionFileService(mockFileSystem);

            var projects = await solutionFileService.GetSolutionProjectReferencesAsync(XFS.Path(@"c:\SolutionPath\SolutionFile.sln"));

            var sortedProjects = new List <string>(projects);

            sortedProjects.Sort();

            Assert.Collection(sortedProjects,
                              item => Assert.Equal(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), item),
                              item => Assert.Equal(XFS.Path(@"c:\SolutionPath\Project2\Project2.csproj"), item));
        }
Exemple #2
0
        public async Task GetSolutionProjectReferences_ReturnsListOfProjects_IncludingSecondaryProjectReferences()
        {
            var mockFileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\SolutionPath\SolutionFile.sln"), new MockFileData(@"
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""CycloneDX"", ""Project1\Project1.csproj"", ""{88DFA76C-1C0A-4A83-AA48-EA1D28A9ABED}""
                        ") },
                { XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), Helpers.GetEmptyProjectFile() },
                { XFS.Path(@"c:\SolutionPath\Project2\Project2.csproj"), Helpers.GetEmptyProjectFile() }
            });
            var mockProjectFileService = new Mock <IProjectFileService>();

            mockProjectFileService
            .SetupSequence(s => s.RecursivelyGetProjectReferencesAsync(It.IsAny <string>()))
            .ReturnsAsync(new HashSet <string>
            {
                XFS.Path(@"c:\SolutionPath\Project2\Project2.csproj"),
            })
            .ReturnsAsync(new HashSet <string>());
            var solutionFileService = new SolutionFileService(mockFileSystem, mockProjectFileService.Object);

            var projects = await solutionFileService.GetSolutionProjectReferencesAsync(XFS.Path(@"c:\SolutionPath\SolutionFile.sln")).ConfigureAwait(false);

            var sortedProjects = new List <string>(projects);

            sortedProjects.Sort();

            Assert.Collection(sortedProjects,
                              item => Assert.Equal(XFS.Path(@"c:\SolutionPath\Project1\Project1.csproj"), item),
                              item => Assert.Equal(XFS.Path(@"c:\SolutionPath\Project2\Project2.csproj"), item));
        }
Exemple #3
0
 public NewCommand()
 {
     FileSystem          = new FileSystem();
     ZipService          = new ZipFileService(FileSystem);
     KeywordReplacer     = new KeywordReplacer();
     ProcessFactory      = new ProcessFactory();
     PlanExecutor        = new TemplatePlanExecutor(FileSystem);
     SolutionFileService = new SolutionFileService(FileSystem);
     CsProjGatherer      = new CsProjGatherer(FileSystem);
     RakeRunner          = new RakeRunner(ProcessFactory, FileSystem);
 }
        public async Task GetSolutionProjectReferences_ReturnsProjectThatExists()
        {
            var mockFileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\SolutionPath\SolutionFile.sln"), new MockFileData(@"
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""CycloneDX"", ""Project\Project.csproj"", ""{88DFA76C-1C0A-4A83-AA48-EA1D28A9ABED}""
                        ") },
                { XFS.Path(@"c:\SolutionPath\Project\Project.csproj"), new MockFileData(@"<Project></Project>") },
            });
            var solutionFileService = new SolutionFileService(mockFileSystem);

            var projects = await solutionFileService.GetSolutionProjectReferencesAsync(XFS.Path(@"c:\SolutionPath\SolutionFile.sln"));

            Assert.Collection(projects,
                              item => Assert.Equal(XFS.Path(@"c:\SolutionPath\Project\Project.csproj"), item));
        }
Exemple #5
0
        public void should_add_project_references()
        {
            // build it up through a stringbuilder to use the environment-specific newline
            var solutionBuilder = new StringBuilder("Microsoft Visual Studio Solution File, Format Version 11.00")
                                  .AppendLine()
                                  .AppendLine("# Visual Studio 2010")
                                  .AppendLine(@"Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""FubuMVC.StructureMap"", ""FubuMVC.StructureMap\FubuMVC.StructureMap.csproj"", ""{ABFEA520-820C-4B77-9015-6A09E24252FA}""")
                                  .AppendLine("EndProject")
                                  .AppendLine("Global")
                                  .AppendLine("	GlobalSection(SolutionConfigurationPlatforms) = preSolution")
                                  .AppendLine("		Debug|Any CPU = Debug|Any CPU")
                                  .AppendLine("		Release|Any CPU = Release|Any CPU")
                                  .AppendLine("	EndGlobalSection")
                                  .AppendLine("	GlobalSection(SolutionProperties) = preSolution")
                                  .AppendLine("		HideSolutionNode = FALSE")
                                  .AppendLine("	EndGlobalSection")
                                  .AppendLine("EndGlobal");

            var system       = new FileSystem();
            var solutionFile = "tmp.sln";

            system.AppendStringToFile(solutionFile, solutionBuilder.ToString());

            var project = new CsProj
            {
                Name         = "Test",
                ProjectGuid  = "123",
                RelativePath = @"example1\example1.csproj"
            };
            var service = new SolutionFileService(system);

            service.AddProject(solutionFile, project);

            var solutionContents = system.ReadStringFromFile(solutionFile);
            var lines            = service.SplitSolution(solutionContents);

            lines[4].ShouldEqual("Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Test\", \"example1\\example1.csproj\", \"{123}\"");
            lines[5].ShouldEqual("EndProject");

            system.DeleteFile(solutionFile);
        }
        async Task <int> OnExecute()
        {
            Console.WriteLine();

            // check parameter values
            if (string.IsNullOrEmpty(SolutionOrProjectFile))
            {
                Console.Error.WriteLine($"A path is required");
                return((int)ExitCode.SolutionOrProjectFileParameterMissing);
            }

            if (string.IsNullOrEmpty(outputDirectory))
            {
                Console.Error.WriteLine($"The output directory is required");
                return((int)ExitCode.OutputDirectoryParameterMissing);
            }

            if (string.IsNullOrEmpty(githubUsername) ^ string.IsNullOrEmpty(githubToken))
            {
                Console.Error.WriteLine($"Both GitHub username and token are required");
                return((int)ExitCode.GitHubParameterMissing);
            }

            // instantiate services
            var fileDiscoveryService = new FileDiscoveryService(Program.fileSystem);
            // GitHubService requires its own HttpClient as it adds a default authorization header
            GithubService githubService;

            if (string.IsNullOrEmpty(githubUsername) || string.IsNullOrEmpty(githubToken))
            {
                githubService = new GithubService(new HttpClient());
            }
            else
            {
                githubService = new GithubService(new HttpClient(), githubUsername, githubToken);
            }
            var nugetService        = new NugetService(Program.httpClient, githubService, baseUrl);
            var packagesFileService = new PackagesFileService(Program.fileSystem);
            var projectFileService  = new ProjectFileService(Program.fileSystem);
            var solutionFileService = new SolutionFileService(Program.fileSystem);
            var packages            = new HashSet <NugetPackage>();

            // determine what we are analyzing and do the analysis
            var fullSolutionOrProjectFilePath = Program.fileSystem.Path.GetFullPath(SolutionOrProjectFile);
            var attr = Program.fileSystem.File.GetAttributes(fullSolutionOrProjectFilePath);

            if (SolutionOrProjectFile.ToLowerInvariant().EndsWith(".sln", StringComparison.OrdinalIgnoreCase))
            {
                packages = await solutionFileService.GetSolutionNugetPackages(fullSolutionOrProjectFilePath);
            }
            else if (Utils.IsSupportedProjectType(SolutionOrProjectFile) && scanProjectReferences)
            {
                packages = await projectFileService.RecursivelyGetProjectNugetPackagesAsync(fullSolutionOrProjectFilePath);
            }
            else if (Utils.IsSupportedProjectType(SolutionOrProjectFile))
            {
                packages = await projectFileService.GetProjectNugetPackagesAsync(fullSolutionOrProjectFilePath);
            }
            else if (Program.fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase))
            {
                packages = await packagesFileService.GetNugetPackagesAsync(fullSolutionOrProjectFilePath);
            }
            else if (attr.HasFlag(FileAttributes.Directory))
            {
                packages = await packagesFileService.RecursivelyGetNugetPackagesAsync(fullSolutionOrProjectFilePath);
            }
            else
            {
                Console.Error.WriteLine($"Only .sln, .csproj, .vbproj, and packages.config files are supported");
                return((int)ExitCode.InvalidOptions);
            }

            // get all the components from the NuGet packages
            var components = new HashSet <Component>();

            try
            {
                foreach (var package in packages)
                {
                    var component = await nugetService.GetComponentAsync(package);

                    if (component != null)
                    {
                        components.Add(component);
                    }
                }
            }
            catch (InvalidGitHubApiCredentialsException)
            {
                return((int)ExitCode.InvalidGitHubApiCredentials);
            }
            catch (GitHubApiRateLimitExceededException)
            {
                return((int)ExitCode.GitHubApiRateLimitExceeded);
            }

            // create the BOM
            Console.WriteLine();
            Console.WriteLine("Creating CycloneDX BoM");
            var bomXml = BomService.CreateXmlDocument(components, noSerialNumber);

            // check if the output directory exists and create it if needed
            var bomPath = Program.fileSystem.Path.GetFullPath(outputDirectory);

            if (!Program.fileSystem.Directory.Exists(bomPath))
            {
                Program.fileSystem.Directory.CreateDirectory(bomPath);
            }

            // write the BOM to disk
            var bomFile = Program.fileSystem.Path.Combine(bomPath, "bom.xml");

            Console.WriteLine("Writing to: " + bomFile);
            using (var fileStream = Program.fileSystem.FileStream.Create(bomFile, FileMode.Create))
                using (var writer = new StreamWriter(fileStream, new UTF8Encoding(false))) {
                    bomXml.Save(writer);
                }

            return(0);
        }