diff --git a/backend/services/build_service.py b/backend/services/build_service.py index 3faacf8..d639037 100644 --- a/backend/services/build_service.py +++ b/backend/services/build_service.py @@ -497,7 +497,7 @@ async def run_build_task(task_id: str): # 3.5 安装依赖 await asyncio.to_thread(_db_update, db, task, current_step="pod_install") - await run_pod_install(task_id, build_dir) + await run_pod_install(task_id, build_dir, definition["xcode"]) # 4. 代码混淆(可选) if task.obfuscation: @@ -842,17 +842,34 @@ async def patch_project(task_id: str, task, config_data: dict, build_dir: Path, await log_streamer.emit(task_id, "项目配置替换完成") -async def run_pod_install(task_id: str, build_dir: Path): +def _pod_dir(build_dir: Path, xcode: dict = None) -> Path: + """Podfile 所在目录:工程在仓库子目录时(如 sub/App.xcworkspace),优先取该子目录。""" + candidates = [] + for key in ("workspace", "project"): + rel = ((xcode or {}).get(key) or "").strip() + if rel: + candidates.append(build_dir / Path(rel).parent) + candidates.append(build_dir) + for directory in candidates: + if (directory / "Podfile").is_file() or (directory / "podfile").is_file(): + return directory + return None + + +async def run_pod_install(task_id: str, build_dir: Path, xcode: dict = None): """安装 CocoaPods 依赖""" await log_streamer.emit_step(task_id, "安装依赖") - if not (build_dir / "Podfile").is_file() and not (build_dir / "podfile").is_file(): + pod_dir = _pod_dir(build_dir, xcode) + if pod_dir is None: await log_streamer.emit(task_id, "项目未使用 CocoaPods,跳过") return + if pod_dir != build_dir: + await log_streamer.emit(task_id, f"Podfile 目录: {pod_dir.relative_to(build_dir)}") process = await asyncio.create_subprocess_exec( "pod", "install", - cwd=str(build_dir), + cwd=str(pod_dir), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, ) @@ -867,7 +884,7 @@ async def run_pod_install(task_id: str, build_dir: Path): if process.returncode != 0: raise BuildError("pod install 失败", category="config") - patched_files = await asyncio.to_thread(_patch_afnetworking_private_headers, build_dir) + patched_files = await asyncio.to_thread(_patch_afnetworking_private_headers, pod_dir) if patched_files: await log_streamer.emit( task_id, diff --git a/tests/test_build_service.py b/tests/test_build_service.py index bf3ff9b..61d34c3 100644 --- a/tests/test_build_service.py +++ b/tests/test_build_service.py @@ -283,6 +283,18 @@ async def test_run_pod_install_patches_afnetworking(tmp_path, log_streamer): assert "netinet6/in6.h" not in source_file.read_text() +async def test_run_pod_install_uses_xcode_subdirectory(tmp_path, log_streamer): + """工程在仓库子目录时,在 Podfile 所在的子目录执行 pod install。""" + sub = tmp_path / "Demo" + sub.mkdir() + (sub / "Podfile").write_text("pod 'SnapKit'") + + with patch("asyncio.create_subprocess_exec", return_value=_make_mock_process()) as mock_exec: + await run_pod_install("t1", tmp_path, {"workspace": "Demo/App.xcworkspace", "project": ""}) + + assert mock_exec.call_args.kwargs["cwd"] == str(sub) + + async def test_build_project_passes_scheme_as_exec_argument(tmp_path, log_streamer): """Scheme 中的 shell 特殊字符只能作为 xcodebuild 参数,不能被执行。""" build_dir = tmp_path / "build"