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
35 changes: 35 additions & 0 deletions scripts/ci/lib/windows_filenames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations

import re

WINDOWS_FILENAME_INVALID_CHARS_RE = re.compile(r'[<>:"/\\|?*]')
WINDOWS_FILENAME_INVALID_TRAILING_RE = re.compile(r"[ .]+$")
WINDOWS_RESERVED_DEVICE_NAMES = {
"CON",
"PRN",
"AUX",
"NUL",
*{f"COM{i}" for i in range(1, 10)},
*{f"LPT{i}" for i in range(1, 10)},
}


def validate_windows_filename(name: str) -> None:
if not name or name in {".", ".."}:
raise ValueError(f"invalid Windows filename: {name!r}")

if WINDOWS_FILENAME_INVALID_CHARS_RE.search(name):
raise ValueError(
f"invalid Windows filename {name!r}: contains characters invalid in Windows filenames"
)

if WINDOWS_FILENAME_INVALID_TRAILING_RE.search(name):
raise ValueError(
f"invalid Windows filename {name!r}: trailing spaces or dots are not allowed"
)

stem = name.split(".", 1)[0].upper()
if stem in WINDOWS_RESERVED_DEVICE_NAMES:
raise ValueError(
f"invalid Windows filename {name!r}: {stem!r} is a reserved device name"
)
54 changes: 51 additions & 3 deletions scripts/ci/package_windows_portable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
from dataclasses import dataclass
from datetime import datetime
import json
import pathlib
import re
Expand All @@ -14,6 +15,7 @@

from scripts.ci.lib.artifact_arch import normalize_arch_alias
from scripts.ci.lib.release_artifacts import SHORT_SHA_PATTERN
from scripts.ci.lib.windows_filenames import validate_windows_filename


WINDOWS_CANONICAL_INSTALLER_RE = re.compile(
Expand All @@ -23,6 +25,7 @@
WINDOWS_LEGACY_INSTALLER_RE = re.compile(
r"(?P<name>.+?)_(?P<version>.+?)_(?P<arch>x64|amd64|arm64|aarch64)-setup\.exe$"
)
LEGACY_NIGHTLY_BASE_VERSION_RE = re.compile(r"^[0-9A-Za-z.+]+(?:-[0-9A-Za-z.+]+)*$")

PORTABLE_README_NAME = "README-portable.txt"
PORTABLE_README_TEXT = """AstrBot Windows portable package
Expand Down Expand Up @@ -55,6 +58,14 @@ def normalize_arch(arch: str) -> str:
return normalize_arch_alias(arch) or arch


def is_valid_nightly_date(date_value: str) -> bool:
try:
datetime.strptime(date_value, "%Y%m%d")
except ValueError:
return False
return True


def resolve_project_root_from(start_path: pathlib.Path) -> pathlib.Path:
candidate = start_path.resolve()
if candidate.is_file():
Expand Down Expand Up @@ -101,6 +112,31 @@ def load_project_config_from(start_path: pathlib.Path) -> ProjectConfig:
)


def normalize_legacy_nightly_version(version: str) -> tuple[str, str]:
if "-nightly" not in version:
return version, ""

base_version, separator, nightly_part = version.partition("-nightly")
if not separator or not LEGACY_NIGHTLY_BASE_VERSION_RE.fullmatch(base_version):
return version, ""

nightly_part = nightly_part.lstrip("._-")
if not nightly_part:
return base_version, ""

parts = re.split(r"[._-]", nightly_part, maxsplit=2)
if len(parts) != 2:
return base_version, ""

date_value, sha = parts[0], parts[1]
if not is_valid_nightly_date(date_value):
return base_version, ""
if not re.fullmatch(SHORT_SHA_PATTERN, sha):
return base_version, ""

return base_version, f"_nightly_{sha}"


def load_project_config() -> ProjectConfig:
return load_project_config_from(pathlib.Path(__file__))

Expand All @@ -117,9 +153,11 @@ def installer_to_portable_name(installer_name: str) -> str:
legacy_match = WINDOWS_LEGACY_INSTALLER_RE.fullmatch(installer_name)
if legacy_match:
name = legacy_match.group("name")
version = legacy_match.group("version")
version, nightly_suffix = normalize_legacy_nightly_version(
legacy_match.group("version")
)
arch = normalize_arch(legacy_match.group("arch"))
return f"{name}_{version}_windows_{arch}_portable.zip"
return f"{name}_{version}_windows_{arch}_portable{nightly_suffix}.zip"

raise ValueError(
"Unexpected Windows installer name: "
Expand Down Expand Up @@ -179,6 +217,13 @@ def resolve_product_name(project_root: pathlib.Path) -> str:
product_name = str(config.get("productName", "")).strip()
if not product_name:
raise ValueError(f"Missing productName in {TAURI_CONFIG_RELATIVE_PATH}")
if product_name.lower().endswith(".exe"):
product_name = product_name[:-4].rstrip()
if not product_name:
raise ValueError(
f"productName resolves to an empty executable name in {TAURI_CONFIG_RELATIVE_PATH}"
)
validate_windows_filename(product_name)
return product_name


Expand All @@ -205,7 +250,10 @@ def populate_portable_root(
main_executable_path = resolve_main_executable_path(bundle_dir, project_config)

destination_root.mkdir(parents=True, exist_ok=True)
shutil.copy2(main_executable_path, destination_root / main_executable_path.name)
shutil.copy2(
main_executable_path,
destination_root / f"{project_config.product_name}.exe",
)

webview_loader = release_dir / "WebView2Loader.dll"
if webview_loader.is_file():
Expand Down
Loading