Exemplo n.º 1
0
        private static void ValidateFileFrameworks(IEnumerable <IPackageFile> files)
        {
            var set = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var file in files.Where(t => t.Path != null).Select(t => PathUtility.GetPathWithDirectorySeparator(t.Path)))
            {
                set.Add(file);
            }

            var managedCodeConventions = new ManagedCodeConventions(new RuntimeGraph());
            var collection             = new ContentItemCollection();

            collection.Load(set.Select(path => path.Replace('\\', '/')).ToArray());

            var patterns = managedCodeConventions.Patterns;

            var frameworkPatterns = new List <PatternSet>()
            {
                patterns.RuntimeAssemblies,
                patterns.CompileRefAssemblies,
                patterns.CompileLibAssemblies,
                patterns.NativeLibraries,
                patterns.ResourceAssemblies,
                patterns.MSBuildFiles,
                patterns.ContentFiles,
                patterns.ToolsAssemblies,
                patterns.EmbedAssemblies,
                patterns.MSBuildTransitiveFiles
            };
            var warnPaths = new HashSet <string>();

            var frameworksMissingPlatformVersion = new HashSet <string>();

            foreach (var pattern in frameworkPatterns)
            {
                IEnumerable <ContentItemGroup> targetedItemGroups = ContentExtractor.GetContentForPattern(collection, pattern);
                foreach (ContentItemGroup group in targetedItemGroups)
                {
                    foreach (ContentItem item in group.Items)
                    {
                        var framework = (NuGetFramework)item.Properties["tfm"];
                        if (framework == null)
                        {
                            continue;
                        }

                        if (framework.HasPlatform && framework.PlatformVersion == FrameworkConstants.EmptyVersion)
                        {
                            frameworksMissingPlatformVersion.Add(framework.GetShortFolderName());
                        }
                    }
                }
            }

            if (frameworksMissingPlatformVersion.Any())
            {
                throw new PackagingException(NuGetLogCode.NU1012, string.Format(CultureInfo.CurrentCulture, Strings.MissingTargetPlatformVersionsFromIncludedFiles, string.Join(", ", frameworksMissingPlatformVersion.OrderBy(str => str))));
            }
        }
        internal (HashSet <NuGetFramework>, HashSet <NuGetFramework>, HashSet <NuGetFramework>) Categorize(IEnumerable <string> files, IEnumerable <NuGetFramework> tfmsFromNuspec)
        {
            var managedCodeConventions = new ManagedCodeConventions(new RuntimeGraph());
            Func <object, object, bool> isCompatible = managedCodeConventions.Properties["tfm"].CompatibilityTest;
            var collection = new ContentItemCollection();

            collection.Load(files);

            List <ContentItemGroup> libItems = new();
            List <ContentItemGroup> refItems = new();

            ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileLibAssemblies, libItems);
            ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileRefAssemblies, refItems);

            var tfmsFromFilesSet = new HashSet <NuGetFramework>();

            tfmsFromFilesSet.AddRange(ContentExtractor.GetGroupFrameworks(libItems));
            tfmsFromFilesSet.AddRange(ContentExtractor.GetGroupFrameworks(refItems));

            var tfmsFromFiles = tfmsFromFilesSet.Where(t => t != Net00).ToList();

            var noExactMatchesFromFile   = new HashSet <NuGetFramework>();
            var noExactMatchesFromNuspec = new HashSet <NuGetFramework>();
            var compatNotExactMatches    = new HashSet <NuGetFramework>();

            noExactMatchesFromFile.AddRange(tfmsFromFiles.Where(t => !tfmsFromNuspec.Contains(t)));
            noExactMatchesFromNuspec.AddRange(tfmsFromNuspec.Where(t => !tfmsFromFiles.Contains(t)));

            foreach (var fileTFM in noExactMatchesFromFile)
            {
                foreach (var nuspecTFM in noExactMatchesFromNuspec)
                {
                    if (isCompatible(fileTFM, nuspecTFM))
                    {
                        compatNotExactMatches.Add(fileTFM);
                        break;
                    }
                }
            }

            if (compatNotExactMatches.Count != 0)
            {
                noExactMatchesFromFile.RemoveWhere(p => compatNotExactMatches.Contains(p));
            }

            return(compatNotExactMatches, noExactMatchesFromFile, noExactMatchesFromNuspec);
        }
        internal IEnumerable <PackagingLogMessage> Validate(IEnumerable <string> files)
        {
            var managedCodeConventions = new ManagedCodeConventions(new RuntimeGraph());
            var collection             = new ContentItemCollection();

            collection.Load(files);

            List <ContentItemGroup> libItems   = new();
            List <ContentItemGroup> refItems   = new();
            List <ContentItemGroup> buildItems = new();

            ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileLibAssemblies, libItems);
            ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.CompileRefAssemblies, refItems);
            ContentExtractor.GetContentForPattern(collection, managedCodeConventions.Patterns.MSBuildFiles, buildItems);

            var libFrameworks   = ContentExtractor.GetGroupFrameworks(libItems).ToArray();
            var refFrameworks   = ContentExtractor.GetGroupFrameworks(refItems).ToArray();
            var buildFrameworks = ContentExtractor.GetGroupFrameworks(buildItems).ToArray();

            if (libFrameworks.Length == 0 && refFrameworks.Length == 0)
            {
                //if you can't find the ref and lib folder, then find the build folder
                if (buildFrameworks.Length != 0)
                {
                    //if you can find any folders other than native or any, raise an NU5127
                    if (buildFrameworks.Any(t => (FrameworkConstants.DotNetAll.Satisfies(t) || FrameworkNameValidatorUtility.IsValidFrameworkName(t)) &&
                                            t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.DotNet &&
                                            t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.Native))
                    {
                        var possibleFrameworks = buildFrameworks.
                                                 Where(t => t.IsSpecificFramework &&
                                                       t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.DotNet &&
                                                       t.GetShortFolderName() != FrameworkConstants.FrameworkIdentifiers.Native).
                                                 Select(t => t.GetShortFolderName()).ToArray();

                        (var tfmNames, var suggestedDirectories) = GenerateWarningString(possibleFrameworks);

                        var issue = new List <PackagingLogMessage>();
                        issue.Add(PackagingLogMessage.CreateWarning(string.Format(MessageFormat, tfmNames, suggestedDirectories),
                                                                    NuGetLogCode.NU5127));
                        return(issue);
                    }
                }
            }

            return(Array.Empty <PackagingLogMessage>());
        }
        internal static IEnumerable <PackagingLogMessage> ValidateFiles(IEnumerable <string> files)
        {
            var set = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var file in files.Select(t => PathUtility.GetPathWithDirectorySeparator(t)))
            {
                set.Add(file);
            }

            var managedCodeConventions = new ManagedCodeConventions(new RuntimeGraph());
            var collection             = new ContentItemCollection();

            collection.Load(set.Select(path => path.Replace('\\', '/')).ToArray());

            var patterns = managedCodeConventions.Patterns;

            var frameworkPatterns = new List <PatternSet>()
            {
                patterns.RuntimeAssemblies,
                patterns.CompileRefAssemblies,
                patterns.CompileLibAssemblies,
                patterns.NativeLibraries,
                patterns.ResourceAssemblies,
                patterns.MSBuildFiles,
                patterns.ContentFiles,
                patterns.ToolsAssemblies,
                patterns.EmbedAssemblies,
                patterns.MSBuildTransitiveFiles
            };
            var warnPaths = new HashSet <string>();

            foreach (var pattern in frameworkPatterns)
            {
                IEnumerable <ContentItemGroup> targetedItemGroups = ContentExtractor.GetContentForPattern(collection, pattern);
                foreach (ContentItemGroup group in targetedItemGroups)
                {
                    foreach (ContentItem item in group.Items)
                    {
                        var    exists          = item.Properties.TryGetValue("tfm_raw", out var frameworkRaw);
                        string frameworkString = (string)frameworkRaw;
                        if (!exists || string.IsNullOrEmpty(frameworkString))
                        {
                            continue;
                        }

                        if (!FrameworkVersionHasDesiredDots(frameworkString))
                        {
                            warnPaths.Add(item.Path);
                        }
                    }
                }
            }

            var messages = new List <PackagingLogMessage>();

            if (warnPaths.Count > 0)
            {
                messages.Add(
                    PackagingLogMessage.CreateWarning(
                        string.Format(CultureInfo.CurrentCulture, AnalysisResources.InvalidUndottedFrameworkInFilesWarning, string.Join(", ", warnPaths)),
                        NuGetLogCode.NU5501
                        )
                    );
            }

            return(messages);
        }