Skip to content

Commit 8cde132

Browse files
committed
feat(xcode): 支持渠道专属 Xcode 配置合并
从命令行参数读取 -channel 标识,加载 XCodeConfig.{channel}.json 并以最高优先级合并到最终配置中,实现多渠道差异化构建。
1 parent 948c7a6 commit 8cde132

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Editor/PostProcessBuildHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ public static void OnPostProcessBuild(BuildTarget target, string path)
4848
finalConfig.Merge(table);
4949
}
5050

51+
// 合并渠道专属配置(优先级最高,最后合并覆盖)
52+
string channel = SettingLoader.GetChannel();
53+
if (!string.IsNullOrEmpty(channel))
54+
{
55+
var channelPaths = SettingLoader.LoadChannelSettingsData("XCodeConfig.json", channel);
56+
if (channelPaths.Count > 0)
57+
{
58+
LogHelper.Log($"[MergeConfig] 检测到渠道: {channel},正在合并渠道配置...");
59+
foreach (var channelPath in channelPaths)
60+
{
61+
LogHelper.Log($"[MergeConfig] 正在合并渠道配置: {channelPath}");
62+
string channelJson = File.ReadAllText(channelPath);
63+
Hashtable channelTable = channelJson.HashtableFromJson();
64+
if (channelTable == null)
65+
{
66+
LogHelper.Error($"{channelPath} 解析失败, 跳过合并");
67+
continue;
68+
}
69+
70+
finalConfig.Merge(channelTable);
71+
}
72+
}
73+
else
74+
{
75+
LogHelper.Log($"[MergeConfig] 未找到渠道 [{channel}] 的专属配置文件 XCodeConfig.{channel}.json");
76+
}
77+
}
78+
5179
if (finalConfig.Count == 0)
5280
{
5381
LogHelper.Error("合并后的配置为空,跳过设置");

Editor/SettingLoader.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if UNITY_IOS
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using UnityEditor;
@@ -8,6 +9,25 @@ namespace GameFrameX.Xcode.Editor
89
{
910
public class SettingLoader
1011
{
12+
private const string CHANNEL_ARG = "-channel";
13+
14+
/// <summary>
15+
/// 从命令行参数中获取当前渠道标识
16+
/// </summary>
17+
public static string GetChannel()
18+
{
19+
var args = Environment.GetCommandLineArgs();
20+
for (int i = 0; i < args.Length - 1; i++)
21+
{
22+
if (args[i].Equals(CHANNEL_ARG, StringComparison.OrdinalIgnoreCase))
23+
{
24+
return args[i + 1];
25+
}
26+
}
27+
28+
return null;
29+
}
30+
1131
/// <summary>
1232
/// 加载所有匹配的配置文件
1333
/// </summary>
@@ -33,6 +53,40 @@ public static List<string> LoadSettingsData(string fileName)
3353
results.Sort();
3454
return results;
3555
}
56+
57+
/// <summary>
58+
/// 加载渠道专属配置文件,文件名格式为 XCodeConfig.{channel}.json
59+
/// </summary>
60+
/// <param name="baseFileName">基础文件名,如 XCodeConfig.json</param>
61+
/// <param name="channel">渠道标识</param>
62+
/// <returns>文件路径列表</returns>
63+
public static List<string> LoadChannelSettingsData(string baseFileName, string channel)
64+
{
65+
if (string.IsNullOrEmpty(channel))
66+
{
67+
return new List<string>(0);
68+
}
69+
70+
string baseName = Path.GetFileNameWithoutExtension(baseFileName);
71+
string ext = Path.GetExtension(baseFileName);
72+
string channelFileName = $"{baseName}.{channel}{ext}";
73+
74+
var guids = AssetDatabase.FindAssets($"t:textasset");
75+
var results = new List<string>(4);
76+
foreach (var guid in guids)
77+
{
78+
string path = AssetDatabase.GUIDToAssetPath(guid);
79+
var newFileName = Path.GetFileName(path);
80+
81+
if (newFileName.Equals(channelFileName, StringComparison.OrdinalIgnoreCase))
82+
{
83+
results.Add(path);
84+
}
85+
}
86+
87+
results.Sort();
88+
return results;
89+
}
3690
}
3791
}
3892
#endif

0 commit comments

Comments
 (0)