feat: 支持多项目打包

- config.json 改为按项目存储(projects.<id>),上传配置全局共用;旧单项目配置自动迁移为 readoor
- 新增项目管理(管理员):Git 仓库/凭据、源码目录、Xcode 工程、拷贝条目、配置替换方式、自定义字段、替换规则
- 通用配置替换:pbxproj 版本号与签名,patch_rules 支持 define / replace_line 及 ${app.X}、${server.X} 模板
- 请求通过 X-Project 头选择项目;打包任务记录 project_id,历史按项目过滤
- 前端顶部切换项目,App/服务器环境表单按项目显示字段

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-22 14:59:30 +09:00
co-authored by Claude Opus 5.5
parent 2bd5c7057f
commit 8f2f23682f
19 changed files with 1535 additions and 137 deletions
+16 -14
View File
@@ -57,27 +57,29 @@ GIT_USERNAME = os.getenv("GIT_USERNAME", "")
GIT_PASSWORD = os.getenv("GIT_PASSWORD", "")
def get_git_remote_url(with_credentials: bool = False) -> str:
"""获取 Git 远程地址,可选注入用户名密码"""
if not with_credentials or not GIT_REMOTE_URL or not GIT_USERNAME:
return GIT_REMOTE_URL
if "://" not in GIT_REMOTE_URL:
return GIT_REMOTE_URL
def inject_git_credentials(url: str, username: str, password: str = "") -> str:
"""向 http(s) 形式的 Git 地址注入用户名密码;已带凭证或非 URL 形式时原样返回"""
if not url or not username or "://" not in url:
return url
parts = urlsplit(GIT_REMOTE_URL)
parts = urlsplit(url)
if "@" in parts.netloc:
return GIT_REMOTE_URL
return url
username = quote(GIT_USERNAME, safe="")
if GIT_PASSWORD:
password = quote(GIT_PASSWORD, safe="")
auth = f"{username}:{password}"
else:
auth = username
auth = quote(username, safe="")
if password:
auth = f"{auth}:{quote(password, safe='')}"
netloc = f"{auth}@{parts.netloc}"
return urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment))
def get_git_remote_url(with_credentials: bool = False) -> str:
"""获取 .env 中的 Git 远程地址,可选注入用户名密码"""
if not with_credentials:
return GIT_REMOTE_URL
return inject_git_credentials(GIT_REMOTE_URL, GIT_USERNAME, GIT_PASSWORD)
def mask_git_remote_url(url: str) -> str:
"""对 Git 远程地址中的凭证做脱敏"""
if not url or "://" not in url:
+1
View File
@@ -27,6 +27,7 @@ def _migrate_db():
("tasks", "error_category", "VARCHAR"),
("tasks", "superseded_at", "TIMESTAMP"),
("tasks", "dsym_url", "VARCHAR"),
("tasks", "project_id", "VARCHAR DEFAULT 'readoor'"),
]
with engine.connect() as conn:
for table, column, col_type in migrations:
+14 -1
View File
@@ -12,6 +12,7 @@ from .routers import config, apps, tasks, auth, users, operations
from .services.log_streamer import log_streamer
from .config import BACKEND_PORT, CORS_ALLOWED_ORIGINS, TRUSTED_HOSTS, validate_production_security
from .deps import decode_current_user
from .projects import PROJECT_HEADER, current_project_id
STATIC_DIR = Path(__file__).parent / "static"
@@ -37,11 +38,23 @@ if CORS_ALLOWED_ORIGINS:
allow_origins=CORS_ALLOWED_ORIGINS,
allow_credentials=False,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["Authorization", "Content-Type"],
allow_headers=["Authorization", "Content-Type", PROJECT_HEADER],
)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=TRUSTED_HOSTS)
@app.middleware("http")
async def bind_project(request: Request, call_next):
"""按请求头 X-Project(下载链接等无法带请求头时用 ?project=)确定本次请求操作的项目。"""
project_id = request.headers.get(PROJECT_HEADER) or request.query_params.get("project") or None
token = current_project_id.set(project_id)
try:
return await call_next(request)
finally:
current_project_id.reset(token)
# 路由
app.include_router(auth.router)
app.include_router(users.router)
+1
View File
@@ -9,6 +9,7 @@ class Task(Base):
__tablename__ = "tasks"
id = Column(String, primary_key=True)
project_id = Column(String, default="readoor") # 所属项目,见 projects.py
app_id = Column(String, nullable=False)
app_name = Column(String, nullable=False)
build_type = Column(String, nullable=False)
+160
View File
@@ -0,0 +1,160 @@
"""多项目定义:每个项目的源码仓库、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, # 是否支持代码混淆
# —— 项目数据(与原单项目时的顶层字段相同)——
"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",
)
# 原单项目(阅门户)的定义,迁移旧配置时写入。
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,
}
# 当前请求操作的项目,由 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,
}
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 {})}
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,
)
+18 -5
View File
@@ -1,7 +1,9 @@
"""打包选择 API"""
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from .config import get_tags, load_config
from ..deps import get_current_user
from ..projects import PATCHER_READOOR, get_definition, public_definition
from .config import get_tags, load_config, load_projects
router = APIRouter(prefix="/api", tags=["apps"])
@@ -15,11 +17,22 @@ _APP_SCHEME_RULES = {
_DEFAULT_SCHEME_NAMES = ("readoor31", "readoor31OtherPay")
def get_allowed_scheme_names(app: dict) -> tuple[str, ...]:
"""返回 App 可使用的 Scheme 名称。"""
def get_allowed_scheme_names(app: dict, config: dict) -> tuple[str, ...]:
"""返回 App 可使用的 Scheme 名称;空元组表示不限制。
按 App 名称限定 Scheme 是 Readoor 项目的规则,其它项目不限制。
"""
if get_definition(config["_project"], config)["patcher"] != PATCHER_READOOR:
return ()
return _APP_SCHEME_RULES.get(app.get("name", ""), _DEFAULT_SCHEME_NAMES)
@router.get("/projects", dependencies=[Depends(get_current_user)])
async def get_projects():
"""获取项目列表(供切换项目,不含 Git 凭据)"""
return [public_definition(project_id, project) for project_id, project in load_projects().items()]
@router.get("/apps")
async def get_apps_for_build():
"""获取 apps 列表(供打包选择)"""
@@ -27,7 +40,7 @@ async def get_apps_for_build():
apps = {}
for app_id, app in config.get("apps", {}).items():
app_data = app.copy()
app_data["allowed_scheme_names"] = list(get_allowed_scheme_names(app))
app_data["allowed_scheme_names"] = list(get_allowed_scheme_names(app, config))
apps[app_id] = app_data
return apps
+291 -30
View File
@@ -1,5 +1,6 @@
"""配置管理 API"""
import asyncio
import copy
import json
import os
import re
@@ -23,6 +24,17 @@ from ..config import (
SKINS_DIR,
)
from ..deps import get_current_user, require_admin
from ..projects import (
DEFINITION_KEYS,
LEGACY_PROJECT_DEFINITION,
LEGACY_PROJECT_ID,
PATCHER_READOOR,
PATCHERS,
current_project_id,
default_definition,
get_definition,
is_valid_project_id,
)
def _require_config_permission(request: Request, user: dict = Depends(get_current_user)) -> dict:
@@ -533,27 +545,86 @@ def _migrate_old_themes(config: dict) -> bool:
return True
def load_config() -> dict:
"""读取 config.json"""
def _default_project_data(project_id: str) -> dict:
"""新项目的初始数据;旧项目沿用原单项目时的默认服务器环境。"""
legacy = project_id == LEGACY_PROJECT_ID
return {
"apps": {}, "schemes": {},
"servers": dict(DEFAULT_SERVERS) if legacy else {},
"next_app_id_prefix": 5 if legacy else 1,
"branches": ["main"],
}
def _migrate_root(raw: dict) -> tuple:
"""把升级前的单项目配置(顶层即项目数据)包装为 projects.readoor,返回 (root, 是否变更)。"""
if isinstance(raw.get("projects"), dict) and raw["projects"]:
return raw, False
project = {key: value for key, value in raw.items() if key not in ("projects", "upload")}
if not project:
project = _default_project_data(LEGACY_PROJECT_ID)
project = {**copy.deepcopy(LEGACY_PROJECT_DEFINITION), **project}
root = {"projects": {LEGACY_PROJECT_ID: project}}
if "upload" in raw:
root["upload"] = raw["upload"]
return root, True
def _read_root() -> dict:
"""读取 config.json 根对象(含所有项目),必要时迁移旧结构。"""
if not CONFIG_JSON_PATH.exists():
# 首次部署:优先从项目根目录下的 config.json 导入
if BOOTSTRAP_CONFIG_PATH.exists():
CONFIG_JSON_PATH.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(BOOTSTRAP_CONFIG_PATH, CONFIG_JSON_PATH)
else:
return {
"apps": {}, "schemes": {}, "servers": DEFAULT_SERVERS,
"next_app_id_prefix": 5, "branches": ["main"], "upload": DEFAULT_UPLOAD,
}
root, _ = _migrate_root({})
root["upload"] = DEFAULT_UPLOAD
return root
with open(CONFIG_JSON_PATH, "r", encoding="utf-8") as f:
config = json.load(f)
root, migrated = _migrate_root(json.load(f))
if "upload" not in root:
root["upload"] = DEFAULT_UPLOAD
if migrated:
_write_root(root)
return root
def _write_root(root: dict):
"""保存 config.json(自动备份)"""
if CONFIG_JSON_PATH.exists():
backup_path = CONFIG_JSON_PATH.with_suffix(".json.bak")
shutil.copy2(CONFIG_JSON_PATH, backup_path)
CONFIG_JSON_PATH.parent.mkdir(parents=True, exist_ok=True)
with open(CONFIG_JSON_PATH, "w", encoding="utf-8") as f:
json.dump(root, f, indent=2, ensure_ascii=False)
def resolve_project_id(root: dict, project_id: str = None) -> str:
"""确定要操作的项目:显式指定 > 请求头 X-Project > 第一个项目。"""
projects = root["projects"]
project_id = project_id or current_project_id.get()
if not project_id:
return next(iter(projects))
if project_id not in projects:
raise HTTPException(status_code=400, detail=f"项目不存在: {project_id}")
return project_id
def load_config(project_id: str = None) -> dict:
"""读取某个项目的配置视图。
返回值与单项目时的 config.json 结构相同(apps/schemes/servers/... + upload),
并带 "_project" 标记,save_config() 据此写回对应项目。
"""
root = _read_root()
project_id = resolve_project_id(root, project_id)
config = {**root["projects"][project_id], "upload": root["upload"], "_project": project_id}
# 确保必要字段存在
if "servers" not in config:
config["servers"] = DEFAULT_SERVERS
config["servers"] = _default_project_data(project_id)["servers"]
if "branches" not in config:
config["branches"] = ["main"]
if "upload" not in config:
config["upload"] = DEFAULT_UPLOAD
versions_changed = _ensure_versions(config)
# 自动补齐旧配置的环境前缀、特殊 App 覆盖和 upload_key。
prefixes_changed = _ensure_server_id_prefixes(config)
@@ -568,12 +639,25 @@ def load_config() -> dict:
def save_config(config: dict):
"""保存 config.json(自动备份)"""
if CONFIG_JSON_PATH.exists():
backup_path = CONFIG_JSON_PATH.with_suffix(".json.bak")
shutil.copy2(CONFIG_JSON_PATH, backup_path)
with open(CONFIG_JSON_PATH, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
"""保存 load_config() 返回的项目配置视图(其中的 upload 为所有项目共用)。"""
root = _read_root()
project_id = config.get("_project") or resolve_project_id(root)
root["projects"][project_id] = {
key: value for key, value in config.items() if key not in ("upload", "_project")
}
if "upload" in config:
root["upload"] = config["upload"]
_write_root(root)
def load_upload_config() -> dict:
"""读取所有项目共用的上传配置"""
return _read_root()["upload"]
def load_projects() -> dict:
"""读取所有项目(项目 ID → 项目配置,不含共用的 upload)。"""
return _read_root()["projects"]
def get_build_config_value(db: Session, key: str, default=None) -> str:
@@ -584,14 +668,15 @@ def get_build_config_value(db: Session, key: str, default=None) -> str:
@router.get("")
async def get_full_config():
"""获取完整 config.json"""
"""获取当前项目的完整配置"""
return load_config()
@router.put("")
async def update_full_config(config: dict):
"""更新整个 config.json"""
"""更新当前项目的完整配置(upload 为所有项目共用)"""
async with _config_lock:
config["_project"] = load_config()["_project"]
save_config(config)
return {"message": "配置已更新"}
@@ -603,6 +688,25 @@ async def get_apps():
return config.get("apps", {})
def _validate_custom_fields(config: dict, fields_key: str, data: dict):
"""校验项目自定义字段(app_fields / server_fields)中的必填项。"""
definition = get_definition(config["_project"], config)
for field in definition[fields_key]:
if field.get("required") and not str(data.get(field["key"], "")).strip():
raise HTTPException(status_code=400, detail=f"{field.get('label') or field['key']} 不能为空")
def _server_entry(config: dict, server_data: dict, app_id_prefix) -> dict:
"""按项目保留服务器环境字段:Readoor 工程使用 api/assDom/universalLink,另加项目自定义字段。"""
definition = get_definition(config["_project"], config)
keys = [field["key"] for field in definition["server_fields"]]
if definition["patcher"] == PATCHER_READOOR:
keys = ["api", "assDom", "universalLink"] + keys
entry = {key: str(server_data.get(key, "")).strip() for key in keys}
entry["app_id_prefix"] = app_id_prefix
return entry
@router.post("/apps")
async def create_app(app: dict):
"""新增 app"""
@@ -611,6 +715,8 @@ async def create_app(app: dict):
config = load_config()
apps = config.get("apps", {})
_validate_custom_fields(config, "app_fields", app)
# 按服务环境自动生成三位 ID(例如测试环境 1xx、正式环境 2xx)。
_apply_special_app_prefix_override(app)
_normalize_tencent_app_id(app)
@@ -636,6 +742,7 @@ async def update_app(app_id: str, app: dict):
if app_id not in apps:
raise HTTPException(status_code=404, detail="App 不存在")
_validate_custom_fields(config, "app_fields", app)
_normalize_tencent_app_id(app)
apps[app_id] = app
config["apps"] = apps
@@ -1111,13 +1218,9 @@ async def create_server(server_data: dict):
if name in servers:
raise HTTPException(status_code=400, detail="环境名称已存在")
_validate_custom_fields(config, "server_fields", server_data)
prefix = config.get("next_app_id_prefix", 1)
servers[name] = {
"api": server_data.get("api", ""),
"assDom": server_data.get("assDom", ""),
"universalLink": server_data.get("universalLink", ""),
"app_id_prefix": prefix,
}
servers[name] = _server_entry(config, server_data, prefix)
config["servers"] = servers
config["next_app_id_prefix"] = prefix + 1
save_config(config)
@@ -1143,12 +1246,8 @@ async def update_server(server_name: str, server_data: dict):
else:
new_name = server_name
servers[new_name] = {
"api": server_data.get("api", ""),
"assDom": server_data.get("assDom", ""),
"universalLink": server_data.get("universalLink", ""),
"app_id_prefix": servers[new_name].get("app_id_prefix"),
}
_validate_custom_fields(config, "server_fields", server_data)
servers[new_name] = _server_entry(config, server_data, servers[new_name].get("app_id_prefix"))
config["servers"] = servers
save_config(config)
return {"message": "服务器环境已更新"}
@@ -1174,3 +1273,165 @@ async def delete_server(server_name: str):
config["servers"] = servers
save_config(config)
return {"message": "服务器环境已删除"}
# ---- 项目管理 ----
# App / 服务器环境中已有固定含义的字段,项目自定义字段不能占用
_RESERVED_FIELD_KEYS = {
"app_fields": {"name", "server", "certificates", "upload_key", "skins"},
"server_fields": {"name", "app_id_prefix"},
}
_FIELD_KEY_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]{0,63}$")
_RULE_TYPES = {"define": ("file", "name", "value"), "replace_line": ("file", "keyword", "line")}
def _mask_definition(project_id: str, project: dict) -> dict:
"""项目定义(不返回 Git 密码,只标记是否已设置)"""
definition = get_definition(project_id, project)
git = dict(definition["git"])
git["password_set"] = bool(git.pop("password", ""))
return {"id": project_id, **definition, "git": git}
def _clean_fields(fields, fields_key: str) -> list:
if not isinstance(fields, list):
raise HTTPException(status_code=400, detail=f"{fields_key} 必须是数组")
cleaned, seen = [], set()
for field in fields:
key = str((field or {}).get("key", "")).strip()
if not _FIELD_KEY_PATTERN.fullmatch(key):
raise HTTPException(status_code=400, detail=f"字段名无效: {key or '(空)'}(字母、数字、下划线)")
if key in _RESERVED_FIELD_KEYS[fields_key] or key in seen:
raise HTTPException(status_code=400, detail=f"字段名重复或为保留字段: {key}")
seen.add(key)
cleaned.append({
"key": key,
"label": str(field.get("label", "")).strip() or key,
"required": bool(field.get("required")),
})
return cleaned
def _clean_patch_rules(rules) -> list:
if not isinstance(rules, list):
raise HTTPException(status_code=400, detail="patch_rules 必须是数组")
cleaned = []
for index, rule in enumerate(rules, start=1):
rule_type = (rule or {}).get("type")
if rule_type not in _RULE_TYPES:
raise HTTPException(status_code=400, detail=f"第 {index} 条替换规则类型无效(define / replace_line)")
item = {"type": rule_type}
for key in _RULE_TYPES[rule_type]:
value = str(rule.get(key, ""))
if key != "value" and key != "line" and not value.strip():
raise HTTPException(status_code=400, detail=f"第 {index} 条替换规则缺少 {key}")
item[key] = value.strip() if key in ("file", "name", "keyword") else value
file = item["file"]
if file.startswith("/") or ".." in Path(file).parts:
raise HTTPException(status_code=400, detail=f"第 {index} 条替换规则文件需为仓库内相对路径")
cleaned.append(item)
return cleaned
def _apply_definition(project: dict, data: dict, existing_git: dict = None):
"""把请求中的项目定义字段校验后写入 project(只更新请求中出现的字段)。"""
if "name" in data:
name = str(data["name"]).strip()
if not name:
raise HTTPException(status_code=400, detail="项目名称不能为空")
project["name"] = name
if "git" in data:
git = data["git"] or {}
password = str(git.get("password", ""))
if not password and existing_git:
password = existing_git.get("password", "")
project["git"] = {
"remote_url": str(git.get("remote_url", "")).strip(),
"username": str(git.get("username", "")).strip(),
"password": password,
"use_env": bool(git.get("use_env")),
}
if "source_dir" in data:
project["source_dir"] = str(data["source_dir"] or "").strip()
if "xcode" in data:
xcode = data["xcode"] or {}
project["xcode"] = {
"workspace": str(xcode.get("workspace", "")).strip(),
"project": str(xcode.get("project", "")).strip(),
}
if "copy_items" in data:
items = data["copy_items"]
if not isinstance(items, list):
raise HTTPException(status_code=400, detail="copy_items 必须是数组")
project["copy_items"] = [str(item).strip() for item in items if str(item).strip()]
if "patcher" in data:
if data["patcher"] not in PATCHERS:
raise HTTPException(status_code=400, detail=f"配置替换方式无效: {data['patcher']}")
project["patcher"] = data["patcher"]
if "patch_rules" in data:
project["patch_rules"] = _clean_patch_rules(data["patch_rules"])
for fields_key in ("app_fields", "server_fields"):
if fields_key in data:
project[fields_key] = _clean_fields(data[fields_key], fields_key)
if "obfuscation" in data:
project["obfuscation"] = bool(data["obfuscation"])
xcode = project.get("xcode") or {}
if not xcode.get("workspace") and not xcode.get("project"):
raise HTTPException(status_code=400, detail="Xcode workspace 和 project 至少填写一个")
if project.get("patcher") != PATCHER_READOOR and not xcode.get("project"):
raise HTTPException(status_code=400, detail="通用配置替换需要填写 Xcode project(.xcodeproj)")
@router.get("/projects")
async def get_project_definitions():
"""获取所有项目定义"""
return [_mask_definition(project_id, project) for project_id, project in load_projects().items()]
@router.post("/projects")
async def create_project(data: dict):
"""新增项目"""
project_id = str(data.get("id", "")).strip()
if not is_valid_project_id(project_id):
raise HTTPException(status_code=400, detail="项目 ID 需以小写字母开头,仅含小写字母、数字、-、_,最长 32 位")
async with _config_lock:
root = _read_root()
if project_id in root["projects"]:
raise HTTPException(status_code=400, detail="项目 ID 已存在")
project = {**copy.deepcopy(default_definition(project_id)), **_default_project_data(project_id)}
_apply_definition(project, data)
root["projects"][project_id] = project
_write_root(root)
return {"id": project_id, "message": "项目已创建"}
@router.put("/projects/{project_id}")
async def update_project(project_id: str, data: dict):
"""更新项目定义(Git 密码留空则保留原密码)"""
async with _config_lock:
root = _read_root()
if project_id not in root["projects"]:
raise HTTPException(status_code=404, detail="项目不存在")
project = root["projects"][project_id]
current = get_definition(project_id, project)
for key in DEFINITION_KEYS:
project.setdefault(key, copy.deepcopy(current[key]))
_apply_definition(project, {key: data[key] for key in DEFINITION_KEYS if key in data}, current["git"])
_write_root(root)
return {"message": "项目已更新"}
@router.delete("/projects/{project_id}")
async def delete_project(project_id: str):
"""删除项目(至少保留一个项目)"""
async with _config_lock:
root = _read_root()
if project_id not in root["projects"]:
raise HTTPException(status_code=404, detail="项目不存在")
if len(root["projects"]) == 1:
raise HTTPException(status_code=400, detail="至少需要保留一个项目")
del root["projects"][project_id]
_write_root(root)
return {"message": "项目已删除"}
+18 -11
View File
@@ -48,13 +48,18 @@ async def create_task(task: TaskCreate, request: Request, user: dict = Depends(g
raise HTTPException(status_code=400, detail=f"该 App 未配置 {task.build_type} 证书")
from .apps import get_allowed_scheme_names
allowed_scheme_names = get_allowed_scheme_names(app)
allowed_scheme_names = get_allowed_scheme_names(app, config)
if allowed_scheme_names and scheme_name not in allowed_scheme_names:
raise HTTPException(
status_code=400,
detail=f"该 App 只能使用 Scheme: {', '.join(allowed_scheme_names)}",
)
project_id = config["_project"]
from ..projects import get_definition
if task.obfuscation and not get_definition(project_id, config)["obfuscation"]:
raise HTTPException(status_code=400, detail="该项目不支持代码混淆")
# 分支与 Tag 不允许重名,按名字即可确定要检出的引用类型
from .config import get_ref_type
ref_type = get_ref_type(config, task.branch)
@@ -62,6 +67,7 @@ async def create_task(task: TaskCreate, request: Request, user: dict = Depends(g
task_id = str(uuid.uuid4())
db_task = Task(
id=task_id,
project_id=project_id,
app_id=task.app_id,
app_name=app.get("name", ""),
build_type=task.build_type,
@@ -119,8 +125,9 @@ async def list_tasks(
user: dict = Depends(get_current_user),
db: Session = Depends(get_db)
):
"""获取任务列表"""
query = db.query(Task)
"""获取当前项目的任务列表"""
from .config import load_config
query = db.query(Task).filter(Task.project_id == load_config()["_project"])
if include_superseded:
if not user.get("is_admin"):
raise HTTPException(status_code=403, detail="需要管理员权限")
@@ -200,11 +207,11 @@ async def delete_task(task_id: str, request: Request, user: dict = Depends(get_c
raise HTTPException(status_code=400, detail="缺少打包配置快照,无法安全删除远端文件")
try:
build_config = json.loads(task.config_json)
from .config import load_config
from .config import load_upload_config
from ..services.distribution import DistributionError, delete_published_artifacts
delete_published_artifacts(
build_config,
load_config().get("upload", {}),
load_upload_config(),
task.oss_url,
)
except (json.JSONDecodeError, DistributionError) as exc:
@@ -223,9 +230,9 @@ async def delete_task(task_id: str, request: Request, user: dict = Depends(get_c
raise HTTPException(status_code=400, detail="缺少打包配置快照,无法安全删除远端 dSYM 文件")
try:
build_config = json.loads(task.config_json)
from .config import load_config
from .config import load_upload_config
from ..services.distribution import DistributionError, delete_published_dsym
delete_published_dsym(build_config, load_config().get("upload", {}), task.dsym_url)
delete_published_dsym(build_config, load_upload_config(), task.dsym_url)
except (json.JSONDecodeError, DistributionError) as exc:
raise HTTPException(status_code=502, detail=f"远端 dSYM 删除失败,记录未删除:{exc}") from exc
@@ -295,13 +302,13 @@ async def get_download_link(task_id: str, request: Request, user: dict = Depends
if not task.oss_url:
raise HTTPException(status_code=404, detail="下载文件不存在")
try:
from .config import load_config
from .config import load_upload_config
from ..config import DOWNLOAD_URL_EXPIRE_SECONDS
from ..services.distribution import DistributionError, get_published_download_url
task_config = json.loads(task.config_json or "{}")
download_url = get_published_download_url(
task_config,
load_config().get("upload", {}),
load_upload_config(),
task.oss_url,
)
except (json.JSONDecodeError, DistributionError) as exc:
@@ -353,11 +360,11 @@ async def download_dsym(task_id: str, request: Request, user: dict = Depends(get
# dSYM 需要携带认证信息才能访问该接口,浏览器无法直接跳转到本接口的
# 302 目标,因此改为返回 JSON 让前端自行发起下载。
try:
from .config import load_config
from .config import load_upload_config
from ..services.distribution import DistributionError, get_published_dsym_url
download_url = get_published_dsym_url(
json.loads(task.config_json or "{}"),
load_config().get("upload", {}),
load_upload_config(),
task.dsym_url,
)
except (json.JSONDecodeError, DistributionError) as exc:
+1
View File
@@ -15,6 +15,7 @@ class TaskCreate(BaseModel):
class TaskResponse(BaseModel):
id: str
project_id: Optional[str] = "readoor"
app_id: str
app_name: str
build_type: str
+120 -37
View File
@@ -3,6 +3,7 @@ import asyncio
import json
import os
import plistlib
import re
import shutil
import subprocess
import zipfile
@@ -12,16 +13,14 @@ from typing import List
from ..config import (
BUILD_BASE_DIR,
COPY_ITEMS,
DEFAULT_BUILD_DIR_RETENTION_HOURS,
BUILD_TIMEOUT_HOURS,
SKINS_DIR,
AUTOMATION_DIR,
get_source_dir,
get_shared_source_dir,
get_git_remote_url,
mask_git_remote_url,
)
from ..projects import LEGACY_PROJECT_ID, get_definition, get_project_source
from .log_streamer import log_streamer
from .distribution import DistributionError, publish_dsym, publish_ipa
from .notification import NotificationError, send_dingtalk_notification
@@ -37,9 +36,36 @@ class BuildError(Exception):
self.detail = detail
# 共享源码目录只能同时被一个任务切换、清理和复制。
# 后续在各自 build_dir 中执行的配置、依赖安装和编译不受此锁限制。
source_prepare_lock = asyncio.Lock()
# 共享源码目录只能同时被一个任务切换、清理和复制,每个源码目录一把锁,
# 不同项目的源码准备互不阻塞。后续在各自 build_dir 中执行的配置、依赖安装和编译不受此锁限制。
_source_prepare_locks: dict = {}
# 打包目录名:build_<项目 ID>_<任务 ID>
_BUILD_DIR_PATTERN = re.compile(r"^build_[a-z][a-z0-9_-]{0,31}_.+")
def _source_prepare_lock(source_dir: Path) -> asyncio.Lock:
return _source_prepare_locks.setdefault(str(source_dir.resolve()), asyncio.Lock())
def _task_project_id(task) -> str:
project_id = getattr(task, "project_id", None)
return project_id if isinstance(project_id, str) and project_id else LEGACY_PROJECT_ID
def load_task_project(task) -> dict:
"""读取任务所属项目的配置视图(见 routers.config.load_config)。"""
from fastapi import HTTPException
from ..routers.config import load_config
try:
return load_config(_task_project_id(task))
except HTTPException as exc:
raise BuildError(str(exc.detail), category="config") from exc
def get_task_definition(task) -> dict:
project_id = _task_project_id(task)
return get_definition(project_id, load_task_project(task))
def _patch_afnetworking_private_headers(build_dir: Path) -> int:
@@ -217,7 +243,7 @@ def _cleanup_old_builds(db):
_cleanup_superseded_tasks(db)
return
for d in base.iterdir():
if d.is_dir() and d.name.startswith("build_readoor_"):
if d.is_dir() and _BUILD_DIR_PATTERN.match(d.name):
try:
if d.stat().st_mtime < cutoff:
shutil.rmtree(d)
@@ -291,16 +317,21 @@ def _db_update(db, task, **fields):
db.commit()
async def update_source(task_id: str, source_dir: Path, branch: str, ref_type: str = "branch"):
"""将共享源码工作目录强制同步到指定远程分支或 Tag(ref_type="tag" 时 branch 为 Tag 名)。"""
remote_url = get_git_remote_url(with_credentials=True)
async def update_source(task_id: str, source_dir: Path, branch: str, ref_type: str = "branch",
remote_url: str = None):
"""将共享源码工作目录强制同步到指定远程分支或 Tag(ref_type="tag" 时 branch 为 Tag 名)。
remote_url 为带凭证的项目仓库地址,未传时使用 .env 中的 GIT_REMOTE_URL。
"""
if remote_url is None:
remote_url = get_git_remote_url(with_credentials=True)
remote_url_masked = mask_git_remote_url(remote_url)
git_env = _git_env()
cloned = not source_dir.exists()
if cloned:
if not remote_url:
raise Exception(f"源码目录不存在且未配置 GIT_REMOTE_URL: {source_dir}")
raise Exception(f"源码目录不存在且未配置 Git 仓库地址: {source_dir}")
source_dir.parent.mkdir(parents=True, exist_ok=True)
await log_streamer.emit(task_id, f"初始化共享源码目录: {remote_url_masked}")
process = await asyncio.create_subprocess_exec(
@@ -381,25 +412,34 @@ async def get_source_commit(source_dir: Path) -> str:
async def prepare_source_snapshot(task_id: str, task) -> tuple[Path, str]:
"""准备任务独立源码快照;远程仓库模式下此过程全局串行。"""
if not get_git_remote_url():
source_dir = get_source_dir(task.branch)
"""准备任务独立源码快照;远程仓库模式下同一源码目录的准备过程串行。"""
project_id = _task_project_id(task)
project = load_task_project(task)
definition = get_definition(project_id, project)
source = get_project_source(project_id, project)
await log_streamer.emit(task_id, f"项目: {definition['name']}({project_id})")
if not source.remote_url:
if source.manual_base is None:
raise BuildError(f"项目 {definition['name']} 未配置 Git 仓库地址", category="config")
source_dir = source.manual_base / task.branch
if not source_dir.exists():
raise Exception(
f"分支源码目录不存在: {source_dir},请先在 GIT_SOURCE_BASE 下准备分支(或 Tag)代码,或配置 GIT_REMOTE_URL"
)
await log_streamer.emit(task_id, f"使用分支源码: {source_dir}")
return await copy_source_code(task_id, task, source_dir), ""
return await copy_source_code(task_id, task, source_dir, definition), ""
source_dir = get_shared_source_dir()
source_dir = source.shared_dir
await log_streamer.emit(task_id, "等待共享源码准备队列...")
async with source_prepare_lock:
async with _source_prepare_lock(source_dir):
await log_streamer.emit(task_id, "开始准备共享源码快照")
ref_type = "tag" if task.ref_type == "tag" else "branch"
await update_source(task_id, source_dir, task.branch, ref_type)
await update_source(task_id, source_dir, task.branch, ref_type,
remote_url=source.remote_url_with_credentials)
commit = await get_source_commit(source_dir)
await log_streamer.emit(task_id, f"源码版本: {task.branch} @ {commit}")
build_dir = await copy_source_code(task_id, task, source_dir)
build_dir = await copy_source_code(task_id, task, source_dir, definition)
await log_streamer.emit(task_id, "共享源码快照已就绪,开始并行打包")
return build_dir, commit
@@ -438,6 +478,7 @@ async def run_build_task(task_id: str):
nonlocal build_dir
# 1. 串行准备源码快照;后续步骤在任务独立目录中并行执行。
await asyncio.to_thread(_db_update, db, task, current_step="source_prepare")
definition = await asyncio.to_thread(get_task_definition, task)
build_dir, source_commit = await prepare_source_snapshot(task_id, task)
await asyncio.to_thread(_db_update, db, task, build_dir=str(build_dir))
@@ -452,7 +493,7 @@ async def run_build_task(task_id: str):
# 3. 替换项目配置
await asyncio.to_thread(_db_update, db, task, current_step="patch")
await patch_project(task_id, task, config_data, build_dir)
await patch_project(task_id, task, config_data, build_dir, definition)
# 3.5 安装依赖
await asyncio.to_thread(_db_update, db, task, current_step="pod_install")
@@ -465,7 +506,7 @@ async def run_build_task(task_id: str):
# 5. 构建项目
await asyncio.to_thread(_db_update, db, task, current_step="build")
ipa_path = await build_project(task_id, task, config_data, build_dir)
ipa_path = await build_project(task_id, task, config_data, build_dir, definition["xcode"])
# 查找 dSYM
dsym_path = await find_dsym(task_id, build_dir, config_data.get("SCHEME", ""))
@@ -590,16 +631,36 @@ async def run_build_task(task_id: str):
db.close()
async def copy_source_code(task_id: str, task, source_dir: Path) -> Path:
async def copy_source_code(task_id: str, task, source_dir: Path, definition: dict = None) -> Path:
"""从分支源码目录拷贝代码到打包目录"""
await log_streamer.emit_step(task_id, "拷贝代码")
build_dir = BUILD_BASE_DIR / f"build_readoor_{task_id}"
project_id = _task_project_id(task)
if definition is None:
definition = get_definition(project_id, {})
build_dir = BUILD_BASE_DIR / f"build_{project_id}_{task_id}"
if build_dir.exists():
shutil.rmtree(build_dir)
build_dir.mkdir(parents=True)
for item in COPY_ITEMS:
# 未指定拷贝条目时拷贝整个仓库(不含 Git 数据和编译缓存)
copy_items = definition.get("copy_items") or []
if not copy_items:
await log_streamer.emit(task_id, "拷贝整个仓库")
root = str(source_dir)
def _ignore(directory, names):
ignored = {name for name in names if name in (".git", "DerivedData")}
# 仓库根目录的 build 与打包产物目录同名,不拷贝
if directory == root and "build" in names:
ignored.add("build")
return ignored
shutil.copytree(source_dir, build_dir, symlinks=True, ignore=_ignore)
await log_streamer.emit(task_id, "代码拷贝完成")
return build_dir
build_dir.mkdir(parents=True)
for item in copy_items:
src = source_dir / item
# 历史分支中该文件可能仍是小写 podfile;构建副本统一使用 Pods.xcodeproj
# 引用的标准名称 Podfile,避免大小写敏感文件系统报路径不一致。
@@ -626,7 +687,8 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
is_release_track, next_build_no, _ensure_track,
)
config = load_config()
project_id = _task_project_id(task)
config = load_config(project_id)
apps = config.get("apps", {})
schemes = config.get("schemes", {})
@@ -652,7 +714,7 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
# feature 轨始终使用手动设定的构建号。
if is_release and task.build_type == "App_Store":
async with _config_lock:
config = load_config()
config = load_config(project_id)
track = _ensure_track(config, track_name)
track.setdefault("app_ver", app_ver)
n = next_build_no(config["versions"]["tracks"], app_ver)
@@ -666,6 +728,7 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
# 构建配置
config_data = {
"PROJECT_ID": project_id,
"VERSION": app_ver,
"BUILD_VERSION": build_ver,
"SERVER": app.get("server", ""),
@@ -739,6 +802,16 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
if key in app:
config_data[key] = app[key]
# 项目自定义字段,供通用替换规则以 ${app.X} / ${server.X} 引用
definition = get_definition(project_id, config)
server = config.get("servers", {}).get(app.get("server", ""), {})
config_data["APP_VARS"] = {
field["key"]: app.get(field["key"], "") for field in definition["app_fields"] if field.get("key")
}
config_data["SERVER_VARS"] = {
field["key"]: server.get(field["key"], "") for field in definition["server_fields"] if field.get("key")
}
# 写入配置文件
config_output_path = build_dir / "config_output.json"
with open(config_output_path, "w", encoding="utf-8") as f:
@@ -748,12 +821,12 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
return config_data
async def patch_project(task_id: str, task, config_data: dict, build_dir: Path):
async def patch_project(task_id: str, task, config_data: dict, build_dir: Path, definition: dict = None):
"""替换项目配置"""
await log_streamer.emit_step(task_id, "替换项目配置")
try:
patch_lines = await asyncio.to_thread(apply_project_config, build_dir, config_data)
patch_lines = await asyncio.to_thread(apply_project_config, build_dir, config_data, definition)
except ProjectPatchError as exc:
raise BuildError(str(exc), category="config") from exc
(build_dir / "patch_result.log").write_text("\n".join(patch_lines), encoding="utf-8")
@@ -766,6 +839,10 @@ async def run_pod_install(task_id: str, build_dir: Path):
"""安装 CocoaPods 依赖"""
await log_streamer.emit_step(task_id, "安装依赖")
if not (build_dir / "Podfile").is_file() and not (build_dir / "podfile").is_file():
await log_streamer.emit(task_id, "项目未使用 CocoaPods,跳过")
return
process = await asyncio.create_subprocess_exec(
"pod", "install",
cwd=str(build_dir),
@@ -845,14 +922,21 @@ async def run_obfuscation(task_id: str, task, config_data: dict, build_dir: Path
await log_streamer.emit(task_id, "代码混淆完成")
async def build_project(task_id: str, task, config_data: dict, build_dir: Path) -> Path:
"""构建项目"""
async def build_project(task_id: str, task, config_data: dict, build_dir: Path, xcode: dict = None) -> Path:
"""构建项目;xcode 为项目定义中的 {workspace, project},优先使用 workspace"""
await log_streamer.emit_step(task_id, "构建项目")
scheme = config_data.get("SCHEME", "")
export_path = build_dir / "build"
archive_path = export_path / f"{scheme}.xcarchive"
workspace_path = build_dir / "readoor.xcworkspace"
if xcode is None:
xcode = get_definition(LEGACY_PROJECT_ID, {})["xcode"]
if xcode.get("workspace"):
container_args = ("-workspace", xcode["workspace"])
elif xcode.get("project"):
container_args = ("-project", xcode["project"])
else:
raise BuildError("项目未配置 Xcode workspace 或 project", category="config")
export_path.mkdir(parents=True, exist_ok=True)
@@ -860,7 +944,7 @@ async def build_project(task_id: str, task, config_data: dict, build_dir: Path)
await log_streamer.emit(task_id, "清理项目...")
process = await asyncio.create_subprocess_exec(
"xcodebuild", "clean",
"-workspace", workspace_path.name,
*container_args,
"-scheme", scheme,
"-configuration", "Release",
"-derivedDataPath", str(export_path / "derived_data"),
@@ -879,7 +963,7 @@ async def build_project(task_id: str, task, config_data: dict, build_dir: Path)
archive_output = []
process = await asyncio.create_subprocess_exec(
"xcodebuild", "archive",
"-workspace", workspace_path.name,
*container_args,
"-scheme", scheme,
"-configuration", "Release",
"-archivePath", str(archive_path),
@@ -957,9 +1041,8 @@ async def upload_ipa(task_id: str, task, config_data: dict, ipa_path: Path, buil
await log_streamer.emit_step(task_id, "上传分发平台")
# 从当前服务的 config.json 读取上传配置,不执行分支源码中的上传脚本。
from ..routers.config import load_config
full_config = load_config()
config_data["_upload_config"] = full_config.get("upload", {})
from ..routers.config import load_upload_config
config_data["_upload_config"] = load_upload_config()
try:
await log_streamer.emit(task_id, f"使用 {config_data['_upload_config'].get('mode', '')} 发布 IPA...")
return await asyncio.to_thread(publish_ipa, config_data, ipa_path, build_dir)
+140 -2
View File
@@ -135,8 +135,14 @@ def _apply_dictionary_config(build_dir: Path, config: dict, messages: list[str])
messages.append("已替换词典版本配置")
def apply_project_config(build_dir: Path, config: dict) -> list[str]:
"""写入项目配置,返回适合实时日志展示的变更说明。"""
def apply_project_config(build_dir: Path, config: dict, definition: dict = None) -> list[str]:
"""写入项目配置,返回适合实时日志展示的变更说明。
definition 为项目定义(见 backend/projects.py),未传或 patcher 为 readoor 时按 Readoor 工程处理。
"""
if definition and definition.get("patcher") != "readoor":
return apply_generic_config(build_dir, config, definition)
messages: list[str] = []
if config.get("SCHEME") == "readoorDict":
_apply_dictionary_config(build_dir, config, messages)
@@ -190,3 +196,135 @@ def apply_project_config(build_dir: Path, config: dict) -> list[str]:
_write_export_options(build_dir, config)
messages.append("工程、签名、版本和分发配置已更新")
return messages
# —— 通用工程(patcher = generic)——
_TEMPLATE_PATTERN = re.compile(r"\$\{([A-Za-z0-9_.]+)\}")
_BUILD_SETTINGS_PATTERN = re.compile(r"buildSettings = \{\n(.*?)\n\t*\};", re.DOTALL)
_PLAIN_PBX_VALUE = re.compile(r"^[A-Za-z0-9_.$/-]+$")
def _render_template(template: str, config: dict) -> str:
"""替换 ${app.X}、${server.X}、${KEY}:分别取 App 字段、服务器环境字段和打包配置;值为空时报错。"""
def _lookup(match):
name = match.group(1)
if name.startswith("app."):
value = config.get("APP_VARS", {}).get(name[4:], "")
elif name.startswith("server."):
value = config.get("SERVER_VARS", {}).get(name[7:], "")
else:
value = config.get(name, "")
if value is None or str(value) == "":
raise ProjectPatchError(f"替换规则引用的 {name} 未配置")
return str(value)
return _TEMPLATE_PATTERN.sub(_lookup, template)
def _pbx_value(value: str) -> str:
return value if _PLAIN_PBX_VALUE.match(value) else '"' + value.replace('"', '\\"') + '"'
def _patch_pbxproj(path: Path, settings: dict) -> dict:
"""在 App 目标的 buildSettings 中替换设置(含 [sdk=iphoneos*] 变体),返回每个设置的替换次数。
测试目标(含 TEST_HOST / TEST_TARGET_NAME)有自己的 Bundle ID 和签名,跳过。
"""
if not path.exists():
raise ProjectPatchError(f"工程文件不存在: {path.parent.name}/{path.name}")
counts = {key: 0 for key in settings}
def _patch_block(match):
block = match.group(1)
if "TEST_HOST" in block or "TEST_TARGET_NAME" in block:
return match.group(0)
for key, value in settings.items():
pattern = re.compile(
rf'^(\s*)("?{re.escape(key)}(?:\[sdk=iphoneos\*\])?"?) = .*;$', re.MULTILINE
)
block, n = pattern.subn(lambda m: f"{m.group(1)}{m.group(2)} = {_pbx_value(value)};", block)
counts[key] += n
return match.group(0).replace(match.group(1), block, 1)
content = _BUILD_SETTINGS_PATTERN.sub(_patch_block, path.read_text(encoding="utf-8"))
path.write_text(content, encoding="utf-8")
return counts
def _apply_define_rule(path: Path, name: str, value: str) -> int:
"""替换所有未注释的 #define NAME ... 行(#if/#else 各分支中的同名宏都会被替换)。"""
pattern = re.compile(rf"^([ \t]*#define[ \t]+{re.escape(name)})(?:[ \t].*)?$", re.MULTILINE)
content, count = pattern.subn(lambda m: f"{m.group(1)} {value}", path.read_text(encoding="utf-8"))
path.write_text(content, encoding="utf-8")
return count
def _resolve_rule_file(build_dir: Path, file: str) -> Path:
path = (build_dir / file).resolve()
if not file or not path.is_relative_to(build_dir.resolve()):
raise ProjectPatchError(f"替换规则文件路径无效: {file}")
if not path.is_file():
raise ProjectPatchError(f"替换规则文件不存在: {file}")
return path
def apply_generic_config(build_dir: Path, config: dict, definition: dict) -> list[str]:
"""通用工程:替换 pbxproj 的版本号与签名、生成 exportOptions.plist,再执行项目的 patch_rules。
patch_rules 每项:
{"type": "define", "file": "相对路径", "name": "宏名", "value": "模板"}
替换所有未注释的 #define 宏名 行
{"type": "replace_line", "file": "相对路径", "keyword": "关键字", "line": "模板"}
整行替换包含关键字且未注释的行
模板中的 ${app.X} / ${server.X} / ${KEY} 取 App 字段、服务器环境字段、打包配置。
"""
messages: list[str] = []
xcodeproj = (definition.get("xcode") or {}).get("project", "")
if not xcodeproj:
raise ProjectPatchError("项目未配置 Xcode project(.xcodeproj)")
settings = {
"MARKETING_VERSION": config.get("VERSION", ""),
"CURRENT_PROJECT_VERSION": config.get("BUILD_VERSION", ""),
}
if config.get("BUNDLE_ID"):
settings["PRODUCT_BUNDLE_IDENTIFIER"] = config["BUNDLE_ID"]
if not config.get("NO_CERT"):
if config.get("TEAM_ID"):
settings["DEVELOPMENT_TEAM"] = config["TEAM_ID"]
if config.get("PROVISIONING_NAME"):
settings["PROVISIONING_PROFILE_SPECIFIER"] = config["PROVISIONING_NAME"]
settings["CODE_SIGN_STYLE"] = "Manual"
settings["CODE_SIGN_IDENTITY"] = "iPhone Distribution"
counts = _patch_pbxproj(build_dir / xcodeproj / "project.pbxproj", settings)
missing = [key for key in ("MARKETING_VERSION", "CURRENT_PROJECT_VERSION") if counts[key] == 0]
if missing:
raise ProjectPatchError(f"未找到待替换配置: {', '.join(missing)}")
messages.append(
f"工程配置已更新: 版本 {settings['MARKETING_VERSION']} ({settings['CURRENT_PROJECT_VERSION']})"
+ (f",Bundle ID {settings['PRODUCT_BUNDLE_IDENTIFIER']}" if "PRODUCT_BUNDLE_IDENTIFIER" in settings else "")
)
for index, rule in enumerate(definition.get("patch_rules") or [], start=1):
rule_type = rule.get("type")
path = _resolve_rule_file(build_dir, rule.get("file", ""))
if rule_type == "define":
name = rule.get("name", "")
value = _render_template(rule.get("value", ""), config)
if _apply_define_rule(path, name, value) == 0:
raise ProjectPatchError(f"{rule['file']} 中未找到 #define {name}")
messages.append(f"{rule['file']}: #define {name} {value}")
elif rule_type == "replace_line":
keyword = rule.get("keyword", "")
line = _render_template(rule.get("line", ""), config)
if not keyword or _replace_lines(path, keyword, line) == 0:
raise ProjectPatchError(f"{rule['file']} 中未找到包含 {keyword} 的行")
messages.append(f"{rule['file']}: {line.strip()}")
else:
raise ProjectPatchError(f"第 {index} 条替换规则类型无效: {rule_type}")
_write_export_options(build_dir, config)
messages.append("签名和分发配置已更新")
return messages