コード例 #1
0
        public IEnumerable <PackageVerifierIssue> Validate(PackageAnalysisContext context)
        {
            if (context.Metadata.PackageTypes.Any(p => p == Constants.DotNetTool))
            {
                // Skip for dotnet global tool packages which contain assemblies from other teams and projects
                yield break;
            }

            AssemblyAttributesDataHelper.SetAssemblyAttributesData(context);
            foreach (var assemblyData in context.AssemblyData)
            {
                var assemblyInformationalVersionAttribute = assemblyData.Value.AssemblyAttributes.SingleOrDefault(a =>
                                                                                                                  a.AttributeType.FullName.Equals(
                                                                                                                      typeof(AssemblyInformationalVersionAttribute).FullName,
                                                                                                                      StringComparison.Ordinal));

                var infoVersion = assemblyInformationalVersionAttribute?.ConstructorArguments[0].Value?.ToString();

                if (!NuGetVersion.TryParse(infoVersion, out var assemblyInformationalNuGetVersion) ||
                    !VersionEquals(context.Metadata.Version, assemblyInformationalNuGetVersion))
                {
                    yield return(PackageIssueFactory.AssemblyInformationalVersionDoesNotMatchPackageVersion(
                                     assemblyData.Key,
                                     infoVersion,
                                     context.Metadata.Version,
                                     context.Metadata.Id));
                }

                var assemblyFileVersionAttribute = assemblyData.Value.AssemblyAttributes.SingleOrDefault(a =>
                                                                                                         a.AttributeType.FullName.Equals(
                                                                                                             typeof(AssemblyFileVersionAttribute).FullName,
                                                                                                             StringComparison.Ordinal));

                var assemblyFileNuGetVersion = new NuGetVersion(assemblyFileVersionAttribute.ConstructorArguments[0].Value.ToString());
                if (!VersionEquals(context.Metadata.Version, assemblyFileNuGetVersion))
                {
                    yield return(PackageIssueFactory.AssemblyFileVersionDoesNotMatchPackageVersion(
                                     assemblyData.Key,
                                     assemblyFileNuGetVersion,
                                     context.Metadata.Version,
                                     context.Metadata.Id));
                }

                var assemblyVersion = assemblyData.Value.AssemblyName.Version;
                if (!context.Metadata.Version.Version.Equals(assemblyVersion))
                {
                    yield return(PackageIssueFactory.AssemblyVersionDoesNotMatchPackageVersion(
                                     assemblyData.Key,
                                     assemblyVersion,
                                     context.Metadata.Version.Version,
                                     context.Metadata.Id));
                }
            }
        }