From 86cb1a191371c40146cbdc1d18fc59bfc8066068 Mon Sep 17 00:00:00 2001 From: Oliver Lipkau <oliver@lipkau.net> Date: Mon, 22 Jan 2018 15:38:57 +0100 Subject: [PATCH] Reverted breaking changes #37 describes breaking changes that are not aligned to a major version bump --- PSIni.build.ps1 | 1 - PSIni/Functions/Set-IniContent.ps1 | 30 ++++++++++++++++++++++-------- Tests/PsIni.Tests.ps1 | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/PSIni.build.ps1 b/PSIni.build.ps1 index 0b4f3f5..7e398bf 100644 --- a/PSIni.build.ps1 +++ b/PSIni.build.ps1 @@ -156,7 +156,6 @@ task GetVersion { } $script:Version = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major, $currentVersion.Minor, - $currentVersion.Build, $newRevision } diff --git a/PSIni/Functions/Set-IniContent.ps1 b/PSIni/Functions/Set-IniContent.ps1 index 9a779df..7ec92f6 100644 --- a/PSIni/Functions/Set-IniContent.ps1 +++ b/PSIni/Functions/Set-IniContent.ps1 @@ -91,7 +91,11 @@ Function Set-IniContent { [Parameter(ParameterSetName="File",Mandatory=$True)] [Parameter(ParameterSetName="Object",Mandatory=$True)] [ValidateNotNullOrEmpty()] - [HashTable]$NameValuePairs, + [String]$NameValuePairs, + + [char]$NameValueDelimiter = '=', + [char]$NameValuePairDelimiter = ',', + [char]$SectionDelimiter = ',', [Parameter(ParameterSetName="File")] [Parameter(ParameterSetName="Object")] @@ -112,16 +116,26 @@ Function Set-IniContent { { param ($content, $section) - foreach($pair in $NameValuePairs.GetEnumerator()) - { - if (!($content[$section])) - { + foreach ($pair in $NameValuePairs.Split($NameValuePairDelimiter)) { + + $splitPair = $pair.Split($NameValueDelimiter) + + if ($splitPair.Length -ne 2) { + Write-Warning("$($MyInvocation.MyCommand.Name):: Unable to split '{0}' into a distinct key/value pair." -f $pair) + continue + } + + $key = $splitPair[0].Trim() + $value = $splitPair[1].Trim() + Write-Debug ("Split key is {0}, split value is {1}" -f $key, $value) + + if (!($content[$section])) { Write-Verbose ("$($MyInvocation.MyCommand.Name):: '{0}' section does not exist, creating it." -f $section) $content[$section] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase) } - Write-Verbose ("$($MyInvocation.MyCommand.Name):: Setting '{0}' key in section {1} to '{2}'." -f $pair.key, $section, $pair.value) - $content[$section][$pair.key] = $pair.value + Write-Verbose ("$($MyInvocation.MyCommand.Name):: Setting '{0}' key in section {1} to '{2}'." -f $key, $section, $value) + $content[$section][$key] = $value } } } @@ -135,7 +149,7 @@ Function Set-IniContent { # Specific section(s) were requested. if ($Sections) { - foreach ($section in $Sections) + foreach ($section in $Sections.Split($SectionDelimiter)) { # Get rid of whitespace and section brackets. $section = $section.Trim() -replace '[][]','' diff --git a/Tests/PsIni.Tests.ps1 b/Tests/PsIni.Tests.ps1 index 9f343fa..685dcea 100644 --- a/Tests/PsIni.Tests.ps1 +++ b/Tests/PsIni.Tests.ps1 @@ -127,7 +127,7 @@ Describe "PsIni functionality" { $content["Category2"]["Key3"] = "Value3" $content["Category2"]["Key4"] = "Value4" - $content | Set-IniContent -Sections 'Category1' -NameValuePairs @{'Key1'='NewValue1'} + $content | Set-IniContent -Sections 'Category1' -NameValuePairs 'Key1=NewValue1' # assert It "updates INI content with the new value" {