Exemplo n.º 1
0
        public void ExecuteTask()
        {
            if (IsExcluded())
            {
                Log.LogMessage("Project {0} excluded from task", HubProjectName);
            }
            else
            {
                // Creates output directory if it doesn't already exist
                Directory.CreateDirectory(OutputDirectory);

                // Define output files
                string bdioFilePath     = $"{OutputDirectory}/{HubProjectName}.jsonld";
                string flatListFilePath = $"{OutputDirectory}/{HubProjectName}_flat.txt";

                // Execute task functionality
                if (CreateFlatDependencyList)
                {
                    string[] externalIds = CreateFlatList().ToArray();
                    File.WriteAllLines(flatListFilePath, externalIds, Encoding.UTF8);
                }

                if (CreateHubBdio)
                {
                    BdioContent bdioContent = BuildBOM();
                    File.WriteAllText(bdioFilePath, bdioContent.ToString());
                }

                if (DeployHubBdio)
                {
                    string      bdio        = File.ReadAllText(bdioFilePath);
                    BdioContent bdioContent = BdioContent.Parse(bdio);
                    DeployBdioDataService.Deploy(bdioContent);
                }

                // Only wait for scan if we have to
                if (DeployHubBdio && (CheckPolicies || CreateHubBdio || WaitForDeployment))
                {
                    WaitForHub();
                }

                if (CreateHubReport)
                {
                    ProjectView        projectView        = ProjectDataService.GetProjectView(HubProjectName);
                    ProjectVersionView projectVersionView = ProjectDataService.GetMostRecentVersion(projectView);
                    ReportData         reportData         = RiskReportDataService.GetReportData(projectView, projectVersionView);
                    RiskReportDataService.WriteToRiskReport(reportData, OutputDirectory);
                }

                if (CheckPolicies)
                {
                    PolicyStatus policyStatus = new PolicyStatus(GetPolicies());
                    LogPolicyViolations(policyStatus);
                }
            }
        }
        public override bool Execute()
        {
            bool   result = true;
            string originalOutputDirectory = OutputDirectory;
            string originalHubProjectName  = HubProjectName;
            string originalHubVersionName  = HubVersionName;

            List <string>   alreadyMergedComponents = new List <string>();
            List <BdioNode> mergedComponentList     = new List <BdioNode>();

            try
            {
                // TODO: clean up this code to generate the BDIO first then perform the deploy and checks for each project
                // Also aggregate the results of the check policies.
                Dictionary <string, string> projectData = ParseSolutionFile(SolutionPath);
                Console.WriteLine("Parsed Solution File");
                if (projectData.Count > 0)
                {
                    string solutionDirectory = Path.GetDirectoryName(SolutionPath);
                    Console.WriteLine("Solution directory: {0}", solutionDirectory);
                    foreach (string key in projectData.Keys)
                    {
                        if (String.IsNullOrWhiteSpace(originalOutputDirectory))
                        {
                            OutputDirectory = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{key}";
                        }
                        else
                        {
                            OutputDirectory = $"{originalOutputDirectory}{Path.DirectorySeparatorChar}{key}";
                        }
                        string        projectRelativePath = projectData[key];
                        List <string> projectPathSegments = new List <string>();
                        projectPathSegments.Add(solutionDirectory);
                        projectPathSegments.Add(projectRelativePath);

                        ProjectPath = CreatePath(projectPathSegments);

                        if (String.IsNullOrWhiteSpace(originalHubProjectName))
                        {
                            HubProjectName = key;
                        }

                        if (String.IsNullOrWhiteSpace(originalHubVersionName))
                        {
                            HubVersionName = originalHubVersionName;
                        }

                        bool projectResult = base.Execute();
                        PackagesConfigPath = ""; // reset to use the project packages file.
                        result             = result && projectResult;

                        if (projectResult && GenerateMergedBdio)
                        {
                            string      bdioFilePath = $"{OutputDirectory}{Path.DirectorySeparatorChar}{HubProjectName}.jsonld";
                            string      bdio         = File.ReadAllText(bdioFilePath);
                            BdioContent bdioContent  = BdioContent.Parse(bdio);

                            foreach (BdioComponent component in bdioContent.Components)
                            {
                                if (!alreadyMergedComponents.Contains(component.BdioExternalIdentifier.ExternalId))
                                {
                                    mergedComponentList.Add(component);
                                    alreadyMergedComponents.Add(component.BdioExternalIdentifier.ExternalId);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No project data found for solution {0}", SolutionPath);
                }
            }
            catch (Exception ex)
            {
                if (HubIgnoreFailure)
                {
                    result = true;
                    Console.WriteLine("Error executing Build BOM task. Cause: {0}", ex);
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                OutputDirectory = originalOutputDirectory; // reset the settings to original, for use by merge operations
                HubProjectName  = originalHubProjectName;
                HubVersionName  = originalHubVersionName;
            }

            //Generate after so the "output directory" is the one for the solution, not just the last project processed
            if (GenerateMergedBdio)
            {
                GenerateMergedFile(mergedComponentList);
            }

            return(result);
        }