/// <summary> /// Initialize AllModulesViewModel. /// </summary> /// <param name="importedModules">All loaded modules.</param> /// <param name="commands">List of commands in all modules.</param> /// <param name="noCommonParameterInModel">Whether showing common parameter.</param> private void Initialization(Dictionary <string, ShowCommandModuleInfo> importedModules, IEnumerable <ShowCommandCommandInfo> commands, bool noCommonParameterInModel) { if (commands == null) { return; } Dictionary <string, ModuleViewModel> rawModuleViewModels = new Dictionary <string, ModuleViewModel>(); this.noCommonParameter = noCommonParameterInModel; // separates commands in their Modules foreach (ShowCommandCommandInfo command in commands) { ModuleViewModel moduleViewModel; if (!rawModuleViewModels.TryGetValue(command.ModuleName, out moduleViewModel)) { moduleViewModel = new ModuleViewModel(command.ModuleName, importedModules); rawModuleViewModels.Add(command.ModuleName, moduleViewModel); } CommandViewModel commandViewModel; try { commandViewModel = CommandViewModel.GetCommandViewModel(moduleViewModel, command, noCommonParameterInModel); } catch (RuntimeException) { continue; } moduleViewModel.Commands.Add(commandViewModel); moduleViewModel.SetAllModules(this); } // populates this.modules this.modules = new List <ModuleViewModel>(); // if there is just one module then use only it if (rawModuleViewModels.Values.Count == 1) { this.modules.Add(rawModuleViewModels.Values.First()); this.modules[0].SortCommands(false); this.SelectedModule = this.modules[0]; return; } // If there are more modules, create an additional module to agregate all commands ModuleViewModel allCommandsModule = new ModuleViewModel(ShowCommandResources.All, null); this.modules.Add(allCommandsModule); allCommandsModule.SetAllModules(this); if (rawModuleViewModels.Values.Count > 0) { foreach (ModuleViewModel module in rawModuleViewModels.Values) { module.SortCommands(false); this.modules.Add(module); allCommandsModule.Commands.AddRange(module.Commands); } } allCommandsModule.SortCommands(true); this.modules.Sort(this.Compare); this.SelectedModule = this.modules.Count == 0 ? null : this.modules[0]; }
/// <summary> /// Creates a new CommandViewModel out the <paramref name="commandInfo"/>. /// </summary> /// <param name="module">Module to which the CommandViewModel will belong to.</param> /// <param name="commandInfo">Will showing command.</param> /// <param name="noCommonParameters">True to ommit displaying common parameter.</param> /// <exception cref="ArgumentNullException">If commandInfo is null</exception> /// <exception cref="RuntimeException"> /// If could not create the CommandViewModel. For instance the ShowCommandCommandInfo corresponding to /// the following function will throw a RuntimeException when the ShowCommandCommandInfo Parameters /// are retrieved: /// function CrashMe ([I.Am.A.Type.That.Does.Not.Exist]$name) {} /// </exception> /// <returns>The CommandViewModel corresponding to commandInfo.</returns> internal static CommandViewModel GetCommandViewModel(ModuleViewModel module, ShowCommandCommandInfo commandInfo, bool noCommonParameters) { if (commandInfo == null) { throw new ArgumentNullException("commandInfo"); } CommandViewModel returnValue = new CommandViewModel(); returnValue.commandInfo = commandInfo; returnValue.noCommonParameters = noCommonParameters; returnValue.parentModule = module; Dictionary <string, ParameterViewModel> commonParametersTable = new Dictionary <string, ParameterViewModel>(); foreach (ShowCommandParameterSetInfo parameterSetInfo in commandInfo.ParameterSets) { if (parameterSetInfo.IsDefault) { returnValue.defaultParameterSetName = parameterSetInfo.Name; } List <ParameterViewModel> parametersForParameterSet = new List <ParameterViewModel>(); foreach (ShowCommandParameterInfo parameterInfo in parameterSetInfo.Parameters) { bool isCommon = Cmdlet.CommonParameters.Contains(parameterInfo.Name); if (isCommon) { if (!commonParametersTable.ContainsKey(parameterInfo.Name)) { commonParametersTable.Add(parameterInfo.Name, new ParameterViewModel(parameterInfo, parameterSetInfo.Name)); } continue; } parametersForParameterSet.Add(new ParameterViewModel(parameterInfo, parameterSetInfo.Name)); } if (parametersForParameterSet.Count != 0) { returnValue.ParameterSets.Add(new ParameterSetViewModel(parameterSetInfo.Name, parametersForParameterSet)); } } List <ParameterViewModel> commonParametersList = commonParametersTable.Values.ToList <ParameterViewModel>(); returnValue.comonParameters = new ParameterSetViewModel(string.Empty, commonParametersList); returnValue.parameterSets.Sort(returnValue.Compare); if (returnValue.parameterSets.Count > 0) { // Setting SelectedParameterSet will also set SelectedParameterSetAllMandatoryParametersHaveValues returnValue.SelectedParameterSet = returnValue.ParameterSets[0]; } else { returnValue.SelectedParameterSetAllMandatoryParametersHaveValues = true; } returnValue.SetCommonParametersHeight(); return(returnValue); }
/// <summary> /// Initialize AllModulesViewModel. /// </summary> /// <param name="importedModules">All loaded modules</param> /// <param name="commands">List of commands in all modules</param> /// <param name="noCommonParameterInModel">Whether showing common parameter</param> private void Initialization(Dictionary<string, ShowCommandModuleInfo> importedModules, IEnumerable<ShowCommandCommandInfo> commands, bool noCommonParameterInModel) { if (commands == null) { return; } Dictionary<string, ModuleViewModel> rawModuleViewModels = new Dictionary<string, ModuleViewModel>(); this.noCommonParameter = noCommonParameterInModel; // separates commands in their Modules foreach (ShowCommandCommandInfo command in commands) { ModuleViewModel moduleViewModel; if (!rawModuleViewModels.TryGetValue(command.ModuleName, out moduleViewModel)) { moduleViewModel = new ModuleViewModel(command.ModuleName, importedModules); rawModuleViewModels.Add(command.ModuleName, moduleViewModel); } CommandViewModel commandViewModel; try { commandViewModel = CommandViewModel.GetCommandViewModel(moduleViewModel, command, noCommonParameterInModel); } catch (RuntimeException) { continue; } moduleViewModel.Commands.Add(commandViewModel); moduleViewModel.SetAllModules(this); } // populates this.modules this.modules = new List<ModuleViewModel>(); // if there is just one module then use only it if (rawModuleViewModels.Values.Count == 1) { this.modules.Add(rawModuleViewModels.Values.First()); this.modules[0].SortCommands(false); this.SelectedModule = this.modules[0]; return; } // If there are more modules, create an additional module to aggregate all commands ModuleViewModel allCommandsModule = new ModuleViewModel(ShowCommandResources.All, null); this.modules.Add(allCommandsModule); allCommandsModule.SetAllModules(this); if (rawModuleViewModels.Values.Count > 0) { foreach (ModuleViewModel module in rawModuleViewModels.Values) { module.SortCommands(false); this.modules.Add(module); allCommandsModule.Commands.AddRange(module.Commands); } } allCommandsModule.SortCommands(true); this.modules.Sort(this.Compare); this.SelectedModule = this.modules.Count == 0 ? null : this.modules[0]; }
/// <summary> /// Compare two ModuleViewModel target and source. /// </summary> /// <param name="source">The source ModuleViewModel</param> /// <param name="target">The target ModuleViewModel</param> /// <returns>Compare result</returns> private int Compare(ModuleViewModel source, ModuleViewModel target) { if (AllModulesViewModel.IsAll(source.Name) && !AllModulesViewModel.IsAll(target.Name)) { return -1; } if (!AllModulesViewModel.IsAll(source.Name) && AllModulesViewModel.IsAll(target.Name)) { return 1; } return String.Compare(source.Name, target.Name, StringComparison.OrdinalIgnoreCase); }
/// <summary> /// Creates a new CommandViewModel out the <paramref name="commandInfo"/>. /// </summary> /// <param name="module">Module to which the CommandViewModel will belong to</param> /// <param name="commandInfo">Will showing command</param> /// <param name="noCommonParameters">true to omit displaying common parameter</param> /// <exception cref="ArgumentNullException">If commandInfo is null</exception> /// <exception cref="RuntimeException"> /// If could not create the CommandViewModel. For instance the ShowCommandCommandInfo corresponding to /// the following function will throw a RuntimeException when the ShowCommandCommandInfo Parameters /// are retrieved: /// function CrashMe ([I.Am.A.Type.That.Does.Not.Exist]$name) {} /// </exception> /// <returns>The CommandViewModel corresponding to commandInfo</returns> internal static CommandViewModel GetCommandViewModel(ModuleViewModel module, ShowCommandCommandInfo commandInfo, bool noCommonParameters) { if (commandInfo == null) { throw new ArgumentNullException("commandInfo"); } CommandViewModel returnValue = new CommandViewModel(); returnValue.commandInfo = commandInfo; returnValue.noCommonParameters = noCommonParameters; returnValue.parentModule = module; Dictionary<string, ParameterViewModel> commonParametersTable = new Dictionary<string, ParameterViewModel>(); bool isWorkflow = (commandInfo.CommandType & CommandTypes.Workflow) != 0; foreach (ShowCommandParameterSetInfo parameterSetInfo in commandInfo.ParameterSets) { if (parameterSetInfo.IsDefault) { returnValue.defaultParameterSetName = parameterSetInfo.Name; } List<ParameterViewModel> parametersForParameterSet = new List<ParameterViewModel>(); foreach (ShowCommandParameterInfo parameterInfo in parameterSetInfo.Parameters) { bool isCommon = Cmdlet.CommonParameters.Contains(parameterInfo.Name); bool isCommonWorkflow = isWorkflow && SMAI.CommonParameters.CommonWorkflowParameters.Contains(parameterInfo.Name, StringComparer.OrdinalIgnoreCase); if (isCommon || isCommonWorkflow) { if (!commonParametersTable.ContainsKey(parameterInfo.Name)) { commonParametersTable.Add(parameterInfo.Name, new ParameterViewModel(parameterInfo, parameterSetInfo.Name)); } continue; } parametersForParameterSet.Add(new ParameterViewModel(parameterInfo, parameterSetInfo.Name)); } if (parametersForParameterSet.Count != 0) { returnValue.ParameterSets.Add(new ParameterSetViewModel(parameterSetInfo.Name, parametersForParameterSet)); } } List<ParameterViewModel> commonParametersList = commonParametersTable.Values.ToList<ParameterViewModel>(); returnValue.comonParameters = new ParameterSetViewModel(String.Empty, commonParametersList); returnValue.parameterSets.Sort(returnValue.Compare); if (returnValue.parameterSets.Count > 0) { // Setting SelectedParameterSet will also set SelectedParameterSetAllMandatoryParametersHaveValues returnValue.SelectedParameterSet = returnValue.ParameterSets[0]; } else { returnValue.SelectedParameterSetAllMandatoryParametersHaveValues = true; } returnValue.SetCommonParametersHeight(); return returnValue; }