166 lines
6.7 KiB
Python
166 lines
6.7 KiB
Python
"""多项目定义:每个项目的源码仓库、Xcode 工程、配置替换方式等。
|
||
|
||
config.json 结构:
|
||
{
|
||
"projects": {
|
||
"<项目 ID>": {
|
||
# —— 项目定义(管理员在「项目管理」维护)——
|
||
"name": "显示名称",
|
||
"git": {"remote_url": "", "username": "", "password": "", "use_env": false},
|
||
"source_dir": "", # 共享源码工作目录,留空为 <服务目录>/sources/<项目 ID>
|
||
"xcode": {"workspace": "", "project": "X.xcodeproj"},
|
||
"copy_items": [], # 拷贝到打包目录的条目,留空拷贝整个仓库
|
||
"patcher": "generic", # readoor(阅门户专用)/ generic(通用 + patch_rules)
|
||
"patch_rules": [], # generic 的额外替换规则,见 project_patcher
|
||
"app_fields": [], # App 的项目自定义字段 [{key, label, required}]
|
||
"server_fields": [], # 服务器环境的项目自定义字段
|
||
"obfuscation": false, # 是否支持代码混淆
|
||
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
|
||
# 本项目钉钉 @ 名单,为空则用 upload.dingtalk 的全局名单
|
||
# —— 项目数据(与原单项目时的顶层字段相同)——
|
||
"apps": {}, "schemes": {}, "servers": {}, "branches": [], "tags": [],
|
||
"versions": {}, "branch_track": {}, "next_app_id_prefix": 1
|
||
}
|
||
},
|
||
"upload": {...} # 上传与钉钉通知,所有项目共用
|
||
}
|
||
"""
|
||
import re
|
||
from contextvars import ContextVar
|
||
from pathlib import Path
|
||
|
||
from .config import (
|
||
COPY_ITEMS,
|
||
GIT_PASSWORD,
|
||
GIT_REMOTE_URL,
|
||
GIT_SOURCE_BASE,
|
||
GIT_SOURCE_DIR,
|
||
GIT_USERNAME,
|
||
SERVER_ROOT,
|
||
inject_git_credentials,
|
||
)
|
||
|
||
# 升级前的单项目配置迁移为这个项目,并沿用 .env 中的 Git 配置。
|
||
LEGACY_PROJECT_ID = "readoor"
|
||
|
||
PATCHER_READOOR = "readoor"
|
||
PATCHER_GENERIC = "generic"
|
||
PATCHERS = (PATCHER_READOOR, PATCHER_GENERIC)
|
||
|
||
# 项目定义字段;其余字段(apps、schemes、versions 等)都是项目数据。
|
||
DEFINITION_KEYS = (
|
||
"name", "git", "source_dir", "xcode", "copy_items", "patcher",
|
||
"patch_rules", "app_fields", "server_fields", "obfuscation", "dingtalk_at",
|
||
)
|
||
|
||
# 原单项目(阅门户)的定义,迁移旧配置时写入。
|
||
LEGACY_PROJECT_DEFINITION = {
|
||
"name": "Readoor",
|
||
"git": {"remote_url": "", "username": "", "password": "", "use_env": True},
|
||
"source_dir": "",
|
||
"xcode": {"workspace": "readoor.xcworkspace", "project": "readoor.xcodeproj"},
|
||
"copy_items": list(COPY_ITEMS),
|
||
"patcher": PATCHER_READOOR,
|
||
"patch_rules": [],
|
||
"app_fields": [],
|
||
"server_fields": [],
|
||
"obfuscation": True,
|
||
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
|
||
}
|
||
|
||
# 当前请求操作的项目,由 main.py 的中间件按请求头 X-Project 设置;
|
||
# load_config() 未显式指定项目时使用它,未设置则取第一个项目。
|
||
PROJECT_HEADER = "X-Project"
|
||
current_project_id: ContextVar = ContextVar("current_project_id", default=None)
|
||
|
||
_PROJECT_ID_PATTERN = re.compile(r"^[a-z][a-z0-9_-]{0,31}$")
|
||
|
||
|
||
def is_valid_project_id(project_id: str) -> bool:
|
||
"""项目 ID 会用于目录名和请求头,只允许小写字母开头的短标识。"""
|
||
return bool(_PROJECT_ID_PATTERN.fullmatch(project_id or ""))
|
||
|
||
|
||
def default_definition(project_id: str) -> dict:
|
||
"""项目定义缺省值:旧项目沿用原有行为,新项目为通用配置。"""
|
||
if project_id == LEGACY_PROJECT_ID:
|
||
return LEGACY_PROJECT_DEFINITION
|
||
return {
|
||
"name": project_id,
|
||
"git": {"remote_url": "", "username": "", "password": "", "use_env": False},
|
||
"source_dir": "",
|
||
"xcode": {"workspace": "", "project": ""},
|
||
"copy_items": [],
|
||
"patcher": PATCHER_GENERIC,
|
||
"patch_rules": [],
|
||
"app_fields": [],
|
||
"server_fields": [],
|
||
"obfuscation": False,
|
||
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
|
||
}
|
||
|
||
|
||
def get_definition(project_id: str, project: dict) -> dict:
|
||
"""取项目定义,缺失字段用缺省值补齐(只读,不写回配置)。"""
|
||
defaults = default_definition(project_id)
|
||
definition = {key: project.get(key, defaults[key]) for key in DEFINITION_KEYS}
|
||
definition["git"] = {**defaults["git"], **(definition.get("git") or {})}
|
||
definition["xcode"] = {**defaults["xcode"], **(definition.get("xcode") or {})}
|
||
definition["dingtalk_at"] = {**defaults["dingtalk_at"], **(definition.get("dingtalk_at") or {})}
|
||
if definition["patcher"] not in PATCHERS:
|
||
definition["patcher"] = PATCHER_GENERIC
|
||
return definition
|
||
|
||
|
||
def public_definition(project_id: str, project: dict) -> dict:
|
||
"""打包页和配置页渲染所需的项目信息,不含 Git 凭据。"""
|
||
definition = get_definition(project_id, project)
|
||
return {
|
||
"id": project_id,
|
||
"name": definition["name"],
|
||
"patcher": definition["patcher"],
|
||
"obfuscation": bool(definition["obfuscation"]),
|
||
"app_fields": definition["app_fields"],
|
||
"server_fields": definition["server_fields"],
|
||
}
|
||
|
||
|
||
class ProjectSource:
|
||
"""项目源码的获取方式。"""
|
||
|
||
def __init__(self, remote_url: str, remote_url_with_credentials: str,
|
||
shared_dir: Path, manual_base: Path = None):
|
||
self.remote_url = remote_url
|
||
self.remote_url_with_credentials = remote_url_with_credentials
|
||
# 配置远程仓库时,所有分支共用的源码工作目录
|
||
self.shared_dir = shared_dir
|
||
# 未配置远程仓库时,按 <manual_base>/<branch> 读取人工维护的分支目录(仅旧项目支持)
|
||
self.manual_base = manual_base
|
||
|
||
|
||
def get_project_source(project_id: str, project: dict) -> ProjectSource:
|
||
definition = get_definition(project_id, project)
|
||
git = definition["git"]
|
||
use_env = bool(git.get("use_env"))
|
||
|
||
remote_url = (git.get("remote_url") or "").strip() or (GIT_REMOTE_URL if use_env else "")
|
||
username = git.get("username") or (GIT_USERNAME if use_env else "")
|
||
password = git.get("password") or (GIT_PASSWORD if use_env else "")
|
||
|
||
source_dir = (definition.get("source_dir") or "").strip()
|
||
if source_dir:
|
||
shared_dir = Path(source_dir).expanduser()
|
||
if not shared_dir.is_absolute():
|
||
shared_dir = SERVER_ROOT / shared_dir
|
||
elif use_env:
|
||
shared_dir = GIT_SOURCE_DIR
|
||
else:
|
||
shared_dir = SERVER_ROOT / "sources" / project_id
|
||
|
||
return ProjectSource(
|
||
remote_url=remote_url,
|
||
remote_url_with_credentials=inject_git_credentials(remote_url, username, password),
|
||
shared_dir=shared_dir,
|
||
manual_base=GIT_SOURCE_BASE if use_env else None,
|
||
)
|