File size: 13,348 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Text;
using Microsoft.PowerShell.Commands.ShowCommandExtension;
namespace Microsoft.PowerShell.Commands.ShowCommandInternal
{
/// <summary>
/// Contains information about a single ParameterSet inside a cmdlet.
/// </summary>
public class ParameterSetViewModel : INotifyPropertyChanged
{
/// <summary>
/// Field used for the Name parameter.
/// </summary>
private string name;
/// <summary>
/// value indicating all mandatory parameters have values.
/// </summary>
private bool allMandatoryParametersHaveValues;
/// <summary>
/// Field used for the Parameters parameter.
/// </summary>
private List<ParameterViewModel> parameters;
#region Construction and Destructor
/// <summary>
/// Initializes a new instance of the ParameterSetViewModel class.
/// </summary>
/// <param name="name">The name of the parameterSet.</param>
/// <param name="parameters">The array parameters of the parameterSet.</param>
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "this type is internal, made public only for WPF Binding")]
public ParameterSetViewModel(
string name,
List<ParameterViewModel> parameters)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(parameters);
parameters.Sort(Compare);
this.name = name;
this.parameters = parameters;
foreach (ParameterViewModel parameter in this.parameters)
{
if (!parameter.IsMandatory)
{
continue;
}
parameter.PropertyChanged += this.MandatoryParameter_PropertyChanged;
}
this.EvaluateAllMandatoryParametersHaveValues();
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// PropertyChanged Event.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Public Property
/// <summary>
/// Gets the ParameterSet Name.
/// </summary>
public string Name
{
get { return this.name; }
}
/// <summary>
/// Gets the Parameters of this parameterset.
/// </summary>
public List<ParameterViewModel> Parameters
{
get { return this.parameters; }
}
/// <summary>
/// Gets or sets a value indicating whether all mandatory parameters have values.
/// </summary>
public bool AllMandatoryParametersHaveValues
{
get
{
return this.allMandatoryParametersHaveValues;
}
set
{
if (this.allMandatoryParametersHaveValues != value)
{
this.allMandatoryParametersHaveValues = value;
this.OnNotifyPropertyChanged("AllMandatoryParametersHaveValues");
}
}
}
#endregion
#region Public Method
/// <summary>
/// Creates script according parameters of this parameterset.
/// </summary>
/// <returns>Return script of this parameterset parameters.</returns>
public string GetScript()
{
if (this.Parameters == null || this.Parameters.Count == 0)
{
return string.Empty;
}
StringBuilder builder = new StringBuilder();
foreach (ParameterViewModel parameter in this.Parameters)
{
if (parameter.Value == null)
{
continue;
}
if (parameter.Parameter.ParameterType.IsSwitch)
{
if (((bool?)parameter.Value) == true)
{
builder.Append($"-{parameter.Name} ");
}
continue;
}
string parameterValueString = parameter.Value.ToString();
if (parameterValueString.Length == 0)
{
continue;
}
ShowCommandParameterType parameterType = parameter.Parameter.ParameterType;
if (parameterType.IsEnum || parameterType.IsString || (parameterType.IsArray && parameterType.ElementType.IsString))
{
parameterValueString = ParameterSetViewModel.GetDelimitedParameter(parameterValueString, "\"", "\"");
}
else if (parameterType.IsScriptBlock)
{
parameterValueString = ParameterSetViewModel.GetDelimitedParameter(parameterValueString, "{", "}");
}
else
{
parameterValueString = ParameterSetViewModel.GetDelimitedParameter(parameterValueString, "(", ")");
}
builder.Append($"-{parameter.Name} {parameterValueString} ");
}
return builder.ToString().Trim();
}
/// <summary>
/// Gets the individual parameter count of this parameterset.
/// </summary>
/// <returns>Return individual parameter count of this parameterset.</returns>
public int GetIndividualParameterCount()
{
if (this.Parameters == null || this.Parameters.Count == 0)
{
return 0;
}
int i = 0;
foreach (ParameterViewModel p in this.Parameters)
{
if (p.IsInSharedParameterSet)
{
return i;
}
i++;
}
return i;
}
#endregion
#region Internal Method
/// <summary>
/// Compare source parametermodel is equal like target parametermodel.
/// </summary>
/// <param name="source">The source of parametermodel.</param>
/// <param name="target">The target of parametermodel.</param>
/// <returns>Return compare result.</returns>
internal static int Compare(ParameterViewModel source, ParameterViewModel target)
{
if (source.Parameter.IsMandatory && !target.Parameter.IsMandatory)
{
return -1;
}
if (!source.Parameter.IsMandatory && target.Parameter.IsMandatory)
{
return 1;
}
return string.Compare(source.Parameter.Name, target.Parameter.Name);
}
#endregion
/// <summary>
/// Gets the delimited parameter if it needs delimitation and is not delimited.
/// </summary>
/// <param name="parameterValue">Value needing delimitation.</param>
/// <param name="openDelimiter">Open delimitation.</param>
/// <param name="closeDelimiter">Close delimitation.</param>
/// <returns>The delimited parameter if it needs delimitation and is not delimited.</returns>
private static string GetDelimitedParameter(string parameterValue, string openDelimiter, string closeDelimiter)
{
string parameterValueTrimmed = parameterValue.Trim();
if (parameterValueTrimmed.Length == 0)
{
return openDelimiter + parameterValue + closeDelimiter;
}
char delimitationChar = ParameterSetViewModel.ParameterNeedsDelimitation(parameterValueTrimmed, openDelimiter.Length == 1 && openDelimiter[0] == '{');
switch (delimitationChar)
{
case '1':
return openDelimiter + parameterValue + closeDelimiter;
case '\'':
return '\'' + parameterValue + '\'';
case '\"':
return '\"' + parameterValue + '\"';
default:
return parameterValueTrimmed;
}
}
/// <summary>
/// Returns '0' if the <paramref name="parameterValue"/> does not need delimitation, '1' if it does, and a quote character if it needs to be delimited with a quote.
/// </summary>
/// <param name="parameterValue">Parameter value to check.</param>
/// <param name="requireScriptblock">True if the parameter value should be a scriptblock.</param>
/// <returns>'0' if the parameter does not need delimitation, '1' if it needs, '\'' if it needs to be delimited with single quote and '\"' if it needs to be delimited with double quotes.</returns>
private static char ParameterNeedsDelimitation(string parameterValue, bool requireScriptblock)
{
Token[] tokens;
ParseError[] errors;
ScriptBlockAst values = Parser.ParseInput("commandName -parameterName " + parameterValue, out tokens, out errors);
if (values == null || values.EndBlock == null || values.EndBlock.Statements.Count == 0)
{
return '1';
}
PipelineAst pipeline = values.EndBlock.Statements[0] as PipelineAst;
if (pipeline == null || pipeline.PipelineElements.Count == 0)
{
return '1';
}
CommandAst commandAst = pipeline.PipelineElements[0] as CommandAst;
if (commandAst == null || commandAst.CommandElements.Count == 0)
{
return '1';
}
// 3 is for CommandName, Parameter and its value
if (commandAst.CommandElements.Count != 3)
{
return '1';
}
if (requireScriptblock)
{
ScriptBlockExpressionAst scriptAst = commandAst.CommandElements[2] as ScriptBlockExpressionAst;
return scriptAst == null ? '1' : '0';
}
StringConstantExpressionAst stringValue = commandAst.CommandElements[2] as StringConstantExpressionAst;
if (stringValue != null)
{
if (errors.Length == 0)
{
return '0';
}
char stringTerminationChar;
if (stringValue.StringConstantType == StringConstantType.BareWord)
{
stringTerminationChar = parameterValue[0];
}
else if (stringValue.StringConstantType == StringConstantType.DoubleQuoted || stringValue.StringConstantType == StringConstantType.DoubleQuotedHereString)
{
stringTerminationChar = '\"';
}
else
{
stringTerminationChar = '\'';
}
char oppositeTerminationChar = stringTerminationChar == '\"' ? '\'' : '\"';
// If the string is not terminated, it should be delimited by the opposite string termination character
return oppositeTerminationChar;
}
if (errors.Length != 0)
{
return '1';
}
return '0';
}
/// <summary>
/// Called to evaluate the value of AllMandatoryParametersHaveValues.
/// </summary>
private void EvaluateAllMandatoryParametersHaveValues()
{
bool newCanRun = true;
foreach (ParameterViewModel parameter in this.parameters)
{
if (!parameter.IsMandatory)
{
continue;
}
if (!parameter.HasValue)
{
newCanRun = false;
break;
}
}
this.AllMandatoryParametersHaveValues = newCanRun;
}
/// <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));
}
}
/// <summary>
/// Used to track changes to parameter values in order to verify the enabled state of buttons.
/// </summary>
/// <param name="sender">Event arguments.</param>
/// <param name="e">Event sender.</param>
private void MandatoryParameter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!e.PropertyName.Equals("Value", StringComparison.Ordinal))
{
return;
}
this.EvaluateAllMandatoryParametersHaveValues();
}
}
}
|