-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCMPPostProcessBuild.cs
More file actions
157 lines (137 loc) · 6.34 KB
/
CMPPostProcessBuild.cs
File metadata and controls
157 lines (137 loc) · 6.34 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#if UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System;
using System.Diagnostics;
using System.IO;
using UnityEditor.iOS.Xcode.Extensions;
using UnityEngine;
using ConsentManagementProvider;
public static class CMPPostProcessBuild
{
[PostProcessBuild(800)]
public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath)
{
if (buildTarget == BuildTarget.iOS)
{
PBXProject pbxProject = new PBXProject();
string projPath = PBXProject.GetPBXProjectPath(buildPath);
pbxProject.ReadFromFile(projPath);
string unityProjectGuid = pbxProject.ProjectGuid();
string unityMainTargetGuid = pbxProject.GetUnityMainTargetGuid();
pbxProject.AddBuildProperty(unityMainTargetGuid, "LIBRARY_SEARCH_PATHS", "\"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\" \"/usr/lib/swift\"");
pbxProject.AddBuildProperty(unityProjectGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "YES");
ConfigureFrameworks(pbxProject, unityProjectGuid);
TieBridgingHeader(pbxProject, unityMainTargetGuid);
EnableCppModules(pbxProject);
EnableObjectiveCExceptions(pbxProject);
EnableBitcode(pbxProject, unityProjectGuid, false);
string bridgePath="Libraries/ConsentManagementProvider/Plugins/iOS/Source/SwiftBridge.swift";
string newBridgePath="Pods/ConsentViewController/ConsentViewController/Classes/SwiftBridge.swift";
RemoveBridge(pbxProject,bridgePath);
ExecuteProcessTerminal("mv "+buildPath+"/"+bridgePath+" "+buildPath+"/"+newBridgePath);
pbxProject.WriteToFile(projPath);
AddBridgeToPods(buildPath, newBridgePath);
string plistPath = buildPath + "/Info.plist";
AddParameterToInfoPlist(plistPath);
}
}
static void RemoveBridge(PBXProject proj, string bridgePath)
{
string bridgeGuid = proj.FindFileGuidByProjectPath(bridgePath);
proj.RemoveFile(bridgeGuid);
}
static void AddBridgeToPods(string path, string bridgePath)
{
PBXProject pbxPods = new PBXProject();
string podsPath = path+"/Pods/Pods.xcodeproj/project.pbxproj";
pbxPods.ReadFromFile(podsPath);
string bridgeGuid = pbxPods.AddFile(path+"/"+bridgePath,"Pods/ConsentViewController/SwiftBridge.swift");
string cmpGuid = pbxPods.TargetGuidByName("ConsentViewController");
string cmpCmpGuid = pbxPods.TargetGuidByName("ConsentViewController-ConsentViewController");
pbxPods.AddFileToBuild(cmpGuid,bridgeGuid);
pbxPods.AddBuildProperty(cmpGuid, "CODE_SIGN_IDENTITY", "");
pbxPods.AddBuildProperty(cmpCmpGuid, "CODE_SIGN_IDENTITY", "");
pbxPods.WriteToFile(podsPath);
}
static void AddParameterToInfoPlist(string plistPath)
{
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
var buildKey = "NSUserTrackingUsageDescription";
rootDict.SetString(buildKey,"This identifier will be used to deliver personalized ads to you.");
buildKey = "SPLogLevel";
var buildValue="prod";
if (CmpDebugUtil.IsLogging)
{
buildValue="debug";
}
rootDict.SetString(buildKey,buildValue);
File.WriteAllText(plistPath, plist.WriteToString());
}
static void ConfigureFrameworks(PBXProject pbxProject, string targetGuid)
{
pbxProject.AddBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "/usr/lib/swift");
}
static void TieBridgingHeader(PBXProject pbxProject, string targetGuid)
{
pbxProject.SetBuildProperty(targetGuid, "SWIFT_OBJC_BRIDGING_HEADER", "Libraries/Plugins/iOS/Source/UnityPlugin-Bridging-Header.h");
}
private static void EnableBitcode(PBXProject pbxProject, string targetGuid, bool enabled)
{
pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", enabled ? "YES" : "NO");
}
static void EnableCppModules(PBXProject pbxProject)
{
pbxProject.AddBuildProperty(pbxProject.GetUnityMainTargetGuid(), "CLANG_ENABLE_MODULES", "YES");
pbxProject.AddBuildProperty(pbxProject.GetUnityFrameworkTargetGuid(), "CLANG_ENABLE_MODULES", "YES");
}
static void EnableObjectiveCExceptions(PBXProject pbxProject)
{
pbxProject.SetBuildProperty (pbxProject.GetUnityMainTargetGuid(), "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
pbxProject.SetBuildProperty (pbxProject.GetUnityFrameworkTargetGuid(), "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
}
static void LinkBinaryWithLibraries(PBXProject pbxProject, string targetGuid, string frameworkName)
{
//"true" will add the framework in the "Link Binary With Libraries" section with status "Optional", "false" will be "Required".
pbxProject.AddFrameworkToProject(targetGuid, frameworkName, false);
}
static void EnableSwift(PBXProject pbxProject, string targetGuid)
{
pbxProject.AddBuildProperty(targetGuid, "DEFINES_MODULE", "YES");
pbxProject.AddBuildProperty(targetGuid, "SWIFT_VERSION", "4.0");
pbxProject.AddBuildProperty(targetGuid, "COREML_CODEGEN_LANGUAGE", "Swift");
pbxProject.AddBuildProperty(targetGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "YES");
}
private static void ExecuteProcessTerminal(string argument)
{
try
{
UnityEngine.Debug.Log("============== Start Executing [" + argument + "] ===============");
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "/bin/bash",
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
Arguments = " -c \"" + argument + " \""
};
Process myProcess = new Process
{
StartInfo = startInfo
};
myProcess.Start();
myProcess.WaitForExit();
UnityEngine.Debug.Log("============== End ===============");
}
catch (Exception e)
{
UnityEngine.Debug.Log("Excetion on CMPPostProcessBuild");
}
}
}
#endif