Beispiel #1
0
        private static async Task <bool> RestorePackageAsync(NuGetPackageManager nuGetPackageManager,
                                                             PackageIdentity packageIdentity,
                                                             INuGetProjectContext nuGetProjectContext,
                                                             EventHandler <PackageRestoredEventArgs> packageRestoredEvent,
                                                             IEnumerable <SourceRepository> sourceRepositories,
                                                             CancellationToken token)
        {
            bool restored = await nuGetPackageManager.RestorePackageAsync(packageIdentity, nuGetProjectContext, sourceRepositories, token);

            // At this point, it is guaranteed that package restore did not fail
            if (packageRestoredEvent != null)
            {
                packageRestoredEvent(null, new PackageRestoredEventArgs(packageIdentity, restored));
            }

            return(restored);
        }
Beispiel #2
0
        public async Task <NugetDownloadResult> DownloadAsync(string packageFolder, string packageName, string packageVersion = null,
                                                              bool includePrerelease = false,
                                                              NuGetFeed packageFeed  = null, bool onlyDownload            = false, bool includeSecondaryRepositories = false, string targetFramework = null, string targetRid = null,
                                                              bool filterOurRefFiles = true, List <string> ignoredSources = null)
        {
            if (!Directory.Exists(packageFolder))
            {
                Directory.CreateDirectory(packageFolder);
            }

            var providers                = GetNugetResourceProviders();
            var settings                 = Settings.LoadDefaultSettings(packageFolder, null, new MachineWideSettings());
            var packageSourceProvider    = new PackageSourceProvider(settings);
            var sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, providers);

            IPackageSearchMetadata package    = null;
            SourceRepository       sourceRepo = null;

            if (!string.IsNullOrWhiteSpace(packageFeed?.Feed))
            {
                sourceRepo = GetSourceRepo(packageFeed, providers);

                package = await SearchPackageAsync(packageName, packageVersion, includePrerelease, sourceRepo);
            }
            else
            {
                foreach (var repo in sourceRepositoryProvider.GetRepositories())
                {
                    if (packageFeed != null && repo.PackageSource.Name != packageFeed.Name)
                    {
                        continue;
                    }

                    package = await SearchPackageAsync(packageName, packageVersion, includePrerelease, repo);

                    if (package != null)
                    {
                        sourceRepo = repo;

                        break;
                    }
                }
            }

            if (package == null)
            {
                throw new PackageNotFoundException($"Couldn't find package '{packageVersion}'.{packageVersion}.");
            }

            if (string.IsNullOrWhiteSpace(targetFramework))
            {
                targetFramework = Assembly
                                  .GetEntryAssembly()
                                  .GetCustomAttribute <TargetFrameworkAttribute>()?
                                  .FrameworkName;
            }

            var frameworkNameProvider = new FrameworkNameProvider(
                new[] { DefaultFrameworkMappings.Instance },
                new[] { DefaultPortableFrameworkMappings.Instance });

            var nuGetFramework = NuGetFramework.ParseFrameworkName(targetFramework, frameworkNameProvider);

            var project        = new PluginFolderNugetProject(packageFolder, package, nuGetFramework, onlyDownload, targetRid, filterOurRefFiles);
            var packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, packageFolder)
            {
                PackagesFolderNuGetProject = project
            };

            var clientPolicyContext = ClientPolicyContext.GetClientPolicy(settings, _logger);

            var projectContext = new FolderProjectContext(_logger)
            {
                PackageExtractionContext = new PackageExtractionContext(
                    PackageSaveMode.Defaultv3,
                    PackageExtractionBehavior.XmlDocFileSaveMode,
                    clientPolicyContext,
                    _logger)
            };

            var resolutionContext = new ResolutionContext(
                DependencyBehavior.Lowest,
                includePrerelease,
                includeUnlisted: false,
                VersionConstraints.None);

            var downloadContext = new PackageDownloadContext(
                resolutionContext.SourceCacheContext,
                packageFolder,
                resolutionContext.SourceCacheContext.DirectDownload);

            var secondaryRepos = sourceRepositoryProvider.GetRepositories();

            if (includeSecondaryRepositories == false)
            {
                secondaryRepos = new List <SourceRepository>();
            }

            if (ignoredSources?.Any() == true)
            {
                secondaryRepos = secondaryRepos.Where(x => ignoredSources.Contains(x.PackageSource.Name) == false).ToList();
            }

            await packageManager.InstallPackageAsync(
                project,
                package.Identity,
                resolutionContext,
                projectContext,
                downloadContext,
                sourceRepo,
                secondaryRepos,
                CancellationToken.None);

            await project.PostProcessAsync(projectContext, CancellationToken.None);

            await project.PreProcessAsync(projectContext, CancellationToken.None);

            await packageManager.RestorePackageAsync(package.Identity, projectContext, downloadContext, new[] { sourceRepo }, CancellationToken.None);

            var result = new NugetDownloadResult
            {
                Context = new NugetContext(nuGetFramework.ToString(), nuGetFramework.GetShortFolderName(), nuGetFramework.Version.ToString(), packageFolder,
                                           packageName, packageVersion, project.Rid, project.SupportedRids)
            };

            if (onlyDownload)
            {
                var versionFolder = Path.Combine(packageFolder, package.Identity.ToString());

                result.PackageAssemblyFiles = new List <string>(Directory.GetFiles(versionFolder, "*.*", SearchOption.AllDirectories));

                return(result);
            }

            result.InstalledDlls     = new List <DllInfo>(project.InstalledDlls);
            result.RunTimeDlls       = new List <RunTimeDll>(project.RuntimeDlls);
            result.InstalledPackages = new List <string>(project.InstalledPackages);

            var packageAssemblies = await project.GetPluginAssemblyFilesAsync();

            result.PackageAssemblyFiles = new List <string>(packageAssemblies);

            return(result);
        }