Private/Generator/ConvertTo-OpenApiGenManifest.ps1
|
function ConvertTo-OpenApiGenManifest { <# .SYNOPSIS Renders the module manifest (.psd1) of a generated module. .DESCRIPTION RequiredModules names tcs.openapi with the generator's version as the minimum version and FunctionsToExport lists every command (sorted, ordinal). The GUID is derived from the module name (a name-based UUID, version 5 layout, from SHA-1), so regenerating keeps it. PSData gets ProjectUri (the document's externalDocs URL, otherwise its contact URL) and LicenseUri (the licence URL) when they are absolute http(s) URLs. #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory = $true)] [object]$Option, [Parameter(Mandatory = $true)] [object]$Document, [Parameter(Mandatory = $true)] [AllowEmptyCollection()] [string[]]$FunctionName, [Parameter(Mandatory = $true)] [string]$Template ) $sha1 = [System.Security.Cryptography.SHA1]::Create() try { $hash = $sha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes('tcs.openapi/' + $Option.ModuleName.ToLowerInvariant())) } finally { $sha1.Dispose() } $bytes = New-Object -TypeName 'byte[]' -ArgumentList 16 [System.Array]::Copy($hash, $bytes, 16) $bytes[6] = ($bytes[6] -band 0x0F) -bor 0x50 $bytes[8] = ($bytes[8] -band 0x3F) -bor 0x80 $hex = -join ($bytes | ForEach-Object -Process { $_.ToString('x2', [System.Globalization.CultureInfo]::InvariantCulture) }) $guid = '{0}-{1}-{2}-{3}-{4}' -f $hex.Substring(0, 8), $hex.Substring(8, 4), $hex.Substring(12, 4), $hex.Substring(16, 4), $hex.Substring(20, 12) $title = ([string]$Document.Title).Trim() if ($title -eq '') { $title = $Option.ModuleName } $description = "PowerShell commands for $title" if (-not [string]::IsNullOrWhiteSpace([string]$Document.Version)) { $description += " (API version $(([string]$Document.Version).Trim()))" } $description += ', generated by tcs.openapi.' $webUri = { param([string]$Value) $parsed = $null if (-not [string]::IsNullOrWhiteSpace($Value) -and [uri]::TryCreate($Value.Trim(), [System.UriKind]::Absolute, [ref]$parsed) -and @('http', 'https') -contains $parsed.Scheme) { return $Value.Trim() } return '' } $uriLines = @() $projectUri = & $webUri ([string]$Document.ExternalDocsUrl) if ($projectUri -eq '') { $projectUri = & $webUri ([string]$Document.ContactUrl) } if ($projectUri -ne '') { $uriLines += ' ProjectUri = ' + (ConvertTo-OpenApiGenLiteral -Value $projectUri) } $licenseUri = & $webUri ([string]$Document.LicenseUrl) if ($licenseUri -ne '') { $uriLines += ' LicenseUri = ' + (ConvertTo-OpenApiGenLiteral -Value $licenseUri) } $sorted = Get-OpenApiGenOrdinalSorted -InputObject $FunctionName $exportLines = @() for ($i = 0; $i -lt $sorted.Count; $i++) { $line = ' ' + (ConvertTo-OpenApiGenLiteral -Value $sorted[$i]) if ($i -lt $sorted.Count - 1) { $line += ',' } $exportLines += $line } return Expand-OpenApiGenTemplate -Template $Template -Value @{ ModuleName = $Option.ModuleName ModuleVersion = $Option.ModuleVersion Guid = $guid AuthorLiteral = (ConvertTo-OpenApiGenLiteral -Value $Option.Author) CopyrightLiteral = (ConvertTo-OpenApiGenLiteral -Value "(c) $($Option.Author). All rights reserved.") DescriptionLiteral = (ConvertTo-OpenApiGenLiteral -Value $description) GeneratorVersion = $Option.GeneratorVersion FunctionsToExport = ($exportLines -join "`n") PSDataUris = ($uriLines -join "`n") } } |