/// <summary>
        /// Sorts commands and optionally sets ModuleQualifyCommandName
        /// </summary>
        /// <param name="markRepeatedCmdlets">true to mark repeated commands with a flag that will produce a module qualified name in GetScript</param>
        internal void SortCommands(bool markRepeatedCmdlets)
        {
            this.commands.Sort(this.Compare);

            if (!markRepeatedCmdlets || this.commands.Count == 0)
            {
                return;
            }

            CommandViewModel reference = this.commands[0];

            for (int i = 1; i < this.commands.Count; i++)
            {
                CommandViewModel command = this.commands[i];
                if (reference.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase))
                {
                    reference.ModuleQualifyCommandName = true;
                    command.ModuleQualifyCommandName   = true;
                }
                else
                {
                    reference = command;
                }
            }
        }
        /// <summary>
        /// DataContextChanged event.
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event args</param>
        private void ParameterSetTabControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext == null)
            {
                return;
            }

            CommandViewModel viewModel = (CommandViewModel)this.DataContext;
            this.currentCommandViewModel = viewModel;

            if (viewModel.ParameterSets.Count == 0)
            {
                return;
            }

            this.ParameterSetTabControl.SelectedItem = viewModel.ParameterSets[0];
        }
Beispiel #3
0
        /// <summary>
        /// DataContextChanged event.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void ParameterSetTabControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext == null)
            {
                return;
            }

            CommandViewModel viewModel = (CommandViewModel)this.DataContext;

            this.currentCommandViewModel = viewModel;

            if (viewModel.ParameterSets.Count == 0)
            {
                return;
            }

            this.ParameterSetTabControl.SelectedItem = viewModel.ParameterSets[0];
        }
Beispiel #4
0
        /// <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);
        }
Beispiel #6
0
 /// <summary>
 /// Initializes a new instance of the CommandEventArgs class.
 /// </summary>
 /// <param name="command">the command targeted by the event</param>
 public CommandEventArgs(CommandViewModel command)
 {
     this.command = command;
 }
Beispiel #7
0
        private void ShowCommandWindow(PSCmdlet cmdlet, object commandViewModelObj, double windowWidth, double windowHeight, bool passThrough)
        {
            this.methodThatReturnsDialog = new DispatcherOperationCallback(delegate(object ignored)
            {
                Diagnostics.Assert(commandViewModelObj != null, "verified by caller");
                this.commandViewModel = (CommandViewModel)commandViewModelObj;
                ShowCommandWindow showCommandWindow = new ShowCommandWindow();

                this.commandViewModel.HelpNeeded += new EventHandler<HelpNeededEventArgs>(this.CommandNeedsHelp);
                showCommandWindow.DataContext = this.commandViewModel;

                this.SetupButtonEvents(showCommandWindow.Run, showCommandWindow.Copy, showCommandWindow.Cancel, passThrough);
                this.SetupWindow(showCommandWindow);

                CommonHelper.SetStartingPositionAndSize(
                    showCommandWindow,
                    ShowCommandSettings.Default.ShowOneCommandTop,
                    ShowCommandSettings.Default.ShowOneCommandLeft,
                    windowWidth != 0.0 && windowWidth > showCommandWindow.MinWidth ? windowWidth : ShowCommandSettings.Default.ShowOneCommandWidth,
                    windowHeight != 0.0 && windowHeight > showCommandWindow.MinHeight ? windowHeight : ShowCommandSettings.Default.ShowOneCommandHeight,
                    showCommandWindow.Width,
                    showCommandWindow.Height,
                    ShowCommandSettings.Default.ShowOneCommandWindowMaximized);

                return showCommandWindow;
            });

            this.CallShowDialog(cmdlet);
        }
Beispiel #8
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 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;
        }
 /// <summary>
 /// Compare source commandmodule is equal like target commandmodule.
 /// </summary>
 /// <param name="source">Source commandmodule.</param>
 /// <param name="target">Target commandmodule.</param>
 /// <returns>Return compare result.</returns>
 private int Compare(CommandViewModel source, CommandViewModel target)
 {
     return(string.Compare(source.Name, target.Name, StringComparison.OrdinalIgnoreCase));
 }
 /// <summary>
 /// Initializes a new instance of the CommandEventArgs class.
 /// </summary>
 /// <param name="command">The command targeted by the event.</param>
 public CommandEventArgs(CommandViewModel command)
 {
     this.command = command;
 }
Beispiel #11
0
 /// <summary>
 /// Compare source commandmodule is equal like target commandmodule
 /// </summary>
 /// <param name="source">source commandmodule </param>
 /// <param name="target">target commandmodule</param>
 /// <returns>return compare result</returns>
 private int Compare(CommandViewModel source, CommandViewModel target)
 {
     return String.Compare(source.Name, target.Name, StringComparison.OrdinalIgnoreCase);
 }