Example #1
0
        public async Task InstallPackageAsync(IPackageDetails package, bool allowedPrerelease = false, CancellationToken token = default)
        {
            Argument.IsNotNull(() => package);

            var installedIdentity = package.GetIdentity();
            var operationPath     = _defaultProject.GetInstallPath(installedIdentity);

            try
            {
                ValidatePackage(package);

                //repositories retrieved inside package manager now
                //todo use PackageOperationContextService instead on repositoryContextService

                //here was used a flag 'ignoreDependencies = false' and 'ignoreWalkInfo = false' in old code

                _packageOperationNotificationService.NotifyOperationStarting(operationPath, PackageOperationType.Install, package);
                await _nuGetPackageManager.InstallPackageForProjectAsync(_defaultProject, package.GetIdentity(), token);
            }
            catch (Exception ex)
            {
                await _logger.LogAsync(LogLevel.Error, ex.Message);

                _packageOperationContextService.CurrentContext.Exceptions.Add(ex);
            }
            finally
            {
                FinishOperation(PackageOperationType.Install, operationPath, package);
            }
        }
        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);