PS AddType - Add the ability to supply custom compile symbols for C# code (#54582)

pull/54598/head
Jordan Borean 5 years ago committed by GitHub
parent bce75824b1
commit 2750f39391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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) {

@ -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

Loading…
Cancel
Save