Exemplo n.º 1
0
        /// <summary>
        /// Configures the resolver based on a collection of packages on disk
        /// </summary>
        public static WorkspaceNugetModuleResolver ConfigureWithPackages(this WorkspaceNugetModuleResolver resolver, params PackageOnDisk[] packagesOnDisk)
        {
            var allPackages = packagesOnDisk.ToDictionary(kvp => kvp.Package.Id, kvp => kvp.Package);

            var allAnalyzedPackages = packagesOnDisk.ToDictionary(package => package.Package.Id, package => resolver.AnalyzeNugetPackage(package, false).Result);

            resolver.SetDownloadedPackagesForTesting(allAnalyzedPackages);

            return(resolver);
        }
Exemplo n.º 2
0
        private WorkspaceNugetModuleResolver CreateWorkspaceNugetResolverForTesting()
        {
            var host = CreateFrontEndHostControllerForTesting();

            var nugetResolver = new WorkspaceNugetModuleResolver(m_testContext.StringTable, new FrontEndStatistics());

            nugetResolver.TryInitialize(host, m_testContext, new ConfigurationImpl(), new NugetResolverSettings());

            return(nugetResolver);
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public bool TryCreateWorkspaceResolver(IResolverSettings resolverSettings, out IWorkspaceModuleResolver workspaceResolver)
        {
            workspaceResolver = m_workspaceResolverCache.GetOrAdd(
                resolverSettings,
                (settings) =>
            {
                var resolver = new WorkspaceNugetModuleResolver(Context.StringTable, FrontEndStatistics);
                if (resolver.TryInitialize(FrontEndHost, Context, Configuration, settings))
                {
                    return(resolver);
                }

                return(null);
            });

            return(workspaceResolver != null);
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override async Task <bool> InitResolverAsync(IResolverSettings resolverSettings, object workspaceResolver)
        {
            Contract.Requires(resolverSettings != null);

            Contract.Assert(m_resolverState == State.Created);
            Contract.Assert(
                resolverSettings is INugetResolverSettings,
                I($"Wrong type for resolver settings, expected {nameof(INugetResolverSettings)} but got {nameof(resolverSettings.GetType)}"));

            Name            = resolverSettings.Name;
            m_resolverState = State.ResolverInitializing;

            m_nugetWorkspaceResolver = workspaceResolver as WorkspaceNugetModuleResolver;
            Contract.Assert(m_nugetWorkspaceResolver != null, "Workspace module resolver is expected to be of source type");

            // TODO: We could do something smarter in the future and just download/generate what is needed
            // Use this result to populate the dictionaries that are used for package retrieval (m_packageDirectories, m_packages and m_owningModules)
            var maybePackages = await m_nugetWorkspaceResolver.GetAllKnownPackagesAsync();

            if (!maybePackages.Succeeded)
            {
                // Error should have been reported.
                return(false);
            }

            m_owningModules = new Dictionary <ModuleId, Package>();

            foreach (var package in maybePackages.Result)
            {
                m_packageDirectories[package.Path.GetParent(Context.PathTable)] = new List <Package> {
                    package
                };
                m_packages[package.Id]            = package;
                m_owningModules[package.ModuleId] = package;
            }

            m_resolverState = State.ResolverInitialized;

            return(true);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public override async Task <bool> InitResolverAsync(IResolverSettings resolverSettings, object workspaceResolver)
        {
            Contract.Requires(resolverSettings != null);

            Contract.Assert(m_resolverState == State.Created);
            Contract.Assert(
                resolverSettings is INugetResolverSettings,
                I($"Wrong type for resolver settings, expected {nameof(INugetResolverSettings)} but got {nameof(resolverSettings.GetType)}"));

            Name            = resolverSettings.Name;
            m_resolverState = State.ResolverInitializing;

            m_nugetWorkspaceResolver = workspaceResolver as WorkspaceNugetModuleResolver;
            Contract.Assert(m_nugetWorkspaceResolver != null, "Workspace module resolver is expected to be of source type");

            // TODO: We could do something smarter in the future and just download/generate what is needed
            // Use this result to populate the dictionaries that are used for package retrieval (m_packageDirectories, m_packages and m_owningModules)
            var maybePackages = await m_nugetWorkspaceResolver.GetAllKnownPackagesAsync();

            if (!maybePackages.Succeeded)
            {
                // Error should have been reported.
                return(false);
            }

            m_owningModules = new Dictionary <ModuleId, Package>();

            foreach (var package in maybePackages.Result.Values.SelectMany(v => v))
            {
                m_packages[package.Id]            = package;
                m_owningModules[package.ModuleId] = package;
            }

            if (Configuration.FrontEnd.GenerateCgManifestForNugets.IsValid ||
                Configuration.FrontEnd.ValidateCgManifestForNugets.IsValid)
            {
                var    cgManfiestGenerator = new NugetCgManifestGenerator(Context);
                string generatedCgManifest = cgManfiestGenerator.GenerateCgManifestForPackages(maybePackages.Result);

                if (Configuration.FrontEnd.ValidateCgManifestForNugets.IsValid)
                {
                    // Validate existing CG Manifest with newly generated CG Manifest
                    if (!ValidateCgManifestFile(generatedCgManifest))
                    {
                        return(false);
                    }
                }

                if (Configuration.FrontEnd.GenerateCgManifestForNugets.IsValid &&
                    !Configuration.FrontEnd.GenerateCgManifestForNugets.Equals(Configuration.FrontEnd.ValidateCgManifestForNugets))
                {
                    // Save the generated CG Manifets to File
                    if (!(await SaveCgManifetsFileAsync(generatedCgManifest)))
                    {
                        return(false);
                    }
                }
            }

            m_resolverState = State.ResolverInitialized;

            return(true);
        }
Exemplo n.º 6
0
        public override async Task <bool> InitResolverAsync(IResolverSettings resolverSettings, object workspaceResolver)
        {
            Contract.Requires(resolverSettings != null);

            Contract.Assert(m_resolverState == State.Created);
            Contract.Assert(
                resolverSettings is INugetResolverSettings,
                I($"Wrong type for resolver settings, expected {nameof(INugetResolverSettings)} but got {nameof(resolverSettings.GetType)}"));

            Name            = resolverSettings.Name;
            m_resolverState = State.ResolverInitializing;

            m_nugetWorkspaceResolver = workspaceResolver as WorkspaceNugetModuleResolver;
            Contract.Assert(m_nugetWorkspaceResolver != null, "Workspace module resolver is expected to be of source type");

            // TODO: We could do something smarter in the future and just download/generate what is needed
            // Use this result to populate the dictionaries that are used for package retrieval (m_packageDirectories, m_packages and m_owningModules)
            var maybePackages = await m_nugetWorkspaceResolver.GetAllKnownPackagesAsync();

            if (!maybePackages.Succeeded)
            {
                // Error should have been reported.
                return(false);
            }

            m_owningModules = new Dictionary <ModuleId, Package>();

            foreach (var package in maybePackages.Result.Values.SelectMany(v => v))
            {
                m_packages[package.Id]            = package;
                m_owningModules[package.ModuleId] = package;
            }

            if (Configuration.FrontEnd.GenerateCgManifestForNugets.IsValid ||
                Configuration.FrontEnd.ValidateCgManifestForNugets.IsValid)
            {
                var    cgManfiestGenerator = new NugetCgManifestGenerator(Context);
                string generatedCgManifest = cgManfiestGenerator.GenerateCgManifestForPackages(maybePackages.Result);
                string existingCgManifest  = "INVALID";

                if (!Configuration.FrontEnd.GenerateCgManifestForNugets.IsValid &&
                    Configuration.FrontEnd.ValidateCgManifestForNugets.IsValid)
                {
                    // Validation of existing cgmainfest.json results in failure due to mismatch. Should fail the build in this case.
                    try
                    {
                        existingCgManifest = File.ReadAllText(Configuration.FrontEnd.ValidateCgManifestForNugets.ToString(Context.PathTable));
                        FrontEndHost.Engine.RecordFrontEndFile(
                            Configuration.FrontEnd.ValidateCgManifestForNugets,
                            CGManifestResolverName);
                    }
                    // CgManifest FileNotFound, log error and fail build
                    catch (DirectoryNotFoundException e)
                    {
                        Logger.ReportComponentGovernanceValidationError(Context.LoggingContext, "Cannot read Component Governance Manifest file from disk\n" + e.ToString());
                        return(false);
                    }
                    catch (FileNotFoundException e)
                    {
                        Logger.ReportComponentGovernanceValidationError(Context.LoggingContext, "Cannot read Component Governance Manifest file from disk\n" + e.ToString());
                        return(false);
                    }
                    if (!cgManfiestGenerator.CompareForEquality(generatedCgManifest, existingCgManifest))
                    {
                        Logger.ReportComponentGovernanceValidationError(Context.LoggingContext, @"Existing Component Governance Manifest file is outdated, please generate a new one using the argument /generateCgManifestForNugets:<path>");
                        return(false);
                    }

                    m_resolverState = State.ResolverInitialized;
                    return(true);
                }

                // GenerateCgManifestForNugets writes a new file when the old file does not match, hence it will always be valid and does not need validation

                try
                {
                    // We are calling FrontEndHost.Engine.RecordFrontEndFile towards the end of this function because we may update this file after the read below
                    // Updating the file will cause a hash mismatch and the build to fail if this file is read again downstream
                    existingCgManifest = File.ReadAllText(Configuration.FrontEnd.GenerateCgManifestForNugets.ToString(Context.PathTable));
                }
                // CgManifest FileNotFound, continue to write the new file
                // No operations required as the empty existingCgManifest will not match with the newly generated cgManifest
                catch (DirectoryNotFoundException) { }
                catch (FileNotFoundException) { }

                if (!cgManfiestGenerator.CompareForEquality(generatedCgManifest, existingCgManifest))
                {
                    if (Configuration.FrontEnd.GenerateCgManifestForNugets.IsValid)
                    {
                        // Overwrite or create new cgmanifest.json file with updated nuget package and version info
                        string targetFilePath = Configuration.FrontEnd.GenerateCgManifestForNugets.ToString(Context.PathTable);

                        try
                        {
                            FileUtilities.CreateDirectory(Path.GetDirectoryName(targetFilePath));
                            await FileUtilities.WriteAllTextAsync(targetFilePath, generatedCgManifest, Encoding.UTF8);
                        }
                        catch (BuildXLException e)
                        {
                            Logger.ReportComponentGovernanceGenerationError(Context.LoggingContext, "Could not write Component Governance Manifest file to disk\n" + e.ToString());
                            return(false);
                        }
                    }
                }

                FrontEndHost.Engine.RecordFrontEndFile(
                    Configuration.FrontEnd.GenerateCgManifestForNugets,
                    CGManifestResolverName);
            }

            m_resolverState = State.ResolverInitialized;

            return(true);
        }