public async Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(LibraryIdentity match, NuGetFramework targetFramework, CancellationToken cancellationToken)
        {
            await EnsureResource();

            var packageInfo = await _findPackagesByIdResource.GetDependencyInfoAsync(match.Name, match.Version, cancellationToken);

            return GetDependencies(packageInfo, targetFramework);
        }
        public async Task CopyToAsync(LibraryIdentity identity, Stream stream, CancellationToken cancellationToken)
        {
            await EnsureResource();

            using (var nupkgStream = await _findPackagesByIdResource.GetNupkgStreamAsync(identity.Name, identity.Version, cancellationToken))
            {
                await nupkgStream.CopyToAsync(stream, bufferSize: 8192, cancellationToken: cancellationToken);
            }
        }
示例#3
0
 public LibraryDescription(
     LibraryRange requestedRange,
     LibraryIdentity identity,
     string path,
     string type,
     IEnumerable<LibraryDependency> dependencies,
     IEnumerable<string> assemblies,
     FrameworkName framework)
 {
     Path = path;
     RequestedRange = requestedRange;
     Identity = identity;
     Type = type;
     Dependencies = dependencies ?? Enumerable.Empty<LibraryDependency>();
     Assemblies = assemblies ?? Enumerable.Empty<string>();
     Framework = framework;
 }
 public TargetLibraryWithAssets(
     LibraryIdentity libraryIdentity,
     string sha512,
     string path,
     LockFileTargetLibrary lockFileLibrary,
     IEnumerable<LibraryRange> dependencies,
     bool compatible,
     bool resolved,
     NuGetFramework framework = null)
     : base(
           libraryIdentity,
           sha512,
           path,
           dependencies: dependencies,
           framework: null,
           resolved: resolved,
           compatible: compatible)
 {
     TargetLibrary = lockFileLibrary;
 }
示例#5
0
 public Task CopyToAsync(LibraryIdentity match, Stream stream, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
示例#6
0
        private CompatibilityData GetCompatibilityData(RestoreTargetGraph graph, LibraryIdentity libraryId)
        {
            LockFileTargetLibrary targetLibrary = null;
            var target = _lockFile.Targets.FirstOrDefault(t => Equals(t.TargetFramework, graph.Framework) && string.Equals(t.RuntimeIdentifier, graph.RuntimeIdentifier, StringComparison.Ordinal));

            if (target != null)
            {
                targetLibrary = target.Libraries.FirstOrDefault(t => t.Name.Equals(libraryId.Name) && t.Version.Equals(libraryId.Version));
            }

            IEnumerable <string> files = null;
            var lockFileLibrary        = _lockFile.Libraries.FirstOrDefault(l => l.Name.Equals(libraryId.Name) && l.Version.Equals(libraryId.Version));

            if (lockFileLibrary != null)
            {
                files = lockFileLibrary.Files;
            }

            if (files != null && targetLibrary != null)
            {
                // Everything we need is in the lock file!
                return(new CompatibilityData(lockFileLibrary.Files, targetLibrary));
            }
            else
            {
                // We need to generate some of the data. We'll need the local packge info to do that
                var packageInfo = NuGetv3LocalRepositoryUtility.GetPackage(
                    _localRepositories,
                    libraryId.Name,
                    libraryId.Version);

                if (packageInfo == null)
                {
                    return(null);
                }

                // Collect the file list if necessary
                if (files == null)
                {
                    using (var packageReader = new PackageFolderReader(packageInfo.Package.ExpandedPath))
                    {
                        if (Path.DirectorySeparatorChar != '/')
                        {
                            files = packageReader
                                    .GetFiles()
                                    .Select(p => p.Replace(Path.DirectorySeparatorChar, '/'))
                                    .ToList();
                        }
                        else
                        {
                            files = packageReader.GetFiles().ToList();
                        }
                    }
                }

                // Generate the target library if necessary
                if (targetLibrary == null)
                {
                    targetLibrary = LockFileUtils.CreateLockFileTargetLibrary(
                        library: null,
                        package: packageInfo.Package,
                        targetGraph: graph,
                        dependencyType: LibraryIncludeFlags.All);
                }

                return(new CompatibilityData(files, targetLibrary));
            }
        }
示例#7
0
 public Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(LibraryIdentity match, NuGetFramework targetFramework, CancellationToken cancellationToken)
 {
     return Task.FromResult(Enumerable.Empty<LibraryDependency>());
 }
示例#8
0
 public GraphItem(LibraryIdentity key)
 {
     Key = key;
 }
        public Task <IEnumerable <LibraryDependency> > GetDependenciesAsync(LibraryIdentity library, NuGetFramework targetFramework, CancellationToken cancellationToken)
        {
            var description = _dependencyProvider.GetLibrary(library, targetFramework);

            return(Task.FromResult(description.Dependencies));
        }
示例#10
0
        internal static async Task InstallFromStream(
            Stream stream,
            LibraryIdentity library,
            string packagesDirectory,
            ILogger log)
        {
            var packagePathResolver = new DefaultPackagePathResolver(packagesDirectory);

            var targetPath = packagePathResolver.GetInstallPath(library.Name, library.Version);
            var targetNuspec = packagePathResolver.GetManifestFilePath(library.Name, library.Version);
            var targetNupkg = packagePathResolver.GetPackageFilePath(library.Name, library.Version);
            var hashPath = packagePathResolver.GetHashPath(library.Name, library.Version);

            // Acquire the lock on a nukpg before we extract it to prevent the race condition when multiple
            // processes are extracting to the same destination simultaneously
            await ConcurrencyUtilities.ExecuteWithFileLocked(targetNupkg, async createdNewLock =>
            {
                // If this is the first process trying to install the target nupkg, go ahead
                // After this process successfully installs the package, all other processes
                // waiting on this lock don't need to install it again.
                if (createdNewLock && !File.Exists(targetNupkg))
                {
                    log.LogInformation($"Installing {library.Name} {library.Version}");

                    Directory.CreateDirectory(targetPath);
                    using (var nupkgStream = new FileStream(
                        targetNupkg,
                        FileMode.Create,
                        FileAccess.ReadWrite,
                        FileShare.ReadWrite | FileShare.Delete,
                        bufferSize: 4096,
                        useAsync: true))
                    {
                        await stream.CopyToAsync(nupkgStream);
                        nupkgStream.Seek(0, SeekOrigin.Begin);

                        ExtractPackage(targetPath, nupkgStream);
                    }

                    // DNU REFACTORING TODO: delete the hacky FixNuSpecIdCasing() and uncomment logic below after we
                    // have implementation of NuSpecFormatter.Read()
                    // Fixup the casing of the nuspec on disk to match what we expect
                    var nuspecFile = Directory.EnumerateFiles(targetPath, "*" + ManifestExtension).Single();
                    FixNuSpecIdCasing(nuspecFile, targetNuspec, library.Name);

                    /*var actualNuSpecName = Path.GetFileName(nuspecFile);
                    var expectedNuSpecName = Path.GetFileName(targetNuspec);

                    if (!string.Equals(actualNuSpecName, expectedNuSpecName, StringComparison.Ordinal))
                    {
                        MetadataBuilder metadataBuilder = null;
                        var nuspecFormatter = new NuSpecFormatter();
                        using (var nuspecStream = File.OpenRead(nuspecFile))
                        {
                            metadataBuilder = nuspecFormatter.Read(nuspecStream);
                            // REVIEW: any way better hardcoding "id"?
                            metadataBuilder.SetMetadataValue("id", library.Name);
                        }

                        // Delete the previous nuspec file
                        File.Delete(nuspecFile);

                        // Write the new manifest
                        using (var targetNuspecStream = File.OpenWrite(targetNuspec))
                        {
                            nuspecFormatter.Save(metadataBuilder, targetNuspecStream);
                        }
                    }*/

                    stream.Seek(0, SeekOrigin.Begin);
                    string packageHash;
                    using (var sha512 = SHA512.Create())
                    {
                        packageHash = Convert.ToBase64String(sha512.ComputeHash(stream));
                    }

                    // Note: PackageRepository relies on the hash file being written out as the final operation as part of a package install
                    // to assume a package was fully installed.
                    File.WriteAllText(hashPath, packageHash);
                }

                return 0;
            });
        }
        public void GivenAProjectLibraryVerifyFormatDoesNotIncludeTheVersion()
        {
            var library = new LibraryIdentity("A", NuGetVersion.Parse("1.0.0"), LibraryType.Project);

            DiagnosticUtility.FormatIdentity(library).Should().Be("A", "the version is not needed for projects");
        }
示例#12
0
        /// <summary>
        /// Restores a package by querying, downloading, and unzipping it without generating any other files (like project.assets.json).
        /// </summary>
        /// <param name="libraryIdentity">The <see cref="LibraryIdentity"/> of the package.</param>
        /// <param name="settings">The NuGet settings to use.</param>
        /// <param name="logger">An <see cref="ILogger"/> to use for logging.</param>
        /// <returns></returns>
        public static Task <IReadOnlyList <RestoreResultPair> > RunWithoutCommit(LibraryIdentity libraryIdentity, ISettings settings, ILogger logger)
        {
            using (var sourceCacheContext = new SourceCacheContext
            {
                IgnoreFailedSources = true,
            })
            {
                var projectPath = Path.Combine(Path.GetTempPath(), TempProjectName);

                // The package spec details what packages to restore
                var packageSpec = new PackageSpec(TargetFrameworks.Select(i => new TargetFrameworkInformation
                {
                    FrameworkName = i,
                }).ToList())
                {
                    Dependencies = new List <LibraryDependency>
                    {
                        new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(
                                libraryIdentity.Name,
                                new VersionRange(
                                    minVersion: libraryIdentity.Version,
                                    includeMinVersion: true,
                                    maxVersion: libraryIdentity.Version,
                                    includeMaxVersion: true),
                                LibraryDependencyTarget.Package),
                            SuppressParent = LibraryIncludeFlags.All,
                            AutoReferenced = true,
                            IncludeType    = LibraryIncludeFlags.None,
                            Type           = LibraryDependencyType.Build
                        }
                    },
                    RestoreMetadata = new ProjectRestoreMetadata
                    {
                        ProjectPath              = projectPath,
                        ProjectName              = Path.GetFileNameWithoutExtension(TempProjectName),
                        ProjectStyle             = ProjectStyle.PackageReference,
                        ProjectUniqueName        = TempProjectName,
                        OutputPath               = Path.GetTempPath(),
                        OriginalTargetFrameworks = TargetFrameworks.Select(i => i.ToString()).ToList(),
                        ConfigFilePaths          = settings.GetConfigFilePaths(),
                        PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                        Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                        FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                    },
                    FilePath = projectPath,
                    Name     = Path.GetFileNameWithoutExtension(TempProjectName),
                };

                var dependencyGraphSpec = new DependencyGraphSpec();

                dependencyGraphSpec.AddProject(packageSpec);

                dependencyGraphSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);

                IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                var restoreArgs = new RestoreArgs
                {
                    AllowNoOp             = true,
                    CacheContext          = sourceCacheContext,
                    CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings)),
                    Log = logger,
                };

                // Create requests from the arguments
                var requests = requestProvider.CreateRequests(restoreArgs).Result;

                // Restore the package without generating extra files
                return(RestoreRunner.RunWithoutCommit(requests, restoreArgs));
            }
        }
示例#13
0
        public Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(LibraryIdentity library, NuGetFramework targetFramework, CancellationToken cancellationToken)
        {
            var description = _dependencyProvider.GetLibrary(library, targetFramework);

            return Task.FromResult(description.Dependencies);
        }
示例#14
0
 public Task CopyToAsync(LibraryIdentity match, Stream stream, CancellationToken cancellationToken)
 {
     throw new NotSupportedException();
 }
示例#15
0
        internal static async Task InstallFromStream(
            Stream stream,
            LibraryIdentity library,
            string packagesDirectory,
            IReport information,
            string packageHash = null)
        {
            var packagePathResolver = new DefaultPackagePathResolver(packagesDirectory);

            var targetPath     = packagePathResolver.GetInstallPath(library.Name, library.Version);
            var targetNuspec   = packagePathResolver.GetManifestFilePath(library.Name, library.Version);
            var targetNupkg    = packagePathResolver.GetPackageFilePath(library.Name, library.Version);
            var targetHashPath = packagePathResolver.GetHashPath(library.Name, library.Version);

            // Acquire the lock on a nukpg before we extract it to prevent the race condition when multiple
            // processes are extracting to the same destination simultaneously
            await ConcurrencyUtilities.ExecuteWithFileLocked(targetNupkg, action : async _ =>
            {
                if (string.IsNullOrEmpty(packageHash))
                {
                    using (var sha512 = SHA512.Create())
                    {
                        packageHash = Convert.ToBase64String(sha512.ComputeHash(stream));
                    }
                }

                var actionName           = "Installing";
                var installedPackageHash = string.Empty;
                if (File.Exists(targetHashPath) && File.Exists(targetNuspec))
                {
                    installedPackageHash = File.ReadAllText(targetHashPath);
                    actionName           = "Overwriting";
                }

                if (string.Equals(packageHash, installedPackageHash, StringComparison.Ordinal))
                {
                    information.WriteLine($"{library.Name}.{library.Version} already exists");
                }
                else
                {
                    information.WriteLine($"{actionName} {library.Name}.{library.Version}");

                    Directory.CreateDirectory(targetPath);
                    using (var nupkgStream = new FileStream(
                               targetNupkg,
                               FileMode.Create,
                               FileAccess.ReadWrite,
                               FileShare.ReadWrite | FileShare.Delete,
                               bufferSize: 4096,
                               useAsync: true))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        await stream.CopyToAsync(nupkgStream);
                        nupkgStream.Seek(0, SeekOrigin.Begin);

                        ExtractPackage(targetPath, nupkgStream);
                    }

                    // Fixup the casing of the nuspec on disk to match what we expect
                    var nuspecFile = Directory.EnumerateFiles(targetPath, "*" + NuGet.Constants.ManifestExtension).Single();

                    if (!string.Equals(nuspecFile, targetNuspec, StringComparison.Ordinal))
                    {
                        Manifest manifest = null;
                        using (var nuspecStream = File.OpenRead(nuspecFile))
                        {
                            manifest             = Manifest.ReadFrom(nuspecStream, validateSchema: false);
                            manifest.Metadata.Id = library.Name;
                        }

                        // Delete the previous nuspec file
                        File.Delete(nuspecFile);

                        // Write the new manifest
                        using (var targetNuspecStream = File.OpenWrite(targetNuspec))
                        {
                            manifest.Save(targetNuspecStream);
                        }
                    }

                    // Note: PackageRepository relies on the hash file being written out as the final operation as part of a package install
                    // to assume a package was fully installed.
                    File.WriteAllText(targetHashPath, packageHash);
                }

                return(0);
            });
        }
示例#16
0
            public static SdkResultBase GetSdkResult(SdkReference sdk, object nuGetVersion, SdkResolverContextBase context, SdkResultFactoryBase factory)
            {
                // Cast the NuGet version since the caller does not want to consume NuGet classes directly
                NuGetVersion parsedSdkVersion = (NuGetVersion)nuGetVersion;

                // Stores errors and warnings for the result
                ICollection <string> errors   = new List <string>();
                ICollection <string> warnings = new List <string>();

                // Load NuGet settings and a path resolver
                ISettings settings = Settings.LoadDefaultSettings(context.ProjectFilePath);

                FallbackPackagePathResolver fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                string installedPath, installedVersion;

                // Attempt to find a package if its already installed
                if (!TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, sdk.Name, parsedSdkVersion, out installedPath, out installedVersion))
                {
                    try
                    {
                        // Asynchronously run the restore without a commit which find the package on configured feeds, download, and unzip it without generating any other files
                        IReadOnlyList <RestoreResultPair> results = RestoreRunnerEx.RunWithoutCommit(
                            context.ProjectFilePath,
                            sdk.Name,
                            parsedSdkVersion.ToFullString(),
                            settings,
                            new NuGetSdkLogger(context.Logger, warnings, errors))
                                                                    .ConfigureAwait(continueOnCapturedContext: false)
                                                                    .GetAwaiter()
                                                                    .GetResult();

                        fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                        // Look for a successful result, any errors are logged by NuGet
                        foreach (RestoreResult result in results.Select(i => i.Result).Where(i => i.Success))
                        {
                            // Find the information about the package that was installed.  In some cases, the version can be different than what was specified (like you specify 1.0 but get 1.0.0)
                            LibraryIdentity installedPackage = result.GetAllInstalled().FirstOrDefault(i => i.Name.Equals(sdk.Name));

                            if (installedPackage != null)
                            {
                                if (!TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, installedPackage.Name, installedPackage.Version, out installedPath, out installedVersion))
                                {
                                    // This should never happen because we were told the package was successfully installed.
                                    // If we can't find it, we probably did something wrong with the NuGet API
                                    errors.Add(ResourceUtilities.FormatResourceString("CouldNotFindInstalledPackage", sdk));
                                }
                            }
                            else
                            {
                                // This should never happen because we were told the restore succeeded.
                                // If we can't find the package from GetAllInstalled(), we probably did something wrong with the NuGet API
                                errors.Add(ResourceUtilities.FormatResourceString("PackageWasNotInstalled", sdk, sdk.Name));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        errors.Add(e.Message);
                    }
                }

                if (errors.Count == 0)
                {
                    return(factory.IndicateSuccess(path: installedPath, version: installedVersion, warnings: warnings));
                }

                return(factory.IndicateFailure(errors, warnings));
            }
示例#17
0
        /// <summary>
        /// Restores a package by querying, downloading, and unzipping it without generating any other files (like project.assets.json).
        /// </summary>
        /// <param name="libraryIdentity">The <see cref="LibraryIdentity"/> of the package.</param>
        /// <param name="settings">The NuGet settings to use.</param>
        /// <param name="logger">An <see cref="ILogger"/> to use for logging.</param>
        /// <returns></returns>
        public static Task <IReadOnlyList <RestoreResultPair> > RunWithoutCommit(LibraryIdentity libraryIdentity, ISettings settings, ILogger logger)
        {
            using (var sourceCacheContext = new SourceCacheContext
            {
                IgnoreFailedSources = true,
            })
            {
                // Create a unique temporary directory for the project
                var projectDirectory = Directory.CreateDirectory(Path.Combine(NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp), Guid.NewGuid().ToString("N")));

                try
                {
                    var projectName = Guid.NewGuid().ToString("N");

                    var projectFullPath = Path.Combine(projectDirectory.FullName, $"{projectName}.proj");

                    // The package spec details what packages to restore
                    var packageSpec = new PackageSpec(TargetFrameworks.Select(i => new TargetFrameworkInformation
                    {
                        FrameworkName = i,
                    }).ToList())
                    {
                        Dependencies = new List <LibraryDependency>
                        {
                            new LibraryDependency
                            {
                                LibraryRange = new LibraryRange(
                                    libraryIdentity.Name,
                                    new VersionRange(
                                        minVersion: libraryIdentity.Version,
                                        includeMinVersion: true,
                                        maxVersion: libraryIdentity.Version,
                                        includeMaxVersion: true),
                                    LibraryDependencyTarget.Package),
                                SuppressParent = LibraryIncludeFlags.All,
                                AutoReferenced = true,
                                IncludeType    = LibraryIncludeFlags.None,
                                Type           = LibraryDependencyType.Build
                            }
                        },
                        RestoreMetadata = new ProjectRestoreMetadata
                        {
                            ProjectPath              = projectFullPath,
                            ProjectName              = projectName,
                            ProjectStyle             = ProjectStyle.PackageReference,
                            ProjectUniqueName        = projectFullPath,
                            OutputPath               = projectDirectory.FullName,
                            OriginalTargetFrameworks = TargetFrameworks.Select(i => i.ToString()).ToList(),
                            ConfigFilePaths          = settings.GetConfigFilePaths(),
                            PackagesPath             = SettingsUtility.GetGlobalPackagesFolder(settings),
                            Sources         = SettingsUtility.GetEnabledSources(settings).ToList(),
                            FallbackFolders = SettingsUtility.GetFallbackPackageFolders(settings).ToList()
                        },
                        FilePath = projectFullPath,
                        Name     = projectName,
                    };

                    var dependencyGraphSpec = new DependencyGraphSpec();

                    dependencyGraphSpec.AddProject(packageSpec);

                    dependencyGraphSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);

                    IPreLoadedRestoreRequestProvider requestProvider = new DependencyGraphSpecRequestProvider(new RestoreCommandProvidersCache(), dependencyGraphSpec);

                    var restoreArgs = new RestoreArgs
                    {
                        AllowNoOp    = false,
                        CacheContext = sourceCacheContext,
    #pragma warning disable CS0618 // Type or member is obsolete
                        CachingSourceProvider = new CachingSourceProvider(new PackageSourceProvider(settings, enablePackageSourcesChangedEvent: false)),
    #pragma warning restore CS0618 // Type or member is obsolete
                        Log = logger,
                    };

                    // Create requests from the arguments
                    var requests = requestProvider.CreateRequests(restoreArgs).Result;

                    // Restore the package without generating extra files
                    return(RestoreRunner.RunWithoutCommit(requests, restoreArgs));
                }
                finally
                {
                    LocalResourceUtils.DeleteDirectoryTree(projectDirectory.FullName, new List <string>());
                }
            }
        }
 public ResolverRequest(LibraryIdentity requestor, LibraryRange request)
 {
     Requestor = requestor;
     Request   = request;
 }
示例#19
0
        internal static async Task InstallFromStream(
            Stream stream,
            LibraryIdentity library,
            string packagesDirectory,
            ILogger log)
        {
            var packagePathResolver = new DefaultPackagePathResolver(packagesDirectory);

            var targetPath = packagePathResolver.GetInstallPath(library.Name, library.Version);
            var targetNuspec = packagePathResolver.GetManifestFilePath(library.Name, library.Version);
            var targetNupkg = packagePathResolver.GetPackageFilePath(library.Name, library.Version);
            var hashPath = packagePathResolver.GetHashPath(library.Name, library.Version);

            // Acquire the lock on a nukpg before we extract it to prevent the race condition when multiple
            // processes are extracting to the same destination simultaneously
            await ConcurrencyUtilities.ExecuteWithFileLocked(targetNupkg, async createdNewLock =>
                {
                    // If this is the first process trying to install the target nupkg, go ahead
                    // After this process successfully installs the package, all other processes
                    // waiting on this lock don't need to install it again.
                    if (createdNewLock && !File.Exists(targetNupkg))
                    {
                        log.LogInformation($"Installing {library.Name} {library.Version}");

                        Directory.CreateDirectory(targetPath);
                        using (var nupkgStream = new FileStream(
                            targetNupkg,
                            FileMode.Create,
                            FileAccess.ReadWrite,
                            FileShare.ReadWrite | FileShare.Delete,
                            bufferSize: 4096,
                            useAsync: true))
                        {
                            await stream.CopyToAsync(nupkgStream);
                            nupkgStream.Seek(0, SeekOrigin.Begin);

                            ExtractPackage(targetPath, nupkgStream);
                        }

                        // DNU REFACTORING TODO: delete the hacky FixNuSpecIdCasing() and uncomment logic below after we
                        // have implementation of NuSpecFormatter.Read()
                        // Fixup the casing of the nuspec on disk to match what we expect
                        var nuspecFile = Directory.EnumerateFiles(targetPath, "*" + ManifestExtension).Single();
                        FixNuSpecIdCasing(nuspecFile, targetNuspec, library.Name);

                        /*var actualNuSpecName = Path.GetFileName(nuspecFile);
                    var expectedNuSpecName = Path.GetFileName(targetNuspec);

                    if (!string.Equals(actualNuSpecName, expectedNuSpecName, StringComparison.Ordinal))
                    {
                        MetadataBuilder metadataBuilder = null;
                        var nuspecFormatter = new NuSpecFormatter();
                        using (var nuspecStream = File.OpenRead(nuspecFile))
                        {
                            metadataBuilder = nuspecFormatter.Read(nuspecStream);
                            // REVIEW: any way better hardcoding "id"?
                            metadataBuilder.SetMetadataValue("id", library.Name);
                        }

                        // Delete the previous nuspec file
                        File.Delete(nuspecFile);

                        // Write the new manifest
                        using (var targetNuspecStream = File.OpenWrite(targetNuspec))
                        {
                            nuspecFormatter.Save(metadataBuilder, targetNuspecStream);
                        }
                    }*/

                        stream.Seek(0, SeekOrigin.Begin);
                        string packageHash;
                        using (var sha512 = SHA512.Create())
                        {
                            packageHash = Convert.ToBase64String(sha512.ComputeHash(stream));
                        }

                        // Note: PackageRepository relies on the hash file being written out as the final operation as part of a package install
                        // to assume a package was fully installed.
                        File.WriteAllText(hashPath, packageHash);
                    }

                    return 0;
                });
        }
示例#20
0
 public void AddLibrary(LibraryIdentity identity)
 {
     _libraries.Add(identity);
 }
            public static SdkResult GetSdkResult(SdkReference sdk, object nuGetVersion, SdkResolverContext context, SdkResultFactory factory)
            {
                // Cast the NuGet version since the caller does not want to consume NuGet classes directly
                var parsedSdkVersion = (NuGetVersion)nuGetVersion;

                // Load NuGet settings and a path resolver
                ISettings settings = Settings.LoadDefaultSettings(context.ProjectFilePath, configFileName: null, MachineWideSettings.Value as IMachineWideSettings, SettingsLoadContext.Value as SettingsLoadingContext);

                var fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                var libraryIdentity = new LibraryIdentity(sdk.Name, parsedSdkVersion, LibraryType.Package);

                var logger = new NuGetSdkLogger(context.Logger);

                // Attempt to find a package if its already installed
                if (!TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, libraryIdentity, out var installedPath, out var installedVersion))
                {
                    try
                    {
                        DefaultCredentialServiceUtility.SetupDefaultCredentialService(logger, nonInteractive: !context.Interactive);

                        // Asynchronously run the restore without a commit which find the package on configured feeds, download, and unzip it without generating any other files
                        // This must be run in its own task because legacy project system evaluates projects on the UI thread which can cause RunWithoutCommit() to deadlock
                        // https://developercommunity.visualstudio.com/content/problem/311379/solution-load-never-completes-when-project-contain.html
                        var restoreTask = Task.Run(() => RestoreRunnerEx.RunWithoutCommit(
                                                       libraryIdentity,
                                                       settings,
                                                       logger));

                        var results = restoreTask.Result;

                        fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                        // Look for a successful result, any errors are logged by NuGet
                        foreach (var result in results.Select(i => i.Result).Where(i => i.Success))
                        {
                            // Find the information about the package that was installed.  In some cases, the version can be different than what was specified (like you specify 1.0 but get 1.0.0)
                            var installedPackage = result.GetAllInstalled().FirstOrDefault(i => i == libraryIdentity);

                            if (installedPackage != null)
                            {
                                if (TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, installedPackage, out installedPath, out installedVersion))
                                {
                                    break;
                                }

                                // This should never happen because we were told the package was successfully installed.
                                // If we can't find it, we probably did something wrong with the NuGet API
                                logger.LogError(string.Format(CultureInfo.CurrentCulture, Strings.CouldNotFindInstalledPackage, sdk));
                            }
                            else
                            {
                                // This should never happen because we were told the restore succeeded.
                                // If we can't find the package from GetAllInstalled(), we probably did something wrong with the NuGet API
                                logger.LogError(string.Format(CultureInfo.CurrentCulture, Strings.PackageWasNotInstalled, sdk, sdk.Name));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e.ToString());
                    }
                    finally
                    {
                        // The CredentialService lifetime is for the duration of the process. We should not leave a potentially unavailable logger.
                        DefaultCredentialServiceUtility.UpdateCredentialServiceDelegatingLogger(NullLogger.Instance);
                    }
                }

                if (logger.Errors.Count == 0)
                {
                    return(factory.IndicateSuccess(path: installedPath, version: installedVersion, warnings: logger.Warnings));
                }

                return(factory.IndicateFailure(logger.Errors, logger.Warnings));
            }
示例#22
0
        /// <summary>
        /// Create a library for a project.
        /// </summary>
        public static LockFileTargetLibrary CreateLockFileTargetProject(
            GraphItem <RemoteResolveResult> graphItem,
            LibraryIdentity library,
            LibraryIncludeFlags dependencyType,
            RestoreTargetGraph targetGraph,
            ProjectStyle rootProjectStyle)
        {
            var localMatch = (LocalMatch)graphItem.Data.Match;

            // Target framework information is optional and may not exist for csproj projects
            // that do not have a project.json file.
            string projectFramework = null;
            object frameworkInfoObject;

            if (localMatch.LocalLibrary.Items.TryGetValue(
                    KnownLibraryProperties.TargetFrameworkInformation,
                    out frameworkInfoObject))
            {
                // Retrieve the resolved framework name, if this is null it means that the
                // project is incompatible. This is marked as Unsupported.
                var targetFrameworkInformation = (TargetFrameworkInformation)frameworkInfoObject;
                projectFramework = targetFrameworkInformation.FrameworkName?.DotNetFrameworkName
                                   ?? NuGetFramework.UnsupportedFramework.DotNetFrameworkName;
            }

            // Create the target entry
            var projectLib = new LockFileTargetLibrary()
            {
                Name      = library.Name,
                Version   = library.Version,
                Type      = LibraryType.Project,
                Framework = projectFramework,

                // Find all dependencies which would be in the nuspec
                // Include dependencies with no constraints, or package/project/external
                // Exclude suppressed dependencies, the top level project is not written
                // as a target so the node depth does not matter.
                Dependencies = graphItem.Data.Dependencies
                               .Where(
                    d => (d.LibraryRange.TypeConstraintAllowsAnyOf(
                              LibraryDependencyTarget.PackageProjectExternal)) &&
                    d.SuppressParent != LibraryIncludeFlags.All)
                               .Select(d => GetDependencyVersionRange(d))
                               .ToList()
            };

            if (rootProjectStyle == ProjectStyle.PackageReference)
            {
                // Add files under asset groups
                object filesObject;
                object msbuildPath;
                if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.MSBuildProjectPath, out msbuildPath))
                {
                    var files      = new List <ProjectRestoreMetadataFile>();
                    var fileLookup = new Dictionary <string, ProjectRestoreMetadataFile>(StringComparer.OrdinalIgnoreCase);

                    // Find the project path, this is provided by the resolver
                    var msbuildFilePathInfo = new FileInfo((string)msbuildPath);

                    // Ensure a trailing slash for the relative path helper.
                    var projectDir = PathUtility.EnsureTrailingSlash(msbuildFilePathInfo.Directory.FullName);

                    // Read files from the project if they were provided.
                    if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.ProjectRestoreMetadataFiles, out filesObject))
                    {
                        files.AddRange((List <ProjectRestoreMetadataFile>)filesObject);
                    }

                    var targetFrameworkShortName = targetGraph.Framework.GetShortFolderName();
                    var libAnyPath = $"lib/{targetFrameworkShortName}/any.dll";

                    if (files.Count == 0)
                    {
                        // If the project did not provide a list of assets, add in default ones.
                        // These are used to detect transitive vs non-transitive project references.
                        var absolutePath = Path.Combine(projectDir, "bin", "placeholder", $"{localMatch.Library.Name}.dll");

                        files.Add(new ProjectRestoreMetadataFile(libAnyPath, absolutePath));
                    }

                    // Process and de-dupe files
                    for (var i = 0; i < files.Count; i++)
                    {
                        var path = files[i].PackagePath;

                        // LIBANY avoid compatibility checks and will always be used.
                        if (LIBANY.Equals(path, StringComparison.Ordinal))
                        {
                            path = libAnyPath;
                        }

                        if (!fileLookup.ContainsKey(path))
                        {
                            fileLookup.Add(path, files[i]);
                        }
                    }

                    var contentItems = new ContentItemCollection();
                    contentItems.Load(fileLookup.Keys);

                    // Create an ordered list of selection criteria. Each will be applied, if the result is empty
                    // fallback frameworks from "imports" will be tried.
                    // These are only used for framework/RID combinations where content model handles everything.
                    var orderedCriteria = CreateCriteria(targetGraph, targetGraph.Framework);

                    // Compile
                    // ref takes precedence over lib
                    var compileGroup = GetLockFileItems(
                        orderedCriteria,
                        contentItems,
                        targetGraph.Conventions.Patterns.CompileRefAssemblies,
                        targetGraph.Conventions.Patterns.CompileLibAssemblies);

                    projectLib.CompileTimeAssemblies.AddRange(
                        ConvertToProjectPaths(fileLookup, projectDir, compileGroup));

                    // Runtime
                    var runtimeGroup = GetLockFileItems(
                        orderedCriteria,
                        contentItems,
                        targetGraph.Conventions.Patterns.RuntimeAssemblies);

                    projectLib.RuntimeAssemblies.AddRange(
                        ConvertToProjectPaths(fileLookup, projectDir, runtimeGroup));
                }
            }

            // Add frameworkAssemblies for projects
            object frameworkAssembliesObject;

            if (localMatch.LocalLibrary.Items.TryGetValue(
                    KnownLibraryProperties.FrameworkAssemblies,
                    out frameworkAssembliesObject))
            {
                projectLib.FrameworkAssemblies.AddRange((List <string>)frameworkAssembliesObject);
            }

            // Exclude items
            ExcludeItems(projectLib, dependencyType);

            return(projectLib);
        }
示例#23
0
 public void AddLibrary(LibraryIdentity identity)
 {
     _libraries.Add(identity);
 }
        public void GivenAPackageLibraryVerifyFormatDoesIncludeTheVersion()
        {
            var library = new LibraryIdentity("A", NuGetVersion.Parse("1.0.0"), LibraryType.Package);

            DiagnosticUtility.FormatIdentity(library).Should().Be("A 1.0.0", "the version is shown for packages");
        }
            /// <summary>
            /// Attempts to find a NuGet package if it is already installed.
            /// </summary>
            private static bool TryGetMSBuildSdkPackageInfo(FallbackPackagePathResolver fallbackPackagePathResolver, LibraryIdentity libraryIdentity, out string installedPath, out string installedVersion)
            {
                // Find the package
                var packageInfo = fallbackPackagePathResolver.GetPackageInfo(libraryIdentity.Name, libraryIdentity.Version);

                if (packageInfo == null)
                {
                    installedPath    = null;
                    installedVersion = null;
                    return(false);
                }

                // Get the installed path and add the expected "Sdk" folder.  Windows file systems are not case sensitive
                installedPath = Path.Combine(packageInfo.PathResolver.GetInstallPath(packageInfo.Id, packageInfo.Version), "Sdk");


                if (!NuGet.Common.RuntimeEnvironmentHelper.IsWindows && !Directory.Exists(installedPath))
                {
                    // Fall back to lower case "sdk" folder in case the file system is case sensitive
                    installedPath = Path.Combine(packageInfo.PathResolver.GetInstallPath(packageInfo.Id, packageInfo.Version), "sdk");
                }

                installedVersion = packageInfo.Version.ToString();

                return(true);
            }
        public void GivenAnUnresolvedLibraryVerifyFormatDoesNotIncludeTheVersion()
        {
            var library = new LibraryIdentity("A", null, LibraryType.Unresolved);

            DiagnosticUtility.FormatIdentity(library).Should().Be("A", "the version is not used for non-projects");
        }
        public void GivenAPackageLibraryVerifyFormatNormalizesTheVersion()
        {
            var library = new LibraryIdentity("A", NuGetVersion.Parse("1.0+abc"), LibraryType.Package);

            DiagnosticUtility.FormatIdentity(library).Should().Be("A 1.0.0");
        }
示例#28
0
        internal static async Task InstallFromStream(
            Stream stream,
            LibraryIdentity library,
            string packagesDirectory,
            IReport information,
            string packageHash = null)
        {
            var packagePathResolver = new DefaultPackagePathResolver(packagesDirectory);

            var targetPath = packagePathResolver.GetInstallPath(library.Name, library.Version);
            var targetNuspec = packagePathResolver.GetManifestFilePath(library.Name, library.Version);
            var targetNupkg = packagePathResolver.GetPackageFilePath(library.Name, library.Version);
            var targetHashPath = packagePathResolver.GetHashPath(library.Name, library.Version);

            // Acquire the lock on a nukpg before we extract it to prevent the race condition when multiple
            // processes are extracting to the same destination simultaneously
            await ConcurrencyUtilities.ExecuteWithFileLocked(targetNupkg, action: async _ =>
            {
                if (string.IsNullOrEmpty(packageHash))
                {
                    using (var sha512 = SHA512.Create())
                    {
                        packageHash = Convert.ToBase64String(sha512.ComputeHash(stream));
                    }
                }

                var actionName = "Installing";
                var installedPackageHash = string.Empty;
                if (File.Exists(targetHashPath) && File.Exists(targetNuspec))
                {
                    installedPackageHash = File.ReadAllText(targetHashPath);
                    actionName = "Overwriting";
                }

                if (string.Equals(packageHash, installedPackageHash, StringComparison.Ordinal))
                {
                    information.WriteLine($"{library.Name}.{library.Version} already exists");
                }
                else
                {
                    information.WriteLine($"{actionName} {library.Name}.{library.Version}");

                    Directory.CreateDirectory(targetPath);
                    using (var nupkgStream = new FileStream(
                        targetNupkg,
                        FileMode.Create,
                        FileAccess.ReadWrite,
                        FileShare.ReadWrite | FileShare.Delete,
                        bufferSize: 4096,
                        useAsync: true))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        await stream.CopyToAsync(nupkgStream);
                        nupkgStream.Seek(0, SeekOrigin.Begin);

                        ExtractPackage(targetPath, nupkgStream);
                    }

                    // Fixup the casing of the nuspec on disk to match what we expect
                    var nuspecFile = Directory.EnumerateFiles(targetPath, "*" + NuGet.Constants.ManifestExtension).Single();

                    Manifest manifest = null;
                    using (var nuspecStream = File.OpenRead(nuspecFile))
                    {
                        manifest = Manifest.ReadFrom(nuspecStream, validateSchema: false);
                        manifest.Metadata.Id = library.Name;
                        manifest.Metadata.Version = library.Version;
                    }

                    // Delete the previous nuspec file
                    File.Delete(nuspecFile);

                    // Write the new manifest
                    using (var targetNuspecStream = File.OpenWrite(targetNuspec))
                    {
                        manifest.Save(targetNuspecStream);
                    }

                    // Note: PackageRepository relies on the hash file being written out as the final operation as part of a package install
                    // to assume a package was fully installed.
                    File.WriteAllText(targetHashPath, packageHash);
                }

                return 0;
            });
        }
        private async Task <LibraryDependencyInfo> GetDependenciesCoreAsync(
            LibraryIdentity match,
            NuGetFramework targetFramework,
            SourceCacheContext cacheContext,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            FindPackageByIdDependencyInfo packageInfo = null;

            try
            {
                await EnsureResource();

                if (_throttle != null)
                {
                    await _throttle.WaitAsync();
                }

                // Read package info, this will download the package if needed.
                packageInfo = await _findPackagesByIdResource.GetDependencyInfoAsync(
                    match.Name,
                    match.Version,
                    cacheContext,
                    logger,
                    cancellationToken);
            }
            catch (FatalProtocolException e) when(e is not InvalidCacheProtocolException)
            {
                if (_ignoreFailedSources)
                {
                    await LogWarningAsync(logger, match.Name, e);
                }
                else
                {
                    await LogErrorAsync(logger, match.Name, e);

                    throw;
                }
            }
            finally
            {
                _throttle?.Release();
            }

            if (packageInfo == null)
            {
                // Package was not found
                return(LibraryDependencyInfo.CreateUnresolved(match, targetFramework));
            }
            else
            {
                // Package found
                var originalIdentity = new LibraryIdentity(
                    packageInfo.PackageIdentity.Id,
                    packageInfo.PackageIdentity.Version,
                    match.Type);

                IEnumerable <LibraryDependency> dependencyGroup = GetDependencies(packageInfo, targetFramework);

                return(LibraryDependencyInfo.Create(originalIdentity, targetFramework, dependencies: dependencyGroup));
            }
        }