Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .template_sync/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
template_version: 0.1.44
template_version: 0.1.45
migrations:
- from: 0.0.0
to: 0.0.1
Expand Down Expand Up @@ -138,3 +138,6 @@ migrations:
- from: 0.1.43
to: 0.1.44
script: migrations/v0_1_43_to_v0_1_44.py
- from: 0.1.44
to: 0.1.45
script: migrations/v0_1_44_to_v0_1_45.py
158 changes: 158 additions & 0 deletions .template_sync/migrations/v0_1_44_to_v0_1_45.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
"""Template 0.1.44 -> 0.1.45: one entry per app in Windows "Installed apps", which the uninstaller removes.

Design note: https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_45.md

post_install.bat wrote its own "Installed apps" entry (key <app>) next to the one constructor writes for the installer (key and name
"<app> <version> (Python 3.x 64-bit)"), both pointing to the same Uninstall-<app>.exe. The uninstaller removes only the first entry whose
uninstaller is its own, and Windows lists the shorter key first: after an uninstall the "(Python ...)" entry stayed, and removing it failed
because its uninstaller was gone. Now post_install.bat writes no entry, and the release names constructor's one entry after the app
(uninstall_name) with the repository owner as publisher (company), as the removed block did; the version and the icon come from the installer.

What this changes in an app repository (each step is independent, idempotent, never deletes a file and never raises for a customised file):

1. .tools/python/bump_constructor.py is replaced by the 0.1.45 file when it is still the template file last shipped (the 0.1.44 copy;
compared with LF line endings; the file keeps its own line endings). A changed file stays as it is (SKIPPED, with what to do by hand).
2. The Windows Apps-list block of app/bash_bat_scripts/post_install.bat (the template's lines, with the app's names) is replaced by three
REM lines. A block changed by hand stays as it is (SKIPPED, with what to do by hand).

construct.yaml is not changed here: bump_constructor.py writes uninstall_name and company at the next release. Both changes reach users with
the app's next release; an app installed before it keeps two entries until it is uninstalled (the design note says how to remove the stale one).
"""
from __future__ import annotations

import re
from pathlib import Path
from typing import Any

DOCS_URL = "https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_45.md"
TAG = "[migration 0.1.45]"
RESOURCE_ROOT = Path(__file__).resolve().parents[1] / "resources" / "v0_1_44_to_v0_1_45"
PREVIOUS_ROOT = RESOURCE_ROOT / "previous"

BUMP = Path(".tools/python/bump_constructor.py")
POST_INSTALL_BAT = Path("app/bash_bat_scripts/post_install.bat")
FILES = (BUMP,)

BUMP_BY_HAND = (
"after save_construct_surgical, write the top-level lines uninstall_name: <app name> and company: <GITHUB_REPOSITORY_OWNER> in "
"construct.yaml, as set_windows_apps_entry of the template's script does"
)
BAT_BY_HAND = (
"delete the block that starts with SET \"ARP_KEY=...\" and ends with the reg add ... /v NoRepair line (constructor writes the "
"installer's own entry)"
)

# The 0.1.33-0.1.44 template block; the placeholders the initialisation fills in match the app's values (one line each, no quotes).
OLD_ARP_TEMPLATE = (
'SET "ARP_KEY=HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\UNDERSCORED_PROJECT_NAME"',
'SET "UNINSTALL_EXE=%PREFIX%\\Uninstall-UNDERSCORED_PROJECT_NAME.exe"',
"REM Windows Apps list: the version of this installer (the version: line of the bundled construct.yaml, written by each release),",
"REM and the app icon the installer copied (the first .ico in the app folder); no icon entry when there is none.",
'SET "DISPLAY_VERSION=VERSION_NUMBER"',
'IF EXIST "%PROJECT_ROOT%\\construct.yaml" FOR /F "usebackq tokens=1,* delims=: " %%A IN (`findstr /B /C:"version:" "%PROJECT_ROOT%\\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B"',
'SET "DISPLAY_ICON="',
'FOR %%I IN ("%PROJECT_ROOT%\\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI"',
'SET "PUBLISHER=GITHUB_OWNER"',
'echo Registering PROJECT_NAME in Windows Apps list >> "%LOG_FILE%"',
'reg add "%ARP_KEY%" /v DisplayName /d "PROJECT_NAME" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v DisplayVersion /d "%DISPLAY_VERSION%" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v Publisher /d "%PUBLISHER%" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v InstallLocation /d "%PREFIX%" /f >> "%LOG_FILE%" 2>&1',
'IF DEFINED DISPLAY_ICON reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v UninstallString /d "\\"%UNINSTALL_EXE%\\"" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v QuietUninstallString /d "\\"%UNINSTALL_EXE%\\" /S" /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v NoModify /t REG_DWORD /d 1 /f >> "%LOG_FILE%" 2>&1',
'reg add "%ARP_KEY%" /v NoRepair /t REG_DWORD /d 1 /f >> "%LOG_FILE%" 2>&1',
)
NEW_ARP_LINES = (
'REM Windows "Installed apps": the installer\'s own entry, which constructor writes after this script (uninstall_name, company and version of',
"REM construct.yaml; the release sets the first two, see bump_constructor.py) and its uninstaller removes. This script writes no entry: a second",
'REM one pointing to the same uninstaller was left behind by it, as "<app> <version> (Python 3.x 64-bit)" with an uninstaller that was gone.',
)
PLACEHOLDERS = ("UNDERSCORED_PROJECT_NAME", "PROJECT_NAME", "VERSION_NUMBER", "GITHUB_OWNER")


def old_arp_pattern() -> re.Pattern[str]:
"""The old block with each placeholder as 'any text on the line without a quote', and LF or CRLF between the lines."""
lines = []
for line in OLD_ARP_TEMPLATE:
escaped = re.escape(line)
for name in PLACEHOLDERS:
escaped = escaped.replace(name, '[^"\\r\\n]*')
lines.append(escaped)
return re.compile("^" + r"\r?\n".join(lines) + r"(?=\r?\n|\Z)", re.MULTILINE)


OLD_ARP = old_arp_pattern()


def log(message: str) -> None:
print(f"{TAG} {message}")


def lf_text(path: Path) -> str:
return path.read_bytes().decode("utf-8").replace("\r\n", "\n")


def app_name_in(template_lf: str, text_lf: str) -> str | None:
"""The app name the initialisation put in place of every PROJECT_NAME of `template_lf` when `text_lf` is that file, else None."""
pieces = template_lf.split("PROJECT_NAME")
if len(pieces) == 1:
return "" if text_lf == template_lf else None
if not text_lf.startswith(pieces[0]):
return None
rest = text_lf[len(pieces[0]) :]
end = rest.find(pieces[1])
name = rest[:end] if end >= 0 else None
if name is None or "\n" in name or '"' in name:
return None
return name if template_lf.replace("PROJECT_NAME", name) == text_lf else None


def replace_bump(path: Path) -> str:
data = path.read_bytes().decode("utf-8")
newline = "\r\n" if "\r\n" in data else "\n"
text = data.replace("\r\n", "\n")
template_new = lf_text(RESOURCE_ROOT / BUMP)
if app_name_in(template_new, text) is not None:
return "unchanged: already up to date"
name = app_name_in(lf_text(PREVIOUS_ROOT / BUMP), text)
if name is None:
return f"SKIPPED: customised (not the 0.1.44 template file), so it was left unchanged. By hand: {BUMP_BY_HAND}. See {DOCS_URL}"
new = template_new.replace("PROJECT_NAME", name) if name else template_new
path.write_bytes(new.replace("\n", newline).encode("utf-8"))
return "updated: replaced by the 0.1.45 file (the release names the Windows Installed apps entry)"


def remove_apps_entry(path: Path) -> str:
text = path.read_bytes().decode("utf-8")
if NEW_ARP_LINES[0] in text:
return "unchanged: already writes no Installed apps entry of its own"
matches = list(OLD_ARP.finditer(text))
if len(matches) != 1:
return f"SKIPPED: no template Apps-list block (changed by hand), so it was left unchanged. By hand: {BAT_BY_HAND}. See {DOCS_URL}"
newline = "\r\n" if "\r\n" in text else "\n"
match = matches[0]
path.write_bytes((text[: match.start()] + newline.join(NEW_ARP_LINES) + text[match.end() :]).encode("utf-8"))
return "updated: writes no Installed apps entry of its own (the installer's entry is the only one, and the uninstaller removes it)"


def migrate(repo_root: Path, context: dict[str, Any]) -> None:
"""Apply the 0.1.45 changes (idempotent, never raises for a customised file, creates and deletes nothing)."""
_ = context
changed = 0
for relative, step in ((BUMP, replace_bump), (POST_INSTALL_BAT, remove_apps_entry)):
path = repo_root / relative
if not path.is_file():
log(f"{relative}: not present in this repository (nothing was created)")
continue
try:
status = step(path)
except (OSError, UnicodeDecodeError) as exc:
status = f"SKIPPED: {exc}; left unchanged. See {DOCS_URL}"
changed += status.startswith("updated")
log(f"{relative}: {status}")
if changed:
log(f"done: {changed} change(s). They reach users with the next release of the app. See {DOCS_URL}")
else:
log("done: no repository changes were required.")
Loading
Loading