Exemple #1
0
        public async Task <IEnumerable <IFeatureInfo> > EnableFeaturesAsync(ShellDescriptor shellDescriptor, IEnumerable <IFeatureInfo> features, bool force)
        {
            var featuresToEnable = features
                                   .SelectMany(feature => GetFeaturesToEnable(feature, force))
                                   .Distinct()
                                   .ToList();

            if (featuresToEnable.Count > 0)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    foreach (var feature in featuresToEnable)
                    {
                        _logger.LogInformation("Enabling feature '{FeatureName}'", feature.Id);
                    }
                }

                var enabledFeatures = await _extensionManager
                                      .LoadFeaturesAsync(shellDescriptor.Features.Select(x => x.Id).ToArray());

                shellDescriptor.Features = enabledFeatures
                                           .Select(x => x.FeatureInfo)
                                           .Concat(featuresToEnable)
                                           .Distinct()
                                           .Select(x => new ShellFeature(x.Id))
                                           .ToList();

                await _shellDescriptorManager.UpdateShellDescriptorAsync(
                    shellDescriptor.SerialNumber,
                    shellDescriptor.Features,
                    shellDescriptor.Parameters);
            }

            return(featuresToEnable);
        }
Exemple #2
0
        /// <summary>
        /// Enables a list of features.
        /// </summary>
        /// <param name="featureIds">The IDs for the features to be enabled.</param>
        /// <param name="force">Boolean parameter indicating if the feature should enable it's dependencies if required or fail otherwise.</param>
        public async Task <IEnumerable <string> > EnableFeaturesAsync(IEnumerable <string> featureIds, bool force)
        {
            ShellDescriptor shellDescriptor = await _shellDescriptorManager.GetShellDescriptorAsync();

            List <ShellFeature> enabledFeatures = shellDescriptor.Features.ToList();

            IDictionary <FeatureDescriptor, bool> availableFeatures = (await GetAvailableFeaturesAsync())
                                                                      .ToDictionary(featureDescriptor => featureDescriptor,
                                                                                    featureDescriptor => enabledFeatures.FirstOrDefault(shellFeature => shellFeature.Name == featureDescriptor.Id) != null);

            IEnumerable <string> featuresToEnable = featureIds
                                                    .Select(featureId => EnableFeature(featureId, availableFeatures, force)).ToList()
                                                    .SelectMany(ies => ies.Select(s => s));

            if (featuresToEnable.Any())
            {
                foreach (string featureId in featuresToEnable)
                {
                    enabledFeatures.Add(new ShellFeature {
                        Name = featureId
                    });
                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation("{0} was enabled", featureId);
                    }
                }

                await _shellDescriptorManager.UpdateShellDescriptorAsync(
                    shellDescriptor.SerialNumber,
                    enabledFeatures,
                    shellDescriptor.Parameters);
            }

            return(featuresToEnable);
        }
Exemple #3
0
        public async Task <IEnumerable <IFeatureInfo> > EnableFeaturesAsync(ShellDescriptor shellDescriptor, IEnumerable <IFeatureInfo> features, bool force)
        {
            var extensions      = _extensionManager.GetExtensions();
            var enabledFeatures = _extensionManager.GetEnabledFeatures(shellDescriptor).ToList();

            IDictionary <IFeatureInfo, bool> availableFeatures = extensions
                                                                 .Features
                                                                 .ToDictionary(featureDescriptor => featureDescriptor,
                                                                               featureDescriptor => enabledFeatures.Any(shellFeature => shellFeature.Id == featureDescriptor.Id));

            var featuresToEnable = features
                                   .Select(feature => EnableFeature(feature, availableFeatures, false))
                                   .SelectMany(ies => ies)
                                   .Distinct()
                                   .ToList();

            if (featuresToEnable.Count > 0)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Enabling features {0}", string.Join(",", featuresToEnable.Select(x => x.Id)));
                }

                shellDescriptor.Features = enabledFeatures.Concat(featuresToEnable).Select(x => new ShellFeature(x.Id)).ToList();

                await _shellDescriptorManager.UpdateShellDescriptorAsync(
                    shellDescriptor.SerialNumber,
                    shellDescriptor.Features,
                    shellDescriptor.Parameters);
            }

            return(featuresToEnable);
        }
        public async Task <(IEnumerable <IFeatureInfo>, IEnumerable <IFeatureInfo>)> UpdateFeaturesAsync(ShellDescriptor shellDescriptor,
                                                                                                         IEnumerable <IFeatureInfo> featuresToDisable, IEnumerable <IFeatureInfo> featuresToEnable, bool force)
        {
            var alwaysEnabledIds = _alwaysEnabledFeatures.Select(sf => sf.Id).ToArray();

            var enabledFeatures = _extensionManager.GetFeatures().Where(f =>
                                                                        shellDescriptor.Features.Any(sf => sf.Id == f.Id)).ToList();

            var enabledFeatureIds = enabledFeatures.Select(f => f.Id).ToArray();

            var AllFeaturesToDisable = featuresToDisable
                                       .Where(f => !alwaysEnabledIds.Contains(f.Id))
                                       .SelectMany(feature => GetFeaturesToDisable(feature, enabledFeatureIds, force))
                                       .Distinct()
                                       .ToList();

            if (AllFeaturesToDisable.Count > 0)
            {
                foreach (var feature in AllFeaturesToDisable)
                {
                    enabledFeatures.Remove(feature);

                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation("Feature '{FeatureName}' was disabled", feature.Id);
                    }
                }
            }

            enabledFeatureIds = enabledFeatures.Select(f => f.Id).ToArray();

            var AllFeaturesToEnable = featuresToEnable
                                      .SelectMany(feature => GetFeaturesToEnable(feature, enabledFeatureIds, force))
                                      .Distinct()
                                      .ToList();

            if (AllFeaturesToEnable.Count > 0)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    foreach (var feature in AllFeaturesToEnable)
                    {
                        _logger.LogInformation("Enabling feature '{FeatureName}'", feature.Id);
                    }
                }

                enabledFeatures = enabledFeatures.Concat(AllFeaturesToEnable).Distinct().ToList();
            }

            if (AllFeaturesToDisable.Count > 0 || AllFeaturesToEnable.Count > 0)
            {
                await _shellDescriptorManager.UpdateShellDescriptorAsync(
                    shellDescriptor.SerialNumber,
                    enabledFeatures.Select(x => new ShellFeature(x.Id)).ToList(),
                    shellDescriptor.Parameters);
            }

            return(AllFeaturesToDisable, AllFeaturesToEnable);
        }
        private void UpdateShell()
        {
            var descriptor = _shellDescriptorManager.GetShellDescriptorAsync().Result;

            _shellDescriptorManager.UpdateShellDescriptorAsync(descriptor.SerialNumber, descriptor.Features, descriptor.Parameters).Wait();
        }
        public async Task <(IEnumerable <IFeatureInfo>, IEnumerable <IFeatureInfo>)> UpdateFeaturesAsync(ShellDescriptor shellDescriptor,
                                                                                                         IEnumerable <IFeatureInfo> featuresToDisable, IEnumerable <IFeatureInfo> featuresToEnable, bool force)
        {
            var featureEventHandlers = ShellScope.Services.GetServices <IFeatureEventHandler>();

            var enabledFeatureIds = _extensionManager.GetFeatures()
                                    .Where(f => shellDescriptor.Features.Any(sf => sf.Id == f.Id))
                                    .Select(f => f.Id)
                                    .ToHashSet();

            var installedFeatureIds = enabledFeatureIds
                                      .Concat(shellDescriptor.Installed.Select(sf => sf.Id))
                                      .ToHashSet();

            var alwaysEnabledIds = _alwaysEnabledFeatures.Select(sf => sf.Id).ToArray();

            var allFeaturesToDisable = featuresToDisable
                                       .Where(f => !alwaysEnabledIds.Contains(f.Id))
                                       .SelectMany(feature => GetFeaturesToDisable(feature, enabledFeatureIds, force))
                                       .Distinct()
                                       .Reverse()
                                       .ToList();

            foreach (var feature in allFeaturesToDisable)
            {
                enabledFeatureIds.Remove(feature.Id);

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Disabling feature '{FeatureName}'", feature.Id);
                }

                await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.DisablingAsync(featureInfo), feature, _logger);
            }

            var allFeaturesToEnable = featuresToEnable
                                      .SelectMany(feature => GetFeaturesToEnable(feature, enabledFeatureIds, force))
                                      .Distinct()
                                      .ToList();

            foreach (var feature in allFeaturesToEnable)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Enabling feature '{FeatureName}'", feature.Id);
                }

                await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.EnablingAsync(featureInfo), feature, _logger);
            }

            var allFeaturesToInstall = allFeaturesToEnable
                                       .Where(f => !installedFeatureIds.Contains(f.Id))
                                       .ToList();

            foreach (var feature in allFeaturesToInstall)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Installing feature '{FeatureName}'", feature.Id);
                }

                await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.InstallingAsync(featureInfo), feature, _logger);
            }

            if (allFeaturesToEnable.Count > 0)
            {
                enabledFeatureIds.UnionWith(allFeaturesToEnable.Select(f => f.Id));
            }

            if (allFeaturesToDisable.Count > 0 || allFeaturesToEnable.Count > 0)
            {
                await _shellDescriptorManager.UpdateShellDescriptorAsync(
                    shellDescriptor.SerialNumber,
                    enabledFeatureIds.Select(id => new ShellFeature(id)).ToArray());

                ShellScope.AddDeferredTask(async scope =>
                {
                    var featureEventHandlers = scope.ServiceProvider.GetServices <IFeatureEventHandler>();
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <ShellFeaturesManager> >();

                    foreach (var feature in allFeaturesToInstall)
                    {
                        if (logger.IsEnabled(LogLevel.Information))
                        {
                            logger.LogInformation("Feature '{FeatureName}' was installed", feature.Id);
                        }

                        await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.InstalledAsync(featureInfo), feature, logger);
                    }

                    foreach (var feature in allFeaturesToEnable)
                    {
                        if (logger.IsEnabled(LogLevel.Information))
                        {
                            logger.LogInformation("Feature '{FeatureName}' was enabled", feature.Id);
                        }

                        await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.EnabledAsync(featureInfo), feature, logger);
                    }

                    foreach (var feature in allFeaturesToDisable)
                    {
                        if (logger.IsEnabled(LogLevel.Information))
                        {
                            logger.LogInformation("Feature '{FeatureName}' was disabled", feature.Id);
                        }

                        await featureEventHandlers.InvokeAsync((handler, featureInfo) => handler.DisabledAsync(featureInfo), feature, logger);
                    }
                });
            }

            return(allFeaturesToDisable, allFeaturesToEnable);
        }