File size: 19,046 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Management.Automation;
using System.Windows;
using Microsoft.Management.UI.Internal;
using Microsoft.PowerShell.Commands.ShowCommandExtension;
namespace Microsoft.PowerShell.Commands.ShowCommandInternal
{
/// <summary>
/// ModuleViewModel Contains information about a PowerShell module.
/// </summary>
public class ModuleViewModel : INotifyPropertyChanged
{
/// <summary>
/// True if the module is imported.
/// </summary>
private bool isModuleImported;
/// <summary>
/// Field used for the Name parameter.
/// </summary>
private string name;
/// <summary>
/// Filter commands property of this module.
/// </summary>
private ObservableCollection<CommandViewModel> filteredCommands;
/// <summary>
/// The selected command property of this module.
/// </summary>
private CommandViewModel selectedCommand;
/// <summary>
/// Field used for the Commands parameter.
/// </summary>
private List<CommandViewModel> commands;
/// <summary>
/// value indicating whether there is a selected command which belongs to an imported module,
/// with no parameter sets or with a selected parameter set where all mandatory parameters have values
/// </summary>
private bool isThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues;
/// <summary>
/// value indicating whether there is a selected command.
/// </summary>
private bool isThereASelectedCommand;
/// <summary>
/// The AllModulesViewModel containing this, if any.
/// </summary>
private AllModulesViewModel allModules;
#region Construction and Destructor
/// <summary>
/// Initializes a new instance of the ModuleViewModel class.
/// </summary>
/// <param name="name">Module name.</param>
/// <param name="importedModules">All loaded modules.</param>
public ModuleViewModel(string name, Dictionary<string, ShowCommandModuleInfo> importedModules)
{
ArgumentNullException.ThrowIfNull(name);
this.name = name;
this.commands = new List<CommandViewModel>();
this.filteredCommands = new ObservableCollection<CommandViewModel>();
// This check looks to see if the given module name shows up in
// the set of modules that are known to be imported in the current
// session. In remote PowerShell sessions, the core cmdlet module
// Microsoft.PowerShell.Core doesn't appear as being imported despite
// always being loaded by default. To make sure we don't incorrectly
// mark this module as not imported, check for it by name.
this.isModuleImported =
importedModules == null ? true : name.Length == 0 ||
importedModules.ContainsKey(name) ||
string.Equals("Microsoft.PowerShell.Core", name, StringComparison.OrdinalIgnoreCase);
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// PropertyChanged Event.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
/// <summary>
/// Indicates the selected command in needs to display the help for a command.
/// </summary>
public event EventHandler<HelpNeededEventArgs> SelectedCommandNeedsHelp;
/// <summary>
/// Indicates the selected command needs to import a module.
/// </summary>
public event EventHandler<ImportModuleEventArgs> SelectedCommandNeedsImportModule;
/// <summary>
/// Indicates the selected command should be run.
/// </summary>
public event EventHandler<CommandEventArgs> RunSelectedCommand;
#region Public Property
/// <summary>
/// Gets the name property of this ModuleView.
/// </summary>
public string Name
{
get { return this.name; }
}
/// <summary>
/// Gets the GUI friendly module name.
/// </summary>
public string DisplayName
{
get
{
if (!string.IsNullOrEmpty(this.name))
{
return this.name;
}
return ShowCommandResources.NoModuleName;
}
}
/// <summary>
/// Gets CommandControl is visibility or not.
/// </summary>
public Visibility CommandControlVisibility
{
get { return this.selectedCommand == null ? Visibility.Collapsed : Visibility.Visible; }
}
/// <summary>
/// Gets CommandControl Height.
/// </summary>
public GridLength CommandRowHeight
{
get { return this.selectedCommand == null ? GridLength.Auto : CommandViewModel.Star; }
}
/// <summary>
/// Gets the commands under in this module.
/// </summary>
public List<CommandViewModel> Commands
{
get { return this.commands; }
}
/// <summary>
/// Gets the filter commands of this module.
/// </summary>
public ObservableCollection<CommandViewModel> FilteredCommands
{
get { return this.filteredCommands; }
}
/// <summary>
/// Gets or sets the selected commands of this module.
/// </summary>
public CommandViewModel SelectedCommand
{
get
{
return this.selectedCommand;
}
set
{
if (value == this.selectedCommand)
{
return;
}
if (this.selectedCommand != null)
{
this.selectedCommand.PropertyChanged -= this.SelectedCommand_PropertyChanged;
this.selectedCommand.HelpNeeded -= this.SelectedCommand_HelpNeeded;
this.selectedCommand.ImportModule -= this.SelectedCommand_ImportModule;
}
this.selectedCommand = value;
this.SetIsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues();
if (this.selectedCommand != null)
{
this.selectedCommand.PropertyChanged += this.SelectedCommand_PropertyChanged;
this.selectedCommand.HelpNeeded += this.SelectedCommand_HelpNeeded;
this.selectedCommand.ImportModule += this.SelectedCommand_ImportModule;
this.IsThereASelectedCommand = true;
}
else
{
this.IsThereASelectedCommand = false;
}
this.OnNotifyPropertyChanged("SelectedCommand");
this.OnNotifyPropertyChanged("CommandControlVisibility");
this.OnNotifyPropertyChanged("CommandRowHeight");
}
}
/// <summary>
/// Gets or sets a value indicating whether there is a selected command.
/// </summary>
public bool IsThereASelectedCommand
{
get
{
return this.isThereASelectedCommand;
}
set
{
if (value == this.isThereASelectedCommand)
{
return;
}
this.isThereASelectedCommand = value;
this.OnNotifyPropertyChanged("IsThereASelectedCommand");
}
}
/// <summary>
/// Gets or sets a value indicating whether there is a selected command which belongs
/// to an imported module, with no parameter sets or with a selected parameter set
/// where all mandatory parameters have values
/// </summary>
public bool IsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues
{
get
{
return this.isThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues;
}
set
{
if (value == this.isThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues)
{
return;
}
this.isThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues = value;
this.OnNotifyPropertyChanged("IsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues");
}
}
/// <summary>
/// Gets the AllModulesViewModel containing this, if any.
/// </summary>
public AllModulesViewModel AllModules
{
get
{
return this.allModules;
}
}
#endregion
/// <summary>
/// Gets a value indicating whether the module is imported.
/// </summary>
internal bool IsModuleImported
{
get
{
return this.isModuleImported;
}
}
/// <summary>
/// Sets the AllModulesViewModel containing this.
/// </summary>
/// <param name="parentAllModules">The AllModulesViewModel containing this.</param>
internal void SetAllModules(AllModulesViewModel parentAllModules)
{
this.allModules = parentAllModules;
}
/// <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>
/// According commandNameFilter to filter command,and added the filter commands into filteredCommands property.
/// </summary>
/// <param name="filter">Current filter.</param>
internal void RefreshFilteredCommands(string filter)
{
this.filteredCommands.Clear();
if (string.IsNullOrEmpty(filter))
{
foreach (CommandViewModel command in this.Commands)
{
this.filteredCommands.Add(command);
}
return;
}
WildcardPattern filterPattern = null;
if (WildcardPattern.ContainsWildcardCharacters(filter))
{
filterPattern = new WildcardPattern(filter, WildcardOptions.IgnoreCase);
}
foreach (CommandViewModel command in this.Commands)
{
if (ModuleViewModel.Matches(filterPattern, command.Name, filter))
{
this.filteredCommands.Add(command);
continue;
}
if (filterPattern != null)
{
continue;
}
string[] textSplit = filter.Split(' ');
if (textSplit.Length != 2)
{
continue;
}
if (ModuleViewModel.Matches(filterPattern, command.Name, textSplit[0] + "-" + textSplit[1]))
{
this.filteredCommands.Add(command);
}
}
}
/// <summary>
/// Called in response to a GUI event that requires the command to be run.
/// </summary>
internal void OnRunSelectedCommand()
{
EventHandler<CommandEventArgs> handler = this.RunSelectedCommand;
if (handler != null)
{
handler(this, new CommandEventArgs(this.SelectedCommand));
}
}
/// <summary>
/// Triggers the SelectedCommandNeedsHelp event.
/// </summary>
/// <param name="e">Event arguments.</param>
internal void OnSelectedCommandNeedsHelp(HelpNeededEventArgs e)
{
EventHandler<HelpNeededEventArgs> handler = this.SelectedCommandNeedsHelp;
if (handler != null)
{
handler(this, e);
}
}
/// <summary>
/// Triggers the SelectedCommandNeedsImportModule event.
/// </summary>
internal void OnSelectedCommandNeedsImportModule()
{
EventHandler<ImportModuleEventArgs> handler = this.SelectedCommandNeedsImportModule;
if (handler != null)
{
handler(this, new ImportModuleEventArgs(this.SelectedCommand.Name, this.SelectedCommand.ModuleName, this.Name));
}
}
#region Private Method
/// <summary>
/// Uses pattern matching if pattern is not null or calls MatchesEvenIfInPlural otherwise.
/// </summary>
/// <param name="filterPattern">Pattern corresponding to filter.</param>
/// <param name="commandName">Command name string.</param>
/// <param name="filter">Filter string.</param>
/// <returns>True if coparisonText matches str or pattern.</returns>
private static bool Matches(WildcardPattern filterPattern, string commandName, string filter)
{
if (filterPattern != null)
{
return filterPattern.IsMatch(commandName);
}
return ModuleViewModel.MatchesEvenIfInPlural(commandName, filter);
}
/// <summary>
/// Returns true if filter matches commandName, even when filter is in the plural.
/// </summary>
/// <param name="commandName">Command name string.</param>
/// <param name="filter">Filter string.</param>
/// <returns>Return match result.</returns>
private static bool MatchesEvenIfInPlural(string commandName, string filter)
{
if (commandName.Contains(filter, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (filter.Length > 5 && filter.EndsWith("es", StringComparison.OrdinalIgnoreCase))
{
ReadOnlySpan<char> filterSpan = filter.AsSpan(0, filter.Length - 2);
return commandName.AsSpan().Contains(filterSpan, StringComparison.OrdinalIgnoreCase);
}
if (filter.Length > 4 && filter.EndsWith("s", StringComparison.OrdinalIgnoreCase))
{
ReadOnlySpan<char> filterSpan = filter.AsSpan(0, filter.Length - 1);
return commandName.AsSpan().Contains(filterSpan, StringComparison.OrdinalIgnoreCase);
}
return false;
}
/// <summary>
/// Handles the HelpNeeded event in the selected command and triggers the SelectedCommandNeedsHelp event.
/// </summary>
/// <param name="sender">HelpNeeded event sender.</param>
/// <param name="e">HelpNeeded event argument.</param>
private void SelectedCommand_HelpNeeded(object sender, HelpNeededEventArgs e)
{
this.OnSelectedCommandNeedsHelp(e);
}
/// <summary>
/// Handles the ImportModule event in the selected command and triggers the SelectedCommandNeedsImportModule event.
/// </summary>
/// <param name="sender">HelpNeeded event sender.</param>
/// <param name="e">HelpNeeded event argument.</param>
private void SelectedCommand_ImportModule(object sender, EventArgs e)
{
this.OnSelectedCommandNeedsImportModule();
}
/// <summary>
/// Called when the SelectedCommand property changes to update IsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void SelectedCommand_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!e.PropertyName.Equals("SelectedParameterSetAllMandatoryParametersHaveValues"))
{
return;
}
this.SetIsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues();
}
/// <summary>
/// Called to set IsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues when
/// SelectedParameterSetAllMandatoryParametersHaveValues changes in the SelectedCommand or
/// when the SelectedCommand changes
/// </summary>
private void SetIsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues()
{
this.IsThereASelectedImportedCommandWhereAllMandatoryParametersHaveValues =
this.selectedCommand != null &&
this.selectedCommand.IsImported &&
this.selectedCommand.SelectedParameterSetAllMandatoryParametersHaveValues;
}
/// <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);
}
#endregion
/// <summary>
/// If property changed will be notify.
/// </summary>
/// <param name="propertyName">The changed property.</param>
private void OnNotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
|