diff options
author | Jordan Borean <jborean93@gmail.com> | 2019-12-17 09:06:54 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-17 09:06:54 +1000 |
commit | ae6fc265c93e306192fde697d8470d3881b5330e (patch) | |
tree | 4ec5a82aa2a6e8b2727d59c762636c91fcfed5ec /lib | |
parent | 83ad1231b865f309c70a8ecd773f3c427923f513 (diff) | |
download | ansible-ae6fc265c93e306192fde697d8470d3881b5330e.tar.gz |
Add-Type - Added a way to reference assemblies by type name (#65866)
* Add-Type - Added a way to reference assemblies by type name
* Also add architecture symbols during compile time
* Fix stray output values
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/module_utils/csharp/Ansible.Basic.cs | 11 | ||||
-rw-r--r-- | lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 | 57 |
2 files changed, 57 insertions, 11 deletions
diff --git a/lib/ansible/module_utils/csharp/Ansible.Basic.cs b/lib/ansible/module_utils/csharp/Ansible.Basic.cs index 629e4fdcf2..f0d1020766 100644 --- a/lib/ansible/module_utils/csharp/Ansible.Basic.cs +++ b/lib/ansible/module_utils/csharp/Ansible.Basic.cs @@ -20,12 +20,11 @@ using System.Web.Script.Serialization; // loaded in PSCore, ignore CS1702 so the code will ignore this warning //NoWarn -Name CS1702 -CLR Core -//AssemblyReference -Name Newtonsoft.Json.dll -CLR Core -//AssemblyReference -Name System.ComponentModel.Primitives.dll -CLR Core -//AssemblyReference -Name System.Diagnostics.EventLog.dll -CLR Core -//AssemblyReference -Name System.IO.FileSystem.AccessControl.dll -CLR Core -//AssemblyReference -Name System.Security.Principal.Windows.dll -CLR Core -//AssemblyReference -Name System.Security.AccessControl.dll -CLR Core +//AssemblyReference -Type Newtonsoft.Json.JsonConvert -CLR Core +//AssemblyReference -Type System.Diagnostics.EventLog -CLR Core +//AssemblyReference -Type System.Security.AccessControl.NativeObjectSecurity -CLR Core +//AssemblyReference -Type System.Security.AccessControl.DirectorySecurity -CLR Core +//AssemblyReference -Type System.Security.Principal.IdentityReference -CLR Core //AssemblyReference -Name System.Web.Extensions.dll -CLR Framework diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 index 19dec32d9c..ba38159d5f 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 @@ -38,6 +38,33 @@ Function Add-CSharpType { [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. + + .NOTES + The following features were added to control the compiling options from the + code itself. + + * Predefined compiler SYMBOLS + + * CORECLR - Added when running on PowerShell Core. + * WINDOWS - Added when running on Windows. + * UNIX - Added when running on non-Windows. + * X86 - Added when running on a 32-bit process (Ansible 2.10+) + * AMD64 - Added when running on a 64-bit process (Ansible 2.10+) + + * Ignore compiler warnings inline with the following comment inline + + //NoWarn -Name <rule code> [-CLR Core|Framework] + + * Specify custom assembly references inline + + //AssemblyReference -Name Dll.Location.dll [-CLR Core|Framework] + + # Added in Ansible 2.10 + //AssemblyReference -Type System.Type.Name [-CLR Core|Framework] + + * Create automatic type accelerators to simplify long namespace names (Ansible 2.9+) + + //TypeAccelerator -Name <AcceleratorName> -TypeName <Name of compiled type> #> param( [Parameter(Mandatory=$true)][AllowEmptyCollection()][String[]]$References, @@ -56,6 +83,13 @@ Function Add-CSharpType { # 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]$CompileSymbols + + if ([System.IntPtr]::Size -eq 4) { + $defined_symbols.Add('X86') > $null + } else { + $defined_symbols.Add('AMD64') > $null + } + $is_coreclr = Get-Variable -Name IsCoreCLR -ErrorAction SilentlyContinue if ($null -ne $is_coreclr) { if ($is_coreclr.Value) { @@ -77,7 +111,7 @@ Function Add-CSharpType { $type_accelerators = [System.Collections.Generic.List`1[Hashtable]]@() # pattern used to find referenced assemblies in the code - $assembly_pattern = [Regex]"//\s*AssemblyReference\s+-Name\s+(?<Name>[\w.]*)(\s+-CLR\s+(?<CLR>Core|Framework))?" + $assembly_pattern = [Regex]"//\s*AssemblyReference\s+-(?<Parameter>(Name)|(Type))\s+(?<Name>[\w.]*)(\s+-CLR\s+(?<CLR>Core|Framework))?" $no_warn_pattern = [Regex]"//\s*NoWarn\s+-Name\s+(?<Name>[\w\d]*)(\s+-CLR\s+(?<CLR>Core|Framework))?" $type_pattern = [Regex]"//\s*TypeAccelerator\s+-Name\s+(?<Name>[\w.]*)\s+-TypeName\s+(?<TypeName>[\w.]*)" @@ -116,9 +150,15 @@ Function Add-CSharpType { if ($clr -and $clr -ne "Core") { continue } - $assembly_path = $match.Groups["Name"] - if (-not ([System.IO.Path]::IsPathRooted($assembly_path))) { - $assembly_path = Join-Path -Path $lib_assembly_location -ChildPath $assembly_path + + $parameter_type = $match.Groups["Parameter"].Value + $assembly_path = $match.Groups["Name"].Value + if ($parameter_type -eq "Type") { + $assembly_path = ([Type]$assembly_path).Assembly.Location + } else { + if (-not ([System.IO.Path]::IsPathRooted($assembly_path))) { + $assembly_path = Join-Path -Path $lib_assembly_location -ChildPath $assembly_path + } } $assemblies.Add([Microsoft.CodeAnalysis.MetadataReference]::CreateFromFile($assembly_path)) > $null } @@ -256,7 +296,13 @@ Function Add-CSharpType { if ($clr -and $clr -ne "Framework") { continue } - $assemblies.Add($match.Groups["Name"].Value) > $null + + $parameter_type = $match.Groups["Parameter"].Value + $assembly_path = $match.Groups["Name"].Value + if ($parameter_type -eq "Type") { + $assembly_path = ([Type]$assembly_path).Assembly.Location + } + $assemblies.Add($assembly_path) > $null } $warn_matches = $no_warn_pattern.Matches($reference) foreach ($match in $warn_matches) { @@ -321,3 +367,4 @@ Function Add-CSharpType { } Export-ModuleMember -Function Add-CSharpType + |