This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIosDependencyResolver.cs
262 lines (222 loc) · 10.7 KB
/
IosDependencyResolver.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using YamlDotNet.Serialization;
namespace Commons.Editor {
/// <summary>
/// The Podfile creator that replaces 'unity-jar-resolver'.
/// It doesn't run 'pod install' so you will have to do that yourself,
/// on the first build and any subsequent builds where pods changed.
/// </summary>
public static class IosDependencyResolver {
[MenuItem("Build/Advanced/Test IosDependencyResolver")]
private static void MenuTest() {
string podfilePath = Path.Combine(Application.dataPath, "..", "..", "Podfile");
CreatePodfile(podfilePath);
}
[MenuItem("Build/Advanced/List *IosDependencies.yml files")]
private static void MenuListFiles() {
var depFiles = GetDeclarationFiles();
Debug.Log("List of all *IosDependencies.yml files:" +
"\n " + String.Join("\n ", depFiles));
}
private static string[] GetDeclarationFiles() {
return FileFinder.FindAssetsNamed("IosDependencies.yml");
}
public static void CreatePodfile(string outputPathToPodfile) {
// 1. find all the *IosDependencies.yml
var depFiles = GetDeclarationFiles();
// 2. read the files and create set of PodDependency's
var depFilesContents = depFiles.Select(File.ReadAllText);
var deserializer = new Deserializer();
var pods = new HashSet<IPodDependency>();
foreach (var contents in depFilesContents) {
// ignore the example yml file
if (contents.Contains("example:")) {
continue;
}
var declarations = deserializer.Deserialize<Dictionary<string, PodDeclaration>>(contents);
// collect all declared pods, dropping exact duplicates
foreach (var declaration in declarations) {
var podName = declaration.Key;
var declaredPod = declaration.Value.ToPodDependency(podName, outputPathToPodfile);
pods.Add(declaredPod);
}
}
if (pods.Count == 0) {
// nothing to do here, this project doesnt require cocoapods.
return;
}
// 3. ensure that none of the PodDependency's have same .name
var errorMessages = "";
foreach (var pod in pods) {
Debug.Log(pod);
var withSameName = pods.Where(p => p.name == pod.name).ToArray();
if (withSameName.Length == 1) continue;
// else: pod declared twice and not with same source!
errorMessages =
$"The pod '{pod.name}' has been declared {withSameName.Length} times and they point to different sources."
+ "\nThe conflicting pod declarations:\n " +
String.Join("\n ", (IEnumerable<IPodDependency>) withSameName);
errorMessages += "\n";
}
// make build failed if two have same name
if (!String.IsNullOrEmpty(errorMessages)) {
throw new BuildFailedException(errorMessages);
}
// 4. create Podfile that declares the pods
var podfileContents = CreatePodfileContents(pods);
File.WriteAllText(outputPathToPodfile, podfileContents);
}
private static string CreatePodfileContents(IEnumerable<IPodDependency> pods) {
const string templateFileName = "PodfileTemplate.rb";
var templateFile = FileFinder.FindAssetsNamed(templateFileName)
.First(path => path.EndsWith($"IosDependencyResolver/{templateFileName}", StringComparison.Ordinal));
var frameworkTargetLines = new StringBuilder();
var appTargetLines = new StringBuilder();
string nseTargetContents = String.Empty;
foreach (var pod in pods) {
var targets = pod.targets ?? new[] { UnityXcodeTargets.UnityFramework };
if (targets.Contains(UnityXcodeTargets.UnityFramework)) {
frameworkTargetLines.AppendLine(pod.ToDeclarationLine());
// firebase cloud messaging pod is needed by the NSE target
if (pod.name == "Firebase/Messaging") {
nseTargetContents = new string(' ', 4) + pod.ToDeclarationLine();
}
}
if (targets.Contains(UnityXcodeTargets.UnityApp)) {
appTargetLines.AppendLine(pod.ToDeclarationLine());
}
}
string podfile = File.ReadAllText(templateFile);
var iosVersion = PlayerSettings.iOS.targetOSVersionString;
podfile = podfile.Replace("**IOS_VERSION**", iosVersion);
string frameworkTargetContents = frameworkTargetLines.ToString();
podfile = podfile.Replace("**UNITY_FRAMEWORK_TARGET_CONTENTS**", frameworkTargetContents);
// todo: create this app extension only when app actually requires it
if (nseTargetContents != String.Empty) {
appTargetLines.AppendLine(new string(' ', 2) + "target 'NotificationServiceExtension' do");
appTargetLines.AppendLine(nseTargetContents);
appTargetLines.Append(new string(' ', 2) + "end");
}
string appTargetContents = appTargetLines.ToString();
// note: PodfileTemplate.rb assumes that Unity-iPhone app target does not depend on any cocoapods.
podfile = podfile.Replace("**UNITY_APP_TARGET_CONTENTS**", appTargetContents);
return podfile;
}
}
internal class PodDeclaration {
// these fields are assigned by yaml deserialize
#pragma warning disable 0649
public string[]? targets;
public string? version;
public string? path;
public string? git;
public string? commit;
#pragma warning restore 0649
public IPodDependency ToPodDependency(string name, string outputPathToPodfile) {
if (version != null) {
return new PodStandard(name, version);
}
if (path != null) {
// find where 'path' is pointing to
// assume 'path' is relative to repo root
var resolvedPath = Path.Combine(FileFinder.GetRepoRoot(), path);
var exists = Directory.Exists(resolvedPath) || File.Exists(resolvedPath);
if (!exists) {
throw new BuildFailedException($"Don't know where else to look. Couldn't resolve path to pod '{name}'" +
$" with only the specified path of '{path}'");
}
// resolve any relative stuff like '/..'
resolvedPath = Path.GetFullPath(resolvedPath);
var buildDirectory = Path.GetDirectoryName(outputPathToPodfile);
return new PodLocal(name, resolvedPath, buildDirectory!, targets);
}
if (git != null && commit != null) {
return new PodGit(name, git, commit);
} else {
throw new Exception("'git' and 'commit' must both be declared when 'version' and 'path' are not.");
}
}
}
internal interface IPodDependency {
/// Pod name
string name { get; }
/// Targets that require this pod.
/// Null implies ["UnityFramework"].
string[]? targets { get; }
/// Podfile line that goes 'pod ...'
string ToDeclarationLine();
}
/// Pod that exists in cocoapods centralized repository
internal readonly struct PodStandard : IPodDependency {
private readonly string version;
public string name { get; }
public string[]? targets => null;
public PodStandard(string name, string version) {
this.version = version;
this.name = name;
}
public string ToDeclarationLine() {
return $"pod '{name}', '{version}'";
}
public override string ToString() {
return $"{nameof(IPodDependency)} {{ name={name}, version={version} }}";
}
}
/// Pod that has source files in a (public/accessible) git repository
internal readonly struct PodGit : IPodDependency {
public PodGit(string name, string git, string commit) {
this.git = git;
// We only care about the commit being a unique identifier.
// This makes sure that two files can declare 'abc12345' and 'abc12345678906789'
// and they are treated at the same source.
if (commit.Length < 8) throw new BuildFailedException("commit hash must be unique (at least 8 characters)!" +
$" '{commit}' is too short. Declared pod is '{name}' with git '{git}'");
var shortCommit = commit.Substring(0, 8);
this.commit = shortCommit;
this.name = name;
}
private readonly string git;
private readonly string commit;
public string name { get; }
public string[]? targets => null;
public string ToDeclarationLine() {
return $"pod '{name}', :git => '{git}', :commit => '{commit}'";
}
public override string ToString() {
return $"{nameof(IPodDependency)} {{ name={name}, git={git}, commit={commit} }}";
}
}
internal readonly struct PodLocal : IPodDependency {
private readonly string path;
public string name { get; }
public string[]? targets { get; }
public PodLocal(string name, string absolutePath, string buildDirectory, string[]? targets = null) {
this.name = name;
this.targets = targets;
// create relative path from the build directory to the local pod path
var relative = DirectoryUtil.GetRelativePath(absolutePath, buildDirectory);
path = relative;
}
public string ToDeclarationLine() {
return $"pod '{name}', :path => '{path}'";
}
public override string ToString() {
return $"{nameof(IPodDependency)} {{ name={name}, path={path} }}";
}
}
/// <summary>
/// All Xcode targets created by Unity.
/// </summary>
public static class UnityXcodeTargets {
public const string UnityApp = "Unity-iPhone";
public const string UnityFramework = "UnityFramework";
}
}