build/windows: Port fix_msg.sh to Python

This port made clear that it is almost impossible to
do the langs patching with PowerShell alone.

The new output of the patching will display the encoding.
This commit is contained in:
Bruno Lopes 2025-04-19 14:58:21 -03:00
parent 1fe8db7a94
commit 278946f767
No known key found for this signature in database
3 changed files with 55 additions and 34 deletions

View file

@ -26,8 +26,12 @@ if (-not $GITLAB_CI)
}
# This script needs a bit of MSYS2 to work
Invoke-Expression ((Get-Content build\windows\1_build-deps-msys2.ps1 | Select-String 'MSYS_ROOT\)' -Context 0,13) -replace '> ','')
# This script needs a bit of Python to work
#FIXME: Restore the condition when TWAIN 32-bit support is dropped
#if (-not (Get-Command "python" -ErrorAction SilentlyContinue) -or "$(Get-Command "python" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source)" -like '*WindowsApps*')
# {
Invoke-Expression ((Get-Content build\windows\1_build-deps-msys2.ps1 | Select-String 'MSYS_ROOT\)' -Context 0,13) -replace '> ','')
# }
# 1. GET INNO
@ -163,25 +167,8 @@ function fix_msg ([array]$langsArray, [string]$AppVer)
{
Copy-Item "$langfilePath" "$Env:Tmp\$(Split-Path $langfile -Leaf).bak" -Force
#Prefer MSYS2 since PowerShell (even 7.1+) doesn't handle well files with mixed encodings
$langfilePathUnix = "$langfilePath" -replace '\\','/' -replace '//','/'
bash build/windows/installer/lang/fix_msg.sh "$langfilePathUnix" $AppVer
#Write-Output "(INFO): temporarily patching $langfilePath with $AppVer"
#$Encoding = 'utf8NoBOM'
#$bytes = $(Get-Content $langfilePath -AsByteStream)[0..1]
#if ("$bytes" -eq "239 187")
# {
# $Encoding = 'utf8BOM'
# }
#$msg = Get-Content $langfilePath -Encoding $Encoding
#$linenumber = $msg | Select-String 'SetupWindowTitle' | Select-Object -ExpandProperty LineNumber
#$msg | ForEach-Object { If ($_.ReadCount -eq $linenumber) {$_ -Replace "%1", "%1 $AppVer"} Else {$_} } |
# Set-Content "$langfilePath" -Encoding $Encoding
#$msg = Get-Content $langfilePath -Encoding $Encoding
#$linenumber = $msg | Select-String 'UninstallAppFullTitle' | Select-Object -ExpandProperty LineNumber
#$msg | ForEach-Object { If ($_.ReadCount -eq $linenumber) {$_ -Replace "%1", "%1 $AppVer"} Else {$_} } |
# Set-Content "$langfilePath" -Encoding $Encoding
#Prefer Python since PowerShell/.NET doesn't handle well files with different encodings
python build\windows\installer\lang\fix_msg.py "$langfilePath" $AppVer
}
else #($AppVer -eq 'revert')
@ -202,7 +189,7 @@ Write-Output "$([char]27)[0Ksection_end:$(Get-Date -UFormat %s -Millisecond 0):i
Write-Output "$([char]27)[0Ksection_start:$(Get-Date -UFormat %s -Millisecond 0):installer_files[collapsed=true]$([char]13)$([char]27)[0KGenerating 32-bit TWAIN dependencies list"
$twain_list_file = 'build\windows\installer\base_twain32on64.list'
Copy-Item $twain_list_file "$twain_list_file.bak"
$twain_list = (python3 build/windows/2_bundle-gimp-uni_dep.py --debug debug-only $(Resolve-Path $GIMP32/lib/gimp/*/plug-ins/twain/twain.exe) $MSYS_ROOT/mingw32/ $GIMP32/ 32 |
$twain_list = (python build\windows\2_bundle-gimp-uni_dep.py --debug debug-only $(Resolve-Path $GIMP32/lib/gimp/*/plug-ins/twain/twain.exe) $MSYS_ROOT/mingw32/ $GIMP32/ 32 |
Select-String 'Installed' -CaseSensitive -Context 0,1000) -replace " `t- ",'bin\'
(Get-Content $twain_list_file) | Foreach-Object {$_ -replace "@DEPS_GENLIST@","$twain_list"} | Set-Content $twain_list_file
(Get-Content $twain_list_file) | Select-string 'Installed' -notmatch | Set-Content $twain_list_file

View file

@ -0,0 +1,46 @@
#!/usr/bin/python3
import platform
import os
import subprocess
import sys
import re
try:
import charset_normalizer
except ImportError:
result = subprocess.run([sys.executable, '--version', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = result.stdout + result.stderr
if re.search(r'\bMSC\b', output, re.IGNORECASE):
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'charset_normalizer'])
else:
MINGW_PACKAGE_PREFIX = "mingw-w64-clang-aarch64" if platform.machine() == "ARM64" else "mingw-w64-clang-x86_64"
subprocess.check_call(['powershell', 'pacman', '--noconfirm', '-S', '--needed', f"{MINGW_PACKAGE_PREFIX}-python-charset-normalizer"])
finally:
from charset_normalizer import detect
langfilePath = sys.argv[1]
AppVer = sys.argv[2]
# Detect the encoding of the file
with open(langfilePath, 'rb') as file:
raw_data = file.read()
detected_encoding = detect(raw_data)['encoding']
print(f"(INFO): temporarily patching {detected_encoding} {langfilePath} with {AppVer}")
# Read the file content with detected encoding
with open(langfilePath, 'r', encoding=detected_encoding) as file:
lines = file.readlines()
# Patch 'SetupWindowTitle' and 'UninstallAppFullTitle'
for i, line in enumerate(lines):
if 'SetupWindowTitle' in line:
before = line.strip()
after = re.sub(r'%1', f'%1 {AppVer}', before)
lines[i] = line.replace(before, after)
if 'UninstallAppFullTitle' in line:
before = line.strip()
after = re.sub(r'%1', f'%1 {AppVer}', before)
lines[i] = line.replace(before, after)
# Write the patched content back to the file using the detected encoding
with open(langfilePath, 'w', encoding=detected_encoding) as file:
file.writelines(lines)

View file

@ -1,12 +0,0 @@
# Manually patches .isl to mimic AppVerName
# https://groups.google.com/g/innosetup/c/w0sebw5YAeg
echo "(INFO): temporarily patching $(echo $1 | sed 's|\/|\\|g') with $2"
before=$(cat "$1" | grep -a 'SetupWindowTitle')
after=$(cat "$1" | grep -a 'SetupWindowTitle' | sed "s|%1|%1 $2|")
sed -i "s|$before|$after|" "$1" >/dev/null 2>&1
before=$(cat "$1" | grep -a 'UninstallAppFullTitle')
after=$(cat "$1" | grep -a 'UninstallAppFullTitle' | sed "s|%1|%1 $2|")
sed -i "s|$before|$after|" "$1" >/dev/null 2>&1