Skip to content

Commit 274dec1

Browse files
committed
Don't generate the Podfile when empty
1 parent 9094725 commit 274dec1

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

source/IOSResolver/src/IOSResolver.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ protected override bool Read(string filename, Logger logger) {
407407

408408
// List of pods to ignore because they are replaced by Swift packages.
409409
private static HashSet<string> podsToIgnore = new HashSet<string>();
410+
// Flag used to denoted if generating the Podfile was skipped because of no pods.
411+
// Used by subsequent steps to know they should be skipped as well.
412+
private static bool podfileGenerationSkipped = false;
410413

411414
// Order of post processing operations.
412415
private const int BUILD_ORDER_REFRESH_DEPENDENCIES = 10;
@@ -2352,6 +2355,21 @@ public static void GenPodfile(BuildTarget buildTarget,
23522355
}
23532356
}
23542357

2358+
var filteredPods = new List<Pod>();
2359+
foreach (var pod in pods.Values) {
2360+
if (podsToIgnore != null && podsToIgnore.Contains(pod.name)) {
2361+
Log(String.Format("Skipping pod {0} because it is replaced by a Swift Package.", pod.name), verbose: true);
2362+
continue;
2363+
}
2364+
filteredPods.Add(pod);
2365+
}
2366+
if (filteredPods.Count == 0) {
2367+
Log("Found no pods to add, skipping generation of the Podfile");
2368+
podfileGenerationSkipped = true;
2369+
return;
2370+
}
2371+
podfileGenerationSkipped = false;
2372+
23552373
Log(String.Format("Generating Podfile {0} with {1} integration.", podfilePath,
23562374
(CocoapodsWorkspaceIntegrationEnabled ? "Xcode workspace" :
23572375
(CocoapodsProjectIntegrationEnabled ? "Xcode project" : "no target"))),
@@ -2376,11 +2394,7 @@ public static void GenPodfile(BuildTarget buildTarget,
23762394

23772395
foreach (var target in XcodeTargetNames) {
23782396
file.WriteLine(String.Format("target '{0}' do", target));
2379-
foreach(var pod in pods.Values) {
2380-
if (podsToIgnore != null && podsToIgnore.Contains(pod.name)) {
2381-
Log(String.Format("Skipping pod {0} because it is replaced by a Swift Package.", pod.name), verbose: true);
2382-
continue;
2383-
}
2397+
foreach(var pod in filteredPods) {
23842398
file.WriteLine(String.Format(" {0}", pod.PodFilePodLine));
23852399
}
23862400
file.WriteLine("end");
@@ -2390,10 +2404,7 @@ public static void GenPodfile(BuildTarget buildTarget,
23902404
file.WriteLine(String.Format("target '{0}' do", XcodeMainTargetName));
23912405
bool allowPodsInMultipleTargets = PodfileAllowPodsInMultipleTargets;
23922406
int podAdded = 0;
2393-
foreach(var pod in pods.Values) {
2394-
if (podsToIgnore != null && podsToIgnore.Contains(pod.name)) {
2395-
continue;
2396-
}
2407+
foreach(var pod in filteredPods) {
23972408
if (pod.addToAllTargets) {
23982409
file.WriteLine(String.Format(" {0}{1}",
23992410
allowPodsInMultipleTargets ? "" : "# ",
@@ -2420,7 +2431,7 @@ public static void GenPodfile(BuildTarget buildTarget,
24202431
int maxProperties = 0;
24212432
int maxSources = Pod.Sources.Count;
24222433
int fromXmlFileCount = 0;
2423-
foreach (var pod in pods.Values) {
2434+
foreach (var pod in filteredPods) {
24242435
maxProperties = Math.Max(maxProperties, pod.propertiesByName.Count);
24252436
maxSources = Math.Max(maxSources, pod.sources.Count);
24262437
if (!String.IsNullOrEmpty(pod.version)) versionCount++;
@@ -2430,7 +2441,7 @@ public static void GenPodfile(BuildTarget buildTarget,
24302441
}
24312442
analytics.Report("generatepodfile/podinfo",
24322443
new KeyValuePair<string, string>[] {
2433-
new KeyValuePair<string, string>("numPods", pods.Count.ToString()),
2444+
new KeyValuePair<string, string>("numPods", filteredPods.Count.ToString()),
24342445
new KeyValuePair<string, string>("numPodsWithVersions",
24352446
versionCount.ToString()),
24362447
new KeyValuePair<string, string>("numLocalPods",
@@ -2779,6 +2790,11 @@ public static void OnPostProcessInstallPods(BuildTarget buildTarget,
27792790
string pathToBuiltProject) {
27802791
if (!InjectDependencies() || !PodfileGenerationEnabled) return;
27812792

2793+
// If the Podfile Generation was skipped because of no pods to install, skip this step.
2794+
if (podfileGenerationSkipped) {
2795+
return;
2796+
}
2797+
27822798
if(EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS) {
27832799
UpdateTargetIosSdkVersion(true);
27842800
}
@@ -2885,7 +2901,8 @@ public static void OnPostProcessUpdateProjectDeps(
28852901
BuildTarget buildTarget, string pathToBuiltProject) {
28862902
if (!InjectDependencies() || !PodfileGenerationEnabled ||
28872903
!CocoapodsProjectIntegrationEnabled || // Early out for Workspace level integration.
2888-
!cocoapodsToolsInstallPresent) {
2904+
!cocoapodsToolsInstallPresent ||
2905+
podfileGenerationSkipped) {
28892906
return;
28902907
}
28912908

source/IOSResolver/src/SwiftPackageManager.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using System.Reflection;
2525
using System.Xml.Linq;
2626
using UnityEditor;
27-
using UnityEditor.iOS.Xcode;
2827

2928
namespace Google {
3029
/// <summary>
@@ -242,11 +241,11 @@ internal static void AddPackagesToProject(List<RemoteSwiftPackage> resolvedPacka
242241
return;
243242
}
244243

245-
string pbxProjectPath = PBXProject.GetPBXProjectPath(projectPath);
246-
PBXProject project = new PBXProject();
244+
string pbxProjectPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath(projectPath);
245+
var project = new UnityEditor.iOS.Xcode.PBXProject();
247246
project.ReadFromFile(pbxProjectPath);
248247

249-
string mainTargetGuid = project.GetUnityMainTargetGuid();
248+
string frameworkTargetGuid = project.GetUnityFrameworkTargetGuid();
250249

251250
foreach (var remotePackage in resolvedPackages) {
252251
try {
@@ -259,11 +258,11 @@ internal static void AddPackagesToProject(List<RemoteSwiftPackage> resolvedPacka
259258
methodName = "AddRemotePackageReferenceAtVersion";
260259
}
261260

262-
VersionHandler.InvokeInstanceMethod(project, methodName, new object[] { mainTargetGuid, remotePackage.Url, remotePackage.Version });
261+
var packageGuid = VersionHandler.InvokeInstanceMethod(project, methodName, new object[] { remotePackage.Url, remotePackage.Version });
263262
logger.Log(string.Format("Added SPM package {0} version {1} to project.", remotePackage.Url, remotePackage.Version), level: LogLevel.Info);
264263

265264
foreach (var swiftPackage in remotePackage.Packages) {
266-
VersionHandler.InvokeInstanceMethod(project, "AddRemotePackageFrameworkToProject", new object[] { mainTargetGuid, swiftPackage.Name, swiftPackage.Weak });
265+
VersionHandler.InvokeInstanceMethod(project, "AddRemotePackageFrameworkToProject", new object[] { frameworkTargetGuid, swiftPackage.Name, packageGuid, swiftPackage.Weak });
267266
logger.Log(string.Format(" - Added framework {0} to project.", swiftPackage.Name), level: LogLevel.Info);
268267
}
269268
} catch (Exception e) {
@@ -275,4 +274,4 @@ internal static void AddPackagesToProject(List<RemoteSwiftPackage> resolvedPacka
275274
}
276275
}
277276
}
278-
#endif // UNITY_IOS
277+
#endif // UNITY_IOS

0 commit comments

Comments
 (0)