Skip to content

Commit d24ae9c

Browse files
committed
Modified import script
1 parent cdc4f05 commit d24ae9c

File tree

1 file changed

+88
-26
lines changed

1 file changed

+88
-26
lines changed

tools/ImportExamples.ps1

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Start-Generator {
2424
)
2525

2626
$GraphMapping = @{
27-
"v1.0" = "v1.0\examples"
27+
#"v1.0" = "v1.0\examples"
2828
"beta" = "beta\examples"
2929
}
3030
if ($GenerationMode -eq "auto") {
@@ -365,7 +365,7 @@ function Update-ExampleFile {
365365
else {
366366
Clear-Content $ExampleFile -Force
367367
if ($Content | Select-String -pattern $CommandPattern) {
368-
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile
368+
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $CommandPattern
369369
}
370370
else {
371371
#Replace everything with boiler plate code
@@ -378,7 +378,7 @@ function Update-ExampleFile {
378378
$CheckIfFileEmpty = Test-FileEmpty $ExampleFile
379379
if ($CheckIfFileEmpty) {
380380
if ($Content) {
381-
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile
381+
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $CommandPattern
382382
}
383383
}
384384
#----------------------------------------------------------------------------------------------#
@@ -442,33 +442,95 @@ function Test-FileEmpty {
442442
if ((Test-Path -LiteralPath $file) -and !((Get-Content -LiteralPath $file -Raw) -match '\S')) { return $true } else { return $false }
443443

444444
}
445-
function Retain-ExistingCorrectExamples {
446445

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) {
452-
453-
if ($Line.StartsWith("$CommandPattern ")) {
454-
$CorrectMaintainableHeaderCount++
455-
break
446+
function Retain-ExistingCorrectExamples {
447+
Param (
448+
[object]$Content,
449+
[string]$File,
450+
[string]$CommandPattern
451+
)
452+
$RetainedExamples = New-Object Collections.Generic.List[string]
453+
$End = 0
454+
$NoOfExamples = 0
455+
foreach ($C in $Content) {
456+
if ($C.StartsWith("### Example")) {
457+
$NoOfExamples++
456458
}
457-
$LineCounter++
459+
$End++
458460
}
461+
Get-ExistingCorrectExamples -Content $Content -File $File -CommandPattern $CommandPattern -start 0 -end $End -NoOfExamples $NoOfExamples
462+
$TitleCount = 1
463+
$RetainedContent = $null
464+
foreach ($Ex in $RetainedExamples) {
465+
$ContentBody = $Ex.Split("|")[0]
466+
$ContentTitle = $Ex.Split("|")[1]
467+
$ContentDescription = $Ex.Split("|")[2]
468+
if ($ContentBody -match "\b$CommandPattern\b") {
469+
$Val = $ContentTitle.Split("### Example ")
470+
$ToBeReplaced = $Val[1].Substring(0, 1)
471+
$ModifiedTitle = $ContentTitle.Replace($ToBeReplaced, $TitleCount)
472+
$ContentBody = $ContentBody.Replace($ContentTitle, $ModifiedTitle)
473+
$RetainedContent += "$ContentBody$ContentDescription"
474+
$TitleCount++
475+
}
476+
477+
}
478+
Set-Content -Path $File -Value $RetainedContent
479+
#Remove the last two empty lines at the end of the file
480+
$Stream = [IO.File]::OpenWrite($ExampleFile)
481+
$Stream.SetLength($stream.Length - 2)
482+
$Stream.Close()
483+
$Stream.Dispose()
484+
$RetainedExamples.Clear()
485+
}
486+
function Get-ExistingCorrectExamples {
459487

460-
for ($j = 0; $j -lt $LineCounter + 1; $j++) {
461-
$Val = $Content[$j]
462-
$Header += "$Val`r`n"
488+
Param (
489+
[object]$Content,
490+
[string]$File,
491+
[string]$CommandPattern,
492+
[int]$Start,
493+
[int]$End,
494+
[int]$NoOfExamples
495+
)
496+
$Title = $null
497+
$ContentBlock = $null
498+
499+
for ($i = $Start; $i -lt $End; $i++) {
500+
$Value = $Content[$i]
501+
$ContentBlock += "$Value`n"
502+
if ($Content[$i].StartsWith("### Example")) {
503+
$Title = $Content[$i]
504+
}
505+
if ($Content[$i].EndsWith("``")) {
506+
$Start = $i
507+
break;
508+
}
463509
}
464-
if (($Null -eq $Header) -or ($Header -eq "")) {
465-
$Header = "### Example " + $CorrectMaintainableHeaderCount + ": Code snippet"
510+
$RetainedDescription = $null
511+
for ($j = $Start + 1; $j -lt $end; $j++) {
512+
513+
if ($Content[$j].StartsWith("### Example")) {
514+
break;
515+
}
516+
$DescVal = $Content[$j]
517+
$RetainedDescription += "$DescVal`n"
466518
}
467-
468-
$MaintainedCorrectExample = "$Header```````n$Description`r`n"
469-
$CorrectMaintainableHeaderCount++
470-
Add-Content -Path $File -Value $MaintainedCorrectExample
471-
}
519+
$RetainedExamples.Add("$ContentBlock|$Title|$RetainedDescription")
520+
if ($NoOfExamples -gt 1) {
521+
$NoOfExamples--
522+
for ($k = $Start; $k -lt $End; $k++) {
523+
if ($Content[$k].StartsWith("### Example")) {
524+
$Start = $k
525+
break;
526+
}
527+
}
528+
529+
Get-ExistingCorrectExamples -Content $Content -File $File -CommandPattern $CommandPattern -start $Start -end $End -NoOfExamples $NoOfExamples
530+
}
531+
532+
}
533+
$RetainedExamples = New-Object Collections.Generic.List[string]
472534
$JsonContent = Get-Content -Path $MetaDataJsonFile
473535
$DeserializedContent = $JsonContent | ConvertFrom-Json
474536
foreach ($Data in $DeserializedContent) {
@@ -508,7 +570,7 @@ if ($ModulesToGenerate.Count -eq 0) {
508570
[HashTable] $ModuleMapping = Get-Content $ModuleMappingConfigPath | ConvertFrom-Json -AsHashTable
509571
$ModulesToGenerate = $ModuleMapping.Keys
510572
}
511-
#Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
573+
Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
512574

513575
#Comment the above and uncomment the below start command, if you manually want to manually pass ExternalDocs url.
514576
#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.
@@ -528,4 +590,4 @@ if ($ModulesToGenerate.Count -eq 0) {
528590

529591
#4. Test for beta updates from api reference
530592
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/serviceprincipal-post-approleassignedto?view=graph-rest-beta" -GraphCommand "New-MgBetaServicePrincipalAppRoleAssignedTo" -GraphModule "Applications" -Profile "beta"
531-
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/meetingattendancereport-list?view=graph-rest-1.0" -GraphCommand "Update-MgBetaUserSettingShiftPreference" -GraphModule "Users" -Profile "beta"
593+
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/meetingattendancereport-list?view=graph-rest-1.0" -GraphCommand "New-MgBetaUserEventAttachment" -GraphModule "Calendar" -Profile "beta"

0 commit comments

Comments
 (0)