Ejemplo n.º 1
0
        public static NuspecFileData ReadFile(string packagesFilePath)
        {
            var packagesDocument = XDocument.Load(packagesFilePath);

            var result = new NuspecFileData();

            var dependencies = packagesDocument
                               ?.GetElementWithLocalName("package")
                               ?.GetElementWithLocalName("metadata")
                               ?.GetElementWithLocalName("dependencies")
                               ?.GetElementsWithLocalName("dependency") ?? new XElement[0];

            foreach (var xmlUsedPackage in dependencies)
            {
                var packageId      = xmlUsedPackage.Attribute("id")?.Value ?? string.Empty;
                var packageVersion = xmlUsedPackage.Attribute("version")?.Value ?? string.Empty;

                if (string.IsNullOrWhiteSpace(packageId) || string.IsNullOrWhiteSpace(packageVersion))
                {
                    continue;
                }

                result.Dependencies.Add(new NuspecDependencyInfo(packageId, packageVersion));
            }

            return(result);
        }
        public ValidationResult ValidateAllProjects(XElement xmlRuleConfigs, params string[] projectFilePaths)
        {
            var errors = new List <string>();

            ConfigValidation.ValidateConfigSectionForAllowedElements(xmlRuleConfigs, errors, Id, NuspecTagName, PackageProjectException.ExceptionTagName);

            if (errors.Any())
            {
                return(new ValidationResult(Id, true, true, errors.ToArray()));
            }

            var config = RuleConfiguration.Parse(xmlRuleConfigs);

            if (!config.IsEnabled)
            {
                return(new ValidationResult(Id, false, false, new string[0]));
            }

            var projectAndPackages = GetProjectToPackagesMap(projectFilePaths.Where(File.Exists));

            foreach (var nuspecTag in config.Specs)
            {
                var exceptions = config.Exceptions.Concat(nuspecTag.Exceptions).ToArray();

                var packageFiles = projectAndPackages
                                   .Select(kv => kv.Value.WithoutExceptions(exceptions, kv.Key))
                                   .ToList();

                var packageToVersions = GetPackageToAllowedVersion(packageFiles);

                foreach (var specPathesPattern in nuspecTag.Pathes)
                {
                    var resolvedNuspecPathes = GetNuspecFiles(specPathesPattern);

                    if (ReferenceEquals(resolvedNuspecPathes, null))
                    {
                        errors.Add($"Unable to find nuspec files by using pattern {specPathesPattern}");

                        continue;
                    }

                    foreach (var nuspecFilePath in resolvedNuspecPathes)
                    {
                        var nuspecFile = NuspecFileData.ReadFile(nuspecFilePath);

                        foreach (var dependency in nuspecFile.Dependencies)
                        {
                            var packageId = dependency.PackageId;

                            if (!packageToVersions.ContainsKey(packageId))
                            {
                                continue;
                            }

                            var allowedVersions = packageToVersions[packageId];

                            var version = dependency.PackageVersion;

                            if (allowedVersions.Contains(version))
                            {
                                continue;
                            }

                            errors.Add($"There is inconsistent between Nuspec file {nuspecFilePath} and solution. Nuspec has dependency '{packageId}' with version '{version}'. Solution has the same packages, but with versions: {string.Join(" ,", allowedVersions)}");
                        }
                    }
                }
            }

            return(new ValidationResult(Id, true, false, errors.ToArray()));
        }