Skip to content

Commit 63bfcb5

Browse files
committed
Fixed encoding bug in the ScriptGenerator
Also added Encoding parmeter to ConvertTo-Script
1 parent fff0373 commit 63bfcb5

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

Source/Private/SetModuleContent.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function SetModuleContent {
22
<#
33
.SYNOPSIS
4-
A wrapper for Set-Content that handles arrays of file paths
4+
A wrapper for Set-Content that copies lists of files
55
.DESCRIPTION
66
The implementation here is strongly dependent on Build-Module doing the right thing
77
Build-Module can optionally pass a PREFIX or SUFFIX, but otherwise only passes files
@@ -15,7 +15,7 @@ function SetModuleContent {
1515
[CmdletBinding()]
1616
param(
1717
# Where to write the joined output
18-
[Parameter(Position=0, Mandatory)]
18+
[Parameter(Position = 0, Mandatory)]
1919
[string]$OutputPath,
2020

2121
# Input files, the scripts that will be copied to the output path
@@ -28,7 +28,7 @@ function SetModuleContent {
2828
# The working directory (allows relative paths for other values)
2929
[string]$WorkingDirectory = $pwd,
3030

31-
# The encoding defaults to UTF8 (or UTF8NoBom on Core)
31+
# The encoding defaults to UTF8 (or UTF8Bom on Core)
3232
[Parameter(DontShow)]
3333
[string]$Encoding = $(if($IsCoreCLR) { "UTF8Bom" } else { "UTF8" })
3434
)

Source/Public/Build-Module.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ function Build-Module {
306306
Join-Path -Path $ModuleInfo.ModuleBase -ChildPath $_ | Convert-Path -ErrorAction SilentlyContinue
307307
}
308308

309-
$ModuleInfo.Generators | Invoke-ScriptGenerator -Path $RootModule -Overwrite
309+
$ModuleInfo.Generators | Invoke-ScriptGenerator -Path $RootModule -Overwrite -Encoding $ModuleInfo.Encoding
310310

311311

312312
# This is mostly for testing ...

Source/Public/ConvertTo-Script.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ function ConvertTo-Script {
5353
# This is used to find the module manifest,
5454
# But the the script will be saved in the same location
5555
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
56-
[string]$Path
56+
[string]$Path,
57+
58+
# File encoding for output RootModule (defaults to UTF8)
59+
# Converted to System.Text.Encoding for PowerShell 6 (and something else for PowerShell 5)
60+
[ValidateSet("UTF8", "UTF8Bom", "UTF8NoBom", "UTF7", "ASCII", "Unicode", "UTF32")]
61+
[string]$Encoding = $(if ($IsCoreCLR) { "UTF8Bom" } else { "UTF8" })
5762
)
5863
begin {
5964
Write-Debug " ENTER: ConvertTo-Script BEGIN $Path $FunctionName"
@@ -81,6 +86,8 @@ function ConvertTo-Script {
8186
return [AstVisitAction]::Continue
8287
}
8388
}
89+
90+
$SetContentCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Set-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
8491
Write-Debug " EXIT: ConvertTo-Script BEGIN"
8592
}
8693
process {
@@ -131,7 +138,7 @@ function ConvertTo-Script {
131138
Get-Item $Path
132139
) | CompressToBase64 -ExpandScriptName ImportBase64Module
133140
"$FunctionName @PSBoundParameters"
134-
) | Set-Content "$FunctionName.ps1"
141+
) | & $SetContentCmd -Path "$FunctionName.ps1" -Encoding $Encoding
135142

136143
Update-ScriptFileInfo "$FunctionName.ps1" -Version $Manifest.ModuleVersion -Author $Manifest.Author -CompanyName $Manifest.CompanyName -Copyright $Manifest.Copyright -Tags $Manifest.PrivateData.PSData.Tags -ProjectUri $Manifest.PrivateData.PSData.ProjectUri -LicenseUri $Manifest.PrivateData.PSData.LicenseUri -IconUri $Manifest.PrivateData.PSData.IconUri -ReleaseNotes $Manifest.PrivateData.PSData.ReleaseNotes
137144

Source/Public/Invoke-ScriptGenerator.ps1

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ function Invoke-ScriptGenerator {
6868

6969
# If set, will overwrite the Source with the generated content.
7070
# Use with care, as this will modify the source file!
71-
[switch]$Overwrite
71+
[switch]$Overwrite,
72+
73+
# The encoding defaults to UTF8 (or UTF8Bom on Core)
74+
[Parameter(DontShow)]
75+
[string]$Encoding = $(if($IsCoreCLR) { "UTF8Bom" } else { "UTF8" })
7276
)
7377
begin {
7478
$AstParam = @{} + $PSBoundParameters
@@ -77,6 +81,7 @@ function Invoke-ScriptGenerator {
7781
$null = $AstParam.Remove("Parameters")
7882
$ParseResults = ConvertToAst @AstParam
7983
[StringBuilder]$Builder = $ParseResults.Ast.Extent.Text
84+
$SetContentCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Set-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
8085
}
8186
process {
8287
if (-not $PSBoundParameters.ContainsKey("Generator") -and $Parameters.ContainsKey("Generator")) {
@@ -115,10 +120,10 @@ function Invoke-ScriptGenerator {
115120

116121
# Find that generator...
117122
$GeneratorCmd = Get-Command -Name ${Generator} -ParameterType Ast -ErrorAction Ignore <# -CommandType Function #> |
118-
Where-Object {
119-
$_.OutputType.Name -eq "TextReplacement" -or ($_.CommandType -eq "Alias" -and $_.Definition -like "PesterMock*" )
120-
} |
121-
Select-Object -First 1
123+
Where-Object {
124+
$_.OutputType.Name -eq "TextReplacement" -or ($_.CommandType -eq "Alias" -and $_.Definition -like "PesterMock*" )
125+
} |
126+
Select-Object -First 1
122127

123128
if (-not $GeneratorCmd) {
124129
Write-Error "Generator missconfiguration. Unable to find Generator = '$Generator'"
@@ -137,14 +142,14 @@ function Invoke-ScriptGenerator {
137142
$ParseResults = ConvertToAst -Code $Builder.ToString() -Path $ParseResults.Path
138143
# In case a Generator tries to use the actual files, update the content
139144
if ($Overwrite -and $ParseResults.Path -and $ParseResults.Path -ne "scriptblock") {
140-
Set-Content $ParseResults.Path $Builder
145+
& $SetContentCmd -Path $ParseResults.Path -Value $Builder -Encoding $Encoding
141146
}
142147
}
143148
}
144149
end {
145150
Write-Debug "Overwrite: $Overwrite and it's a file: $(([bool]$ParseResults.Path) -and $ParseResults.Path -ne "scriptblock") (Content is $($Builder.Length) long)"
146151
if ($Overwrite -and $ParseResults.Path -and $ParseResults.Path -ne "scriptblock") {
147-
Set-Content $ParseResults.Path $Builder
152+
& $SetContentCmd -Path $ParseResults.Path -Value $Builder -Encoding $Encoding
148153
} else {
149154
$Builder.ToString()
150155
}

0 commit comments

Comments
 (0)