private RestoreSummaryRequest Create(
                string inputPath,
                RestoreArgs restoreContext)
            {
                var file = new FileInfo(inputPath);

                // Get settings relative to the input file
                var settings = restoreContext.GetSettings(file.DirectoryName);

                // BUGFIX
                // The null here causes an exception downstream. Instead, inline the important code.
                //var sources = restoreContext.GetEffectiveSources(settings, null);
                var packageSourceProvider = new PackageSourceProvider(settings);
                CachingSourceProvider cachingSourceProvider = new CachingSourceProvider(packageSourceProvider);
                var sources = packageSourceProvider
                              .LoadPackageSources()
                              .Select(cachingSourceProvider.CreateRepository)
                              .ToList();
                // END BUGFIX
                var FallbackPackageFolders = restoreContext.GetEffectiveFallbackPackageFolders(settings);

                var globalPath = restoreContext.GetEffectiveGlobalPackagesFolder(file.DirectoryName, settings);

                var sharedCache = _providerCache.GetOrCreate(
                    globalPath,
                    FallbackPackageFolders,
                    sources,
                    restoreContext.CacheContext,
                    restoreContext.Log);

                var project = JsonPackageSpecReader.GetPackageSpec(file.Directory.Name, file.FullName);

                // BUGFIX
                // ApplyStandardProperties tries to access RestoreMetadata with no null check. Add
                // a default value.
                project.RestoreMetadata = new ProjectRestoreMetadata();
                // END BUGFIX

                var request = new RestoreRequest(
                    project,
                    sharedCache,
                    restoreContext.CacheContext,
                    restoreContext.Log);

                restoreContext.ApplyStandardProperties(request);

                IEnumerable <string> configFiles = SettingsUtility.GetConfigFilePaths(settings);

                var summaryRequest = new RestoreSummaryRequest(request, inputPath, configFiles, sources);

                return(summaryRequest);
            }
Ejemplo n.º 2
0
        static async Task Main(string[] args)
        {
            using var cacheContext = new SourceCacheContext();
            using var pathContext  = new SimpleTestPathContext();
            var providerCache = new RestoreCommandProvidersCache();
            var dgFile        = new DependencyGraphSpec();
            var sources       = new List <string>()
            {
                pathContext.PackageSource
            };
            var restoreContext = new RestoreArgs()
            {
                CacheContext         = cacheContext,
                DisableParallel      = true,
                GlobalPackagesFolder = pathContext.UserPackagesFolder,
                Sources = sources,
                Log     = new NullLogger(),
                CachingSourceProvider =
                    new CachingSourceProvider(new TestPackageSourceProvider(
                                                  new List <PackageSource>()
                {
                    new PackageSource(pathContext.PackageSource)
                })),
                PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>()
                {
                    new DependencyGraphSpecRequestProvider(providerCache, dgFile)
                }
            };

            var request   = (await RestoreRunner.GetRequests(restoreContext)).Single();
            var providers = providerCache.GetOrCreate(pathContext.UserPackagesFolder, sources,
                                                      new List <SourceRepository>(), cacheContext, new NullLogger());
            var command = new RestoreCommand(request.Request);

            // Add to cache before install on all providers
            var globalPackages = providers.GlobalPackages;
            var packages       = globalPackages.FindPackagesById("a");

            foreach (var local in providers.LocalProviders)
            {
                await local.GetDependenciesAsync(
                    new LibraryIdentity("a", NuGetVersion.Parse("1.0.0"), LibraryType.Package),
                    NuGetFramework.Parse("net46"), cacheContext, new NullLogger(), CancellationToken.None);
            }

            // Run restore using an incorrect cache
            var result = await command.ExecuteAsync();
        }
Ejemplo n.º 3
0
        private async Task BuildIntegratedProjectRestoreAsync(
            BuildIntegratedNuGetProject project,
            string solutionDirectory,
            List <SourceRepository> enabledSources,
            ExternalProjectReferenceContext context,
            RestoreCommandProvidersCache providerCache,
            CancellationToken token)
        {
            // Go off the UI thread to perform I/O operations
            await TaskScheduler.Default;

            var projectName = NuGetProject.GetUniqueNameOrName(project);

            var nugetPathContext = NuGetPathContext.Create(Settings);

            using (var cacheContext = new SourceCacheContext())
            {
                providerCache.GetOrCreate(
                    nugetPathContext.UserPackageFolder,
                    nugetPathContext.FallbackPackageFolders,
                    enabledSources,
                    cacheContext,
                    context.Logger);

                // Pass down the CancellationToken from the dialog
                var restoreResult = await BuildIntegratedRestoreUtility.RestoreAsync(project,
                                                                                     context,
                                                                                     enabledSources,
                                                                                     nugetPathContext.UserPackageFolder,
                                                                                     nugetPathContext.FallbackPackageFolders,
                                                                                     token);

                if (!restoreResult.Success)
                {
                    // Mark this as having errors
                    _hasErrors = true;

                    // Invalidate cached results for the project. This will cause it to restore the next time.
                    _buildIntegratedCache.Remove(projectName);
                    await BuildIntegratedProjectReportErrorAsync(projectName, restoreResult, token);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task InstallPackageFromAnotherProcessVerifyCacheIsClearedAsync()
        {
            // Arrange
            var logger = new TestLogger();

            using (var cacheContext = new SourceCacheContext())
                using (var pathContext = new SimpleTestPathContext())
                {
                    var tfi = new List <TargetFrameworkInformation>
                    {
                        new TargetFrameworkInformation()
                        {
                            FrameworkName = NuGetFramework.Parse("net462")
                        }
                    };

                    var spec = NETCoreRestoreTestUtility.GetProject(projectName: "projectA", framework: "net46");
                    spec.Dependencies.Add(new LibraryDependency()
                    {
                        LibraryRange = new LibraryRange("a", VersionRange.Parse("1.0.0"), LibraryDependencyTarget.Package)
                    });

                    var project = NETCoreRestoreTestUtility.CreateProjectsFromSpecs(pathContext, spec).Single();

                    var packageA = new SimpleTestPackageContext("a");
                    await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packageA);

                    // Create dg file
                    var dgFile = new DependencyGraphSpec();
                    dgFile.AddProject(spec);
                    dgFile.AddRestore(spec.RestoreMetadata.ProjectUniqueName);

                    dgFile.Save(Path.Combine(pathContext.WorkingDirectory, "out.dg"));

                    var providerCache = new RestoreCommandProvidersCache();
                    var sources       = new List <string>()
                    {
                        pathContext.PackageSource
                    };

                    var restoreContext = new RestoreArgs()
                    {
                        CacheContext         = cacheContext,
                        DisableParallel      = true,
                        GlobalPackagesFolder = pathContext.UserPackagesFolder,
                        Sources = sources,
                        Log     = logger,
                        CachingSourceProvider = new CachingSourceProvider(new TestPackageSourceProvider(new List <PackageSource>()
                        {
                            new PackageSource(pathContext.PackageSource)
                        })),
                        PreLoadedRequestProviders = new List <IPreLoadedRestoreRequestProvider>()
                        {
                            new DependencyGraphSpecRequestProvider(providerCache, dgFile)
                        }
                    };

                    var request   = (await RestoreRunner.GetRequests(restoreContext)).Single();
                    var providers = providerCache.GetOrCreate(pathContext.UserPackagesFolder, sources, new List <SourceRepository>(), cacheContext, logger, false);
                    var command   = new RestoreCommand(request.Request);

                    // Add to cache before install on all providers
                    var globalPackages = providers.GlobalPackages;
                    var packages       = globalPackages.FindPackagesById("a");
                    packages.Should().BeEmpty("has not been installed yet");

                    foreach (var local in providers.LocalProviders)
                    {
                        await local.GetDependenciesAsync(new LibraryIdentity("a", NuGetVersion.Parse("1.0.0"), LibraryType.Package), NuGetFramework.Parse("net46"), cacheContext, logger, CancellationToken.None);
                    }

                    // Install the package without updating the cache
                    await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.UserPackagesFolder, PackageSaveMode.Defaultv3, packageA);

                    // Run restore using an incorrect cache
                    var result = await command.ExecuteAsync();

                    // Verify a is in the output assets file
                    result.Success.Should().BeTrue();
                    result.LockFile.GetLibrary("a", new NuGetVersion(1, 0, 0)).Should().NotBeNull();
                }
        }
        async Task RestorePackagesAsync(
            IEnumerable <InteractivePackage> packages,
            SourceCacheContext cacheContext,
            CancellationToken cancellationToken)
        {
            var restoreContext = new RestoreArgs {
                CacheContext = cacheContext,
                Log          = Logger,
            };

            // NOTE: This path is typically empty. It could in theory contain nuget.config settings
            //       files, but really we just use it to satisfy nuget API that requires paths,
            //       even when they are never used.
            var rootPath      = packageConfigDirectory;
            var globalPath    = restoreContext.GetEffectiveGlobalPackagesFolder(rootPath, settings);
            var fallbackPaths = restoreContext.GetEffectiveFallbackPackageFolders(settings);

            var providerCache    = new RestoreCommandProvidersCache();
            var restoreProviders = providerCache.GetOrCreate(
                globalPath,
                fallbackPaths,
                SourceRepositories,
                cacheContext,
                Logger);

            // Set up a project spec similar to what you would see in a project.json.
            // This is sufficient for the dependency graph work done within RestoreCommand.
            // TODO: XF version pinning during restore?
            var targetFrameworkInformation = new TargetFrameworkInformation {
                FrameworkName = TargetFramework,
                Dependencies  = packages.Select(ToLibraryDependency).ToList(),
            };
            var projectSpec = new PackageSpec(new [] { targetFrameworkInformation })
            {
                Name     = project.Name,
                FilePath = rootPath,
            };

            var restoreRequest = new RestoreRequest(projectSpec, restoreProviders, cacheContext, Logger);
            var restoreCommand = new RestoreCommand(restoreRequest);
            var result         = await restoreCommand.ExecuteAsync(cancellationToken);

            if (!result.Success)
            {
                return;
            }

            project.ResetInstallationContext();

            // As with installation, restore simply ensures that packages are present in the user's
            // global package cache. We reference them out of there just like .NET core projects do.
            //
            // All resolved packages, including the explicit inputs and their dependencies, are
            // available as LockFileLibrary instances.
            foreach (var library in result.LockFile.Libraries)
            {
                project.InstallationContext.AddInstalledPackage(
                    GetInteractivePackageFromLibrary(library, project, packages));
            }

            installedPackages = project.InstallationContext.InstalledPackages;
            UpdateInstalledPackages();
        }