Esempio n. 1
0
        private void ValidatePackagesConfigLockFiles(IEnumerable <NuGetProject> allProjects, CancellationToken token)
        {
            var pcProjects = allProjects.Where(p => p.ProjectStyle == ProjectModel.ProjectStyle.PackagesConfig);

            foreach (MSBuildNuGetProject project in pcProjects)
            {
                string projectFile  = project.MSBuildProjectPath;
                string pcFile       = project.PackagesConfigNuGetProject.FullPath;
                var    projectName  = (string)project.GetMetadataOrNull("Name");
                var    lockFileName = (string)project.GetMetadataOrNull("NuGetLockFilePath");
                var    restorePackagesWithLockFile = (string)project.GetMetadataOrNull("RestorePackagesWithLockFile");
                var    projectTfm        = (NuGetFramework)project.GetMetadataOrNull("TargetFramework");
                bool   restoreLockedMode = MSBuildStringUtility.GetBooleanOrNull((string)project.GetMetadataOrNull("LockedMode")) ?? false;

                IReadOnlyList <IRestoreLogMessage> validationLogs = PackagesConfigLockFileUtility.ValidatePackagesConfigLockFiles(
                    projectFile,
                    pcFile,
                    projectName,
                    lockFileName,
                    restorePackagesWithLockFile,
                    projectTfm,
                    project.FolderNuGetProject.Root,
                    restoreLockedMode,
                    token);

                if (validationLogs != null)
                {
                    foreach (var logItem in validationLogs)
                    {
                        _logger.Log(logItem);
                    }
                }
            }
        }
        public void GetBooleanOrNullTests(string value, bool?expected)
        {
            // Act
            bool?result = MSBuildStringUtility.GetBooleanOrNull(value);

            // Assert
            Assert.Equal(expected, result);
        }
        public static IReadOnlyList <IRestoreLogMessage> ValidatePackagesConfigLockFiles(
            string projectFile,
            string packagesConfigFile,
            string projectName,
            string nuGetLockFilePath,
            string restorePackagesWithLockFile,
            NuGetFramework projectTfm,
            string packagesFolderPath,
            bool restoreLockedMode,
            CancellationToken token)
        {
            var lockFilePath   = GetPackagesLockFilePath(Path.GetDirectoryName(packagesConfigFile), nuGetLockFilePath, projectName);
            var lockFileExists = File.Exists(lockFilePath);
            var lockFileOptIn  = MSBuildStringUtility.GetBooleanOrNull(restorePackagesWithLockFile);
            var useLockFile    = lockFileOptIn == true || lockFileExists;

            if (lockFileOptIn == false && lockFileExists)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Strings.Error_InvalidLockFileInput, lockFilePath);
                var errors  = new List <IRestoreLogMessage>();
                var log     = RestoreLogMessage.CreateError(NuGetLogCode.NU1005, message, packagesConfigFile);
                log.ProjectPath = projectFile ?? packagesConfigFile;
                errors.Add(log);
                return(errors);
            }

            if (useLockFile)
            {
                PackagesLockFile projectLockFileEquivalent = PackagesConfigLockFileUtility.FromPackagesConfigFile(packagesConfigFile,
                                                                                                                  projectTfm,
                                                                                                                  packagesFolderPath,
                                                                                                                  token);

                if (!lockFileExists)
                {
                    PackagesLockFileFormat.Write(lockFilePath, projectLockFileEquivalent);
                    return(null);
                }
                else
                {
                    PackagesLockFile lockFile = PackagesLockFileFormat.Read(lockFilePath);
                    PackagesLockFileUtilities.LockFileValidityWithMatchedResults comparisonResult = PackagesLockFileUtilities.IsLockFileStillValid(projectLockFileEquivalent, lockFile);
                    if (comparisonResult.IsValid)
                    {
                        // check sha hashes
                        bool allContentHashesMatch = comparisonResult.MatchedDependencies.All(pair => pair.Key.ContentHash == pair.Value.ContentHash);
                        if (allContentHashesMatch)
                        {
                            return(null);
                        }
                        else
                        {
                            var errors = new List <IRestoreLogMessage>();
                            foreach (var difference in comparisonResult.MatchedDependencies.Where(kvp => kvp.Key.ContentHash != kvp.Value.ContentHash))
                            {
                                var message = string.Format(CultureInfo.CurrentCulture, Strings.Error_PackageValidationFailed, difference.Key.Id + "." + difference.Key.ResolvedVersion);
                                var log     = RestoreLogMessage.CreateError(NuGetLogCode.NU1403, message, packagesConfigFile);
                                log.ProjectPath = projectFile ?? packagesConfigFile;
                                errors.Add(log);
                            }
                            return(errors);
                        }
                    }
                    else
                    {
                        if (restoreLockedMode)
                        {
                            var errors = new List <IRestoreLogMessage>();
                            var log    = RestoreLogMessage.CreateError(NuGetLogCode.NU1004, Strings.Error_RestoreInLockedModePackagesConfig, packagesConfigFile);
                            log.ProjectPath = projectFile ?? packagesConfigFile;
                            errors.Add(log);
                            return(errors);
                        }
                        else
                        {
                            PackagesLockFileFormat.Write(lockFilePath, projectLockFileEquivalent);
                            return(null);
                        }
                    }
                }
            }
            else
            {
                return(null);
            }
        }