Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5193d38

Browse files
author
Stewart Miles
authored
Merge pull request #335 from santoshbagadi/feat/add_ability_to_set_target_from_dependencies_file
Add ability to set XCode target from Dependencies file
2 parents 9ac0c9f + 16ce8d0 commit 5193d38

File tree

2 files changed

+80
-15
lines changed

2 files changed

+80
-15
lines changed

‎sample/Assets/ExternalDependencyManager/Editor/SampleDependencies.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
Cocoapod version specification for the named pod.
6363
If this is not specified the latest version is used.
6464
NOTE: This can't be used when "path" is set.
65+
* "target" (optional)
66+
The Xcode target to which to add the pod to.
6567
* "bitcodeEnabled" (optional)
6668
Whether this Cocoapod requires bitcode to be enabled in Unity's
6769
generated Xcode project. This is "true" by default.

‎source/IOSResolver/src/IOSResolver.cs‎

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ private class Pod {
5151
/// </summary>
5252
public string version = null;
5353

54+
/// <summary>
55+
/// The App's target to which to add the pod.
56+
///
57+
/// See: XcodeTargetNames for valid target names.
58+
/// </summary>
59+
public string target = null;
60+
5461
/// <summary>
5562
/// Properties applied to the pod declaration.
5663
///
@@ -158,6 +165,7 @@ public string PodFilePodLine {
158165
/// </summary>
159166
/// <param name="name">Name of the pod.</param>
160167
/// <param name="version">Version of the pod.</param>
168+
/// <param name="target">The App's target to which to add the pod.</param>
161169
/// <param name="bitcodeEnabled">Whether this pod was compiled with
162170
/// bitcode.</param>
163171
/// <param name="minTargetSdk">Minimum target SDK revision required by
@@ -168,10 +176,12 @@ public string PodFilePodLine {
168176
/// a source.</param>
169177
/// <param name="propertiesByName">Dictionary of additional properties for the pod
170178
/// reference.</param>
171-
public Pod(string name, string version, bool bitcodeEnabled, string minTargetSdk,
172-
IEnumerable<string> sources, Dictionary<string, string> propertiesByName) {
179+
public Pod(string name, string version, string target, bool bitcodeEnabled,
180+
string minTargetSdk, IEnumerable<string> sources,
181+
Dictionary<string, string> propertiesByName) {
173182
this.name = name;
174183
this.version = version;
184+
this.target = target;
175185
if (propertiesByName != null) {
176186
this.propertiesByName = new Dictionary<string, string>(propertiesByName);
177187
}
@@ -209,6 +219,7 @@ public override bool Equals(System.Object obj) {
209219
return pod != null &&
210220
name == pod.name &&
211221
version == pod.version &&
222+
target == pod.target &&
212223
propertiesByName.Count == pod.propertiesByName.Count &&
213224
propertiesByName.Keys.All(key =>
214225
pod.propertiesByName.ContainsKey(key) &&
@@ -303,6 +314,7 @@ protected override bool Read(string filename, Logger logger) {
303314
var falseStrings = new HashSet<string> { "false", "0" };
304315
string podName = null;
305316
string versionSpec = null;
317+
string target = null;
306318
bool bitcodeEnabled = true;
307319
string minTargetSdk = null;
308320
var propertiesByName = new Dictionary<string, string>();
@@ -327,6 +339,20 @@ protected override bool Read(string filename, Logger logger) {
327339
}
328340
}
329341
versionSpec = reader.GetAttribute("version");
342+
target = reader.GetAttribute("target");
343+
if (target == null) {
344+
target = DefaultXcodeTarget;
345+
} else {
346+
// Make sure that the provided target is supported.
347+
if (!XcodeTargetNames.Contains(target)) {
348+
logger.Log(string.Format("Incorrect target name passed {0}." +
349+
"Adding the pod to the default" +
350+
"target: {1}",
351+
target,
352+
DefaultXcodeTarget));
353+
target = DefaultXcodeTarget;
354+
}
355+
}
330356
var bitcodeEnabledString =
331357
(reader.GetAttribute("bitcode") ?? "").ToLower();
332358
bitcodeEnabled |= trueStrings.Contains(bitcodeEnabledString);
@@ -341,7 +367,9 @@ protected override bool Read(string filename, Logger logger) {
341367
return false;
342368
}
343369
} else {
344-
AddPodInternal(podName, preformattedVersion: versionSpec,
370+
AddPodInternal(podName,
371+
target,
372+
preformattedVersion: versionSpec,
345373
bitcodeEnabled: bitcodeEnabled,
346374
minTargetSdk: minTargetSdk,
347375
sources: sources,
@@ -532,6 +560,9 @@ protected override bool Read(string filename, Logger logger) {
532560
// Parses a source URL from a Podfile.
533561
private static Regex PODFILE_SOURCE_REGEX = new Regex(@"^\s*source\s+'([^']*)'");
534562

563+
// Regex that matches the start of a target line in a pod file.
564+
private static Regex PODFILE_TARGET_START_LINE_REGEX = new Regex("^target '([^']+)' do");
565+
535566
// Parses dependencies from XML dependency files.
536567
private static IOSXmlDependencies xmlDependencies = new IOSXmlDependencies();
537568

@@ -1080,6 +1111,7 @@ public static void AddPod(string podName, string version = null,
10801111
string minTargetSdk = null,
10811112
IEnumerable<string> sources = null) {
10821113
AddPodInternal(podName,
1114+
DefaultXcodeTarget,
10831115
preformattedVersion: PodVersionExpressionFromVersionDep(version),
10841116
bitcodeEnabled: bitcodeEnabled, minTargetSdk: minTargetSdk,
10851117
sources: sources);
@@ -1108,6 +1140,7 @@ public static void AddPod(string podName, string version = null,
11081140
/// <param name="propertiesByName">Dictionary of additional properties for the pod
11091141
/// reference.</param>
11101142
private static void AddPodInternal(string podName,
1143+
string target,
11111144
string preformattedVersion = null,
11121145
bool bitcodeEnabled = true,
11131146
string minTargetSdk = null,
@@ -1116,7 +1149,7 @@ private static void AddPodInternal(string podName,
11161149
string createdBy = null,
11171150
bool fromXmlFile = false,
11181151
Dictionary<string, string> propertiesByName = null) {
1119-
var pod = new Pod(podName, preformattedVersion, bitcodeEnabled, minTargetSdk,
1152+
var pod = new Pod(podName, preformattedVersion, target,bitcodeEnabled, minTargetSdk,
11201153
sources, propertiesByName);
11211154
pod.createdBy = createdBy ?? pod.createdBy;
11221155
pod.fromXmlFile = fromXmlFile;
@@ -1648,10 +1681,21 @@ public static IEnumerable<string> XcodeTargetNames {
16481681
get {
16491682
// Return hard coded names in the UnityEditor.iOS.Xcode.PBXProject DLL.
16501683
return MultipleXcodeTargetsSupported ?
1651-
new List<string>() { "UnityFramework" } :
1684+
new List<string>() { "Unity-iPhone","UnityFramework" } :
16521685
new List<string>() { InitializeTargetName() };
16531686
}
16541687
}
1688+
1689+
/// <summary>
1690+
/// Get the default Xcode target name to which to add pods.
1691+
/// </summary>
1692+
/// <returns>The Xcode target to which to add the pods to by default.</returns>
1693+
public static string DefaultXcodeTarget {
1694+
get {
1695+
return MultipleXcodeTargetsSupported ? "UnityFramework" : InitializeTargetName();
1696+
}
1697+
}
1698+
16551699
/// <summary>
16561700
/// Get Xcode target GUIDs using a method that works across all Unity versions.
16571701
/// </summary>
@@ -1687,7 +1731,6 @@ public static IEnumerable<string> GetXcodeTargetGuids(object xcodeProject) {
16871731
}
16881732
return targets;
16891733
}
1690-
16911734
// Implementation of OnPostProcessPatchProject().
16921735
// NOTE: This is separate from the post-processing method to prevent the
16931736
// Mono runtime from loading the Xcode API before calling the post
@@ -1765,10 +1808,11 @@ private static void ParseUnityDeps(string unityPodfilePath) {
17651808
string line;
17661809

17671810
// We are only interested in capturing the dependencies "Pod depName, depVersion", inside
1768-
// of the specific target. However there can be nested targets such as for testing, so we're
1769-
// counting the depth to determine when to capture the pods. Also we only ever enter the
1770-
// first depth if we're in the exact right target.
1811+
// of the targets in XcodeTargetNames. However there can be nested targets such as for
1812+
// testing, so we're counting the depth to determine when to capture the pods. Also we only
1813+
// ever enter the first depth if we're in the exact right target.
17711814
int capturingPodsDepth = 0;
1815+
string currentTarget = null;
17721816
var sources = new List<string>();
17731817
while ((line = unityPodfile.ReadLine()) != null) {
17741818
line = line.Trim();
@@ -1777,14 +1821,24 @@ private static void ParseUnityDeps(string unityPodfilePath) {
17771821
sources.Add(sourceLineMatch.Groups[1].Value);
17781822
continue;
17791823
}
1780-
if (line.StartsWith("target 'Unity-iPhone' do")) {
1781-
capturingPodsDepth++;
1782-
continue;
1824+
1825+
var podFileTargetMatch = PODFILE_TARGET_START_LINE_REGEX.Match(line);
1826+
if (podFileTargetMatch.Success) {
1827+
var target = podFileTargetMatch.Groups[1].Value;
1828+
if (XcodeTargetNames.Contains(target))
1829+
{
1830+
capturingPodsDepth++;
1831+
currentTarget = target;
1832+
continue;
1833+
}
17831834
}
17841835

1785-
if (capturingPodsDepth == 0) continue;
1836+
if (capturingPodsDepth == 0) {
1837+
currentTarget = null;
1838+
continue;
1839+
}
17861840

1787-
// handle other scopes roughly
1841+
// Handle other scopes roughly
17881842
if (line.EndsWith(" do")) {
17891843
capturingPodsDepth++; // Ignore nested targets like tests
17901844
} else if (line == "end") {
@@ -1793,9 +1847,16 @@ private static void ParseUnityDeps(string unityPodfilePath) {
17931847

17941848
if (capturingPodsDepth != 1) continue;
17951849

1850+
if (currentTarget == null) {
1851+
Log(string.Format("Couldn't find a valid target for pod {0}, skipping...", line),
1852+
verbose: true);
1853+
continue;
1854+
}
1855+
17961856
// Parse "pod" lines from the default target in the file.
17971857
var podLineMatch = PODFILE_POD_REGEX.Match(line);
17981858
var podGroups = podLineMatch.Groups;
1859+
17991860
if (podGroups.Count > 1) {
18001861
var podName = podGroups["podname"].ToString();
18011862
var podVersion = podGroups["podversion"].ToString();
@@ -1809,6 +1870,7 @@ private static void ParseUnityDeps(string unityPodfilePath) {
18091870
}
18101871
AddPodInternal(
18111872
podName,
1873+
currentTarget,
18121874
preformattedVersion: String.IsNullOrEmpty(podVersion) ? null : podVersion,
18131875
sources: sources, createdBy: unityPodfilePath, overwriteExistingPod: false,
18141876
propertiesByName: propertiesByName);
@@ -1889,8 +1951,9 @@ public static void GenPodfile(BuildTarget buildTarget,
18891951
file.WriteLine(GeneratePodfileSourcesSection() +
18901952
String.Format("platform :ios, '{0}'\n", TargetSdk));
18911953
foreach (var target in XcodeTargetNames) {
1954+
var podsToAddToThisTarget = pods.Values.Where(pod => pod.target.Equals(target));
18921955
file.WriteLine(String.Format("target '{0}' do", target));
1893-
foreach(var pod in pods.Values) {
1956+
foreach(var pod in podsToAddToThisTarget) {
18941957
file.WriteLine(String.Format(" {0}", pod.PodFilePodLine));
18951958
}
18961959
file.WriteLine("end");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /