Skip to content

Commit d15f32d

Browse files
Microsoft Graph DevX ToolingMicrosoft Graph DevX Tooling
authored andcommitted
Added Start-Generator function to a try-catch block
1 parent 2983d8f commit d15f32d

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

tools/ImportExamples.ps1

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,71 @@ function Start-Generator {
1717
[string] $ProfilePath = "v1.0",
1818
[string] $ManualExternalDocsUrl = "https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=powershell"
1919
)
20-
21-
if ($GenerationMode -eq "auto") {
22-
#Test path first
23-
if (Test-Path $CommandMetadataPath) {
24-
$CommandMetadataContent = Get-Content $CommandMetadataPath | ConvertFrom-Json
25-
$CommandMetadataContent | ForEach-Object {
26-
$GraphCommand = $_.Command
27-
$GraphModule = $_.Module
28-
$UriPath = $_.Uri
29-
$ExternalDocsUrl = $_.ApiReferenceLink
30-
$ApiVersion = $_.ApiVersion
31-
$ProfilePathMapping = "v1.0\examples"
32-
if ($ApiVersion -eq "beta") {
33-
$ProfilePathMapping = "beta\examples"
34-
$GraphModule = $GraphModule.Replace("Beta.", "")
35-
}
36-
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
37-
38-
$ExampleFile = "$ModulePath\$GraphCommand.md"
39-
Write-Host $ExampleFile
40-
Test-Commands -Command $GraphCommand -CommandPath $ExampleFile
41-
if ($null -ne $ExternalDocsUrl) {
42-
if (-not (Test-Path $ExampleFile)) {
43-
New-Item -Path $ExampleFile -ItemType File -Force
20+
try {
21+
if ($GenerationMode -eq "auto") {
22+
#Test path first
23+
if (Test-Path $CommandMetadataPath) {
24+
$CommandMetadataContent = Get-Content $CommandMetadataPath | ConvertFrom-Json
25+
$CommandMetadataContent | ForEach-Object {
26+
$GraphCommand = $_.Command
27+
$GraphModule = $_.Module
28+
$UriPath = $_.Uri
29+
$ExternalDocsUrl = $_.ApiReferenceLink
30+
$ApiVersion = $_.ApiVersion
31+
$ProfilePathMapping = "v1.0\examples"
32+
if ($ApiVersion -eq "beta") {
33+
$ProfilePathMapping = "beta\examples"
34+
$GraphModule = $GraphModule.Replace("Beta.", "")
4435
}
45-
$IntuneUrl = $ExternalDocsUrl.Replace("intune-onboarding-", "")
46-
$IntuneUrl = $IntuneUrl.Replace("intune-mam-", "")
47-
$IsValid = IsValidEndPoint -GraphEndpoint $IntuneUrl
48-
if ($IsValid) {
49-
$ExternalDocsUrl = $IntuneUrl
36+
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
37+
38+
$ExampleFile = "$ModulePath\$GraphCommand.md"
39+
Write-Host $ExampleFile
40+
Test-Commands -Command $GraphCommand -CommandPath $ExampleFile
41+
if ($null -ne $ExternalDocsUrl) {
42+
if (-not (Test-Path $ExampleFile)) {
43+
New-Item -Path $ExampleFile -ItemType File -Force
44+
}
45+
$IntuneUrl = $ExternalDocsUrl.Replace("intune-onboarding-", "")
46+
$IntuneUrl = $IntuneUrl.Replace("intune-mam-", "")
47+
$IsValid = IsValidEndPoint -GraphEndpoint $IntuneUrl
48+
if ($IsValid) {
49+
$ExternalDocsUrl = $IntuneUrl
50+
}
51+
Start-WebScrapping -GraphProfile $ApiVersion -ExternalDocUrl $ExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -UriPath $UriPath -Module $GraphModule
5052
}
51-
Start-WebScrapping -GraphProfile $ApiVersion -ExternalDocUrl $ExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -UriPath $UriPath -Module $GraphModule
52-
}
53-
else {
54-
if (Test-Path $ExampleFile) {
55-
#Check file content and retain correct examples
56-
$Content = Get-Content -Path $ExampleFile
57-
Clear-Content $ExampleFile -Force
58-
if ($Content | Select-String -pattern $GraphCommand) {
59-
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $GraphCommand
60-
}
53+
else {
54+
if (Test-Path $ExampleFile) {
55+
#Check file content and retain correct examples
56+
$Content = Get-Content -Path $ExampleFile
57+
Clear-Content $ExampleFile -Force
58+
if ($Content | Select-String -pattern $GraphCommand) {
59+
Retain-ExistingCorrectExamples -Content $Content -File $ExampleFile -CommandPattern $GraphCommand
60+
}
61+
}
6162
}
62-
}
6363

64+
}
65+
}
66+
else {
67+
Write-Host "The path to the command metadata file is invalid. Please ensure that the path is correct"
6468
}
6569
}
6670
else {
67-
Write-Host "The path to the command metadata file is invalid. Please ensure that the path is correct"
68-
}
69-
}
70-
else {
7171

72-
$ProfilePathMapping = "v1.0\examples"
73-
if ($ProfilePath -eq "beta") {
74-
$ProfilePathMapping = "beta\examples"
72+
$ProfilePathMapping = "v1.0\examples"
73+
if ($ProfilePath -eq "beta") {
74+
$ProfilePathMapping = "beta\examples"
75+
}
76+
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
77+
Start-WebScrapping -GraphProfile $ProfilePath -ExternalDocUrl $ManualExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -Module $GraphModule
7578
}
76-
$ModulePath = Join-Path $PSScriptRoot "..\src\$GraphModule\$ProfilePathMapping"
77-
Start-WebScrapping -GraphProfile $ProfilePath -ExternalDocUrl $ManualExternalDocsUrl -Command $GraphCommand -GraphProfilePath $ModulePath -Module $GraphModule
79+
}
80+
catch {
81+
Write-Host "`nError Message: " $_.Exception.Message
82+
Write-Host "`nError in Line: " $_.InvocationInfo.Line
83+
Write-Host "`nError in Line Number: "$_.InvocationInfo.ScriptLineNumber
84+
Write-Host "`nError Item Name: "$_.Exception.ItemName
7885
}
7986
}
8087
function Test-Commands {

0 commit comments

Comments
 (0)