Skip to content

Commit

Permalink
added support for graph modules
Browse files Browse the repository at this point in the history
  • Loading branch information
techthoughts2 committed Apr 10, 2024
1 parent 789d13b commit c4444e3
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0]

- Module Changes
- Added support for Azure Graph modules
- Build Updates
- Added `MarkdownRepair.ps1` and added Markdown repair logic to InvokeBuild script.

## [1.0.0]

- Module Changes
Expand Down
6 changes: 3 additions & 3 deletions docs/Find-CloudCommand.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Searches for PowerShell cloud commands matching a given query.
## SYNTAX

```
Find-CloudCommand [-Query] <String> [[-Filter] <String>] [-AllResults] [<CommonParameters>]
Find-CloudCommand [-Query] <String> [[-Filter] <String>] [-AllResults]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -126,7 +127,7 @@ Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -Verbose, -WarningAction, -WarningVariable, and -ProgressAction.
For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS
Expand All @@ -143,4 +144,3 @@ Author: Jake Morrison - @jakemorrison - https://www.techthoughts.info/
[https://github.com/Azure/azure-powershell/blob/main/documentation/azure-powershell-modules.md](https://github.com/Azure/azure-powershell/blob/main/documentation/azure-powershell-modules.md)

[https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/powershell.htm](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/powershell.htm)

2 changes: 1 addition & 1 deletion docs/Get-AllCloudCommandInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -Verbose, -WarningAction, -WarningVariable, and -ProgressAction.
For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS
Expand Down
2 changes: 1 addition & 1 deletion docs/Get-CloudCommandFromFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -Verbose, -WarningAction, -WarningVariable, and -ProgressAction.
For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS
Expand Down
2 changes: 1 addition & 1 deletion docs/pwshCloudCommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module Name: pwshCloudCommands
Module Guid: be3705bf-2c38-413a-8973-9e409e826d35
Download Help Link: NA
Help Version: 1.0.0
Help Version: 1.1.0
Locale: en-US
---

Expand Down
135 changes: 135 additions & 0 deletions src/MarkdownRepair.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<#
.SYNOPSIS
Repair PlatyPS generated markdown files.
.NOTES
This file is temporarily required to handle platyPS help generation.
https://github.com/PowerShell/platyPS/issues/595
This is a result of a breaking change introduced in PowerShell 7.4.0:
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-74?view=powershell-7.4
Breaking Changes: Added the ProgressAction parameter to the Common Parameters
modified from source: https://github.com/PowerShell/platyPS/issues/595#issuecomment-1820971702
#>

function Remove-CommonParameterFromMarkdown {
<#
.SYNOPSIS
Remove a PlatyPS generated parameter block.
.DESCRIPTION
Removes parameter block for the provided parameter name from the markdown file provided.
#>
param(
[Parameter(Mandatory)]
[string[]]
$Path,

[Parameter(Mandatory = $false)]
[string[]]
$ParameterName = @('ProgressAction')
)
$ErrorActionPreference = 'Stop'
foreach ($p in $Path) {
$content = (Get-Content -Path $p -Raw).TrimEnd()
$updateFile = $false
foreach ($param in $ParameterName) {
if (-not ($Param.StartsWith('-'))) {
$param = "-$($param)"
}
# Remove the parameter block
$pattern = "(?m)^### $param\r?\n[\S\s]*?(?=#{2,3}?)"
$newContent = $content -replace $pattern, ''
# Remove the parameter from the syntax block
$pattern = " \[$param\s?.*?]"
$newContent = $newContent -replace $pattern, ''
if ($null -ne (Compare-Object -ReferenceObject $content -DifferenceObject $newContent)) {
Write-Verbose "Added $param to $p"
# Update file content
$content = $newContent
$updateFile = $true
}
}
# Save file if content has changed
if ($updateFile) {
$newContent | Out-File -Encoding utf8 -FilePath $p
Write-Verbose "Updated file: $p"
}
}
return
}

function Add-MissingCommonParameterToMarkdown {
param(
[Parameter(Mandatory)]
[string[]]
$Path,

[Parameter(Mandatory = $false)]
[string[]]
$ParameterName = @('ProgressAction')
)
$ErrorActionPreference = 'Stop'
foreach ($p in $Path) {
$content = (Get-Content -Path $p -Raw).TrimEnd()
$updateFile = $false
foreach ($NewParameter in $ParameterName) {
if (-not ($NewParameter.StartsWith('-'))) {
$NewParameter = "-$($NewParameter)"
}
$pattern = '(?m)^This cmdlet supports the common parameters:(.+?)\.'
$replacement = {
$Params = $_.Groups[1].Captures[0].ToString() -split ' '
$CommonParameters = @()
foreach ($CommonParameter in $Params) {
if ($CommonParameter.StartsWith('-')) {
if ($CommonParameter.EndsWith(',')) {
$CleanParam = $CommonParameter.Substring(0, $CommonParameter.Length - 1)
}
elseif ($p.EndsWith('.')) {
$CleanParam = $CommonParameter.Substring(0, $CommonParameter.Length - 1)
}
else {
$CleanParam = $CommonParameter
}
$CommonParameters += $CleanParam
}
}
if ($NewParameter -notin $CommonParameters) {
$CommonParameters += $NewParameter
}
$CommonParameters[-1] = "and $($CommonParameters[-1]). "
return "This cmdlet supports the common parameters: " + (($CommonParameters | Sort-Object) -join ', ')
}
$newContent = $content -replace $pattern, $replacement
if ($null -ne (Compare-Object -ReferenceObject $content -DifferenceObject $newContent)) {
Write-Verbose "Added $NewParameter to $p"
$updateFile = $true
$content = $newContent
}
}
# Save file if content has changed
if ($updateFile) {
$newContent | Out-File -Encoding utf8 -FilePath $p
Write-Verbose "Updated file: $p"
}
}
return
}

function Repair-PlatyPSMarkdown {
param(
[Parameter(Mandatory)]
[string[]]
$Path,

[Parameter()]
[string[]]
$ParameterName = @('ProgressAction')
)
$ErrorActionPreference = 'Stop'
$Parameters = @{
Path = $Path
ParameterName = $ParameterName
}
$null = Remove-CommonParameterFromMarkdown @Parameters
$null = Add-MissingCommonParameterToMarkdown @Parameters
return
}
10 changes: 10 additions & 0 deletions src/Tests/Integration/pwshCloudCommands.Infra.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ InModuleScope 'pwshCloudCommands' {
$eval.Count | Should -BeGreaterThan 20
} #it

It 'should return expected results for graph module query' {
$eval = Find-CloudCommand -Query 'Get-MgUser' -Filter Azure
$eval.ModuleName | Should -BeExactly 'Microsoft.Graph.Users'
} #it

It 'should return expected results for oracle module query' {
$eval = Find-CloudCommand -Query 'New-OCIComputeInstance' -Filter Oracle
$eval.ModuleName | Should -BeExactly 'OCI.PSModules.Core'
} #it

} #context_Find-CloudCommand
Context 'Get-CloudCommandFromFile' {

Expand Down
27 changes: 21 additions & 6 deletions src/Tests/Unit/Private/Search-XMLDataSet.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,28 @@ in order to stop getting charged for the storage of uploaded parts, you should
$xmlObj
} -Verifiable
Search-XMLDataSet -Query $cleanQuery -Filter AWS

Assert-VerifiableMock
Should -Invoke -CommandName Get-ChildItem -Times 1 -Exactly
} #it

It 'should filter properly for Azure based on user input' {
Mock -CommandName Get-ChildItem {
$Filter | Should -BeExactly 'Az.*'
# $ErrorAction | Should -BeExactly 'Stop'
$xmlObj
} -Verifiable
$script:mockCalled = 0
Mock -CommandName Get-ChildItem -MockWith {
$script:mockCalled++
if ($script:mockCalled -eq 1) {
$Filter | Should -BeExactly 'Az.*'
}
elseif ($script:mockCalled -eq 2) {
$Filter | Should -BeExactly 'Microsoft.Graph.*'
}
# Return a mock XML object or similar to simulate the command's output
return $mockXmlObj
}

Search-XMLDataSet -Query $cleanQuery -Filter Azure
Assert-VerifiableMock

Should -Invoke -CommandName Get-ChildItem -Times 2 -Exactly
} #it

It 'should filter properly for Oracle based on user input' {
Expand All @@ -181,7 +192,9 @@ in order to stop getting charged for the storage of uploaded parts, you should
$xmlObj
} -Verifiable
Search-XMLDataSet -Query $cleanQuery -Filter Oracle

Assert-VerifiableMock
Should -Invoke -CommandName Get-ChildItem -Times 1 -Exactly
} #it

It 'should filter properly for free form query based on user input' {
Expand All @@ -191,7 +204,9 @@ in order to stop getting charged for the storage of uploaded parts, you should
$xmlObj
} -Verifiable
Search-XMLDataSet -Query $cleanQuery

Assert-VerifiableMock
Should -Invoke -CommandName Get-ChildItem -Times 1 -Exactly
} #it

} #context_Filter
Expand Down
11 changes: 11 additions & 0 deletions src/pwshCloudCommands.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,17 @@ Add-BuildTask CreateMarkdownHelp -After CreateHelpStart {
throw 'Missing GUID. Please review and rebuild.'
}

Write-Build Gray ' Evaluating if running 7.4.0 or higher...'
# https://github.com/PowerShell/platyPS/issues/595
if ($PSVersionTable.PSVersion -ge [version]'7.4.0') {
Write-Build Gray ' Performing Markdown repair'
# dot source markdown repair
. $BuildRoot\MarkdownRepair.ps1
$OutputDir | Get-ChildItem -File | ForEach-Object {
Repair-PlatyPSMarkdown -Path $_.FullName
}
}

Write-Build Gray ' Checking for missing documentation in md files...'
$MissingDocumentation = Select-String -Path "$script:ArtifactsPath\docs\*.md" -Pattern "({{.*}})"
if ($MissingDocumentation.Count -gt 0) {
Expand Down
19 changes: 19 additions & 0 deletions src/pwshCloudCommands/Private/Search-XMLDataSet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ function Search-XMLDataSet {
throw
}

# special case for Azure selection where we will also retrieve Graph Modules
if ($Filter -eq 'Azure') {
Write-Debug -Message 'Retrieving Azure Graph xml file info...'
$getChildItemSplat = @{
Path = $xmlDataPath
Filter = 'Microsoft.Graph.*'
ErrorAction = 'Stop'
}
try {
$graphFiles = Get-ChildItem @getChildItemSplat
}
catch {
Write-Warning -Message 'An error was encountered getting xml file info.'
Write-Error $_
throw
}
$xmlDataFiles += $graphFiles
}

Write-Verbose -Message 'Running query...'
if ($PSCmdlet.ParameterSetName -eq 'Function') {
#------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/pwshCloudCommands/pwshCloudCommands.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'pwshCloudCommands.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'
ModuleVersion = '1.1.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down

0 comments on commit c4444e3

Please sign in to comment.