Skip to content

Commit fe25a05

Browse files
authored
Add -CountVariable parameter. (#352)
1 parent 43224d1 commit fe25a05

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/readme.graph.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,27 @@ directive:
469469
{
470470
return $;
471471
} else {
472-
// Add custom -PageSize parameter to *_List cmdlets that support Odata next link.
473472
let odataNextLinkRegex = /(^\s*)(if\s*\(\s*result.OdataNextLink\s*!=\s*null\s*\))/gmi
474473
if($.match(odataNextLinkRegex)) {
474+
// Add custom -PageSize parameter to *_List cmdlets that support Odata next link.
475475
$ = $.replace(odataNextLinkRegex, '$1if (result.OdataNextLink != null && this.ShouldIteratePages(this.InvocationInformation.BoundParameters, result.Value.Length))\n$1');
476476
477477
let psBaseClassImplementationRegex = /(\s*:\s*)(global::System.Management.Automation.PSCmdlet)/gmi
478478
$ = $.replace(psBaseClassImplementationRegex, '$1Microsoft.Graph.PowerShell.Cmdlets.Custom.ListCmdlet');
479479
480480
let beginProcessingRegex = /(^\s*)(protected\s*override\s*void\s*BeginProcessing\(\)\s*{)/gmi
481-
$ = $.replace(beginProcessingRegex, '$1$2\n$1 if (this.InvocationInformation?.BoundParameters != null){ InitializePaging(ref this.__invocationInfo, ref this._top); }\n$1');
481+
$ = $.replace(beginProcessingRegex, '$1$2\n$1 if (this.InvocationInformation?.BoundParameters != null){ InitializeCmdlet(ref this.__invocationInfo, ref this._top, ref this._count); }\n$1');
482482
483483
let odataNextLinkCallRegex = /(^\s*)(await\s*this\.Client\.UsersUserListUser_Call\(requestMessage\,\s*onOk\,\s*onDefault\,\s*this\,\s*Pipeline\)\;)/gmi
484484
$ = $.replace(odataNextLinkCallRegex, '$1requestMessage.RequestUri = GetOverflowItemsNextLinkUri(requestMessage.RequestUri);\n$1$2');
485+
486+
// Set -Count parameter to private. This will be replaced by -CountVariable
487+
let countParameterRegex = /public(\s*global::System\.Management\.Automation\.SwitchParameter\s*Count\s*)/gm
488+
$ = $.replace(countParameterRegex, 'private$1');
489+
490+
// Call OnBeforeWriteObject to deserialize '@odata.count' from the response object.
491+
let writeObjectRegex = /^(\s*)(WriteObject\(result\.Value,true\);)$/gm
492+
$ = $.replace(writeObjectRegex,'\n$1OnBeforeWriteObject(this.InvocationInformation.BoundParameters, result?.AdditionalProperties);\n$1$2');
485493
}
486494
return $;
487495
}
@@ -502,4 +510,18 @@ directive:
502510
$ = $.replace(propertyContainsRegex, '$1$2, System.StringComparer.OrdinalIgnoreCase$3');
503511
return $;
504512
}
513+
514+
# Serialize all $count parameter to lowercase true or false.
515+
- from: source-file-csharp
516+
where: $
517+
transform: >
518+
if (!$documentPath.match(/generated%5Capi%5C\w*.cs/gm))
519+
{
520+
return $;
521+
} else {
522+
// Add '.ToLower()' at the end of all 'Count.ToString()'
523+
let countRegex = /(Count\.ToString\(\))/gmi
524+
$ = $.replace(countRegex, '$1.ToLower()');
525+
return $;
526+
}
505527
```

tools/Custom/ListCmdlet.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
3434
[global::Microsoft.Graph.PowerShell.Category(global::Microsoft.Graph.PowerShell.ParameterCategory.Runtime)]
3535
public global::System.Management.Automation.SwitchParameter All { get => this._all; set => this._all = value; }
3636

37+
// <summary>Backing field for <see cref="CountVariable" /> property.</summary>
38+
private string _countVariable;
39+
40+
/// <summary>Specifies a count of the total number of items in a collection. </summary>
41+
[global::System.Management.Automation.Parameter(Mandatory = false, HelpMessage = "Specifies a count of the total number of items in a collection. By default, this variable will be set in the global scope.")]
42+
[Microsoft.Graph.PowerShell.Runtime.Info(
43+
Required = false,
44+
ReadOnly = false,
45+
Description = @"Specifies a count of the total number of items in a collection. By default, this variable will be set in the global scope.",
46+
PossibleTypes = new[] { typeof(string) })]
47+
[global::Microsoft.Graph.PowerShell.Category(global::Microsoft.Graph.PowerShell.ParameterCategory.Runtime)]
48+
[global::System.Management.Automation.Alias("CV")]
49+
public string CountVariable { get => this._countVariable; set => this._countVariable = value; }
50+
3751
/// <summary>
3852
/// Default number of items per page.
3953
/// </summary>
@@ -75,7 +89,7 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
7589
/// </summary>
7690
/// <param name="invocationInfo">A reference to <see cref="System.Management.Automation.InvocationInfo"/> object.</param>
7791
/// <param name="top">A reference to top parameter.</param>
78-
public void InitializePaging(ref global::System.Management.Automation.InvocationInfo invocationInfo, ref int top)
92+
public void InitializeCmdlet(ref global::System.Management.Automation.InvocationInfo invocationInfo, ref int top, ref global::System.Management.Automation.SwitchParameter count)
7993
{
8094
if (invocationInfo.BoundParameters.ContainsKey("PageSize") && (PageSize > MaxPageSize || PageSize == default))
8195
{
@@ -110,6 +124,13 @@ public void InitializePaging(ref global::System.Management.Automation.Invocation
110124
requiredPages = limit / currentPageSize;
111125
overflowItemsCount = limit % currentPageSize;
112126
}
127+
128+
if ((!invocationInfo.BoundParameters.ContainsKey("Count")) && invocationInfo.BoundParameters.ContainsKey("CountVariable"))
129+
{
130+
// Set Count to true when CountVariable is set.
131+
invocationInfo.BoundParameters["Count"] = true;
132+
count = true;
133+
}
113134
}
114135

115136
/// <summary>
@@ -150,5 +171,20 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary<str
150171
}
151172
return nextLinkUri.Uri;
152173
}
174+
175+
internal void OnBeforeWriteObject(global::System.Collections.Generic.Dictionary<string, object> boundParameters, global::System.Collections.Generic.IDictionary<string, object> additionalProperties)
176+
{
177+
// Get odata.count from the response.
178+
if (boundParameters.ContainsKey("CountVariable") &&
179+
additionalProperties != null &&
180+
additionalProperties.TryGetValue("@odata.count", out var odataCount))
181+
{
182+
// Save the Count back to the PS environment in a global variable.
183+
// We need to store count in a global variable since these cmdlets are exported as functions.
184+
// i.e. Functions can't modify parent scope.
185+
var psVI = SessionState.PSVariable;
186+
psVI.Set(new PSVariable(CountVariable.Contains(":") ? CountVariable : $"global:{CountVariable}", odataCount));
187+
}
188+
}
153189
}
154190
}

0 commit comments

Comments
 (0)