Skip to content

Commit

Permalink
Merge branch 'fix/#37-RevertBreakingChanges'
Browse files Browse the repository at this point in the history
  • Loading branch information
lipkau committed Jan 22, 2018
2 parents d65d46c + 86cb1a1 commit 8b70bb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 0 additions & 1 deletion PSIni.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ task GetVersion {
}
$script:Version = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major,
$currentVersion.Minor,
$currentVersion.Build,
$newRevision
}

Expand Down
30 changes: 22 additions & 8 deletions PSIni/Functions/Set-IniContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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
}
}
}
Expand All @@ -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 '[][]',''
Expand Down
2 changes: 1 addition & 1 deletion Tests/PsIni.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 8b70bb9

Please sign in to comment.