@@ -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