Skip to content

Commit

Permalink
feat(workflow): Add Python setup and precompile step to NSIS workflow
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fred913 committed Jan 2, 2025
1 parent cce6855 commit d6088b2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/nsis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: write
jobs:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
languages.nsh
79 changes: 79 additions & 0 deletions auto_precompile.py
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 1 addition & 7 deletions installer.nsi
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion languages/zh_TW.nsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; Welcome
; Welcome
LangString WelcomeTitle ${LANG_TRADCHINESE} "歡迎使用 歐卡2車道輔助 / ETS2LA 安裝程式"
LangString WelcomeText ${LANG_TRADCHINESE} "此安裝包將引導您完成 歐卡2車道輔助 (ETS2LA) 的安裝。$\r$\n$\r$\n按下下一步繼續。"

Expand Down

0 comments on commit d6088b2

Please sign in to comment.