示例#1
0
        public BdioContent BuildBOMFromMetadata(List <NuGet.PackageReference> packages, PackageMetadataResource metadataResource)
        {
            BdioPropertyHelper bdioPropertyHelper = new BdioPropertyHelper();
            BdioNodeFactory    bdioNodeFactory    = new BdioNodeFactory(bdioPropertyHelper);
            BdioContent        bdio = new BdioContent();

            // Create bdio bill of materials node
            BdioBillOfMaterials bdioBillOfMaterials = bdioNodeFactory.CreateBillOfMaterials(HubCodeLocationName, HubProjectName, HubVersionName);

            // Create bdio project node
            string projectBdioId = bdioPropertyHelper.CreateBdioId(HubProjectName, HubVersionName);
            BdioExternalIdentifier projectExternalIdentifier = bdioPropertyHelper.CreateNugetExternalIdentifier(HubProjectName, HubVersionName); // Note: Could be different. Look at config file
            BdioProject            bdioProject = bdioNodeFactory.CreateProject(HubProjectName, HubVersionName, projectBdioId, projectExternalIdentifier);

            // Create relationships for every bdio node
            List <BdioNode> bdioComponents = new List <BdioNode>();

            foreach (NuGet.PackageReference packageRef in packages)
            {
                // Create component node
                string componentName    = packageRef.Id;
                string componentVersion = packageRef.Version.ToString();
                string componentBdioId  = bdioPropertyHelper.CreateBdioId(componentName, componentVersion);
                BdioExternalIdentifier componentExternalIdentifier = bdioPropertyHelper.CreateNugetExternalIdentifier(componentName, componentVersion);
                BdioComponent          component = bdioNodeFactory.CreateComponent(componentName, componentVersion, componentBdioId, componentExternalIdentifier);

                // Add references
                List <PackageDependency> packageDependencies = GetPackageDependencies(packageRef, metadataResource);
                foreach (PackageDependency packageDependency in packageDependencies)
                {
                    // Create node from dependency info
                    string dependencyName    = packageDependency.Id;
                    string dependencyVersion = GetDependencyVersion(packageDependency, packages);
                    string dependencyBdioId  = bdioPropertyHelper.CreateBdioId(dependencyName, dependencyVersion);
                    BdioExternalIdentifier dependencyExternalIdentifier = bdioPropertyHelper.CreateNugetExternalIdentifier(dependencyName, dependencyVersion);
                    BdioComponent          dependency = bdioNodeFactory.CreateComponent(dependencyName, dependencyVersion, dependencyBdioId, dependencyExternalIdentifier);

                    // Add relationship
                    bdioPropertyHelper.AddRelationship(component, dependency);
                }

                bdioComponents.Add(component);
            }

            bdio.BillOfMaterials = bdioBillOfMaterials;
            bdio.Project         = bdioProject;
            bdio.Components      = bdioComponents;

            return(bdio);
        }
        private void GenerateMergedFile(List <BdioNode> components)
        {
            BdioPropertyHelper bdioPropertyHelper = new BdioPropertyHelper();
            BdioNodeFactory    bdioNodeFactory    = new BdioNodeFactory(bdioPropertyHelper);
            BdioContent        bdio = new BdioContent();

            // solutions do not have project names or versions by default
            string projectName = HubProjectName;
            string versionName = HubVersionName;

            if (String.IsNullOrWhiteSpace(projectName))
            {
                projectName = Path.GetFileNameWithoutExtension(SolutionPath);
            }

            if (String.IsNullOrWhiteSpace(versionName))
            {
                versionName = DateTime.UtcNow.ToString(ProjectGenerator.DEFAULT_DATETIME_FORMAT);
            }

            // Create bdio bill of materials node
            BdioBillOfMaterials bdioBillOfMaterials = bdioNodeFactory.CreateBillOfMaterials(HubCodeLocationName, projectName, versionName);

            // Create bdio project node
            string projectBdioId = bdioPropertyHelper.CreateBdioId(projectName, versionName);
            BdioExternalIdentifier projectExternalIdentifier = bdioPropertyHelper.CreateNugetExternalIdentifier(projectName, versionName); // Note: Could be different. Look at config file
            BdioProject            bdioProject = bdioNodeFactory.CreateProject(projectName, versionName, projectBdioId, projectExternalIdentifier);

            bdio.BillOfMaterials = bdioBillOfMaterials;
            bdio.Project         = bdioProject;
            bdio.Components      = components;

            string bdioFilePath = Path.Combine(OutputDirectory, $"{projectName}.jsonld");

            File.WriteAllText(bdioFilePath, bdio.ToString());
        }