Skip to content

Commit 25e13ef

Browse files
committed
Allow to add pods to multiple targets
The plugin developers can specify `addToAllTargets` attribute to Dependencies.xml or use `IOSResolver.AddPod()` API in code. The plugin users can enable or disable adding pods to multiple targets globally in iOS Resolver settings menu, or by changing `IOSResolver.PodfileAllowPodsInMultipleTargets` in code Bug: 174515725 Fix: 405 Change-Id: Ic285519a451964c3e3e59c6a2ae8f4b0dc81bf41
1 parent 2783123 commit 25e13ef

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ For example, to add the AdMob pod, version 7.0 or greater with bitcode enabled:
432432
<dependencies>
433433
<iosPods>
434434
<iosPod name="Google-Mobile-Ads-SDK" version="~> 7.0" bitcodeEnabled="true"
435-
minTargetSdk="6.0" />
435+
minTargetSdk="6.0" addToAllTargets="false" />
436436
</iosPods>
437437
</dependencies>
438438
```

sample/Assets/ExternalDependencyManager/Editor/SampleDependencies.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
generated Xcode project. This is "true" by default.
6868
* "minTargetSdk" (optional)
6969
The minimum iOS SDK required by this Cocoapod.
70+
* "addToAllTargets" (optional)
71+
Whether to add this pod to all targets when multiple target is
72+
supported. This is "false" by default.
7073
* "configurations" (optional)
7174
Podfile formatted list of configurations to include this pod in.
7275
* "modular_headers" (optional)
@@ -77,7 +80,7 @@
7780
Subspecs to include for the pod.
7881
-->
7982
<iosPod name="Google-Mobile-Ads-SDK" path="../google-mobile-ads-sdk" version="~> 7.0" bitcodeEnabled="true"
80-
minTargetSdk="6.0">
83+
minTargetSdk="6.0" addToAllTargets="false">
8184
<!-- Set of source URIs to search for this Cocoapod spec.
8285
By default Cocoapods will attempt to fetch the pod specs from:
8386
* $HOME/.cocoapods/repos

source/IOSResolver/src/IOSResolver.cs

+96-7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ private class Pod {
8686
/// </summary>
8787
public string minTargetSdk = null;
8888

89+
/// <summary>
90+
/// Whether to add this pod to all targets when multiple
91+
/// </summary>
92+
public bool addToAllTargets = false;
93+
8994
/// <summary>
9095
/// Tag that indicates where this was created.
9196
/// </summary>
@@ -162,21 +167,25 @@ public string PodFilePodLine {
162167
/// bitcode.</param>
163168
/// <param name="minTargetSdk">Minimum target SDK revision required by
164169
/// this pod.</param>
170+
/// <param name="addToAllTargets">Whether to add this pod to all targets when multiple
171+
/// target is supported.</param>
165172
/// <param name="sources">List of sources to search for all pods.
166173
/// Each source is a URL that is injected in the source section of a Podfile
167174
/// See https://guides.cocoapods.org/syntax/podfile.html#source for the description of
168175
/// a source.</param>
169176
/// <param name="propertiesByName">Dictionary of additional properties for the pod
170177
/// reference.</param>
171178
public Pod(string name, string version, bool bitcodeEnabled, string minTargetSdk,
172-
IEnumerable<string> sources, Dictionary<string, string> propertiesByName) {
179+
bool addToAllTargets, IEnumerable<string> sources,
180+
Dictionary<string, string> propertiesByName) {
173181
this.name = name;
174182
this.version = version;
175183
if (propertiesByName != null) {
176184
this.propertiesByName = new Dictionary<string, string>(propertiesByName);
177185
}
178186
this.bitcodeEnabled = bitcodeEnabled;
179187
this.minTargetSdk = minTargetSdk;
188+
this.addToAllTargets = addToAllTargets;
180189
if (sources != null) {
181190
var allSources = new List<string>(sources);
182191
allSources.AddRange(this.sources);
@@ -305,6 +314,7 @@ protected override bool Read(string filename, Logger logger) {
305314
string versionSpec = null;
306315
bool bitcodeEnabled = true;
307316
string minTargetSdk = null;
317+
bool addToAllTargets = false;
308318
var propertiesByName = new Dictionary<string, string>();
309319
if (!XmlUtilities.ParseXmlTextFileElements(
310320
filename, logger,
@@ -331,7 +341,12 @@ protected override bool Read(string filename, Logger logger) {
331341
(reader.GetAttribute("bitcodeEnabled") ?? "").ToLower();
332342
bitcodeEnabled |= trueStrings.Contains(bitcodeEnabledString);
333343
bitcodeEnabled &= !falseStrings.Contains(bitcodeEnabledString);
344+
var addToAllTargetsString =
345+
(reader.GetAttribute("addToAllTargets") ?? "").ToLower();
346+
addToAllTargets |= trueStrings.Contains(addToAllTargetsString);
347+
addToAllTargets &= !falseStrings.Contains(addToAllTargetsString);
334348
minTargetSdk = reader.GetAttribute("minTargetSdk");
349+
335350
sources = new List<string>();
336351
if (podName == null) {
337352
logger.Log(
@@ -344,6 +359,7 @@ protected override bool Read(string filename, Logger logger) {
344359
AddPodInternal(podName, preformattedVersion: versionSpec,
345360
bitcodeEnabled: bitcodeEnabled,
346361
minTargetSdk: minTargetSdk,
362+
addToAllTargets: addToAllTargets,
347363
sources: sources,
348364
overwriteExistingPod: false,
349365
createdBy: String.Format("{0}:{1}",
@@ -464,6 +480,10 @@ protected override bool Read(string filename, Logger logger) {
464480
// Whether to add an main target to Podfile for Unity 2019.3+.
465481
private const string PREFERENCE_PODFILE_ALWAYS_ADD_MAIN_TARGET =
466482
PREFERENCE_NAMESPACE + "PodfileAlwaysAddMainTarget";
483+
// Whether to allow the same pods to be in multiple targets, if specified in Dependecies.xml.
484+
private const string PREFERENCE_PODFILE_ALLOW_PODS_IN_MULTIPLE_TARGETS =
485+
PREFERENCE_NAMESPACE + "PodfileAllowPodsInMultipleTargets";
486+
467487
// List of preference keys, used to restore default settings.
468488
private static string[] PREFERENCE_KEYS = new [] {
469489
PREFERENCE_COCOAPODS_INSTALL_ENABLED,
@@ -476,7 +496,8 @@ protected override bool Read(string filename, Logger logger) {
476496
PREFERENCE_SKIP_POD_INSTALL_WHEN_USING_WORKSPACE_INTEGRATION,
477497
PREFERENCE_PODFILE_ADD_USE_FRAMEWORKS,
478498
PREFERENCE_PODFILE_STATIC_LINK_FRAMEWORKS,
479-
PREFERENCE_PODFILE_ALWAYS_ADD_MAIN_TARGET
499+
PREFERENCE_PODFILE_ALWAYS_ADD_MAIN_TARGET,
500+
PREFERENCE_PODFILE_ALLOW_PODS_IN_MULTIPLE_TARGETS
480501
};
481502

482503
// Whether the xcode extension was successfully loaded.
@@ -546,6 +567,9 @@ protected override bool Read(string filename, Logger logger) {
546567
// Parses a source URL from a Podfile.
547568
private static Regex PODFILE_SOURCE_REGEX = new Regex(@"^\s*source\s+'([^']*)'");
548569

570+
// Parses comments from a Podfile
571+
private static Regex PODFILE_COMMENT_REGEX = new Regex(@"^\s*\#");
572+
549573
// Parses dependencies from XML dependency files.
550574
private static IOSXmlDependencies xmlDependencies = new IOSXmlDependencies();
551575

@@ -987,6 +1011,18 @@ public static bool PodfileAlwaysAddMainTarget {
9871011
}
9881012
}
9891013

1014+
/// <summary>
1015+
/// Whether to allow the same pods to be in multiple targets, if specified in Dependecies.xml.
1016+
/// </summary>
1017+
public static bool PodfileAllowPodsInMultipleTargets {
1018+
get { return settings.GetBool(PREFERENCE_PODFILE_ALLOW_PODS_IN_MULTIPLE_TARGETS,
1019+
defaultValue: true); }
1020+
set {
1021+
settings.SetBool(PREFERENCE_PODFILE_ALLOW_PODS_IN_MULTIPLE_TARGETS, value);
1022+
}
1023+
}
1024+
1025+
9901026
/// <summary>
9911027
/// Whether to use project level settings.
9921028
/// </summary>
@@ -1166,7 +1202,38 @@ public static void AddPod(string podName, string version = null,
11661202
AddPodInternal(podName,
11671203
preformattedVersion: PodVersionExpressionFromVersionDep(version),
11681204
bitcodeEnabled: bitcodeEnabled, minTargetSdk: minTargetSdk,
1169-
sources: sources);
1205+
addToAllTargets: false, sources: sources);
1206+
}
1207+
1208+
/// <summary>
1209+
/// Tells the app what pod dependencies are needed.
1210+
/// This is called from a deps file in each API to aggregate all of the
1211+
/// dependencies to automate the Podfile generation.
1212+
/// </summary>
1213+
/// <param name="podName">pod path, for example "Google-Mobile-Ads-SDK" to
1214+
/// be included</param>
1215+
/// <param name="version">Version specification.
1216+
/// See PodVersionExpressionFromVersionDep for how the version string is processed.</param>
1217+
/// <param name="bitcodeEnabled">Whether the pod was compiled with bitcode
1218+
/// enabled. If this is set to false on a pod, the entire project will
1219+
/// be configured with bitcode disabled.</param>
1220+
/// <param name="minTargetSdk">Minimum SDK revision required by this
1221+
/// pod.</param>
1222+
/// <param name="addToAllTargets">Whether to add this pod to all targets when multiple
1223+
/// target is supported.</param>
1224+
/// <param name="sources">List of sources to search for all pods.
1225+
/// Each source is a URL that is injected in the source section of a Podfile
1226+
/// See https://guides.cocoapods.org/syntax/podfile.html#source for the description of
1227+
/// a source.</param>
1228+
public static void AddPod(string podName, string version = null,
1229+
bool bitcodeEnabled = true,
1230+
string minTargetSdk = null,
1231+
bool addToAllTargets = false,
1232+
IEnumerable<string> sources = null) {
1233+
AddPodInternal(podName,
1234+
preformattedVersion: PodVersionExpressionFromVersionDep(version),
1235+
bitcodeEnabled: bitcodeEnabled, minTargetSdk: minTargetSdk,
1236+
addToAllTargets: addToAllTargets, sources: sources);
11701237
}
11711238

11721239
/// <summary>
@@ -1182,6 +1249,8 @@ public static void AddPod(string podName, string version = null,
11821249
/// be configured with bitcode disabled.</param>
11831250
/// <param name="minTargetSdk">Minimum SDK revision required by this
11841251
/// pod.</param>
1252+
/// <param name="addToAllTargets">Whether to add this pod to all targets when multiple
1253+
/// target is supported.</param>
11851254
/// <param name="sources">List of sources to search for all pods.
11861255
/// Each source is a URL that is injected in the source section of a Podfile
11871256
/// See https://guides.cocoapods.org/syntax/podfile.html#source for the description of
@@ -1195,22 +1264,24 @@ private static void AddPodInternal(string podName,
11951264
string preformattedVersion = null,
11961265
bool bitcodeEnabled = true,
11971266
string minTargetSdk = null,
1267+
bool addToAllTargets = false,
11981268
IEnumerable<string> sources = null,
11991269
bool overwriteExistingPod = true,
12001270
string createdBy = null,
12011271
bool fromXmlFile = false,
12021272
Dictionary<string, string> propertiesByName = null) {
12031273
var pod = new Pod(podName, preformattedVersion, bitcodeEnabled, minTargetSdk,
1204-
sources, propertiesByName);
1274+
addToAllTargets, sources, propertiesByName);
12051275
pod.createdBy = createdBy ?? pod.createdBy;
12061276
pod.fromXmlFile = fromXmlFile;
12071277

12081278
Log(String.Format(
1209-
"AddPod - name: {0} version: {1} bitcode: {2} sdk: {3} sources: {4}, " +
1210-
"properties: {5}\n" +
1211-
"createdBy: {6}\n\n",
1279+
"AddPod - name: {0} version: {1} bitcode: {2} sdk: {3} alltarget: {4} " +
1280+
"sources: {5} properties: {6}\n" +
1281+
"createdBy: {7}\n\n",
12121282
podName, preformattedVersion ?? "null", bitcodeEnabled.ToString(),
12131283
minTargetSdk ?? "null",
1284+
addToAllTargets.ToString(),
12141285
sources != null ? String.Join(", ", (new List<string>(sources)).ToArray()) : "(null)",
12151286
Pod.PropertyDictionaryToString(pod.propertiesByName),
12161287
createdBy ?? pod.createdBy),
@@ -1874,11 +1945,15 @@ private static void ParseUnityDeps(string unityPodfilePath) {
18741945
var sources = new List<string>();
18751946
while ((line = unityPodfile.ReadLine()) != null) {
18761947
line = line.Trim();
1948+
if (PODFILE_COMMENT_REGEX.IsMatch(line)) {
1949+
continue;
1950+
}
18771951
var sourceLineMatch = PODFILE_SOURCE_REGEX.Match(line);
18781952
if (sourceLineMatch.Groups.Count > 1) {
18791953
sources.Add(sourceLineMatch.Groups[1].Value);
18801954
continue;
18811955
}
1956+
// TODO: Properly support multiple targets.
18821957
if (line.StartsWith("target 'Unity-iPhone' do")) {
18831958
capturingPodsDepth++;
18841959
continue;
@@ -2000,6 +2075,20 @@ public static void GenPodfile(BuildTarget buildTarget,
20002075

20012076
if (MultipleXcodeTargetsSupported && PodfileAlwaysAddMainTarget) {
20022077
file.WriteLine(String.Format("target '{0}' do", "Unity-iPhone"));
2078+
bool allowPodsInMultipleTargets = PodfileAllowPodsInMultipleTargets;
2079+
int podAdded = 0;
2080+
foreach(var pod in pods.Values) {
2081+
if (pod.addToAllTargets) {
2082+
file.WriteLine(String.Format(" {0}{1}",
2083+
allowPodsInMultipleTargets ? "" : "# ",
2084+
pod.PodFilePodLine));
2085+
podAdded++;
2086+
}
2087+
}
2088+
if (!allowPodsInMultipleTargets && podAdded > 0) {
2089+
file.WriteLine(String.Format(
2090+
" # Commented due to iOS Resolver settings."));
2091+
}
20032092
file.WriteLine("end");
20042093
}
20052094
if (PodfileAddUseFrameworks) {

source/IOSResolver/src/IOSResolverSettingsDialog.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private class Settings {
3939
internal bool podfileAddUseFrameworks;
4040
internal bool podfileStaticLinkFrameworks;
4141
internal bool podfileAlwaysAddMainTarget;
42+
internal bool podfileAllowPodsInMultipleTargets;
4243
internal bool useProjectSettings;
4344
internal EditorMeasurement.Settings analyticsSettings;
4445

@@ -55,6 +56,7 @@ internal Settings() {
5556
podfileAddUseFrameworks = IOSResolver.PodfileAddUseFrameworks;
5657
podfileStaticLinkFrameworks = IOSResolver.PodfileStaticLinkFrameworks;
5758
podfileAlwaysAddMainTarget = IOSResolver.PodfileAlwaysAddMainTarget;
59+
podfileAllowPodsInMultipleTargets = IOSResolver.PodfileAllowPodsInMultipleTargets;
5860
useProjectSettings = IOSResolver.UseProjectSettings;
5961
analyticsSettings = new EditorMeasurement.Settings(IOSResolver.analytics);
6062
}
@@ -72,6 +74,7 @@ internal void Save() {
7274
IOSResolver.PodfileAddUseFrameworks = podfileAddUseFrameworks;
7375
IOSResolver.PodfileStaticLinkFrameworks = podfileStaticLinkFrameworks;
7476
IOSResolver.PodfileAlwaysAddMainTarget = podfileAlwaysAddMainTarget;
77+
IOSResolver.PodfileAllowPodsInMultipleTargets = podfileAllowPodsInMultipleTargets;
7578
IOSResolver.UseProjectSettings = useProjectSettings;
7679
analyticsSettings.Save();
7780
}
@@ -104,7 +107,7 @@ private static int FindIndexFromCocoapodsIntegrationMethod(
104107
}
105108

106109
public void Initialize() {
107-
minSize = new Vector2(400, 650);
110+
minSize = new Vector2(400, 700);
108111
position = new Rect(UnityEngine.Screen.width / 3, UnityEngine.Screen.height / 3,
109112
minSize.x, minSize.y);
110113
}
@@ -221,6 +224,18 @@ public void OnGUI() {
221224
GUILayout.Label("Add the following lines to Podfile.");
222225
GUILayout.Label(" target 'Unity-iPhone' do\n" +
223226
" end");
227+
228+
if (settings.podfileAlwaysAddMainTarget) {
229+
GUILayout.BeginHorizontal();
230+
GUILayout.Label("Allow the same pod to be in multiple targets",
231+
EditorStyles.boldLabel);
232+
settings.podfileAllowPodsInMultipleTargets =
233+
EditorGUILayout.Toggle(settings.podfileAllowPodsInMultipleTargets);
234+
GUILayout.EndHorizontal();
235+
236+
GUILayout.Label("Allow to add the same pod to multiple targets, if specified in " +
237+
"Dependencies.xml with 'addToAllTargets' attribute.");
238+
}
224239
}
225240

226241
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
@@ -283,6 +298,9 @@ public void OnGUI() {
283298
new KeyValuePair<string, string>(
284299
"podfileAlwaysAddMainTarget",
285300
IOSResolver.PodfileAlwaysAddMainTarget.ToString()),
301+
new KeyValuePair<string, string>(
302+
"podfileAllowPodsInMultipleTargets",
303+
IOSResolver.PodfileAllowPodsInMultipleTargets.ToString()),
286304
},
287305
"Settings Save");
288306
settings.Save();

0 commit comments

Comments
 (0)