Skip to content

Commit

Permalink
Add UTF8BOM and UTF8NoBOM for Encoding parameter of the KeyValueP…
Browse files Browse the repository at this point in the history
…airFile and ReplaceText
  • Loading branch information
mkht committed Oct 26, 2024
1 parent ee6e067 commit 2a0e9ba
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 248 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
coverage - Fixes [Issue #50](https://github.com/dsccommunity/FileContentDsc/issues/50).
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #51](https://github.com/dsccommunity/FileContentDsc/issues/51).
- Renamed `master` branch to `main` - Fixes [Issue #53](https://github.com/dsccommunity/FileContentDsc/issues/53).
- Added `UTF8BOM` and `UTF8NoBOM` for Encoding parameter of the KeyValuePairFile and ReplaceText - Fixes [Issue #56](https://github.com/dsccommunity/FileContentDsc/issues/56).
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #57](https://github.com/dsccommunity/FileContentDsc/issues/57).
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #60](https://github.com/dsccommunity/FileContentDsc/issues/60).
- Added support for publishing code coverage to `CodeCov.io` and
Expand Down
81 changes: 65 additions & 16 deletions source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ function Get-TargetResource

if (Test-Path -Path $Path)
{
$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

if ($null -ne $fileContent)
{
Expand Down Expand Up @@ -180,15 +191,26 @@ function Set-TargetResource
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding -Path $Path -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8' -ErrorAction SilentlyContinue
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding -ErrorAction SilentlyContinue
}

$fileProperties = @{
Path = $Path
Expand Down Expand Up @@ -248,10 +270,13 @@ function Set-TargetResource
{
if ($results.Count -eq 0)
{
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
if ($PSBoundParameters.ContainsKey('Encoding') -and `
(($Encoding -eq $fileEncoding) -or `
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')))
{
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
}
else
{
Expand Down Expand Up @@ -281,7 +306,7 @@ function Set-TargetResource
$fileProperties.Add('Encoding', $Encoding)
}

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -362,7 +387,7 @@ function Test-TargetResource
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -380,7 +405,19 @@ function Test-TargetResource
return ($Ensure -eq 'Absent')
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

if ($null -eq $fileContent)
{
Expand All @@ -390,7 +427,6 @@ function Test-TargetResource
}

$desiredConfigurationMatch = $true
$fileEncoding = Get-FileEncoding -Path $Path
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)
Expand Down Expand Up @@ -456,10 +492,23 @@ function Test-TargetResource

if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
if ($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*')
{
#If the Encoding specified as UTF8, Either UTF8NoBOM or UTF8BOM is acceptable
$desiredConfigurationMatch = $true
}
elseif ($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')
{
#If the Encoding specified as UTF8NoBOM, Either UTF8NoBOM or ASCII is acceptable
$desiredConfigurationMatch = $true
}
else
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)

$desiredConfigurationMatch = $false
$desiredConfigurationMatch = $false
}
}

return $desiredConfigurationMatch
Expand Down Expand Up @@ -543,7 +592,7 @@ function Assert-ParametersValid
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DSC_KeyValuePairFile : OMI_BaseResource
[write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase;
[Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
91 changes: 59 additions & 32 deletions source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ function Get-TargetResource

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

$text = ''

Expand All @@ -56,14 +67,14 @@ function Get-TargetResource
{
# No matches found - already in state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
$text = ($results.Value -join ',')

Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
$Path, $Search, $text)
$Path, $Search, $text)
} # if

return @{
Expand Down Expand Up @@ -136,15 +147,26 @@ function Set-TargetResource
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

$fileProperties = @{
Path = $Path
Expand All @@ -155,25 +177,17 @@ function Set-TargetResource
if ($Type -eq 'Secret')
{
Write-Verbose -Message ($script:localizedData.StringReplaceSecretMessage -f `
$Path)
$Path)

$Text = $Secret.GetNetworkCredential().Password
}
elseif ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)

# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}
# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}

if ($null -eq $fileContent)
Expand All @@ -194,7 +208,7 @@ function Set-TargetResource

$fileProperties.Add('Value', $fileContent)

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -259,7 +273,7 @@ function Test-TargetResource
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -272,11 +286,22 @@ function Test-TargetResource
return $false
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
if ($null -eq $fileEncoding)
{
$fileContent = Get-Content -Path $Path -Raw
}
elseif ($fileEncoding -like 'UTF8*')
{
$fileContent = Get-Content -Path $Path -Raw -Encoding 'UTF8'
}
else
{
$fileContent = Get-Content -Path $Path -Raw -Encoding $fileEncoding
}

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

# Search the file content for any matches
$results = [regex]::Matches($fileContent, $Search)
Expand All @@ -293,19 +318,21 @@ function Test-TargetResource
}
if ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
if (($Encoding -eq $fileEncoding) -or `
($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*') -or `
($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII'))
{
# No matches found and encoding is in desired state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)

return $true
}
else
{
# No matches found but encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
$fileEncoding, $Encoding)

return $false
}
Expand All @@ -331,12 +358,12 @@ function Test-TargetResource
if ($desiredConfigurationMatch)
{
Write-Verbose -Message ($script:localizedData.StringNoReplacementMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplacementRequiredMessage -f `
$Path, $Search)
$Path, $Search)
} # if

return $desiredConfigurationMatch
Expand Down Expand Up @@ -401,7 +428,7 @@ function Assert-ParametersValid
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class DSC_ReplaceText : OMI_BaseResource
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
[Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
Loading

0 comments on commit 2a0e9ba

Please sign in to comment.