Skip to content

Commit

Permalink
Add support for serializing ScriptBlock and Switch objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed Oct 14, 2017
1 parent 6c77d7e commit e6218f4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 74 deletions.
140 changes: 71 additions & 69 deletions Source/Configuration.psd1
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
@{

# Script module or binary module file associated with this manifest.
ModuleToProcess = '.\Configuration.psm1'

# Version number of this module.
ModuleVersion = '1.0.3'

# ID used to uniquely identify this module
GUID = 'e56e5bec-4d97-4dfd-b138-abbaa14464a6'

# Author of this module
Author = @('Joel Bennett')

# Company or vendor of this module
CompanyName = 'HuddledMasses.org'

# HelpInfo URI of this module
# HelpInfoURI = ''

# Copyright statement for this module
Copyright = 'Copyright (c) 2014-2016 by Joel Bennett, all rights reserved.'

# Description of the functionality provided by this module
Description = 'A module for storing and reading configuration values, with full PS Data serialization, automatic configuration for modules and scripts, etc.'

# We explicitly name the functions we want to be visible, but we export everything with '*'
FunctionsToExport = 'Import-Configuration','Export-Configuration','Get-StoragePath','Add-MetadataConverter',
'ConvertFrom-Metadata','ConvertTo-Metadata','Export-Metadata','Import-Metadata',
'Update-Manifest','Get-ManifestValue','*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module
AliasesToExport = '*'

# List of all files packaged with this module
FileList = @('.\Configuration.psd1','.\Configuration.psm1','.\Metadata.psm1','.\en-US\about_Configuration.help.txt')

PrivateData = @{
# PSData is module packaging and gallery metadata embedded in PrivateData
# It's for the PoshCode and PowerShellGet modules
# We had to do this because it's the only place we're allowed to extend the manifest
# https://connect.microsoft.com/PowerShell/feedback/details/421837
PSData = @{
# Keyword tags to help users find this module via navigations and search.
Tags = @('Development','Configuration','Settings','Storage')

# The web address of this module's project or support homepage.
ProjectUri = "https://github.com/PoshCode/Configuration"

# The web address of this module's license. Points to a page that's embeddable and linkable.
LicenseUri = "http://opensource.org/licenses/MIT"

# Release notes for this particular version of the module
ReleaseNotes = 'Remove almost all verbose output'

# Indicates this is a pre-release/testing version of the module.
IsPrerelease = 'False'
}
}

}


@{

# Script module or binary module file associated with this manifest.
ModuleToProcess = '.\Configuration.psm1'

# Version number of this module.
ModuleVersion = '1.1.0'

# ID used to uniquely identify this module
GUID = 'e56e5bec-4d97-4dfd-b138-abbaa14464a6'

# Author of this module
Author = @('Joel Bennett')

# Company or vendor of this module
CompanyName = 'HuddledMasses.org'

# HelpInfo URI of this module
# HelpInfoURI = ''

# Copyright statement for this module
Copyright = 'Copyright (c) 2014-2016 by Joel Bennett, all rights reserved.'

# Description of the functionality provided by this module
Description = 'A module for storing and reading configuration values, with full PS Data serialization, automatic configuration for modules and scripts, etc.'

# We explicitly name the functions we want to be visible, but we export everything with '*'
FunctionsToExport = 'Import-Configuration','Export-Configuration','Get-StoragePath','Add-MetadataConverter',
'ConvertFrom-Metadata','ConvertTo-Metadata','Export-Metadata','Import-Metadata',
'Update-Manifest','Get-ManifestValue','*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module
AliasesToExport = '*'

# List of all files packaged with this module
FileList = @('.\Configuration.psd1','.\Configuration.psm1','.\Metadata.psm1','.\en-US\about_Configuration.help.txt')

PrivateData = @{
# PSData is module packaging and gallery metadata embedded in PrivateData
# It's for the PoshCode and PowerShellGet modules
# We had to do this because it's the only place we're allowed to extend the manifest
# https://connect.microsoft.com/PowerShell/feedback/details/421837
PSData = @{
# Keyword tags to help users find this module via navigations and search.
Tags = @('Development','Configuration','Settings','Storage')

# The web address of this module's project or support homepage.
ProjectUri = "https://github.com/PoshCode/Configuration"

# The web address of this module's license. Points to a page that's embeddable and linkable.
LicenseUri = "http://opensource.org/licenses/MIT"

# Release notes for this particular version of the module
ReleaseNotes = '
Added support for ScriptBlocks and SwitchParameters
'

# Indicates this is a pre-release/testing version of the module.
IsPrerelease = 'False'
}
}

}


29 changes: 24 additions & 5 deletions Source/Metadata.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,13 @@ function ConvertTo-Metadata {
}) -split "`n" -join "`n$t")
}
elseif($InputObject -is [System.Collections.IEnumerable]) {
# Write-verbose "Enumerable"
"@($($(ForEach($item in @($InputObject)) { ConvertTo-Metadata $item }) -join ","))"
}
elseif($InputObject.GetType().FullName -eq 'System.Management.Automation.PSCustomObject') {
# Write-verbose "Enumerable"
"@($($(ForEach($item in @($InputObject)) { ConvertTo-Metadata $item }) -join ","))"
}
elseif($InputObject -is [System.Management.Automation.ScriptBlock]) {
"(ScriptBlock '$InputObject')"
}
elseif($InputObject.GetType().FullName -eq 'System.Management.Automation.PSCustomObject') {
# Write-verbose "PSCustomObject"
# 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
"(PSObject @{{`n$t{0}`n}})" -f ($(
Expand Down Expand Up @@ -294,7 +297,7 @@ function ConvertFrom-Metadata {
Add-MetadataConverter $Converters
[string[]]$ValidCommands = @(
"PSObject", "ConvertFrom-StringData", "Join-Path", "Split-Path", "ConvertTo-SecureString",
"Guid", "bool", "SecureString", "Version", "DateTime", "DateTimeOffset", "PSCredential", "ConsoleColor"
"Guid", "bool", "SecureString", "Version", "DateTime", "DateTimeOffset", "PSCredential", "ConsoleColor", "ScriptBlock"
) + @($MetadataConverters.Keys.GetEnumerator() | Where-Object { $_ -isnot [Type] })
[string[]]$ValidVariables = "PSScriptRoot", "ScriptRoot", "PoshCodeModuleRoot","PSCulture","PSUICulture","True","False","Null"
}
Expand Down Expand Up @@ -763,6 +766,20 @@ function ConsoleColor {
[ConsoleColor]$Value
}

function ScriptBlock {
<#
.Synopsis
Creates a ScriptBlock from a string
.Description
Just calls [ScriptBlock]::Create with the passed-in value
.Parameter Value
The ScriptBlock as a string
#>
param([string]$Value)
[scriptblock]::Create($Value)
}


$MetadataConverters = @{}

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

[System.Management.Automation.SwitchParameter] = { if($_) { '$True' } else { '$False' } }

[Version] = { "'$_'" }

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

0 comments on commit e6218f4

Please sign in to comment.