Ticket #239: fixing_encoding_and_adding_encoding_directive.ps1

File fixing_encoding_and_adding_encoding_directive.ps1, 3.4 KB (added by patrik.svestka@…, 6 years ago)

powershell script that fixes all the encoding in all files

Line 
1# Author: Patrik Svestka
2# Licence: MIT
3# Version 1.0.0
4
5# Steps to ensure that all the *.st files, maintained in mercurial, are UTF-8 without BOM
6
7# Powershell script:
8# 0) find all files stored in mercurial
9# 1) change the encoding
10# changing encoding to UTF8 without BOM
11# 2) Check if there is a directive:
12# 3) Add it if missing
13# 4) Try to recompile the whole Smalltalk/X solution
14
15# any encoding -> UTF8 without BOM
16# inspired by https://stackoverflow.com/questions/18684793/powershell-batch-change-files-encoding-to-utf-8
17
18$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
19
20#$source_path = 'C:\prg_sdk\stx8-jv_swing\build\stx'
21$source_path = (Resolve-Path .\).Path
22
23$log_file = "$source_path\unicode_conversion.log"
24
25$header = """{ Encoding: utf8 }""`n"
26
27# get only smalltalk source files (*.st) that are stored only in Mercurial
28ForEach ($file_with_path in (Get-ChildItem -Directory -Path $source_path -exclude CVS | where { !(Get-ChildItem $_ -Directory -Recurse -Filter CVS) } | Get-childItem -File -Recurse -Filter *.st | % { $_.FullName }) ) {
29
30 # getting all required details from current file
31 $destination_folder = (Get-Item $file_with_path).DirectoryName
32 $content = Get-Content $file_with_path
33 $file_only = Get-ChildItem $file_with_path | % {$_.Name}
34
35 # change encoding only when file is not empty
36 If ($content -ne $null) {
37 If($content -match '"{ Encoding: utf8 }"') {
38 Write-Output "$file_with_path --> correct file header (changing only encoding)" | Out-File -Append -Encoding UTF8 -FilePath $log_file
39 [System.IO.File]::WriteAllLines($file_with_path, $content, $Utf8NoBomEncoding)
40
41 # Keep the Unix style line end - https://stackoverflow.com/questions/19127741/replace-crlf-using-powershell/19128003#19128003
42 # .NET solution for converting windows end (crlf) to (lf only)
43 $text_with_unix_eol = [IO.File]::ReadAllText($file_with_path) -replace "`r`n", "`n"
44 [IO.File]::WriteAllText($file_with_path, $text_with_unix_eol)
45
46 } Else {
47 Write-Output "$file_with_path -!-> Adding encoding header + changing the encoding" | Out-file -Append -Encoding UTF8 -FilePath $log_file
48
49 # Changing encoding to UTF8 without BOM
50 [System.IO.File]::WriteAllLines($file_with_path, $content, $Utf8NoBomEncoding)
51
52 # storing the original path into a stack
53 pushd -Path $destination_folder -StackName script_directory
54 # adding header to a temp file (random name)
55 $header | Set-Content asd3232d_tempfile.txt
56 # Adding the original file at the end of the temp file
57 Get-Content $file_with_path -ReadCount 8192 | Add-Content asd3232d_tempfile.txt
58 # removing the original file
59 Remove-item $file_with_path
60 # renaming the temp file into the original file name
61 Rename-Item asd3232d_tempfile.txt -NewName $file_only
62 popd -StackName script_directory
63
64 # Keep the Unix style line end
65 # Convert CRLFs to LFs only.
66 # .NET solution for converting windows end (crlf) to (lf only)
67 $text_with_unix_eol = [IO.File]::ReadAllText($file_with_path) -replace "`r`n", "`n"
68 [IO.File]::WriteAllText($file_with_path, $text_with_unix_eol)
69 }
70 } Else {
71 Write-Host "Empty file at: $file_with_path"
72 }
73}
74
75
76
77