From 278946f767bfc3d351a7049eceabc18b122e9d52 Mon Sep 17 00:00:00 2001 From: Bruno Lopes Date: Sat, 19 Apr 2025 14:58:21 -0300 Subject: [PATCH] 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. --- build/windows/installer/3_dist-gimp-inno.ps1 | 31 ++++--------- build/windows/installer/lang/fix_msg.py | 46 ++++++++++++++++++++ build/windows/installer/lang/fix_msg.sh | 12 ----- 3 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 build/windows/installer/lang/fix_msg.py delete mode 100644 build/windows/installer/lang/fix_msg.sh diff --git a/build/windows/installer/3_dist-gimp-inno.ps1 b/build/windows/installer/3_dist-gimp-inno.ps1 index 6652fcb53b..f0ea3370f0 100644 --- a/build/windows/installer/3_dist-gimp-inno.ps1 +++ b/build/windows/installer/3_dist-gimp-inno.ps1 @@ -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 diff --git a/build/windows/installer/lang/fix_msg.py b/build/windows/installer/lang/fix_msg.py new file mode 100644 index 0000000000..42cf502d98 --- /dev/null +++ b/build/windows/installer/lang/fix_msg.py @@ -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) diff --git a/build/windows/installer/lang/fix_msg.sh b/build/windows/installer/lang/fix_msg.sh deleted file mode 100644 index dce317ffe8..0000000000 --- a/build/windows/installer/lang/fix_msg.sh +++ /dev/null @@ -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