Exemplo n.º 1
0
        private async void OnDeletemeMessageAsync(PackagingDeletemeMessage message)
        {
            if (message.Data.OperationType == PackageOperationType.Uninstall)
            {
                if (!_directoryService.Exists(message.Data.OperationPath))
                {
                    return;
                }

                _fileSystemService.CreateDeleteme(message.Data.Package.Id, message.Data.OperationPath);
            }

            if (message.Data.OperationType == PackageOperationType.Install)
            {
                _fileSystemService.RemoveDeleteme(message.Data.Package.Id, message.Data.OperationPath);

                //check is folder broken installation or not
                //this handle cases where we perform installation of version, previously not correctly removed
                if (await _nuGetPackageManager.IsPackageInstalledAsync(_defaultProject, message.Data.Package.GetIdentity(), default))
                {
                    return;
                }

                _fileSystemService.CreateDeleteme(message.Data.Package.Id, message.Data.OperationPath);
            }
        }
        public async Task <bool> RunAsync()
        {
            var folderProject = new FolderNuGetProject(_defaultProject.ContentPath);

            if (!_directoryService.Exists(_defaultProject.ContentPath))
            {
                Log.Info($"Plugins folder does not exist");
                return(false);
            }

            var subFolders = folderProject.GetPackageDirectories();

            var failedIdentities = new List <PackageIdentity>();
            var parsedPackages   = new List <IPackageDetails>();

            bool anyUpgraded = false;

            using (var context = AcquireSourceContextForActions())
            {
                if (context == SourceContext.EmptyContext)
                {
                    Log.Warning($"Source context is empty");

                    return(false);
                }

                foreach (var folder in subFolders)
                {
                    var packageFolderName = Path.GetFileName(folder);
                    var package           = PackageIdentityParser.Parse(packageFolderName);

                    var packageDetails = PackageDetailsFactory.Create(package);

                    parsedPackages.Add(packageDetails);
                }

                //no valid package folders
                if (!parsedPackages.Any())
                {
                    return(false);
                }

                _packageOperationNotificationService.NotifyAutomaticOperationBatchStarting(PackageOperationType.Install, parsedPackages.ToArray());

                foreach (var packageDetails in parsedPackages)
                {
                    var package = packageDetails.GetIdentity();
                    if (package is null)
                    {
                        continue;
                    }

                    var installationPath = _defaultProject.GetInstallPath(package);

                    var isV2packageInstalled = folderProject.PackageExists(package, NuGet.Packaging.PackageSaveMode.Defaultv2);
                    if (!isV2packageInstalled)
                    {
                        Log.Warning($"Package '{package}' is recognized in project folder as v2 NuGet installed package");
                        continue;
                    }

                    if (await _nuGetPackageManager.IsPackageInstalledAsync(_defaultProject, package, default))
                    {
                        Log.Info($"Skipping package '{package}', package is valid");
                        continue;
                    }

                    _packageOperationNotificationService.NotifyAutomaticOperationStarting(installationPath, PackageOperationType.Install, packageDetails);

                    //reinstall
                    try
                    {
                        var isInstalled = await _nuGetPackageManager.InstallPackageForProjectAsync(_defaultProject, package, default, false);
        private async Task BatchUpdateExecuteAsync()
        {
            try
            {
                _progressManager.ShowBar(this);

                var batchedPackages = _parentManagerPage.PackageItems.Where(x => x.IsChecked).ToList();

                if (batchedPackages.Any(x => x.ValidationContext.HasErrors))
                {
                    await _messageService.ShowErrorAsync("Can't perform update. One or multiple package cannot be updated due to validation errors", "Can't update packages");

                    return;
                }

                var projects = _projectLocator.GetAllExtensibleProjects()
                               .Where(x => _projectLocator.IsEnabled(x)).ToList();

                using (var cts = new CancellationTokenSource())
                {
                    var updatePackageList = new List <IPackageDetails>();

                    foreach (var package in batchedPackages)
                    {
                        var targetProjects = new List <IExtensibleProject>();

                        foreach (var project in projects)
                        {
                            if (!await _projectManager.IsPackageInstalledAsync(project, package.Identity, cts.Token))
                            {
                                targetProjects.Add(project);
                            }
                        }

                        var targetVersion = (await package.LoadVersionsAsync() ?? package.Versions)?.FirstOrDefault();

                        if (targetVersion is null)
                        {
                            Log.Warning("Cannot perform upgrade because of 'Target version' is null");
                            return;
                        }


                        var updatePackageDetails = PackageDetailsFactory.Create(PackageOperationType.Update, package.GetMetadata(), targetVersion, null);
                        updatePackageList.Add(updatePackageDetails);
                    }

                    using (var operationContext = _packageOperationContextService.UseOperationContext(PackageOperationType.Update, updatePackageList.ToArray()))
                    {
                        foreach (var updatePackageDetails in updatePackageList)
                        {
                            await _packageCommandService.ExecuteUpdateAsync(updatePackageDetails, operationContext, cts.Token);
                        }
                    }
                }

                await Task.Delay(200);
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error when updating package");
            }
            finally
            {
                _progressManager.HideBar(this);

                _parentManagerPage.StartLoadingTimerOrInvalidateData();
            }
        }