Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multifile #1505

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
520 changes: 520 additions & 0 deletions Extract-function.ps1

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions examples/OpenApi-TuttiFrutti.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Some useful links:
New-PodeOAStringProperty -Name 'shipDate' -Format Date-Time |
New-PodeOAStringProperty -Name 'status' -Description 'Order Status' -Example 'approved' -Enum @('placed', 'approved', 'delivered') |
New-PodeOABoolProperty -Name 'complete' |
New-PodeOASchemaProperty -Name 'Address' -Reference 'Address' |
New-PodeOAComponentSchemaProperty -Name 'Address' -Reference 'Address' |
New-PodeOAObjectProperty -Name 'Order' -XmlName 'order' -AdditionalProperties (New-PodeOAStringProperty ) |
Add-PodeOAComponentSchema -Name 'Order'

Expand Down Expand Up @@ -196,10 +196,10 @@ Some useful links:
New-PodeOAObjectProperty -Name 'Pet' -XmlName 'pet' -Properties (
New-PodeOAIntProperty -Name 'id'-Format Int64 -Example @(10, 2, 4) -ReadOnly |
New-PodeOAStringProperty -Name 'name' -Example 'doggie' -Required |
New-PodeOASchemaProperty -Name 'category' -Reference 'Category' |
New-PodeOAComponentSchemaProperty -Name 'category' -Reference 'Category' |
New-PodeOAStringProperty -Name 'petType' -Example 'dog' -Required |
New-PodeOAStringProperty -Name 'photoUrls' -Array |
New-PodeOASchemaProperty -Name 'tags' -Reference 'Tag' |
New-PodeOAComponentSchemaProperty -Name 'tags' -Reference 'Tag' |
New-PodeOAStringProperty -Name 'status' -Description 'pet status in the store' -Enum @('available', 'pending', 'sold')
))

Expand All @@ -226,7 +226,7 @@ Some useful links:

New-PodeOAStringProperty -Name 'name' |
New-PodeOAStringProperty -Name 'type' |
New-PodeOASchemaProperty -Name 'children' -Array -Reference 'StructPart' |
New-PodeOAComponentSchemaProperty -Name 'children' -Array -Reference 'StructPart' |
New-PodeOAObjectProperty |
Add-PodeOAComponentSchema -Name 'StructPart'

Expand All @@ -252,10 +252,10 @@ Some useful links:
New-PodeOAObjectProperty -Name 'Pet' -XmlName 'pet' } -Properties @(
(New-PodeOAIntProperty -Name 'id'-Format Int64 -Example 10 -ReadOnly),
(New-PodeOAStringProperty -Name 'name' -Example 'doggie' -Required),
(New-PodeOASchemaProperty -Name 'category' -Component 'Category'),
(New-PodeOAComponentSchemaProperty -Name 'category' -Component 'Category'),
(New-PodeOAStringProperty -Name 'petType' -Example 'dog' -Required),
(New-PodeOAStringProperty -Name 'photoUrls' -Array),
(New-PodeOASchemaProperty -Name 'tags' -Component 'Tag')
(New-PodeOAComponentSchemaProperty -Name 'tags' -Component 'Tag')
(New-PodeOAStringProperty -Name 'status' -Description 'pet status in the store' -Enum @('available', 'pending', 'sold'))
)) #>

Expand Down Expand Up @@ -309,7 +309,7 @@ Some useful links:
Set-PodeOARouteInfo -Summary 'Find pets by ID' -Description 'Returns pets based on ID' -OperationId 'getPetsById' -PassThru |
Set-PodeOARequest -PassThru -Parameters @(
( New-PodeOAStringProperty -Name 'id' -Description 'ID of pet to use' -array | ConvertTo-PodeOAParameter -In Path -Style Simple -Required )) |
Add-PodeOAResponse -StatusCode 200 -Description 'pet response' -Content (@{ '*/*' = New-PodeOASchemaProperty -Reference 'Pet' -array }) -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'pet response' -Content (@{ '*/*' = New-PodeOAComponentSchemaProperty -Reference 'Pet' -array }) -PassThru |
Add-PodeOAResponse -Default -Description 'error payload' -Content (@{ 'text/html' = 'ApiResponse' }) -PassThru


Expand Down Expand Up @@ -746,7 +746,7 @@ Some useful links:
New-PodeOAStringProperty -name 'id' -format 'uuid' |
New-PodeOAObjectProperty -name 'address' -NoProperties |
New-PodeOAStringProperty -name 'children' -array |
New-PodeOASchemaProperty -Name 'addresses' -Reference 'Address' -Array |
New-PodeOAComponentSchemaProperty -Name 'addresses' -Reference 'Address' -Array |
New-PodeOAObjectProperty
}) | Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -PassThru |
Add-PodeOAResponse -StatusCode 400 -Description 'Invalid ID supplied' -PassThru |
Expand Down
10 changes: 7 additions & 3 deletions src/Pode.Internal.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ $root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
$sysfuncs = Get-ChildItem Function:

# load private functions
Get-ChildItem "$($root)/Private/*.ps1" | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }
Get-ChildItem -Path "$($root)/Private/*.ps1" -Recurse | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }

# load public functions
Get-ChildItem "$($root)/Public/*.ps1" | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }
Get-ChildItem -Path "$($root)/Public/*.ps1" -Recurse | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }


# get functions from memory and compare to existing to find new functions added
$funcs = Get-ChildItem Function: | Where-Object { $sysfuncs -notcontains $_ }

# export the module's public functions
Export-ModuleMember -Function ($funcs.Name)
Export-ModuleMember -Function ($funcs.Name)

# Ensure backward compatibility by creating aliases for legacy Pode OpenAPI function names.
New-PodeFunctionAlias
15 changes: 9 additions & 6 deletions src/Pode.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ try {
$moduleManifestPath = Join-Path -Path $root -ChildPath 'Pode.psd1'

# Import the module manifest to access its properties
$moduleManifest = Import-PowerShellDataFile -Path $moduleManifestPath -ErrorAction Stop
$PodeManifest = Import-PowerShellDataFile -Path $moduleManifestPath -ErrorAction Stop


$podeDll = [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Pode' }

if ($podeDll) {
if ( $moduleManifest.ModuleVersion -ne '$version$') {
$moduleVersion = ([version]::new($moduleManifest.ModuleVersion + '.0'))
if ( $PodeManifest.ModuleVersion -ne '$version$') {
$moduleVersion = ([version]::new($PodeManifest.ModuleVersion + '.0'))
if ($podeDll.GetName().Version -ne $moduleVersion) {
# An existing incompatible Pode.DLL version {0} is loaded. Version {1} is required. Open a new Powershell/pwsh session and retry.
throw ($PodeLocale.incompatiblePodeDllExceptionMessage -f $podeDll.GetName().Version, $moduleVersion)
Expand Down Expand Up @@ -112,7 +112,7 @@ try {
}

# load private functions
Get-ChildItem "$($root)/Private/*.ps1" | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }
Get-ChildItem "$($root)/Private/*.ps1" -Recurse | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }

# only import public functions
$sysfuncs = Get-ChildItem Function:
Expand All @@ -121,7 +121,10 @@ try {
$sysaliases = Get-ChildItem Alias:

# load public functions
Get-ChildItem "$($root)/Public/*.ps1" | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }
Get-ChildItem "$($root)/Public/*.ps1" -Recurse | ForEach-Object { . ([System.IO.Path]::GetFullPath($_)) }

# Ensure backward compatibility by creating aliases for legacy Pode OpenAPI function names.
New-PodeFunctionAlias

# get functions from memory and compare to existing to find new functions added
$funcs = Get-ChildItem Function: | Where-Object { $sysfuncs -notcontains $_ }
Expand All @@ -141,6 +144,6 @@ catch {
}
finally {
# Cleanup temporary variables
Remove-Variable -Name 'tmpPodeLocale', 'localesPath', 'moduleManifest', 'root', 'version', 'libsPath', 'netFolder', 'podeDll', 'sysfuncs', 'sysaliases', 'funcs', 'aliases', 'moduleManifestPath', 'moduleVersion' -ErrorAction SilentlyContinue
Remove-Variable -Name 'tmpPodeLocale', 'localesPath', 'root', 'version', 'libsPath', 'netFolder', 'podeDll', 'sysfuncs', 'sysaliases', 'funcs', 'aliases', 'moduleManifestPath', 'moduleVersion' -ErrorAction SilentlyContinue
}

78 changes: 50 additions & 28 deletions src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3278,30 +3278,6 @@ function Test-PodePlaceholder {
}


<#
.SYNOPSIS
Retrieves the PowerShell module manifest object for the specified module.

.DESCRIPTION
This function constructs the path to a PowerShell module manifest file (.psd1) located in the parent directory of the script root. It then imports the module manifest file to access its properties and returns the manifest object. This can be useful for scripts that need to dynamically discover and utilize module metadata, such as version, dependencies, and exported functions.

.PARAMETERS
This function does not accept any parameters.

.EXAMPLE
$manifest = Get-PodeModuleManifest
This example calls the `Get-PodeModuleManifest` function to retrieve the module manifest object and stores it in the variable `$manifest`.

#>
function Get-PodeModuleManifest {
# Construct the path to the module manifest (.psd1 file)
$moduleManifestPath = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Pode.psd1'

# Import the module manifest to access its properties
$moduleManifest = Import-PowerShellDataFile -Path $moduleManifestPath
return $moduleManifest
}

<#
.SYNOPSIS
Tests the running PowerShell version for compatibility with Pode, identifying end-of-life (EOL) and untested versions.
Expand Down Expand Up @@ -3335,24 +3311,23 @@ function Test-PodeVersionPwshEOL {
param(
[switch] $ReportUntested
)
$moduleManifest = Get-PodeModuleManifest
if ($moduleManifest.ModuleVersion -eq '$version$') {
if ($PodeManifest.ModuleVersion -eq '$version$') {
return @{
eol = $false
supported = $true
}
}

$psVersion = $PSVersionTable.PSVersion
$eolVersions = $moduleManifest.PrivateData.PwshVersions.Untested -split ','
$eolVersions = $PodeManifest.PrivateData.PwshVersions.Untested -split ','
$isEol = "$($psVersion.Major).$($psVersion.Minor)" -in $eolVersions

if ($isEol) {
# [WARNING] Pode version has not been tested on PowerShell version, as it is EOL
Write-PodeHost ($PodeLocale.eolPowerShellWarningMessage -f $PodeVersion, $PSVersion) -ForegroundColor Yellow
}

$SupportedVersions = $moduleManifest.PrivateData.PwshVersions.Supported -split ','
$SupportedVersions = $PodeManifest.PrivateData.PwshVersions.Supported -split ','
$isSupported = "$($psVersion.Major).$($psVersion.Minor)" -in $SupportedVersions

if ((! $isSupported) -and (! $isEol) -and $ReportUntested) {
Expand Down Expand Up @@ -3966,3 +3941,50 @@ function ConvertTo-PodeSleep {
function Test-PodeIsISEHost {
return ((Test-PodeIsWindows) -and ('Windows PowerShell ISE Host' -eq $Host.Name))
}



<#
.SYNOPSIS
Creates aliases for Pode OpenAPI functions to support legacy naming conventions.

.DESCRIPTION
This function sets up the following aliases in the current script scope:
- New-PodeOASchemaProperty as an alias for New-PodeOAComponentSchemaProperty.
- Enable-PodeOpenApiViewer as an alias for Enable-PodeOAViewer.
- Enable-PodeOA as an alias for Enable-PodeOpenApi.
- Get-PodeOpenApiDefinition as an alias for Get-PodeOADefinition.
The function helps maintain backward compatibility and simplifies calling Pode OpenAPI functions.

.PARAMETER None
This function does not accept any parameters.

.OUTPUTS
None. The function creates aliases and does not output any objects.

.EXAMPLE
PS C:\> New-PodeFunctionAlias
The function creates the necessary aliases for Pode OpenAPI functions in the current session.

.NOTES
This function is part of the Pode project and adheres to the coding standards defined in the Pode GitHub Repository.
Internal function subject to change.
#>
function New-PodeFunctionAlias {
# Alias
if (!(Test-Path Alias:New-PodeOASchemaProperty)) {
New-Alias New-PodeOASchemaProperty -Value New-PodeOAComponentSchemaProperty -Scope Script
}

if (!(Test-Path Alias:Enable-PodeOpenApiViewer)) {
New-Alias Enable-PodeOpenApiViewer -Value Enable-PodeOAViewer -Scope Script
}

if (!(Test-Path Alias:Enable-PodeOA)) {
New-Alias Enable-PodeOA -Value Enable-PodeOpenApi -Scope Script
}

if (!(Test-Path Alias:Get-PodeOpenApiDefinition)) {
New-Alias Get-PodeOpenApiDefinition -Value Get-PodeOADefinition -Scope Script
}
}
14 changes: 1 addition & 13 deletions src/Public/OAComponents.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -950,16 +950,4 @@ function Remove-PodeOAComponent {
$PodeContext.Server.OpenAPI.Definitions[$tag].components[$field ].remove($Name)
}
}
}

if (!(Test-Path Alias:Enable-PodeOpenApiViewer)) {
New-Alias Enable-PodeOpenApiViewer -Value Enable-PodeOAViewer
}

if (!(Test-Path Alias:Enable-PodeOA)) {
New-Alias Enable-PodeOA -Value Enable-PodeOpenApi
}

if (!(Test-Path Alias:Get-PodeOpenApiDefinition)) {
New-Alias Get-PodeOpenApiDefinition -Value Get-PodeOADefinition
}
}
5 changes: 0 additions & 5 deletions src/Public/OAProperties.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2072,9 +2072,4 @@ function New-PodeOAComponentSchemaProperty {
return $param
}
}
}


if (!(Test-Path Alias:New-PodeOASchemaProperty)) {
New-Alias New-PodeOASchemaProperty -Value New-PodeOAComponentSchemaProperty
}
21 changes: 2 additions & 19 deletions src/Public/Utilities.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1367,27 +1367,10 @@ This function does not accept any parameters.
.OUTPUTS
System.String
Returns a string indicating the version of the Pode module or '[dev]' if on a development version.

.EXAMPLE
PS> $moduleManifest = @{ ModuleVersion = '1.2.3' }
PS> Get-PodeVersion

Returns 'v1.2.3'.

.EXAMPLE
PS> $moduleManifest = @{ ModuleVersion = '$version$' }
PS> Get-PodeVersion

Returns '[dev]'.

.NOTES
This function assumes that $moduleManifest is a hashtable representing the loaded module manifest, with a key of ModuleVersion.

#>
function Get-PodeVersion {
$moduleManifest = Get-PodeModuleManifest
if ($moduleManifest.ModuleVersion -ne '$version$') {
return "v$($moduleManifest.ModuleVersion)"
if ($PodeManifest.ModuleVersion -ne '$version$') {
return "v$($PodeManifest.ModuleVersion)"
}
else {
return '[dev]'
Expand Down
Loading