Esempio n. 1
0
        public DestFolder(string destinationFolder, IDefaultNuGetFramework defaultFramework)
        {
#if NETCORE
            var targetFramework = defaultFramework.GetHighest().FirstOrDefault();
            Framework          = targetFramework.DotNetFrameworkName;
            SupportedPlatforms = ImmutableList.Create(FrameworkParser.ToSpecificPlatform(targetFramework));
#else
            Framework = defaultFramework.GetLowest().FirstOrDefault()?.ToString();
#endif
            Log.Info($"Current target framework for plugins set as '{Framework}'");

            // Note: commented part for testing correct package resolving to 4.X versions
            //var tfm472 = (defaultFramework as DefaultNuGetFramework).GetFirst();
            //Framework = tfm472.ToString();
            // SupportedPlatforms = ImmutableList.Create(FrameworkParser.ToSpecificPlatform(tfm472));

            // Default initialization
            if (SupportedPlatforms is null)
            {
                SupportedPlatforms = ImmutableList.Create <NuGetFramework>();
            }

            ContentPath   = destinationFolder;
            _pathResolver = new PackagePathResolver(destinationFolder);
        }
        public ExampleProject(IFrameworkNameProvider frameworkNameProvider)
        {
            _pathResolver = TypeFactory.Default.CreateInstanceWithParametersAndAutoCompletion <ExamplePackagePathResolver>();

            var targetFramework = FrameworkParser.TryParseFrameworkName(Framework, frameworkNameProvider);

            SupportedPlatforms = ImmutableList.Create(FrameworkParser.ToSpecificPlatform(targetFramework));
        }
        /// <summary>
        /// Creates minimal required metadata for initializing NuGet PackagesConfigNuGetProject from
        /// our IExtensibleProject
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public NuGetProject GetProjectConfig(IExtensibleProject project)
        {
            NuGetProjectMetadata metadata = null;

            if (!_storedProjectMetadata.TryGetValue(project, out metadata))
            {
                metadata = new NuGetProjectMetadata();

                var targetFramework = FrameworkParser.TryParseFrameworkName(project.Framework, _frameworkNameProvider);

                metadata.Data.Add(MetadataTargetFramework, targetFramework);
                metadata.Data.Add(MetadataName, project.Name);

                _storedProjectMetadata.Add(project, metadata);
            }

            var packagesConfigProject = new PackagesConfigNuGetProject(project.ContentPath, metadata.Data);

            return(packagesConfigProject);
        }
Esempio n. 4
0
        public async Task UninstallAsync(PackageIdentity package, IExtensibleProject project, IEnumerable <PackageReference> installedPackageReferences,
                                         CancellationToken cancellationToken = default)
        {
            List <string> failedEntries = null;
            ICollection <PackageIdentity> uninstalledPackages;

            var targetFramework = FrameworkParser.TryParseFrameworkName(project.Framework, _frameworkNameProvider);

#if NET5_0_OR_GREATER
            var reducer      = new FrameworkReducer();
            var mostSpecific = reducer.ReduceUpwards(project.SupportedPlatforms).FirstOrDefault();
            targetFramework = mostSpecific;
#endif

            var projectConfig         = _nuGetProjectConfigurationProvider.GetProjectConfig(project);
            var uninstallationContext = new UninstallationContext(false, false);

            _nugetLogger.LogInformation($"Uninstall package {package}, Target framework: {targetFramework}");

            if (projectConfig is null)
            {
                _nugetLogger.LogWarning($"Project {project.Name} doesn't implement any configuration for own packages");
            }

            using (var cacheContext = new SourceCacheContext()
            {
                NoCache = false,
                DirectDownload = false,
            })
            {
                var dependencyInfoResource = await project.AsSourceRepository(_sourceRepositoryProvider)
                                             .GetResourceAsync <DependencyInfoResource>(cancellationToken);

                var dependencyInfoResourceCollection = new DependencyInfoResourceCollection(dependencyInfoResource);

                var resolverContext = await ResolveDependenciesAsync(package, targetFramework, PackageIdentity.Comparer, dependencyInfoResourceCollection, cacheContext, project, true, cancellationToken);

                var packageReferences = installedPackageReferences.ToList();

                if (uninstallationContext.RemoveDependencies)
                {
                    uninstalledPackages = await GetPackagesCanBeUninstalledAsync(resolverContext.AvailablePackages, packageReferences.Select(x => x.PackageIdentity));
                }
                else
                {
                    uninstalledPackages = new List <PackageIdentity>()
                    {
                        package
                    };
                }
            }

            try
            {
                foreach (var removedPackage in uninstalledPackages)
                {
                    if (removedPackage.Version is null)
                    {
                        _nugetLogger.LogWarning($"Skip package {removedPackage.Id} uninstall. Check your package.config for references of this packages");
                        continue;
                    }

                    var folderProject = new FolderNuGetProject(project.ContentPath);

                    if (folderProject.PackageExists(removedPackage))
                    {
                        _directoryService.ForceDeleteDirectory(_fileService, folderProject.GetInstalledPath(removedPackage), out failedEntries);
                    }

                    if (projectConfig is null)
                    {
                        continue;
                    }

                    var result = await projectConfig.UninstallPackageAsync(removedPackage, _nuGetProjectContextProvider.GetProjectContext(FileConflictAction.PromptUser), cancellationToken);

                    if (!result)
                    {
                        _nugetLogger.LogError($"Saving package configuration failed in project {project} when installing package {package}");
                    }
                }
            }
            catch (IOException ex)
            {
                Log.Error(ex);
                _nugetLogger.LogError("Package files cannot be complete deleted by unexpected error (may be directory in use by another process?");
            }
            finally
            {
                LogHelper.LogUnclearedPaths(failedEntries, Log);
            }
        }
Esempio n. 5
0
        public async Task <InstallerResult> InstallAsync(
            PackageIdentity package,
            IExtensibleProject project,
            IReadOnlyList <SourceRepository> repositories,
            bool ignoreMissingPackages          = false,
            CancellationToken cancellationToken = default)
        {
            try
            {
                // Step 1. Decide what framework version used on package resolving
                // Enforce platform-specific framework for .NET 5.0

                var targetFramework = FrameworkParser.TryParseFrameworkName(project.Framework, _frameworkNameProvider);
                var reducer         = new FrameworkReducer();

#if NET5_0_OR_GREATER
                var mostSpecific = reducer.ReduceUpwards(project.SupportedPlatforms).FirstOrDefault();
                targetFramework = mostSpecific;
#endif

                _nugetLogger.LogInformation($"Installing package {package}, Target framework: {targetFramework}");

                // Prepare to step 2. Add globals if cache enabled as available repository with highest priority.
                // Note: This part falls under responsibility of RepositoryContextService but the same logic used to determine what packages are found by IPackageLoaderService
                // To not break behavior for now add here
                if (!project.NoCache)
                {
                    var repositoryList = repositories.ToList();
                    repositoryList.Insert(0, new SourceRepository(new PackageSource(DefaultNuGetFolders.GetGlobalPackagesFolder(), ".nuget"), Repository.Provider.GetCoreV3()));
                    repositories = repositoryList;
                }

                // Step 2. Build list of dependencies and determine DependencyBehavior if some packages are misssed in current feed
                Resolver.PackageResolverContext resolverContext = null;

                using (var cacheContext = new SourceCacheContext())
                {
#pragma warning disable IDISP013 // Await in using.
                    var getDependencyResourcesTasks = repositories.Select(repo => repo.GetResourceAsync <DependencyInfoResource>());
#pragma warning restore IDISP013 // Await in using.

                    var dependencyResources = (await getDependencyResourcesTasks.WhenAllOrExceptionAsync()).Where(x => x.IsSuccess && x.Result is not null)
                                              .Select(x => x.Result).ToArray();

                    var dependencyInfoResources = new DependencyInfoResourceCollection(dependencyResources);

                    resolverContext = await ResolveDependenciesAsync(package, targetFramework, PackageIdentityComparer.Default, dependencyInfoResources, cacheContext, project, ignoreMissingPackages, cancellationToken);

                    if (resolverContext is null ||
                        !(resolverContext?.AvailablePackages?.Any() ?? false))
                    {
                        var errorMessage = $"Package {package} cannot be resolved with current settings (TFM: {targetFramework}) for chosen destination";
                        _nugetLogger.LogWarning(errorMessage);
                        return(new InstallerResult(errorMessage));
                    }

                    // Step 3. Try to check is main package can be downloaded from resource
                    var mainPackageInfo = resolverContext.AvailablePackages.FirstOrDefault(p => p.Id == package.Id);

                    _nugetLogger.LogInformation($"Downloading {package}...");
                    var mainDownloadedFiles = await DownloadPackageResourceAsync(mainPackageInfo, cacheContext, cancellationToken);

                    _nugetLogger.LogInformation($"{package} download completed");

                    if (!mainDownloadedFiles.IsAvailable())
                    {
                        // Downlod failed by some reasons (probably connection issue or package goes deleted before feed updated)
                        var errorMessage = $"Current source lists package {package} but attempts to download it have failed. The source in invalid or required packages were removed while the current operation was in progress";
                        _nugetLogger.LogError(errorMessage);
                        return(new InstallerResult(errorMessage));
                    }

                    // Step 4. Check is main package compatible with target Framework
                    var canBeInstalled = await CheckCanBeInstalledAsync(project, mainDownloadedFiles.PackageReader, targetFramework, cancellationToken);

                    if (!canBeInstalled)
                    {
                        throw new IncompatiblePackageException($"Package {package} incompatible with project target platform {targetFramework}");
                    }

                    // Step 5. Build install list using NuGet Resolver and select available resources.
                    // Track packages which already installed and make sure only one version of package exists
                    var resolver = new Resolver.PackageResolver();
                    var availablePackagesToInstall = await resolver.ResolveWithVersionOverrideAsync(resolverContext, project, DependencyBehavior.Highest,
                                                                                                    (project, conflict) => _fileSystemService.CreateDeleteme(conflict.PackageIdentity.Id, project.GetInstallPath(conflict.PackageIdentity)),
                                                                                                    cancellationToken);

                    // Step 6. Download everything except main package and extract all
                    availablePackagesToInstall.Remove(mainPackageInfo);
                    _nugetLogger.LogInformation($"Downloading package dependencies...");
                    var downloadResults = await DownloadPackagesResourcesAsync(availablePackagesToInstall, cacheContext, cancellationToken);

                    downloadResults[mainPackageInfo] = mainDownloadedFiles;
                    _nugetLogger.LogInformation($"{downloadResults.Count - 1} dependencies downloaded");
                    var extractionContext = GetExtractionContext();
                    await ExtractPackagesResourcesAsync(downloadResults, project, extractionContext, cancellationToken);
                    await CheckLibAndFrameworkItemsAsync(downloadResults, targetFramework, cancellationToken);

                    return(new InstallerResult(downloadResults));
                }
            }
            catch (NuGetResolverInputException ex)
            {
                throw new IncompatiblePackageException($"Package {package} or some of it dependencies are missed for current target framework", ex);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }