|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Compiles Add-Type calls as DLL inside our cache/lib folder |
| 4 | +.DESCRIPTION |
| 5 | + Allows to compile DLLs for .NET related code required for plugins |
| 6 | + or certain tasks on the Windows machine, not natively supported by |
| 7 | + PowerShell. |
| 8 | +
|
| 9 | + All DLL files are compiled within the cache/lib folder within the Framework |
| 10 | + and loaded on demand in case required. Once loaded within a shell session, |
| 11 | + there the function will simply do nothing |
| 12 | +.PARAMETER TypeDefinition |
| 13 | + The code to compile a DLL for. Like Add-Type, the code has to start with @" |
| 14 | + at the beginning and end with "@ at the very beginning of a new line without |
| 15 | + spaces or tabs |
| 16 | +.PARAMETER TypeName |
| 17 | + The name of the DLL and function being generated. The '.dll' name is NOT required |
| 18 | +.PARAMETER Force |
| 19 | + Allows to force create the library again and load it inside the shell |
| 20 | +.EXAMPLE |
| 21 | + $TypeDefinition = @" |
| 22 | + /* |
| 23 | + Your code |
| 24 | + */ |
| 25 | +"@ |
| 26 | + Add-IcingaAddTypeLib -TypeDefinition $TypeDefinition -TypeName 'Example'; |
| 27 | +#> |
| 28 | +function Add-IcingaAddTypeLib() |
| 29 | +{ |
| 30 | + param ( |
| 31 | + $TypeDefinition = $null, |
| 32 | + [string]$TypeName = '', |
| 33 | + [switch]$Force = $FALSE |
| 34 | + ); |
| 35 | + |
| 36 | + # Do nothing if TypeDefinition is null |
| 37 | + if ($null -eq $TypeDefinition) { |
| 38 | + Write-IcingaConsoleError -Message 'Failed to add type with name "{0}". The TypeDefinition is empty' -Objects $TypeName; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + # If no name is set, return an error as we require the name for identification |
| 43 | + if ([string]::IsNullOrEmpty($TypeName)) { |
| 44 | + Write-IcingaConsoleError -Message 'Failed to add type, as no name is specified'; |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + # If the type does already exist within our shell, to not load it again |
| 49 | + if ((Test-IcingaAddTypeExist -Type $TypeName) -And $Force -eq $FALSE) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + # Get our DLL folder |
| 54 | + [string]$DLLFolder = (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'dll'); |
| 55 | + |
| 56 | + if ((Test-Path $DLLFolder) -eq $FALSE) { |
| 57 | + New-Item -Path $DLLFolder -ItemType 'Directory' | Out-Null; |
| 58 | + } |
| 59 | + |
| 60 | + # Update the TypeName to include .dll ending |
| 61 | + if ($TypeName.Contains('.dll') -eq $FALSE) { |
| 62 | + $TypeName = [string]::Format('{0}.dll', $TypeName); |
| 63 | + } |
| 64 | + |
| 65 | + # Create the full path to our file |
| 66 | + [string]$DLLPath = Join-Path -Path $DLLFolder -ChildPath $TypeName; |
| 67 | + |
| 68 | + # If the DLL already exist, load the DLL from disk |
| 69 | + if ((Test-Path $DLLPath) -And $Force -eq $FALSE) { |
| 70 | + Add-Type -Path $DLLPath; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + # If the DLL does not exist or we use -Force, create it |
| 75 | + Add-Type -TypeDefinition $TypeDefinition -OutputType 'Library' -OutputAssembly $DLLPath; |
| 76 | + # Load the newly created DLL |
| 77 | + Add-Type -Path $DLLPath; |
| 78 | +} |
0 commit comments