Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 138 additions & 103 deletions tools/ExamplesGenerator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ function Get-Files {
#Extract command over here
$Command = [System.IO.Path]::GetFileNameWithoutExtension($File)
#Extract URI path
$UriPath = $null
$CommandValue = $null
if($GraphProfile -eq "beta"){
$UriPath = $BetaCommandGetVariantList[$Command]
$CommandValue = $BetaCommandGetVariantList[$Command]

}else{
$UriPath = $V1CommandGetVariantList[$Command]
$CommandValue = $V1CommandGetVariantList[$Command]
}

if ($UriPath) {
$Method = $UriPaths.Method
Get-ExternalDocsUrl -GraphProfile $GraphProfile -Url -UriPath $UriPath -Command $Command -OpenApiContent $OpenApiContent -GraphProfilePath $GraphProfilePath -Method $Method -Module $Module
}
if ($CommandValue) {
$CommandValueParams = $CommandValue.Split(",")
$ApiPath = $CommandValueParams[0]
$Method = $CommandValueParams[1]
Get-ExternalDocsUrl -GraphProfile $GraphProfile -Url -UriPath $ApiPath -Command $Command -OpenApiContent $OpenApiContent -GraphProfilePath $GraphProfilePath -Method $Method.Trim() -Module $Module
}

}
}
Expand Down Expand Up @@ -125,7 +128,6 @@ function Get-ExternalDocsUrl {
[System.Object] $Method = "GET",
[string] $GraphProfilePath = (Join-Path $PSScriptRoot "..\src\Users\v1.0\examples")
)

if ($GenerationMode -eq "manual") {

if (-not([string]::IsNullOrEmpty($ManualExternalDocsUrl))) {
Expand All @@ -139,94 +141,54 @@ function Get-ExternalDocsUrl {

if ($OpenApiContent.openapi && $OpenApiContent.info.version) {
foreach ($Path in $OpenApiContent.paths) {
$MethodName = $Method | Out-String
$ExternalDocUrl = $Path[$UriPath].values.externalDocs.url

if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$PathSplit = $UriPath.Split("/")
$PathToAppend = $PathSplit[$PathSplit.Count - 1]
if ($PathToAppend.StartsWith("{") -or $PathToAppend.StartsWith("$")) {
#skip
}
else {
$PathRebuild = "/" + $PathSplit[0]
for ($i = 1; $i -lt $PathSplit.Count - 1; $i++) {
$PathRebuild += $PathSplit[$i] + "/"
$ExternalDocUrl = $null
switch($Method){
"GET" {
$ExternalDocUrl = $path[$UriPath].get.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$GETApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
if(-not([string]::IsNullOrEmpty($GETApiPath))){
$ExternalDocUrl = $Path[$GETApiPath].get.externalDocs.url
}
}
$RebuiltPath = $PathRebuild + "microsoft.graph." + $PathToAppend
$ExternalDocUrl = $path[$RebuiltPath].get.externalDocs.url
}
"POST" {
$ExternalDocUrl = $Path[$UriPath].post.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$UriPath2 = $null
if($GraphProfile -eq "beta"){
$UriPath2 = $BetaCommandListVariantList[$Command]
}else{
$UriPath2 = $V1CommandListVariantList[$Command]
}
if(-not([string]::IsNullOrEmpty($UriPath2))){
$ExternalDocUrl = $Path[$UriPath2].get.externalDocs.url
}

$POSTApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
if(-not([string]::IsNullOrEmpty($POSTApiPath))){
$ExternalDocUrl = $Path[$POSTApiPath].post.externalDocs.url
}
}
}
"PATCH" {
$ExternalDocUrl = $Path[$UriPath].patch.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$PATCHApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
if(-not([string]::IsNullOrEmpty($PATCHApiPath))){
$ExternalDocUrl = $Path[$PATCHApiPath].patch.externalDocs.url
}
}
}
}
if ($MethodName -eq "POST") {
$ExternalDocUrl = $Path[$UriPath].post.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$UriPath3 = $null
if($GraphProfile -eq "beta"){
$UriPath3 = $BetaCommandListVariantList[$Command]
}else{
$UriPath3 = $V1CommandListVariantList[$Command]
"DELETE" {
$ExternalDocUrl = $Path[$UriPath].delete.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$DELETEApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
if(-not([string]::IsNullOrEmpty($DELETEApiPath))){
$ExternalDocUrl = $Path[$DELETEApiPath].delete.externalDocs.url
}
}
if(-not([string]::IsNullOrEmpty($UriPath3))){
$ExternalDocUrl = $Path[$UriPath3].post.externalDocs.url
}
}
}

if ($MethodName -eq "PATCH") {
$ExternalDocUrl = $Path[$UriPath].patch.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$UriPath4 = $null
if($GraphProfile -eq "beta"){
$UriPath4 = $BetaCommandListVariantList[$Command]
}else{
$UriPath4 = $V1CommandListVariantList[$Command]
}
if(-not([string]::IsNullOrEmpty($UriPath4))){
$ExternalDocUrl = $Path[$UriPath4].post.externalDocs.url
}
}
}

if ($MethodName -eq "DELETE") {
$ExternalDocUrl = $Path[$UriPath].delete.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$UriPath5 = $null
if($GraphProfile -eq "beta"){
$UriPath5 = $BetaCommandListVariantList[$Command]
}else{
$UriPath5 = $V1CommandListVariantList[$Command]
}
if(-not([string]::IsNullOrEmpty($UriPath5))){
$ExternalDocUrl = $Path[$UriPath5].post.externalDocs.url
}
}
}

if ($MethodName -eq "PUT") {
$ExternalDocUrl = $Path[$UriPath].put.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$UriPath6 = $null
if($GraphProfile -eq "beta"){
$UriPath6 = $BetaCommandListVariantList[$Command]
}else{
$UriPath6 = $V1CommandListVariantList[$Command]
}
"PUT" {
$ExternalDocUrl = $Path[$UriPath].put.externalDocs.url
if ([string]::IsNullOrEmpty($ExternalDocUrl)) {
$PUTApiPath = Extract-PathFromListVariant -GraphProfile $GraphProfile -Command $Command
if(-not([string]::IsNullOrEmpty($PUTApiPath))){
$ExternalDocUrl = $Path[$PUTApiPath].put.externalDocs.url
}
}
if(-not([string]::IsNullOrEmpty($UriPath6))){
$ExternalDocUrl = $Path[$UriPath6].post.externalDocs.url
}
}
}

}
if (-not([string]::IsNullOrEmpty($ExternalDocUrl))) {
Start-WebScrapping -GraphProfile $GraphProfile -ExternalDocUrl $ExternalDocUrl -Command $Command -GraphProfilePath $GraphProfilePath -UriPath $UriPath -Module $Module
Expand All @@ -238,6 +200,25 @@ function Get-ExternalDocsUrl {
}

}
function Extract-PathFromListVariant{
param(
[ValidateSet("beta", "v1.0")]
[string] $GraphProfile = "v1.0",
[string] $Command = "Get-MgUser"
)
$ListApiPath = $null
$ListCommandValue = $null
if($GraphProfile -eq "beta"){
$ListCommandValue = $BetaCommandListVariantList[$Command]
}else{
$ListCommandValue = $V1CommandListVariantList[$Command]
}
if(-not([string]::IsNullOrEmpty($ListCommandValue))){
$ListCommandValueParams = $ListCommandValue.Split(",")
$ListApiPath = $ListCommandValueParams[0]
}
return $ListApiPath
}
function Start-WebScrapping {
param(
[ValidateSet("beta", "v1.0")]
Expand All @@ -252,8 +233,12 @@ function Start-WebScrapping {
)
$ExampleFile = "$GraphProfilePath/$Command.md"
$url = $ExternalDocUrl

$Description = "This example shows how to use the $Command Cmdlet.`r`n`r`To learn about permissions for this resource, see the [permissions reference](/graph/permissions-reference)."
$DescriptionCommand = $Command
if($GraphProfile -eq "beta"){
$DescriptionCommand= $Command.Replace("-MgBeta", "-Mg")
}

$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)."
$WebResponse = Invoke-WebRequest -Uri $url
$HeaderList = New-Object -TypeName 'System.Collections.ArrayList';
$ExampleList = New-Object -TypeName 'System.Collections.ArrayList';
Expand Down Expand Up @@ -296,7 +281,10 @@ function Update-ExampleFile {
[ValidateNotNullOrEmpty()]
[string] $ExternalDocUrl = "https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=powershell"
)

$CommandPattern = $Command
if($GraphProfile -eq "beta"){
$CommandPattern = $Command.Replace("-MgBeta", "-Mg")
}
$Content = Get-Content -Path $ExampleFile
$SearchText = "Example"
$SearchTextForNewImports = "{{ Add description here }}"
Expand All @@ -322,12 +310,15 @@ function Update-ExampleFile {
$HeadCount = $HeaderList.Count
$ExampleCount = $ExampleList.Count
$WrongExamplesCount = 0;
$SkippedExample = -1
$ContainsRightExamples = $False

#===========================Importing new examples into files ============================================#
if ($ReplaceEverything -and $ExampleCount -gt 0 -and $HeadCount -eq $ExampleCount) {
Clear-Content $ExampleFile -Force
for ($d = 0; $d -lt $HeaderList.Count; $d++) {
$CodeValue = $ExampleList[$d].Trim()
if($CodeValue.Contains($Command)){
if($CodeValue.Contains($CommandPattern)){
$TitleValue = "### " + $HeaderList[$d].Trim()
$Code = "``````powershell`r$CodeValue`r`n``````"

Expand All @@ -338,12 +329,16 @@ function Update-ExampleFile {
$TotalText = $TotalText.Replace("Microsoft.Graph", "Microsoft.Graph.Beta")
}
Add-Content -Path $ExampleFile -Value $TotalText
$ContainsRightExamples = $True
}else{
$WrongExamplesCount++
$SkippedExample++

}
}
}
#The code below updates existing examples
#------------------------------------------------------------#
$PatternToSearch = "Import-Module Microsoft.Graph.$Module"
if(($Content | Select-String -pattern $SearchText) -and ($Content | Select-String -pattern "This example shows")){
$ContainsPatternToSearch = $False
Expand All @@ -354,21 +349,29 @@ function Update-ExampleFile {
}
if($ContainsPatternToSearch){
Clear-Content $ExampleFile -Force
#Replace everything

for ($d = 0; $d -lt $HeaderList.Count; $d++) {
#We should only add the correct examples from external docs link
if($ExampleList[$d].Contains($CommandPattern)){
$CodeValue = $ExampleList[$d].Trim()
$TitleValue = "### " + $HeaderList[$d].Trim()
$Code = "``````powershell`r$CodeValue`r`n``````"

$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
if($GraphProfile -eq "beta"){
#Replace examples to match the new beta naming convention
$TotalText = $TotalText.Replace("-Mg", "-MgBeta")
$TotalText = $TotalText.Replace("Microsoft.Graph", "Microsoft.Graph.Beta")
}
Add-Content -Path $ExampleFile -Value $TotalText
}else{
$SkippedExample++

}

}


}else{
Clear-Content $ExampleFile -Force
#Replace everything with boiler plate code
Expand All @@ -377,7 +380,37 @@ function Update-ExampleFile {
}

}
if($WrongExamplesCount -gt 0){
#The code below corrects the numbering of the example headers/title if there is a situation where
#some examples are wrong(which are left out) and some are right
#-----------------------------------------------------------------------------------------------#
$AvailableCorrectExamples = 1
if($SkippedExample -gt -1){
$NewContent = Get-Content -Path $ExampleFile
foreach($C in $NewContent){
if($C.Contains("Example")){
$SearchString = $c.Split(":")
$StringToReplace = $SearchString[0]
$ReplacementString = "### Example $AvailableCorrectExamples"
(Get-Content -Path $ExampleFile) -replace $StringToReplace, $ReplacementString | Set-Content $ExampleFile
$AvailableCorrectExamples++
}
}
if(-not(Test-Path -PathType Container $FolderForExamplesToBeReviewed)){
New-Item -ItemType Directory -Force -Path $FolderForExamplesToBeReviewed
}
if (-not (Test-Path "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed")) {
"Command, ExternalDocsUrl, ApiVersion" | Out-File -FilePath "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed" -Encoding ASCII
}

$File = Get-Content "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed"
$containsWord = $File | % { $_ -match "$Command, $ExternalDocUrl, $GraphProfile, $UriPath" }
if (-not($containsWord -contains $true)) {
"$Command, $ExternalDocUrl, $GraphProfile, $UriPath" | Out-File -FilePath "$FolderForExamplesToBeReviewed\$ExamplesToBeReviewed" -Append -Encoding ASCII
}
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------#
if(($WrongExamplesCount -gt 0) -and -not($ContainsRightExamples)){
Clear-Content $ExampleFile -Force
$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"
Add-Content -Path $ExampleFile -Value $DefaultBoilerPlate.Trim()
#Log api path api version and equivalent external doc url giving wron examples
Expand Down Expand Up @@ -407,19 +440,20 @@ foreach($Data in $DeserializedContent)
if($Data.ApiVersion -eq "beta")
{
if((-not($Data.Variants[0].Contains("List")))){
$Beta = $BetaCommandGetVariantList.Add($Data.Command, $Data.Uri)
$BetaAPIPathAndMethod = $Data.Uri,$Data.Method -join ","
$Beta = $BetaCommandGetVariantList.Add($Data.Command, $BetaAPIPathAndMethod)
}else{
$Beta1 = $BetaCommandListVariantList.Add($Data.Command, $Data.Uri)
$Beta1 = $BetaCommandListVariantList.Add($Data.Command, $BetaAPIPathAndMethod)
}
}

if($Data.ApiVersion -eq "v1.0")
{

$V1APIPathAndMethod = $Data.Uri,$Data.Method -join ","
if((-not($Data.Variants[0].Contains("List")))){
$V1 = $V1CommandGetVariantList.Add($Data.Command, $Data.Uri)
$V1 = $V1CommandGetVariantList.Add($Data.Command, $V1APIPathAndMethod)
}else{
$V11 = $V1CommandListVariantList.Add($Data.Command, $Data.Uri)
$V11 = $V1CommandListVariantList.Add($Data.Command, $V1APIPathAndMethod)
}
}
}
Expand Down Expand Up @@ -459,4 +493,5 @@ Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"

#4. Test for beta updates from api reference
#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"
#Write-Host -ForegroundColor Green "-------------Done-------------"
#Write-Host -ForegroundColor Green "-------------Done-------------"
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/meetingattendancereport-get?view=graph-rest-1.0" -GraphCommand "Get-MgBetaCommunicationCallAudioRoutingGroup" -GraphModule "CloudCommunications" -Profile "beta"