public void GetNupkgPath_WithCancellationToken_Throws()
        {
            var obj = new PackagesConfigContentHashProvider(It.IsAny <FolderNuGetProject>());

            Assert.Throws <OperationCanceledException>(() =>
            {
                obj.GetContentHash(It.IsAny <PackageIdentity>(), new CancellationToken(canceled: true));
            });
        }
コード例 #2
0
        public static PackagesLockFile FromPackagesConfigFile(
            string pcFile,
            NuGetFramework projectTfm,
            string packagesFolderPath,
            CancellationToken token)
        {
            if (pcFile == null)
            {
                throw new ArgumentNullException(nameof(pcFile));
            }
            if (!File.Exists(pcFile))
            {
                throw new FileNotFoundException(string.Format(Strings.Error_FileDoesNotExist, pcFile), pcFile);
            }
            if (projectTfm == null)
            {
                throw new ArgumentNullException(nameof(projectTfm));
            }
            if (packagesFolderPath == null)
            {
                throw new ArgumentNullException(nameof(packagesFolderPath));
            }
            if (!Directory.Exists(packagesFolderPath))
            {
                throw new DirectoryNotFoundException(string.Format(Strings.Error_DirectoryDoesNotExist, packagesFolderPath));
            }

            var lockFile = new PackagesLockFile();
            var target   = new PackagesLockFileTarget();

            lockFile.Targets.Add(target);
            target.TargetFramework = projectTfm;

            using (var stream = File.OpenRead(pcFile))
            {
                var contentHashUtil = new PackagesConfigContentHashProvider(new FolderNuGetProject(packagesFolderPath));

                var reader = new PackagesConfigReader(stream);
                foreach (var package in reader.GetPackages(allowDuplicatePackageIds: true))
                {
                    var dependency = new LockFileDependency
                    {
                        Id               = package.PackageIdentity.Id,
                        ContentHash      = contentHashUtil.GetContentHash(package.PackageIdentity, token),
                        RequestedVersion = new VersionRange(package.PackageIdentity.Version, includeMinVersion: true, package.PackageIdentity.Version, includeMaxVersion: true),
                        ResolvedVersion  = package.PackageIdentity.Version,
                        Type             = PackageDependencyType.Direct
                    };

                    target.Dependencies.Add(dependency);
                }
            }

            return(lockFile);
        }
コード例 #3
0
        internal static void UpdateLockFile(
            MSBuildNuGetProject msbuildProject,
            List <NuGetProjectAction> actionsList,
            CancellationToken token)
        {
            if (msbuildProject == null)
            {
                // probably a `nuget.exe install` command, which doesn't support lock files until lock file arguments are implemented
                return;
            }

            var lockFileName   = GetPackagesLockFilePath(msbuildProject);
            var lockFileExists = File.Exists(lockFileName);
            var enableLockFile = IsRestorePackagesWithLockFileEnabled(msbuildProject);

            if (enableLockFile == false && lockFileExists)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Strings.Error_InvalidLockFileInput, lockFileName);
                throw new InvalidOperationException(message);
            }
            else if (enableLockFile == true || lockFileExists)
            {
                var lockFile = GetLockFile(lockFileExists, lockFileName);
                lockFile.Targets[0].TargetFramework = msbuildProject.ProjectSystem.TargetFramework;
                var contentHashUtil = new PackagesConfigContentHashProvider(msbuildProject.FolderNuGetProject);
                ApplyChanges(lockFile, actionsList, contentHashUtil, token);
                PackagesLockFileFormat.Write(lockFileName, lockFile);

                // Add lock file to msbuild project, so it appears in solution explorer and is added to TFS source control.
                if (msbuildProject != null)
                {
                    var projectUri           = new Uri(msbuildProject.MSBuildProjectPath);
                    var lockFileUri          = new Uri(lockFileName);
                    var lockFileRelativePath = projectUri.MakeRelativeUri(lockFileUri).OriginalString;
                    if (Path.DirectorySeparatorChar != '/')
                    {
                        lockFileRelativePath.Replace('/', Path.DirectorySeparatorChar);
                    }
                    msbuildProject.ProjectSystem.AddExistingFile(lockFileRelativePath);
                }
            }
        }