From 2750f39391cf53002b95edf087ab04612f682ab5 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 29 Mar 2019 19:09:55 +1000 Subject: [PATCH] PS AddType - Add the ability to supply custom compile symbols for C# code (#54582) --- .../Ansible.ModuleUtils.AddType.psm1 | 10 ++++++-- .../library/add_type_test.ps1 | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 index ab062cbee80..a9947c77d08 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 @@ -33,6 +33,11 @@ Function Add-CSharpType { [Switch] Whether to include debug information in the compiled assembly. Cannot be used when AnsibleModule is set. This is a no-op when running on PSCore. + + .PARAMETER CompileSymbols + [String[]] A list of symbols to be defined during compile time. These are + added to the existing symbols, 'CORECLR', 'WINDOWS', 'UNIX' that are set + conditionalls in this cmdlet. #> param( [Parameter(Mandatory=$true)][AllowEmptyCollection()][String[]]$References, @@ -40,7 +45,8 @@ Function Add-CSharpType { [Switch]$PassThru, [Parameter(Mandatory=$true, ParameterSetName="Module")][Object]$AnsibleModule, [Parameter(ParameterSetName="Manual")][String]$TempPath = $env:TMP, - [Parameter(ParameterSetName="Manual")][Switch]$IncludeDebugInfo + [Parameter(ParameterSetName="Manual")][Switch]$IncludeDebugInfo, + [String[]]$CompileSymbols = @() ) if ($null -eq $References -or $References.Length -eq 0) { return @@ -49,7 +55,7 @@ Function Add-CSharpType { # define special symbols CORECLR, WINDOWS, UNIX if required # the Is* variables are defined on PSCore, if absent we assume an # older version of PowerShell under .NET Framework and Windows - $defined_symbols = [System.Collections.ArrayList]@() + $defined_symbols = [System.Collections.ArrayList]$CompileSymbols $is_coreclr = Get-Variable -Name IsCoreCLR -ErrorAction SilentlyContinue if ($null -ne $is_coreclr) { if ($is_coreclr.Value) { diff --git a/test/integration/targets/win_module_utils/library/add_type_test.ps1 b/test/integration/targets/win_module_utils/library/add_type_test.ps1 index 4516c35e675..1198afdd690 100644 --- a/test/integration/targets/win_module_utils/library/add_type_test.ps1 +++ b/test/integration/targets/win_module_utils/library/add_type_test.ps1 @@ -204,5 +204,28 @@ Add-CSharpType -References $ignored_warning $actual = [Namespace7.Class7]::GetString() Assert-Equals -actual $actual -expected "abc" +$defined_symbol = @' +using System; + +namespace Namespace8 +{ + public class Class8 + { + public static string GetString() + { +#if SYMBOL1 + string a = "symbol"; +#else + string a = "no symbol"; +#endif + return a; + } + } +} +'@ +Add-CSharpType -References $defined_symbol -CompileSymbols "SYMBOL1" +$actual = [Namespace8.Class8]::GetString() +Assert-Equals -actual $actual -expected "symbol" + $result.res = "success" Exit-Json -obj $result