| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Resolves the scripting language from the file extension of the provided path. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This helper function infers the scripting language (CSharp or VBNet) based on the |
| | | 6 | | file extension of the given path. It throws an error if the extension is unsupported or missing. |
| | | 7 | | .PARAMETER Path |
| | | 8 | | The file path from which to infer the scripting language. |
| | | 9 | | .OUTPUTS |
| | | 10 | | [Kestrun.Scripting.ScriptLanguage] |
| | | 11 | | The inferred scripting language. |
| | | 12 | | .EXAMPLE |
| | | 13 | | $lang = Resolve-KrCodeLanguageFromPath -Path 'script.cs' |
| | | 14 | | This command infers the scripting language as CSharp from the .cs extension. |
| | | 15 | | .EXAMPLE |
| | | 16 | | $lang = Resolve-KrCodeLanguageFromPath -Path 'script.vb' |
| | | 17 | | This command infers the scripting language as VBNet from the .vb extension. |
| | | 18 | | .EXAMPLE |
| | | 19 | | $lang = Resolve-KrCodeLanguageFromPath -Path 'script.ps1' |
| | | 20 | | This command infers the scripting language as PowerShell from the .ps1 extension. |
| | | 21 | | .EXAMPLE |
| | | 22 | | $lang = Resolve-KrCodeLanguageFromPath -Path 'script.txt' |
| | | 23 | | This command throws an error because .txt is not a supported extension. |
| | | 24 | | .EXAMPLE |
| | | 25 | | $lang = Resolve-KrCodeLanguageFromPath -Path 'script' |
| | | 26 | | This command throws an error because there is no file extension to infer the language. |
| | | 27 | | .NOTES |
| | | 28 | | This function is used internally by Set-KrServerHttpsOptions to determine the language for client |
| | | 29 | | certificate validation code snippets. |
| | | 30 | | #> |
| | | 31 | | function Resolve-KrCodeLanguageFromPath { |
| | | 32 | | [CmdletBinding()] |
| | | 33 | | param( |
| | | 34 | | [Parameter(Mandatory)] |
| | | 35 | | [string]$Path |
| | | 36 | | ) |
| | | 37 | | |
| | 1 | 38 | | $ext = [System.IO.Path]::GetExtension($Path) |
| | 1 | 39 | | if ([string]::IsNullOrWhiteSpace($ext)) { |
| | 0 | 40 | | throw "Unable to infer the language code type: '$Path' has no extension." |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | switch ($ext.ToLowerInvariant()) { |
| | 1 | 44 | | '.cs' { return [Kestrun.Scripting.ScriptLanguage]::CSharp } |
| | 1 | 45 | | '.vb' { return [Kestrun.Scripting.ScriptLanguage]::VBNet } |
| | 0 | 46 | | '.ps1' { return [Kestrun.Scripting.ScriptLanguage]::PowerShell } |
| | | 47 | | default { |
| | 0 | 48 | | throw "Unsupported language code file extension '$ext'." |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | } |