@@ -68,15 +68,21 @@ 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 ()]
75+ [string ]$Encoding = $ (if ($IsCoreCLR ) { " UTF8Bom" } else { " UTF8" })
7276 )
7377 begin {
7478 $AstParam = @ {} + $PSBoundParameters
7579 $null = $AstParam.Remove (" Overwrite" )
7680 $null = $AstParam.Remove (" Generator" )
7781 $null = $AstParam.Remove (" Parameters" )
82+ $null = $AstParam.Remove (" Encoding" )
7883 $ParseResults = ConvertToAst @AstParam
7984 [StringBuilder ]$Builder = $ParseResults.Ast.Extent.Text
85+ $SetContentCmd = $ExecutionContext.InvokeCommand.GetCommand (' Microsoft.PowerShell.Management\Set-Content' , [System.Management.Automation.CommandTypes ]::Cmdlet)
8086 }
8187 process {
8288 if (-not $PSBoundParameters.ContainsKey (" Generator" ) -and $Parameters.ContainsKey (" Generator" )) {
@@ -115,10 +121,10 @@ function Invoke-ScriptGenerator {
115121
116122 # Find that generator...
117123 $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
124+ Where-Object {
125+ $_.OutputType.Name -eq " TextReplacement" -or ($_.CommandType -eq " Alias" -and $_.Definition -like " PesterMock*" )
126+ } |
127+ Select-Object - First 1
122128
123129 if (-not $GeneratorCmd ) {
124130 Write-Error " Generator missconfiguration. Unable to find Generator = '$Generator '"
@@ -137,14 +143,14 @@ function Invoke-ScriptGenerator {
137143 $ParseResults = ConvertToAst - Code $Builder.ToString () - Path $ParseResults.Path
138144 # In case a Generator tries to use the actual files, update the content
139145 if ($Overwrite -and $ParseResults.Path -and $ParseResults.Path -ne " scriptblock" ) {
140- Set-Content $ParseResults.Path $Builder
146+ & $SetContentCmd - Path $ParseResults.Path - Value $Builder - Encoding $Encoding
141147 }
142148 }
143149 }
144150 end {
145151 Write-Debug " Overwrite: $Overwrite and it's a file: $ ( ([bool ]$ParseResults.Path ) -and $ParseResults.Path -ne " scriptblock" ) (Content is $ ( $Builder.Length ) long)"
146152 if ($Overwrite -and $ParseResults.Path -and $ParseResults.Path -ne " scriptblock" ) {
147- Set-Content $ParseResults.Path $Builder
153+ & $SetContentCmd - Path $ParseResults.Path - Value $Builder - Encoding $Encoding
148154 } else {
149155 $Builder.ToString ()
150156 }
0 commit comments