| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Brian Scholer <@briantist> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
#Requires -Module Ansible.ModuleUtils.CamelConversion |
| 8 |
#Requires -Module PowerShellGet |
| 9 |
|
| 10 |
$spec = @{ |
| 11 |
options = @{ |
| 12 |
name = @{ type = 'str' ; default = '*' } |
| 13 |
repository = @{ type = 'str' } |
| 14 |
} |
| 15 |
supports_check_mode = $true |
| 16 |
} |
| 17 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 18 |
|
| 19 |
function Convert-ObjectToSnakeCase { |
| 20 |
<# |
| 21 |
.SYNOPSIS |
| 22 |
Converts an object with CamelCase properties to a dictionary with snake_case keys. |
| 23 |
Works in the spirit of and depends on the existing CamelConversion module util. |
| 24 |
#> |
| 25 |
[CmdletBinding()] |
| 26 |
param( |
| 27 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 28 |
[OutputType([System.Collections.Specialized.OrderedDictionary])] |
| 29 |
[Object] |
| 30 |
$InputObject , |
| 31 |
|
| 32 |
[Parameter()] |
| 33 |
[Switch] |
| 34 |
$NoRecurse , |
| 35 |
|
| 36 |
[Parameter()] |
| 37 |
[Switch] |
| 38 |
$OmitNull |
| 39 |
) |
| 40 |
|
| 41 |
Process { |
| 42 |
$result = [Ordered]@{} |
| 43 |
foreach ($property in $InputObject.PSObject.Properties) { |
| 44 |
$value = $property.Value |
| 45 |
if (-not $NoRecurse -and $value -is [System.Collections.IDictionary]) { |
| 46 |
$value = Convert-DictToSnakeCase -dict $value |
| 47 |
} |
| 48 |
elseif (-not $NoRecurse -and ($value -is [Array] -or $value -is [System.Collections.ArrayList])) { |
| 49 |
$value = Convert-ListToSnakeCase -list $value |
| 50 |
} |
| 51 |
elseif ($null -eq $value) { |
| 52 |
if ($OmitNull) { |
| 53 |
continue |
| 54 |
} |
| 55 |
} |
| 56 |
elseif (-not $NoRecurse -and $value -isnot [System.ValueType] -and $value -isnot [string]) { |
| 57 |
$value = Convert-ObjectToSnakeCase -InputObject $value |
| 58 |
} |
| 59 |
|
| 60 |
$name = Convert-StringToSnakeCase -string $property.Name |
| 61 |
$result[$name] = $value |
| 62 |
} |
| 63 |
$result |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
function ConvertTo-SerializableScriptInfo { |
| 68 |
<# |
| 69 |
.SYNOPSIS |
| 70 |
Transforms some members of a PSRepositoryItemInfo object to be more serialization-friendly. |
| 71 |
|
| 72 |
.DESCRIPTION |
| 73 |
Stringifies [DateTime], [enum], and [type] values for serialization |
| 74 |
#> |
| 75 |
[CmdletBinding()] |
| 76 |
param( |
| 77 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 78 |
[Object] |
| 79 |
$InputObject , |
| 80 |
|
| 81 |
[Parameter()] |
| 82 |
[string[]] |
| 83 |
$ExcludeProperty = @( |
| 84 |
<# |
| 85 |
Includes is for modules, containing the stuff they export. |
| 86 |
#> |
| 87 |
'Includes' , |
| 88 |
|
| 89 |
<# |
| 90 |
This is always 'Script' for scripts. |
| 91 |
#> |
| 92 |
'Type' |
| 93 |
) |
| 94 |
) |
| 95 |
|
| 96 |
Process { |
| 97 |
$properties = foreach ($p in $InputObject.PSObject.Properties) { |
| 98 |
$pName = $p.Name |
| 99 |
$pValue = $p.Value |
| 100 |
|
| 101 |
if ($pValue -is [datetime]) { |
| 102 |
@{ |
| 103 |
Name = $pName |
| 104 |
Expression = { $pValue.ToString('o') }.GetNewClosure() |
| 105 |
} |
| 106 |
} |
| 107 |
elseif ($pValue -is [enum] -or $pValue -is [type]) { |
| 108 |
@{ |
| 109 |
Name = $pName |
| 110 |
Expression = { $pValue.ToString() }.GetNewClosure() |
| 111 |
} |
| 112 |
} |
| 113 |
else { |
| 114 |
$pName |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$InputObject | Select-Object -Property $properties -ExcludeProperty $ExcludeProperty |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$module.Result.scripts = @( |
| 123 |
Get-InstalledScript -Name $module.Params.name -ErrorAction SilentlyContinue | |
| 124 |
Where-Object -FilterScript { -not $module.Params.repository -or $_.Repository -eq $module.Params.repository } | |
| 125 |
ConvertTo-SerializableScriptInfo | |
| 126 |
Convert-ObjectToSnakeCase -NoRecurse |
| 127 |
) |
| 128 |
|
| 129 |
$module.ExitJson() |