From d6088b23bc237ae515babca4011dc7e6634db334 Mon Sep 17 00:00:00 2001 From: Sheng Fan Date: Thu, 2 Jan 2025 23:35:09 +0800 Subject: [PATCH] feat(workflow): Add Python setup and precompile step to NSIS workflow feat(installer): Automate language file inclusion with auto_precompile.py fix(language): Add UTF-8 BOM to zh_TW.nsh docs(gitignore): Add languages.nsh to .gitignore --- .github/workflows/nsis.yaml | 11 ++++-- .gitignore | 1 + auto_precompile.py | 79 +++++++++++++++++++++++++++++++++++++ installer.nsi | 8 +--- languages/zh_TW.nsh | 2 +- 5 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 auto_precompile.py diff --git a/.github/workflows/nsis.yaml b/.github/workflows/nsis.yaml index 9f01f37..4f05b6b 100644 --- a/.github/workflows/nsis.yaml +++ b/.github/workflows/nsis.yaml @@ -3,9 +3,7 @@ on: push: branches: - main - pull_request: - branches: - - main + workflow_dispatch: permissions: contents: write jobs: @@ -23,6 +21,13 @@ jobs: Invoke-WebRequest https://nsis.sourceforge.io/mediawiki/images/c/c9/Inetc.zip -OutFile C:\WINDOWS\Temp\Inetc_plugin.zip Expand-Archive "C:\WINDOWS\Temp\Inetc_plugin.zip" -DestinationPath "C:\Program Files (x86)\NSIS" -Force shell: pwsh + - name: Setup Python + uses: actions/setup-python@v3 + with: + python-version: '3.12' + - name: Precompile Installer Sources + run: | + python auto_precompile.py - name: Build Installer run: | makensis installer.nsi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f97bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +languages.nsh diff --git a/auto_precompile.py b/auto_precompile.py new file mode 100644 index 0000000..fb290f3 --- /dev/null +++ b/auto_precompile.py @@ -0,0 +1,79 @@ +import re +# output to languages.nsh +# first, list languages/ +from dataclasses import dataclass +from pathlib import Path + +ALL_LOCALES = [ + "Serbian", "Vietnamese", "Asturian", "Greek", "Turkish", "Georgian", + "Norwegian", "Macedonian", "Hebrew", "Belarusian", "PortugueseBR", "Welsh", + "Korean", "Japanese", "Estonian", "Afrikaans", "ScotsGaelic", "Czech", + "Esperanto", "Kurdish", "Lithuanian", "Latvian", "Pashto", "Bosnian", + "Croatian", "French", "Farsi", "Hindi", "Hungarian", "SerbianLatin", + "Bulgarian", "SimpChinese", "Indonesian", "Slovenian", "Albanian", + "Arabic", "Armenian", "Ukrainian", "German", "Catalan", "Malay", "Swedish", + "Thai", "Portuguese", "Icelandic", "Luxembourgish", "Irish", "TradChinese", + "Uzbek", "SpanishInternational", "Basque", "Polish", "NorwegianNynorsk", + "Tatar", "Russian", "Finnish", "Breton", "Galician", "Mongolian", "Dutch", + "Spanish", "Romanian", "English", "Italian", "Danish", "Slovak", "Corsican" +] + + +def auto_match_locale(name: str) -> str: + if name.startswith("LANG_"): + name = name[5:] + + for locale in ALL_LOCALES: + if locale.lower() == name.lower(): + return locale + + raise ValueError(f"Could not match locale {name}") + + +def find_locale_identifier(path: Path) -> str: + # iterate lines, find \${.+} + with open(path, "r", encoding="utf-8") as f: + for line in f: + match = re.search(r"\${(.+?)}", line) + if match: + return match.group(1) + raise ValueError(f"Could not find locale identifier in {path}") + + +def auto_fix_utf8bom(path: Path): + # if not bom, add it + with open(path, "rb") as f: + if f.read(3) != b'\xef\xbb\xbf': + with open(path, "r+", encoding="utf-8") as f2: + content = f2.read() + f2.seek(0) + f2.write('\ufeff' + content) + f2.truncate() + + +@dataclass +class Locale: + name: str + path: Path + + +locales: list[Locale] = [] +lang_dir = Path("languages") + +for file in lang_dir.glob("*.nsh"): + auto_fix_utf8bom(file) + locales.append(Locale(file.stem, file)) + +with open("languages.nsh", "w", encoding="utf-8") as f: + f.write("; DON'T EDIT!\n") + f.write("; This file is automatically generated by auto_precompile.py\n") + for locale in locales: + locale_identifier = find_locale_identifier(locale.path) + locale_name = auto_match_locale(locale_identifier) + + f.write( + f'LoadLanguageFile "${{NSISDIR}}\\Contrib\\Language files\\{locale_name}.nlf"\n' + ) + + for locale in locales: + f.write(f"!include \"{locale.path.as_posix().replace('/', '\\')}\"\n") diff --git a/installer.nsi b/installer.nsi index ec89549..6b8a658 100644 --- a/installer.nsi +++ b/installer.nsi @@ -1,12 +1,6 @@ Unicode True -; Load and configure language presets -LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf" -LoadLanguageFile "${NSISDIR}\Contrib\Language files\SimpChinese.nlf" - -; Load language strings -!include "languages\\en.nsh" -!include "languages\\zh_CN.nsh" +!include "languages.nsh" ; Set the default installation directory InstallDir "C:\LaneAssist" diff --git a/languages/zh_TW.nsh b/languages/zh_TW.nsh index 02f0aa5..78f7186 100644 --- a/languages/zh_TW.nsh +++ b/languages/zh_TW.nsh @@ -1,4 +1,4 @@ -; Welcome +; Welcome LangString WelcomeTitle ${LANG_TRADCHINESE} "歡迎使用 歐卡2車道輔助 / ETS2LA 安裝程式" LangString WelcomeText ${LANG_TRADCHINESE} "此安裝包將引導您完成 歐卡2車道輔助 (ETS2LA) 的安裝。$\r$\n$\r$\n按下下一步繼續。"