Esempio n. 1
0
        async Task <string> GetCategoriesNameAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return("all");
            }

            if (id.Equals("all", StringComparison.OrdinalIgnoreCase))
            {
                return("all");
            }

            var features = await _shellDescriptorManager.GetFeaturesAsync();

            foreach (var feature in features
                     .GroupBy(f => f.Descriptor.Category)
                     .OrderBy(o => o.Key))
            {
                if (feature.Key.Equals(id, StringComparison.OrdinalIgnoreCase))
                {
                    return(feature.Key);
                }
            }

            return("all");
        }
Esempio n. 2
0
        public async Task <IViewComponentResult> InvokeAsync(FeatureIndexOptions options)
        {
            // Get features
            var features = await _shellDescriptorManager.GetFeaturesAsync();

            // No features
            if (features == null)
            {
                return(View(new FeaturesIndexViewModel()
                {
                    Options = options
                }));
            }

            // Filter features by category
            if (!string.IsNullOrEmpty(options.Category))
            {
                features = features.Where(f => f.Descriptor?.Category != null && f.Descriptor.Category.Equals(options.Category, StringComparison.OrdinalIgnoreCase));
            }

            if (options.HideEnabled)
            {
                features = features.Where(f => f.IsEnabled == false);
            }

            return(View(new FeaturesIndexViewModel()
            {
                Options = options,
                Features = features
            }));
        }
Esempio n. 3
0
        /// <summary>
        /// Returns available feature update.
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <IShellFeature> > GetFeatureUpdatesAsync()
        {
            // Get all features from the file system
            var modules = await _shellDescriptorManager
                          .GetFeaturesAsync();

            var modulesList = modules.ToList();

            // Get enabled features from database
            var features = await _shellFeatureStore
                           .QueryAsync()
                           .ToList();

            // Iterate all enabled features from shell features tables
            // checking if a newer version exists on the file system
            List <IShellFeature> output = null;

            foreach (var feature in features.Data)
            {
                // Get available module descriptor
                var module = modulesList.FirstOrDefault(m => m.ModuleId.Equals(feature.ModuleId));

                // Ensure the module is available
                if (module != null)
                {
                    // Available module version
                    var moduleVersion = module.Descriptor.Version.ToVersion();

                    // Enabled feature version
                    var featureVersion = feature.Version.ToVersion();

                    // Ensure we have versions to compare
                    if (moduleVersion != null && featureVersion != null)
                    {
                        // The module is newer than the installed feature
                        if (moduleVersion > featureVersion)
                        {
                            if (output == null)
                            {
                                output = new List <IShellFeature>();;
                            }
                            output.Add(module);
                        }
                    }
                }
            }

            return(output);
        }
Esempio n. 4
0
        // ------------------------

        private async Task <ICommandResultBase> InstallByCategoryAsync(string categoryName)
        {
            // Our result
            var result = new CommandResultBase();

            // Get all available features
            var features = await _shellDescriptorManager.GetFeaturesAsync();

            // Get all features in feature category
            var categoryFeatures = features?
                                   .Where(f => f.Descriptor.Category.Equals(categoryName, StringComparison.OrdinalIgnoreCase))
                                   .OrderBy(f => f.Descriptor.Id);

            var errors = new List <CommandError>();

            if (categoryFeatures != null)
            {
                foreach (var feature in categoryFeatures)
                {
                    var contexts = await _shellFeatureManager.EnableFeatureAsync(feature.Descriptor.Id);

                    foreach (var context in contexts)
                    {
                        if (context.Errors.Any())
                        {
                            foreach (var error in context.Errors)
                            {
                                errors.Add(new CommandError($"{context.Feature.ModuleId} could not be enabled. {error.Key} - {error.Value}"));
                            }
                        }
                    }
                }
            }

            return(errors.Count > 0
                ? result.Failed(errors.ToArray())
                : result.Success());
        }
Esempio n. 5
0
        public async Task <IEnumerable <IFeatureEventContext> > EnableFeaturesAsync(string[] featureIds)
        {
            // Get distinct Ids
            var ids = featureIds.Distinct().ToArray();

            // Get features to enable
            var features = await _shellDescriptorManager.GetFeaturesAsync(ids);

            var featuresToInvoke = features.Distinct().ToList();

            // Raise installing events for features
            var results = await InvokeFeatureEventsAsync(featuresToInvoke,
                                                         async (context, handler) =>
            {
                // Return if feature is already enabled, no need to enable
                if (context.Feature.IsEnabled)
                {
                    return(null);
                }

                var contexts = new ConcurrentDictionary <string, IFeatureEventContext>();

                try
                {
                    // Invoke FeatureInstalling subscriptions
                    foreach (var brokerHandler in _broker.Pub <IShellFeature>(this, "FeatureInstalling"))
                    {
                        context.Feature = await brokerHandler.Invoke(new Message <IShellFeature>(context.Feature, this));
                    }

                    // Invoke feature event handler
                    await handler.InstallingAsync(context);

                    contexts.AddOrUpdate(context.Feature.ModuleId, context, (k, v) =>
                    {
                        foreach (var error in context.Errors)
                        {
                            v.Errors.Add(error.Key, error.Value);
                        }

                        return(v);
                    });
                }
                catch (Exception e)
                {
                    contexts.AddOrUpdate(context.Feature.ModuleId, context, (k, v) =>
                    {
                        v.Errors.Add(context.Feature.ModuleId, e.Message);
                        return(v);
                    });
                }

                // Did any event encounter errors?
                var hasErrors = contexts
                                .Where(c => c.Value.Errors.Any());

                // No errors update descriptor, raise InstalledAsync and recycle ShellContext
                if (!hasErrors.Any())
                {
                    // Update descriptor within database
                    var descriptor = await AddFeaturesAndSaveAsync(featureIds);

                    try
                    {
                        // Invoke FeatureInstalled subscriptions
                        foreach (var brokerHandler in _broker.Pub <IShellFeature>(this, "FeatureInstalled"))
                        {
                            context.Feature = await brokerHandler.Invoke(new Message <IShellFeature>(context.Feature, this));
                        }

                        await handler.InstalledAsync(context);
                        contexts.AddOrUpdate(context.Feature.ModuleId, context, (k, v) =>
                        {
                            foreach (var error in context.Errors)
                            {
                                v.Errors.Add(error.Key, error.Value);
                            }

                            return(v);
                        });
                    }
                    catch (Exception e)
                    {
                        contexts.AddOrUpdate(context.Feature.ModuleId, context, (k, v) =>
                        {
                            v.Errors.Add(context.Feature.ModuleId, e.Message);
                            return(v);
                        });
                    }
                }

                return(contexts);
            },
                                                         async context =>
            {
                // Return if feature is already enabled, no need to enable
                if (context.Feature.IsEnabled)
                {
                    return(null);
                }

                // Invoke FeatureInstalled subscriptions
                foreach (var brokerHandler in _broker.Pub <IShellFeature>(this, "FeatureInstalled"))
                {
                    context.Feature = await brokerHandler.Invoke(new Message <IShellFeature>(context.Feature, this));
                }

                var contexts = new ConcurrentDictionary <string, IFeatureEventContext>();
                contexts.AddOrUpdate(context.Feature.ModuleId, context, (k, v) =>
                {
                    foreach (var error in context.Errors)
                    {
                        v.Errors.Add(error.Key, error.Value);
                    }

                    return(v);
                });
                return(contexts);
            });

            // Did any event encounter errors?
            var errors = results
                         .Where(c => c.Value.Errors.Any());

            // No errors update descriptor, raise InstalledAsync and recycle ShellContext
            if (!errors.Any())
            {
                // Update descriptor within database
                await AddFeaturesAndSaveAsync(featureIds);
            }

            // dispose current shell context
            RecycleShell();

            // Return all execution contexts
            return(results.Values);
        }