This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
BuildPostProcessXCode.cs
116 lines (100 loc) · 4.91 KB
/
BuildPostProcessXCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Callbacks;
namespace Improbable.Gdk.Mobile
{
public static class BuildPostProcessXCode
{
private static Type pbxType;
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget != BuildTarget.iOS)
{
return;
}
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var type = assembly.GetTypes().FirstOrDefault(t => t.FullName == "UnityEditor.iOS.Xcode.PBXProject");
if (type != null)
{
pbxType = type;
break;
}
}
// Safety check again API changes
if (pbxType == null)
{
throw new BuildFailedException("Unable to find type UnityEditor.iOS.Xcode.PBXProject");
}
var xcodeObject = Activator.CreateInstance(pbxType);
// Instantiate PBXProject and read from xcode project file.
var projPath = InvokeStaticMethod<string>("GetPBXProjectPath", path);
var xcodeText = File.ReadAllText(projPath);
InvokeMethod(xcodeObject, "ReadFromString", xcodeText);
// Get Target GUIDs
var unityTargetName = InvokeStaticMethod<string>("GetUnityTargetName");
var unityTestTargetName = InvokeStaticMethod<string>("GetUnityTestTargetName");
var targetGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTargetName);
var targetTestingGUID = InvokeMethod<string>(xcodeObject, "TargetGuidByName", unityTestTargetName);
// Enumerate configGUIDs that need library path patching
var configNames = InvokeMethod<IEnumerable<string>>(xcodeObject, "BuildConfigNames");
var configGUIDs = configNames
.Select(configName => InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetGUID, configName))
.Concat(configNames.Select(configName =>
InvokeMethod<string>(xcodeObject, "BuildConfigByName", targetTestingGUID, configName)));
// Update library paths for each config
foreach (var configGUID in configGUIDs)
{
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID, "LIBRARY_SEARCH_PATHS",
null, new[]
{
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm",
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64"
});
// Add platform specific paths
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID,
"LIBRARY_SEARCH_PATHS[sdk=iphoneos*]", new[]
{
"$(inherited)",
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/arm"
}, null);
InvokeMethod<string>(xcodeObject, "UpdateBuildPropertyForConfig", configGUID,
"LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]",
new[]
{
"$(inherited)",
"$(SRCROOT)/Libraries/io.improbable.worker.sdk.mobile/Plugins/Improbable/Core/iOS/x86_64"
}, null);
}
// Save changes to xcode project file
InvokeMethod(xcodeObject, "WriteToFile", projPath);
}
private static T InvokeStaticMethod<T>(string methodName, params object[] parameters)
{
return (T) pbxType.InvokeMember(methodName,
BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, parameters);
}
private static void InvokeStaticMethod(string methodName, params object[] parameters)
{
pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
null, null, parameters);
}
private static T InvokeMethod<T>(object target, string methodName, params object[] parameters)
{
return (T) pbxType.InvokeMember(methodName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, target, parameters);
}
private static void InvokeMethod(object target, string methodName, params object[] parameters)
{
pbxType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, target, parameters);
}
}
}