Skip to content

Commit c6b8a6f

Browse files
committed
Corrected formatting
1 parent 5457456 commit c6b8a6f

File tree

1 file changed

+106
-101
lines changed

1 file changed

+106
-101
lines changed

tools/ImportExamples.ps1

Lines changed: 106 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
Param(
44
$ModulesToGenerate = @(),
55
$Available = @(),
6-
[hashtable]$V1CommandGetVariantList= @{},
7-
[hashtable]$BetaCommandGetVariantList= @{},
8-
[hashtable]$V1CommandListVariantList= @{},
9-
[hashtable]$BetaCommandListVariantList= @{},
6+
[hashtable]$V1CommandGetVariantList = @{},
7+
[hashtable]$BetaCommandGetVariantList = @{},
8+
[hashtable]$V1CommandListVariantList = @{},
9+
[hashtable]$BetaCommandListVariantList = @{},
1010
[string] $ModuleMappingConfigPath = (Join-Path $PSScriptRoot "..\config\ModulesMapping.jsonc"),
1111
[string] $FolderForExamplesToBeReviewed = (Join-Path $PSScriptRoot "..\examplesreport"),
1212
[string] $ExamplesToBeReviewed = "ExamplesToBeReviewed.csv",
@@ -86,21 +86,22 @@ function Get-Files {
8686

8787
#Extract command over here
8888
$Command = [System.IO.Path]::GetFileNameWithoutExtension($File)
89-
#Extract URI path
90-
$CommandValue = $null
91-
if($GraphProfile -eq "beta"){
92-
$CommandValue = $BetaCommandGetVariantList[$Command]
89+
#Extract URI path
90+
$CommandValue = $null
91+
if ($GraphProfile -eq "beta") {
92+
$CommandValue = $BetaCommandGetVariantList[$Command]
9393

94-
}else{
95-
$CommandValue = $V1CommandGetVariantList[$Command]
96-
}
94+
}
95+
else {
96+
$CommandValue = $V1CommandGetVariantList[$Command]
97+
}
9798

98-
if ($CommandValue) {
99-
$CommandValueParams = $CommandValue.Split(",")
100-
$ApiPath = $CommandValueParams[0]
101-
$Method = $CommandValueParams[1]
102-
Get-ExternalDocsUrl -GraphProfile $GraphProfile -Url -UriPath $ApiPath -Command $Command -OpenApiContent $OpenApiContent -GraphProfilePath $GraphProfilePath -Method $Method.Trim() -Module $Module
103-
}
99+
if ($CommandValue) {
100+
$CommandValueParams = $CommandValue.Split(",")
101+
$ApiPath = $CommandValueParams[0]
102+
$Method = $CommandValueParams[1]
103+
Get-ExternalDocsUrl -GraphProfile $GraphProfile -Url -UriPath $ApiPath -Command $Command -OpenApiContent $OpenApiContent -GraphProfilePath $GraphProfilePath -Method $Method.Trim() -Module $Module
104+
}
104105

105106
}
106107
}
@@ -142,12 +143,12 @@ function Get-ExternalDocsUrl {
142143
if ($OpenApiContent.openapi && $OpenApiContent.info.version) {
143144
foreach ($Path in $OpenApiContent.paths) {
144145
$ExternalDocUrl = $null
145-
switch($Method){
146+
switch ($Method) {
146147
"GET" {
147148
$ExternalDocUrl = $path[$UriPath].get.externalDocs.url
148149
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
149150
$GETApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
150-
if(-not([string]::IsNullOrEmpty($GETApiPath))){
151+
if (-not([string]::IsNullOrEmpty($GETApiPath))) {
151152
$ExternalDocUrl = $Path[$GETApiPath].get.externalDocs.url
152153
}
153154
}
@@ -156,7 +157,7 @@ function Get-ExternalDocsUrl {
156157
$ExternalDocUrl = $Path[$UriPath].post.externalDocs.url
157158
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
158159
$POSTApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
159-
if(-not([string]::IsNullOrEmpty($POSTApiPath))){
160+
if (-not([string]::IsNullOrEmpty($POSTApiPath))) {
160161
$ExternalDocUrl = $Path[$POSTApiPath].post.externalDocs.url
161162
}
162163
}
@@ -165,7 +166,7 @@ function Get-ExternalDocsUrl {
165166
$ExternalDocUrl = $Path[$UriPath].patch.externalDocs.url
166167
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
167168
$PATCHApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
168-
if(-not([string]::IsNullOrEmpty($PATCHApiPath))){
169+
if (-not([string]::IsNullOrEmpty($PATCHApiPath))) {
169170
$ExternalDocUrl = $Path[$PATCHApiPath].patch.externalDocs.url
170171
}
171172
}
@@ -174,7 +175,7 @@ function Get-ExternalDocsUrl {
174175
$ExternalDocUrl = $Path[$UriPath].delete.externalDocs.url
175176
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
176177
$DELETEApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
177-
if(-not([string]::IsNullOrEmpty($DELETEApiPath))){
178+
if (-not([string]::IsNullOrEmpty($DELETEApiPath))) {
178179
$ExternalDocUrl = $Path[$DELETEApiPath].delete.externalDocs.url
179180
}
180181
}
@@ -183,7 +184,7 @@ function Get-ExternalDocsUrl {
183184
$ExternalDocUrl = $Path[$UriPath].put.externalDocs.url
184185
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
185186
$PUTApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
186-
if(-not([string]::IsNullOrEmpty($PUTApiPath))){
187+
if (-not([string]::IsNullOrEmpty($PUTApiPath))) {
187188
$ExternalDocUrl = $Path[$PUTApiPath].put.externalDocs.url
188189
}
189190
}
@@ -200,20 +201,21 @@ function Get-ExternalDocsUrl {
200201
}
201202

202203
}
203-
function Extract-PathFromListVariant{
204+
function Extract-PathFromListVariant {
204205
param(
205206
[ValidateSet("beta", "v1.0")]
206207
[string] $GraphProfile = "v1.0",
207208
[string] $Command = "Get-MgUser"
208209
)
209210
$ListApiPath = $null
210211
$ListCommandValue = $null
211-
if($GraphProfile -eq "beta"){
212+
if ($GraphProfile -eq "beta") {
212213
$ListCommandValue = $BetaCommandListVariantList[$Command]
213-
}else{
214+
}
215+
else {
214216
$ListCommandValue = $V1CommandListVariantList[$Command]
215217
}
216-
if(-not([string]::IsNullOrEmpty($ListCommandValue))){
218+
if (-not([string]::IsNullOrEmpty($ListCommandValue))) {
217219
$ListCommandValueParams = $ListCommandValue.Split(",")
218220
$ListApiPath = $ListCommandValueParams[0]
219221
}
@@ -233,8 +235,8 @@ function Start-WebScrapping {
233235
)
234236
$ExampleFile = "$GraphProfilePath/$Command.md"
235237
$url = $ExternalDocUrl
236-
if($GraphProfile -eq "beta"){
237-
$url = $url.Replace("graph-rest-1.0","graph-rest-beta")
238+
if ($GraphProfile -eq "beta") {
239+
$url = $url.Replace("graph-rest-1.0", "graph-rest-beta")
238240
}
239241
$DescriptionCommand = $Command
240242
$Description = "This example shows how to use the $DescriptionCommand Cmdlet.`r`n`r`To learn about permissions for this resource, see the [permissions reference](/graph/permissions-reference)."
@@ -314,12 +316,13 @@ function Update-ExampleFile {
314316
for ($d = 0; $d -lt $HeaderList.Count; $d++) {
315317
$CodeValue = $ExampleList[$d].Trim()
316318
if ($CodeValue -match "\b$CommandPattern\b") {
317-
$TitleValue = "### " + $HeaderList[$d].Trim()
318-
$Code = "``````powershell`r$CodeValue`r`n``````"
319-
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
320-
Add-Content -Path $ExampleFile -Value $TotalText
321-
$ContainsRightExamples = $True
322-
}else{
319+
$TitleValue = "### " + $HeaderList[$d].Trim()
320+
$Code = "``````powershell`r$CodeValue`r`n``````"
321+
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
322+
Add-Content -Path $ExampleFile -Value $TotalText
323+
$ContainsRightExamples = $True
324+
}
325+
else {
323326
$WrongExamplesCount++
324327
$SkippedExample++
325328

@@ -329,49 +332,52 @@ function Update-ExampleFile {
329332
#The code below updates existing examples
330333
#------------------------------------------------------------#
331334
$PatternToSearch = "Import-Module Microsoft.Graph.$Module"
332-
if($GraphProfile -eq "beta"){
335+
if ($GraphProfile -eq "beta") {
333336
$PatternToSearch = "Import-Module Microsoft.Graph.Beta.$Module"
334337
}
335-
if(($Content | Select-String -pattern $SearchText) -and ($Content | Select-String -pattern "This example shows")){
338+
if (($Content | Select-String -pattern $SearchText) -and ($Content | Select-String -pattern "This example shows")) {
336339
$ContainsPatternToSearch = $False
337-
foreach($List in $ExampleList){
338-
if($List.Contains($PatternToSearch) -and $List.Contains($CommandPattern)){
339-
$ContainsPatternToSearch = $True
340-
}
340+
foreach ($List in $ExampleList) {
341+
if ($List.Contains($PatternToSearch) -and $List.Contains($CommandPattern)) {
342+
$ContainsPatternToSearch = $True
343+
}
341344
}
342-
if($ContainsPatternToSearch){
345+
if ($ContainsPatternToSearch) {
343346
Clear-Content $ExampleFile -Force
344-
for ($d = 0; $d -lt $HeaderList.Count; $d++) {
345-
#We should only add the correct examples from external docs link
346-
if($ExampleList[$d] -match "\b$CommandPattern\b"){
347-
$CodeValue = $ExampleList[$d].Trim()
348-
$TitleValue = "### " + $HeaderList[$d].Trim()
349-
$Code = "``````powershell`r$CodeValue`r`n``````"
350-
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
351-
Add-Content -Path $ExampleFile -Value $TotalText
352-
}else{
353-
$SkippedExample++
347+
for ($d = 0; $d -lt $HeaderList.Count; $d++) {
348+
#We should only add the correct examples from external docs link
349+
if ($ExampleList[$d] -match "\b$CommandPattern\b") {
350+
$CodeValue = $ExampleList[$d].Trim()
351+
$TitleValue = "### " + $HeaderList[$d].Trim()
352+
$Code = "``````powershell`r$CodeValue`r`n``````"
353+
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
354+
Add-Content -Path $ExampleFile -Value $TotalText
355+
}
356+
else {
357+
$SkippedExample++
354358

355-
}
359+
}
356360

357-
}
361+
}
358362

359363

360-
}else{
364+
}
365+
else {
361366
Clear-Content $ExampleFile -Force
362-
if($Content | Select-String -pattern $CommandPattern){
367+
if ($Content | Select-String -pattern $CommandPattern) {
363368
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile
364-
}else{
365-
#Replace everything with boiler plate code
369+
}
370+
else {
371+
#Replace everything with boiler plate code
366372
$DefaultBoilerPlate = "### Example 1: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n### Example 2: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n"
367373
Add-Content -Path $ExampleFile -Value $DefaultBoilerPlate.Trim()
368374
}
369375
}
370376

371377
}
372378
$CheckIfFileEmpty = Test-FileEmpty $ExampleFile
373-
if($CheckIfFileEmpty){
374-
if($Content){
379+
if ($CheckIfFileEmpty) {
380+
if ($Content) {
375381
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile
376382
}
377383
}
@@ -380,18 +386,18 @@ function Update-ExampleFile {
380386
#some examples are wrong(which are left out) and some are right
381387
#-----------------------------------------------------------------------------------------------#
382388
$AvailableCorrectExamples = 1
383-
if($SkippedExample -gt -1){
389+
if ($SkippedExample -gt -1) {
384390
$NewContent = Get-Content -Path $ExampleFile
385-
foreach($C in $NewContent){
386-
if($C.Contains("Example")){
391+
foreach ($C in $NewContent) {
392+
if ($C.Contains("Example")) {
387393
$SearchString = $c.Split(":")
388-
$StringToReplace = $SearchString[0]
394+
$StringToReplace = $SearchString[0]
389395
$ReplacementString = "### Example $AvailableCorrectExamples"
390396
(Get-Content -Path $ExampleFile) -replace $StringToReplace, $ReplacementString | Set-Content $ExampleFile
391397
$AvailableCorrectExamples++
392398
}
393399
}
394-
if(-not(Test-Path -PathType Container $FolderForExamplesToBeReviewed)){
400+
if (-not(Test-Path -PathType Container $FolderForExamplesToBeReviewed)) {
395401
New-Item -ItemType Directory -Force -Path $FolderForExamplesToBeReviewed
396402
}
397403
if (-not (Test-Path "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed")) {
@@ -405,7 +411,7 @@ function Update-ExampleFile {
405411
}
406412
}
407413
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------#
408-
if(($WrongExamplesCount -gt 0) -and -not($ContainsRightExamples)){
414+
if (($WrongExamplesCount -gt 0) -and -not($ContainsRightExamples)) {
409415
Clear-Content $ExampleFile -Force
410416
$DefaultBoilerPlate = "### Example 1: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n### Example 2: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n"
411417
Add-Content -Path $ExampleFile -Value $DefaultBoilerPlate.Trim()
@@ -414,7 +420,7 @@ function Update-ExampleFile {
414420
#The artifact below will be ignored on git.
415421
#You can download the artificat from the generator pipeline
416422

417-
if(-not(Test-Path -PathType Container $FolderForExamplesToBeReviewed)){
423+
if (-not(Test-Path -PathType Container $FolderForExamplesToBeReviewed)) {
418424
New-Item -ItemType Directory -Force -Path $FolderForExamplesToBeReviewed
419425
}
420426
if (-not (Test-Path "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed")) {
@@ -433,56 +439,55 @@ function Test-FileEmpty {
433439

434440
Param ([Parameter(Mandatory = $true)][string]$File)
435441

436-
if ((Test-Path -LiteralPath $file) -and !((Get-Content -LiteralPath $file -Raw) -match '\S')) {return $true} else {return $false}
442+
if ((Test-Path -LiteralPath $file) -and !((Get-Content -LiteralPath $file -Raw) -match '\S')) { return $true } else { return $false }
437443

438444
}
439445
function Retain-ExistingCorrectExamples {
440446

441-
Param ([Parameter(Mandatory = $true)][object]$Content,[Parameter(Mandatory = $true)][string]$File)
442-
$CorrectMaintainableHeaderCount = 1;
443-
$LineCounter = 0;
444-
$Header = $null
445-
foreach($Line in $Content){
447+
Param ([Parameter(Mandatory = $true)][object]$Content, [Parameter(Mandatory = $true)][string]$File)
448+
$CorrectMaintainableHeaderCount = 1;
449+
$LineCounter = 0;
450+
$Header = $null
451+
foreach ($Line in $Content) {
446452

447-
if($Line.StartsWith("$CommandPattern ")){
448-
$CorrectMaintainableHeaderCount++
449-
break
450-
}
451-
$LineCounter++
453+
if ($Line.StartsWith("$CommandPattern ")) {
454+
$CorrectMaintainableHeaderCount++
455+
break
452456
}
457+
$LineCounter++
458+
}
453459

454-
for($j = 0; $j -lt $LineCounter+1; $j++){
455-
$Val = $Content[$j]
456-
$Header+= "$Val`r`n"
457-
}
458-
if(($Null -eq $Header) -or ($Header -eq "")){
459-
$Header = "### Example " + $CorrectMaintainableHeaderCount + ": Code snippet"
460-
}
460+
for ($j = 0; $j -lt $LineCounter + 1; $j++) {
461+
$Val = $Content[$j]
462+
$Header += "$Val`r`n"
463+
}
464+
if (($Null -eq $Header) -or ($Header -eq "")) {
465+
$Header = "### Example " + $CorrectMaintainableHeaderCount + ": Code snippet"
466+
}
461467

462-
$MaintainedCorrectExample = "$Header```````n$Description`r`n"
463-
$CorrectMaintainableHeaderCount++
464-
Add-Content -Path $File -Value $MaintainedCorrectExample
468+
$MaintainedCorrectExample = "$Header```````n$Description`r`n"
469+
$CorrectMaintainableHeaderCount++
470+
Add-Content -Path $File -Value $MaintainedCorrectExample
465471
}
466472
$JsonContent = Get-Content -Path $MetaDataJsonFile
467473
$DeserializedContent = $JsonContent | ConvertFrom-Json
468-
foreach($Data in $DeserializedContent)
469-
{
470-
if($Data.ApiVersion -eq "beta")
471-
{
472-
if((-not($Data.Variants[0].Contains("List")))){
473-
$BetaAPIPathAndMethod = $Data.Uri,$Data.Method -join ","
474+
foreach ($Data in $DeserializedContent) {
475+
if ($Data.ApiVersion -eq "beta") {
476+
if ((-not($Data.Variants[0].Contains("List")))) {
477+
$BetaAPIPathAndMethod = $Data.Uri, $Data.Method -join ","
474478
$Beta = $BetaCommandGetVariantList.Add($Data.Command, $BetaAPIPathAndMethod)
475-
}else{
479+
}
480+
else {
476481
$Beta1 = $BetaCommandListVariantList.Add($Data.Command, $BetaAPIPathAndMethod)
477482
}
478483
}
479484

480-
if($Data.ApiVersion -eq "v1.0")
481-
{
482-
$V1APIPathAndMethod = $Data.Uri,$Data.Method -join ","
483-
if((-not($Data.Variants[0].Contains("List")))){
485+
if ($Data.ApiVersion -eq "v1.0") {
486+
$V1APIPathAndMethod = $Data.Uri, $Data.Method -join ","
487+
if ((-not($Data.Variants[0].Contains("List")))) {
484488
$V1 = $V1CommandGetVariantList.Add($Data.Command, $V1APIPathAndMethod)
485-
}else{
489+
}
490+
else {
486491
$V11 = $V1CommandListVariantList.Add($Data.Command, $V1APIPathAndMethod)
487492
}
488493
}
@@ -503,7 +508,7 @@ if ($ModulesToGenerate.Count -eq 0) {
503508
[HashTable] $ModuleMapping = Get-Content $ModuleMappingConfigPath | ConvertFrom-Json -AsHashTable
504509
$ModulesToGenerate = $ModuleMapping.Keys
505510
}
506-
Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
511+
#Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
507512

508513
#Comment the above and uncomment the below start command, if you manually want to manually pass ExternalDocs url.
509514
#This is for scenarios where the correponding external docs url to the uri path gotten from Find-MgGraph command, is missing on the openapi.yml file for a particular module.

0 commit comments

Comments
 (0)