Skip to content

Commit e6218f4

Browse files
committed
Add support for serializing ScriptBlock and Switch objects
1 parent 6c77d7e commit e6218f4

2 files changed

Lines changed: 95 additions & 74 deletions

File tree

Source/Configuration.psd1

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,71 @@
1-
@{
2-
3-
# Script module or binary module file associated with this manifest.
4-
ModuleToProcess = '.\Configuration.psm1'
5-
6-
# Version number of this module.
7-
ModuleVersion = '1.0.3'
8-
9-
# ID used to uniquely identify this module
10-
GUID = 'e56e5bec-4d97-4dfd-b138-abbaa14464a6'
11-
12-
# Author of this module
13-
Author = @('Joel Bennett')
14-
15-
# Company or vendor of this module
16-
CompanyName = 'HuddledMasses.org'
17-
18-
# HelpInfo URI of this module
19-
# HelpInfoURI = ''
20-
21-
# Copyright statement for this module
22-
Copyright = 'Copyright (c) 2014-2016 by Joel Bennett, all rights reserved.'
23-
24-
# Description of the functionality provided by this module
25-
Description = 'A module for storing and reading configuration values, with full PS Data serialization, automatic configuration for modules and scripts, etc.'
26-
27-
# We explicitly name the functions we want to be visible, but we export everything with '*'
28-
FunctionsToExport = 'Import-Configuration','Export-Configuration','Get-StoragePath','Add-MetadataConverter',
29-
'ConvertFrom-Metadata','ConvertTo-Metadata','Export-Metadata','Import-Metadata',
30-
'Update-Manifest','Get-ManifestValue','*'
31-
32-
# Cmdlets to export from this module
33-
CmdletsToExport = '*'
34-
35-
# Variables to export from this module
36-
VariablesToExport = '*'
37-
38-
# Aliases to export from this module
39-
AliasesToExport = '*'
40-
41-
# List of all files packaged with this module
42-
FileList = @('.\Configuration.psd1','.\Configuration.psm1','.\Metadata.psm1','.\en-US\about_Configuration.help.txt')
43-
44-
PrivateData = @{
45-
# PSData is module packaging and gallery metadata embedded in PrivateData
46-
# It's for the PoshCode and PowerShellGet modules
47-
# We had to do this because it's the only place we're allowed to extend the manifest
48-
# https://connect.microsoft.com/PowerShell/feedback/details/421837
49-
PSData = @{
50-
# Keyword tags to help users find this module via navigations and search.
51-
Tags = @('Development','Configuration','Settings','Storage')
52-
53-
# The web address of this module's project or support homepage.
54-
ProjectUri = "https://github.com/PoshCode/Configuration"
55-
56-
# The web address of this module's license. Points to a page that's embeddable and linkable.
57-
LicenseUri = "http://opensource.org/licenses/MIT"
58-
59-
# Release notes for this particular version of the module
60-
ReleaseNotes = 'Remove almost all verbose output'
61-
62-
# Indicates this is a pre-release/testing version of the module.
63-
IsPrerelease = 'False'
64-
}
65-
}
66-
67-
}
68-
69-
1+
@{
2+
3+
# Script module or binary module file associated with this manifest.
4+
ModuleToProcess = '.\Configuration.psm1'
5+
6+
# Version number of this module.
7+
ModuleVersion = '1.1.0'
8+
9+
# ID used to uniquely identify this module
10+
GUID = 'e56e5bec-4d97-4dfd-b138-abbaa14464a6'
11+
12+
# Author of this module
13+
Author = @('Joel Bennett')
14+
15+
# Company or vendor of this module
16+
CompanyName = 'HuddledMasses.org'
17+
18+
# HelpInfo URI of this module
19+
# HelpInfoURI = ''
20+
21+
# Copyright statement for this module
22+
Copyright = 'Copyright (c) 2014-2016 by Joel Bennett, all rights reserved.'
23+
24+
# Description of the functionality provided by this module
25+
Description = 'A module for storing and reading configuration values, with full PS Data serialization, automatic configuration for modules and scripts, etc.'
26+
27+
# We explicitly name the functions we want to be visible, but we export everything with '*'
28+
FunctionsToExport = 'Import-Configuration','Export-Configuration','Get-StoragePath','Add-MetadataConverter',
29+
'ConvertFrom-Metadata','ConvertTo-Metadata','Export-Metadata','Import-Metadata',
30+
'Update-Manifest','Get-ManifestValue','*'
31+
32+
# Cmdlets to export from this module
33+
CmdletsToExport = '*'
34+
35+
# Variables to export from this module
36+
VariablesToExport = '*'
37+
38+
# Aliases to export from this module
39+
AliasesToExport = '*'
40+
41+
# List of all files packaged with this module
42+
FileList = @('.\Configuration.psd1','.\Configuration.psm1','.\Metadata.psm1','.\en-US\about_Configuration.help.txt')
43+
44+
PrivateData = @{
45+
# PSData is module packaging and gallery metadata embedded in PrivateData
46+
# It's for the PoshCode and PowerShellGet modules
47+
# We had to do this because it's the only place we're allowed to extend the manifest
48+
# https://connect.microsoft.com/PowerShell/feedback/details/421837
49+
PSData = @{
50+
# Keyword tags to help users find this module via navigations and search.
51+
Tags = @('Development','Configuration','Settings','Storage')
52+
53+
# The web address of this module's project or support homepage.
54+
ProjectUri = "https://github.com/PoshCode/Configuration"
55+
56+
# The web address of this module's license. Points to a page that's embeddable and linkable.
57+
LicenseUri = "http://opensource.org/licenses/MIT"
58+
59+
# Release notes for this particular version of the module
60+
ReleaseNotes = '
61+
Added support for ScriptBlocks and SwitchParameters
62+
'
63+
64+
# Indicates this is a pre-release/testing version of the module.
65+
IsPrerelease = 'False'
66+
}
67+
}
68+
69+
}
70+
71+

Source/Metadata.psm1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,13 @@ function ConvertTo-Metadata {
204204
}) -split "`n" -join "`n$t")
205205
}
206206
elseif($InputObject -is [System.Collections.IEnumerable]) {
207-
# Write-verbose "Enumerable"
208-
"@($($(ForEach($item in @($InputObject)) { ConvertTo-Metadata $item }) -join ","))"
209-
}
210-
elseif($InputObject.GetType().FullName -eq 'System.Management.Automation.PSCustomObject') {
207+
# Write-verbose "Enumerable"
208+
"@($($(ForEach($item in @($InputObject)) { ConvertTo-Metadata $item }) -join ","))"
209+
}
210+
elseif($InputObject -is [System.Management.Automation.ScriptBlock]) {
211+
"(ScriptBlock '$InputObject')"
212+
}
213+
elseif($InputObject.GetType().FullName -eq 'System.Management.Automation.PSCustomObject') {
211214
# Write-verbose "PSCustomObject"
212215
# NOTE: we can't put [ordered] here because we need support for PS v2, but it's ok, because we put it in at parse-time
213216
"(PSObject @{{`n$t{0}`n}})" -f ($(
@@ -294,7 +297,7 @@ function ConvertFrom-Metadata {
294297
Add-MetadataConverter $Converters
295298
[string[]]$ValidCommands = @(
296299
"PSObject", "ConvertFrom-StringData", "Join-Path", "Split-Path", "ConvertTo-SecureString",
297-
"Guid", "bool", "SecureString", "Version", "DateTime", "DateTimeOffset", "PSCredential", "ConsoleColor"
300+
"Guid", "bool", "SecureString", "Version", "DateTime", "DateTimeOffset", "PSCredential", "ConsoleColor", "ScriptBlock"
298301
) + @($MetadataConverters.Keys.GetEnumerator() | Where-Object { $_ -isnot [Type] })
299302
[string[]]$ValidVariables = "PSScriptRoot", "ScriptRoot", "PoshCodeModuleRoot","PSCulture","PSUICulture","True","False","Null"
300303
}
@@ -763,6 +766,20 @@ function ConsoleColor {
763766
[ConsoleColor]$Value
764767
}
765768

769+
function ScriptBlock {
770+
<#
771+
.Synopsis
772+
Creates a ScriptBlock from a string
773+
.Description
774+
Just calls [ScriptBlock]::Create with the passed-in value
775+
.Parameter Value
776+
The ScriptBlock as a string
777+
#>
778+
param([string]$Value)
779+
[scriptblock]::Create($Value)
780+
}
781+
782+
766783
$MetadataConverters = @{}
767784

768785
if($Converters -is [Collections.IDictionary]) {
@@ -773,6 +790,8 @@ if($Converters -is [Collections.IDictionary]) {
773790
Add-MetadataConverter @{
774791
[bool] = { if($_) { '$True' } else { '$False' } }
775792

793+
[System.Management.Automation.SwitchParameter] = { if($_) { '$True' } else { '$False' } }
794+
776795
[Version] = { "'$_'" }
777796

778797
[PSCredential] = { 'PSCredential "{0}" "{1}"' -f $_.UserName, (ConvertFrom-SecureString $_.Password) }

0 commit comments

Comments
 (0)