Beispiel #1
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);
        }
Beispiel #2
0
        public async Task <string[]> DownloadAsync(IPackageSearchMetadata packageIdentity, SourceRepository repository,
                                                   string downloadFolder, bool onlyDownload = false)
        {
            if (packageIdentity == null)
            {
                throw new ArgumentNullException(nameof(packageIdentity));
            }

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

            var dotNetFramework = Assembly
                                  .GetEntryAssembly()
                                  .GetCustomAttribute <TargetFrameworkAttribute>()?
                                  .FrameworkName;

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

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

            var project = new PluginFolderNugetProject(downloadFolder, packageIdentity, nuGetFramework, onlyDownload);

            var packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, downloadFolder)
            {
                PackagesFolderNuGetProject = project
            };

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

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

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

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

            await packageManager.InstallPackageAsync(
                project,
                packageIdentity.Identity,
                resolutionContext,
                projectContext,
                downloadContext,
                repository,
                new List <SourceRepository>(),
                CancellationToken.None);

            var versionFolder = Path.Combine(downloadFolder, packageIdentity.Identity.ToString());

            return(Directory.GetFiles(versionFolder, "*.*", SearchOption.AllDirectories));
        }