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);
            }
Exemple #2
0
        public void RequestFactory_RestorePackagesArgRelativeToCwd()
        {
            // If a packages argument is provided, GetEffectiveGlobalPackagesFolder() should ignore
            // the provided root path and any configuration information and resolve relative to the
            // current working directory.

            // Arrange
            var globalPackagesFolder = "MyPackages";
            var restoreArgs          = new RestoreArgs()
            {
                GlobalPackagesFolder = globalPackagesFolder
            };

            // Act
            var resolvedGlobalPackagesFolder = restoreArgs.GetEffectiveGlobalPackagesFolder("C:\\Dummy", null);

            // Assert
            var expectedResolvedGlobalPackagesFolder = Path.GetFullPath(globalPackagesFolder);

            Assert.Equal(expectedResolvedGlobalPackagesFolder, resolvedGlobalPackagesFolder);
        }
        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();
        }