Skip to content

Commit 7a52e4e

Browse files
committed
fix(xcode): 修复 Unity 子进程找不到 pod 命令及编码错误
Unity 启动的子进程不继承 shell PATH,导致 pod 命令无法找到。 同时 CocoaPods 要求 UTF-8 编码环境,Unity 进程默认不设置。 - 使用绝对路径调用 pod,通过候选路径 + which 兜底解析 - 启动进程时设置 LANG 和 LC_ALL 为 en_US.UTF-8 - 找不到 pod 时输出明确错误提示而非抛异常
1 parent 554560d commit 7a52e4e

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

Editor/PostProcessBuildHelper.PodFile.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,27 @@ private static void RunPodInstall(string path)
198198

199199
LogHelper.Log("[PodInstall] 开始执行 pod install...");
200200

201+
// Unity 子进程不继承 shell PATH,需要解析 pod 的绝对路径
202+
string podPath = GetPodFullPath();
203+
if (string.IsNullOrEmpty(podPath))
204+
{
205+
LogHelper.Error("[PodInstall] 未找到 pod 命令,请确认已安装 CocoaPods (sudo gem install cocoapods)");
206+
return;
207+
}
208+
209+
LogHelper.Log($"[PodInstall] 使用 pod 路径: {podPath}");
210+
201211
var process = new System.Diagnostics.Process();
202-
process.StartInfo.FileName = "pod";
212+
process.StartInfo.FileName = podPath;
203213
process.StartInfo.Arguments = "install";
204214
process.StartInfo.WorkingDirectory = path;
205215
process.StartInfo.UseShellExecute = false;
206216
process.StartInfo.RedirectStandardOutput = true;
207217
process.StartInfo.RedirectStandardError = true;
208218
process.StartInfo.CreateNoWindow = true;
219+
// CocoaPods 要求 UTF-8 编码,Unity 子进程默认不带这些环境变量
220+
process.StartInfo.EnvironmentVariables["LANG"] = "en_US.UTF-8";
221+
process.StartInfo.EnvironmentVariables["LC_ALL"] = "en_US.UTF-8";
209222

210223
process.Start();
211224
string output = process.StandardOutput.ReadToEnd();
@@ -230,6 +243,51 @@ private static void RunPodInstall(string path)
230243

231244
LogHelper.Log("[PodInstall] pod install 完成");
232245
}
246+
247+
private static string GetPodFullPath()
248+
{
249+
// 常见的 pod 安装路径,按优先级尝试
250+
string[] candidates = new string[]
251+
{
252+
"/opt/homebrew/bin/pod",
253+
"/usr/local/bin/pod",
254+
System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("HOME") ?? "", ".gem/ruby/bin/pod"),
255+
"/usr/bin/pod",
256+
};
257+
258+
foreach (var candidate in candidates)
259+
{
260+
if (System.IO.File.Exists(candidate))
261+
{
262+
return candidate;
263+
}
264+
}
265+
266+
// 尝试通过 shell 解析
267+
try
268+
{
269+
var which = new System.Diagnostics.Process();
270+
which.StartInfo.FileName = "/bin/zsh";
271+
which.StartInfo.Arguments = "-lc \"which pod\"";
272+
which.StartInfo.UseShellExecute = false;
273+
which.StartInfo.RedirectStandardOutput = true;
274+
which.StartInfo.CreateNoWindow = true;
275+
which.Start();
276+
string result = which.StandardOutput.ReadToEnd().Trim();
277+
which.WaitForExit();
278+
279+
if (!string.IsNullOrEmpty(result) && System.IO.File.Exists(result))
280+
{
281+
return result;
282+
}
283+
}
284+
catch
285+
{
286+
// ignored
287+
}
288+
289+
return null;
290+
}
233291
}
234292
}
235293
#endif

0 commit comments

Comments
 (0)