Пример #1
0
        /// <summary>
        /// Execute the installs/uninstalls
        /// </summary>
        private async Task ExecuteActionsAsync(
            IEnumerable <ResolvedAction> actions,
            INuGetProjectContext projectContext,
            ICommonOperations commonOperations,
            UserAction userAction,
            SourceCacheContext sourceCacheContext,
            CancellationToken token)
        {
            var nuGetProjects = actions.Select(action => action.Project);
            var nuGetActions  = actions.Select(action => action.Action);

            var directInstall = GetDirectInstall(nuGetActions, userAction, commonOperations);

            if (directInstall != null)
            {
                NuGetPackageManager.SetDirectInstall(directInstall, projectContext);
            }

            await _packageManager.ExecuteNuGetProjectActionsAsync(
                nuGetProjects,
                nuGetActions,
                projectContext,
                sourceCacheContext,
                token);

            NuGetPackageManager.ClearDirectInstall(projectContext);
        }
Пример #2
0
        /// <summary>
        /// Execute the installs/uninstalls
        /// </summary>
        private async Task ExecuteActionsAsync(
            IEnumerable <ResolvedAction> actions,
            INuGetProjectContext projectContext,
            ICommonOperations commonOperations,
            UserAction userAction,
            SourceCacheContext sourceCacheContext,
            CancellationToken token)
        {
            var processedDirectInstalls = new HashSet <PackageIdentity>(PackageIdentity.Comparer);

            foreach (var projectActions in actions.GroupBy(e => e.Project))
            {
                var nuGetProjectActions = projectActions.Select(e => e.Action);
                var directInstall       = GetDirectInstall(nuGetProjectActions, userAction, commonOperations);
                if (directInstall != null &&
                    !processedDirectInstalls.Contains(directInstall))
                {
                    NuGetPackageManager.SetDirectInstall(directInstall, projectContext);
                    processedDirectInstalls.Add(directInstall);
                }
                await _packageManager.ExecuteNuGetProjectActionsAsync(projectActions.Key, nuGetProjectActions, projectContext, sourceCacheContext, token);

                NuGetPackageManager.ClearDirectInstall(projectContext);
            }
        }
        /// <summary>
        /// Install package by Identity
        /// </summary>
        /// <param name="project"></param>
        /// <param name="identity"></param>
        /// <param name="resolutionContext"></param>
        /// <param name="projectContext"></param>
        /// <param name="isPreview"></param>
        /// <param name="isForce"></param>
        /// <param name="uninstallContext"></param>
        /// <returns></returns>
        protected async Task InstallPackageByIdentityAsync(NuGetProject project, PackageIdentity identity, ResolutionContext resolutionContext, INuGetProjectContext projectContext, bool isPreview)
        {
            try
            {
                var actions = await PackageManager.PreviewInstallPackageAsync(project, identity, resolutionContext, projectContext, PrimarySourceRepositories, null, CancellationToken.None);

                if (!actions.Any())
                {
                    // nuget operation status is set to NoOp to log under telemetry event
                    _status = NuGetOperationStatus.NoOp;
                }
                else
                {
                    // update packages count to be logged under telemetry event
                    _packageCount = actions.Select(action => action.PackageIdentity.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count();
                }

                // stop telemetry event timer to avoid UI interaction
                TelemetryServiceUtility.StopTimer();

                if (!ShouldContinueDueToDotnetDeprecation(actions, isPreview))
                {
                    // resume telemetry event timer after ui confirmation
                    TelemetryServiceUtility.StartOrResumeTimer();
                    return;
                }

                // resume telemetry event timer after ui confirmation
                TelemetryServiceUtility.StartOrResumeTimer();

                if (isPreview)
                {
                    PreviewNuGetPackageActions(actions);
                }
                else
                {
                    NuGetPackageManager.SetDirectInstall(identity, projectContext);
                    await PackageManager.ExecuteNuGetProjectActionsAsync(project, actions, this, resolutionContext.SourceCacheContext, CancellationToken.None);

                    NuGetPackageManager.ClearDirectInstall(projectContext);

                    // Refresh Manager UI if needed
                    RefreshUI(actions);
                }
            }
            catch (InvalidOperationException ex)
            {
                if (ex.InnerException is PackageAlreadyInstalledException)
                {
                    // Set nuget operation status to NoOp for telemetry event when package
                    // is already installed.
                    _status = NuGetOperationStatus.NoOp;
                    Log(MessageLevel.Info, ex.Message);
                }
                else
                {
                    throw ex;
                }
            }
        }
        /// <summary>
        /// Install package by Id
        /// </summary>
        /// <param name="project"></param>
        /// <param name="packageId"></param>
        /// <param name="resolutionContext"></param>
        /// <param name="projectContext"></param>
        /// <param name="isPreview"></param>
        /// <param name="isForce"></param>
        /// <param name="uninstallContext"></param>
        /// <returns></returns>
        protected async Task InstallPackageByIdAsync(NuGetProject project, string packageId, ResolutionContext resolutionContext, INuGetProjectContext projectContext, bool isPreview)
        {
            try
            {
                var actions = await PackageManager.PreviewInstallPackageAsync(project, packageId, resolutionContext, projectContext, PrimarySourceRepositories, null, CancellationToken.None);

                if (isPreview)
                {
                    PreviewNuGetPackageActions(actions);
                }
                else
                {
                    var identity = actions.Select(v => v.PackageIdentity).Where(p => p.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                    NuGetPackageManager.SetDirectInstall(identity, projectContext);
                    await PackageManager.ExecuteNuGetProjectActionsAsync(project, actions, this, CancellationToken.None);

                    NuGetPackageManager.ClearDirectInstall(projectContext);
                }
            }
            catch (InvalidOperationException ex)
            {
                if (ex.InnerException is PackageAlreadyInstalledException)
                {
                    Log(ProjectManagement.MessageLevel.Info, ex.Message);
                }
                else
                {
                    throw ex;
                }
            }
        }
        /// <summary>
        /// Install package by Identity
        /// </summary>
        /// <param name="project"></param>
        /// <param name="identity"></param>
        /// <param name="resolutionContext"></param>
        /// <param name="projectContext"></param>
        /// <param name="isPreview"></param>
        /// <param name="isForce"></param>
        /// <param name="uninstallContext"></param>
        /// <returns></returns>
        protected async Task InstallPackageByIdentityAsync(NuGetProject project, PackageIdentity identity, ResolutionContext resolutionContext, INuGetProjectContext projectContext, bool isPreview)
        {
            try
            {
                var actions = await PackageManager.PreviewInstallPackageAsync(project, identity, resolutionContext, projectContext, PrimarySourceRepositories, null, CancellationToken.None);

                if (isPreview)
                {
                    PreviewNuGetPackageActions(actions);
                }
                else
                {
                    NuGetPackageManager.SetDirectInstall(identity, projectContext);
                    await PackageManager.ExecuteNuGetProjectActionsAsync(project, actions, this, CancellationToken.None);

                    NuGetPackageManager.ClearDirectInstall(projectContext);
                }
            }
            catch (InvalidOperationException ex)
            {
                if (ex.InnerException is PackageAlreadyInstalledException)
                {
                    Log(ProjectManagement.MessageLevel.Info, ex.Message);
                }
                else
                {
                    throw ex;
                }
            }
        }
Пример #6
0
        protected async Task InstallPackageByIdAsync(
            NuGetProject project,
            string packageId,
            ResolutionContext resolutionContext,
            INuGetProjectContext projectContext,
            bool isPreview)
        {
            try {
                var packageManager = ConsoleHost.CreatePackageManager();

                var latestVersion = await packageManager.GetLatestVersionAsync(
                    packageId,
                    project,
                    resolutionContext,
                    PrimarySourceRepositories,
                    new LoggerAdapter (projectContext),
                    ConsoleHost.Token);

                if (latestVersion == null)
                {
                    throw new InvalidOperationException(GettextCatalog.GetString("Unable to find package '{0}", packageId));
                }

                var identity = new PackageIdentity(Id, latestVersion.LatestVersion);

                var actions = await packageManager.PreviewInstallPackageAsync(
                    project,
                    identity,
                    resolutionContext,
                    projectContext,
                    PrimarySourceRepositories,
                    null,
                    CancellationToken.None);

                if (isPreview)
                {
                    PreviewNuGetPackageActions(actions);
                }
                else
                {
                    NuGetPackageManager.SetDirectInstall(identity, projectContext);
                    await packageManager.ExecuteNuGetProjectActionsAsync(project, actions, this, ConsoleHost.Token);

                    NuGetPackageManager.ClearDirectInstall(projectContext);
                }
            } catch (InvalidOperationException ex) {
                if (ex.InnerException is PackageAlreadyInstalledException)
                {
                    Log(ProjectManagement.MessageLevel.Info, ex.Message);
                }
                else
                {
                    throw ex;
                }
            }
        }
Пример #7
0
        public async ValueTask ExecuteActionsAsync(IReadOnlyList <ProjectAction> actions, CancellationToken cancellationToken)
        {
            Assumes.NotNullOrEmpty(actions);

            cancellationToken.ThrowIfCancellationRequested();

            await CatchAndRethrowExceptionAsync(async() =>
            {
                INuGetProjectContext?projectContext = null;

                try
                {
                    projectContext = await ServiceLocator.GetComponentModelServiceAsync <INuGetProjectContext>();

                    Assumes.NotNull(projectContext);

                    if (IsDirectInstall(actions))
                    {
                        NuGetPackageManager.SetDirectInstall(_state.PackageIdentity, projectContext);
                    }

                    var nugetProjectActions = new List <NuGetProjectAction>();

                    foreach (ProjectAction action in actions)
                    {
                        if (_state.ResolvedActions.TryGetValue(action.Id, out ResolvedAction resolvedAction))
                        {
                            nugetProjectActions.Add(resolvedAction.Action);
                        }
                    }

                    Assumes.NotNullOrEmpty(nugetProjectActions);

                    NuGetPackageManager packageManager  = await _sharedState.GetPackageManagerAsync(cancellationToken);
                    IEnumerable <NuGetProject> projects = nugetProjectActions.Select(action => action.Project);

                    await packageManager.ExecuteNuGetProjectActionsAsync(
                        projects,
                        nugetProjectActions,
                        projectContext,
                        _state.SourceCacheContext,
                        cancellationToken);
                }
                finally
                {
                    if (projectContext != null)
                    {
                        NuGetPackageManager.ClearDirectInstall(projectContext);
                    }
                }
            });
        }
Пример #8
0
        /// <summary>
        /// Execute the installs/uninstalls
        /// </summary>
        /// <param name="actions"></param>
        /// <param name="projectContext"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        protected async Task ExecuteActions(IEnumerable <Tuple <NuGetProject, NuGetProjectAction> > actions,
                                            NuGetUIProjectContext projectContext, UserAction userAction, CancellationToken token)
        {
            HashSet <PackageIdentity> processedDirectInstalls = new HashSet <PackageIdentity>(PackageIdentity.Comparer);

            foreach (var projectActions in actions.GroupBy(e => e.Item1))
            {
                var nuGetProjectActions = projectActions.Select(e => e.Item2);
                var directInstall       = GetDirectInstall(nuGetProjectActions, userAction, projectContext.CommonOperations);
                if (directInstall != null && !processedDirectInstalls.Contains(directInstall))
                {
                    NuGetPackageManager.SetDirectInstall(directInstall, projectContext);
                    processedDirectInstalls.Add(directInstall);
                }
                await _packageManager.ExecuteNuGetProjectActionsAsync(projectActions.Key, nuGetProjectActions, projectContext, token);

                NuGetPackageManager.ClearDirectInstall(projectContext);
            }
        }
Пример #9
0
        private async Task ExecuteActionsAsync(IEnumerable <ResolvedAction> actions,
                                               PackageIdentity packageIdentity,
                                               INuGetProjectContext projectContext,
                                               SourceCacheContext sourceCacheContext,
                                               CancellationToken token)
        {
            NuGetProject[] nuGetProjects = actions.Select(action => action.Project).Distinct().ToArray();

            IEnumerable <NuGetProjectAction> nuGetActions = actions.Select(action => action.Action);

            if (packageIdentity != null)
            {
                NuGetPackageManager.SetDirectInstall(packageIdentity, projectContext);
            }

            await _packageManager.ExecuteNuGetProjectActionsAsync(nuGetProjects, nuGetActions, projectContext, sourceCacheContext, token);

            NuGetPackageManager.ClearDirectInstall(projectContext);
        }
        public async Task InstallPackageAsync(CancellationToken token)
        {
            using (var sourceCacheContext = new SourceCacheContext()) {
                var actions = await PreviewInstallPackage(sourceCacheContext, token);

                if (!actions.Any())
                {
                    return;
                }

                NuGetPackageManager.SetDirectInstall(identity, projectContext);

                await packageManager.ExecuteNuGetProjectActionsAsync(
                    nugetProject,
                    actions,
                    projectContext,
                    sourceCacheContext,
                    token);

                NuGetPackageManager.ClearDirectInstall(projectContext);
            }
        }
 public void SetDirectInstall(PackageIdentity directInstall, INuGetProjectContext nuGetProjectContext)
 {
     NuGetPackageManager.SetDirectInstall(directInstall, nuGetProjectContext);
 }
Пример #12
0
        /// <summary>
        /// Fetch, if not already downloaded, and install the package represented by
        /// (<paramref name="packageId"/>, <paramref name="version"/>).
        /// </summary>
        /// <remarks>It is safe to call it concurrently be cause we operations are done using the FileLock.</remarks>
        /// <param name="packageId">Name of package to install.</param>
        /// <param name="version">Version of package to install.</param>
        public async Task <NugetLocalPackage> InstallPackage(string packageId, PackageVersion version, ProgressReport progress)
        {
            // Xenko 2.x still installs in GamePackages
            var currentManager = IsPackageV2(packageId, version) ? managerV2 : manager;

            using (GetLocalRepositoryLock())
            {
                currentProgressReport = progress;
                try
                {
                    var identity = new PackageIdentity(packageId, version.ToNuGetVersion());

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

                    var repositories = PackageSources.Select(sourceRepositoryProvider.CreateRepository).ToArray();

                    var projectContext = new EmptyNuGetProjectContext()
                    {
                        ActionType = NuGetActionType.Install,
                        PackageExtractionContext = new PackageExtractionContext(NativeLogger),
                    };

                    ActivityCorrelationId.StartNew();

                    {
                        // Equivalent to:
                        //   await manager.InstallPackageAsync(manager.PackagesFolderNuGetProject,
                        //       identity, resolutionContext, projectContext, repositories,
                        //       Array.Empty<SourceRepository>(),  // This is a list of secondary source respositories, probably empty
                        //       CancellationToken.None);
                        using (var sourceCacheContext = new SourceCacheContext())
                        {
                            var nuGetProject        = currentManager.PackagesFolderNuGetProject;
                            var packageIdentity     = identity;
                            var nuGetProjectContext = projectContext;
                            var primarySources      = repositories;
                            var secondarySources    = Array.Empty <SourceRepository>();
                            var token           = CancellationToken.None;
                            var downloadContext = new PackageDownloadContext(sourceCacheContext);

                            // Step-1 : Call PreviewInstallPackageAsync to get all the nuGetProjectActions
                            var nuGetProjectActions = await currentManager.PreviewInstallPackageAsync(nuGetProject, packageIdentity, resolutionContext,
                                                                                                      nuGetProjectContext, primarySources, secondarySources, token);

                            // Notify that installations started.
                            foreach (var operation in nuGetProjectActions)
                            {
                                if (operation.NuGetProjectActionType == NuGetProjectActionType.Install)
                                {
                                    var installPath = GetInstalledPath(operation.PackageIdentity.Id, operation.PackageIdentity.Version.ToPackageVersion());
                                    OnPackageInstalling(this, new PackageOperationEventArgs(new PackageName(operation.PackageIdentity.Id, operation.PackageIdentity.Version.ToPackageVersion()), installPath));
                                }
                            }

                            NuGetPackageManager.SetDirectInstall(packageIdentity, nuGetProjectContext);

                            // Step-2 : Execute all the nuGetProjectActions
                            if (IsPackageV2(packageId, version))
                            {
                                await currentManager.ExecuteNuGetProjectActionsAsync(
                                    nuGetProject,
                                    nuGetProjectActions,
                                    nuGetProjectContext,
                                    downloadContext,
                                    token);
                            }
                            else
                            {
                                // Download and install package in the global cache (can't use NuGetPackageManager anymore since it is designed for V2)
                                foreach (var operation in nuGetProjectActions)
                                {
                                    if (operation.NuGetProjectActionType == NuGetProjectActionType.Install)
                                    {
                                        using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(primarySources, packageIdentity, downloadContext, InstallPath, NativeLogger, token))
                                        {
                                            if (downloadResult.Status != DownloadResourceResultStatus.Available)
                                            {
                                                throw new InvalidOperationException($"Could not download package {packageIdentity}");
                                            }

                                            using (var installResult = await GlobalPackagesFolderUtility.AddPackageAsync(packageIdentity, downloadResult.PackageStream, InstallPath, NativeLogger, token))
                                            {
                                                if (installResult.Status != DownloadResourceResultStatus.Available)
                                                {
                                                    throw new InvalidOperationException($"Could not install package {packageIdentity}");
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            NuGetPackageManager.ClearDirectInstall(nuGetProjectContext);

                            // Notify that installations completed.
                            foreach (var operation in nuGetProjectActions)
                            {
                                if (operation.NuGetProjectActionType == NuGetProjectActionType.Install)
                                {
                                    var installPath = GetInstalledPath(operation.PackageIdentity.Id, operation.PackageIdentity.Version.ToPackageVersion());
                                    OnPackageInstalled(this, new PackageOperationEventArgs(new PackageName(operation.PackageIdentity.Id, operation.PackageIdentity.Version.ToPackageVersion()), installPath));
                                }
                            }
                        }
                    }

                    // Load the recently installed package
                    var installedPackages = GetPackagesInstalled(new[] { packageId });
                    return(installedPackages.FirstOrDefault(p => p.Version == version));
                }
                finally
                {
                    currentProgressReport = null;
                }
            }
        }