Skip to content

Commit

Permalink
Updated to use Xcode 16's new provisioning profiles directory (#124)
Browse files Browse the repository at this point in the history
* Updated to use Xcode 16's new provisioning profiles directory

Starting with Xcode 16, the location of cached provisioning profiles
has moved from "~/Library/MobileDevice/Provisioning Profiles" to
"~/Library/Developer/Xcode/UserData/Provisioning Profiles"

Fixes xamarin/xamarin-macios#20771

* Support loading profisioning profiles from both locations

* Fixed the unit tests

* Auto-format source code

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
jstedfast and GitHub Actions Autoformatter authored Aug 9, 2024
1 parent 94c2c8c commit bd3d133
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 134 deletions.
61 changes: 61 additions & 0 deletions UnitTests/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// TestHelper.cs
//
// Author: Jeffrey Stedfast <[email protected]>
//
// Copyright (c) 2017 Microsoft Corp.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.IO;

using NUnit.Framework;

namespace UnitTests {
[SetUpFixture]
static class TestHelper {
public static readonly string ProjectDir;

static TestHelper ()
{
#if NET5_0_OR_GREATER
var codeBase = typeof (TestHelper).Assembly.Location;
#else
var codeBase = typeof (TestHelper).Assembly.CodeBase;
if (codeBase.StartsWith ("file://", StringComparison.OrdinalIgnoreCase))
codeBase = codeBase.Substring ("file://".Length);

if (Path.DirectorySeparatorChar == '\\') {
if (codeBase [0] == '/')
codeBase = codeBase.Substring (1);

codeBase = codeBase.Replace ('/', '\\');
}
#endif

var dir = Path.GetDirectoryName (codeBase);

while (Path.GetFileName (dir) != "UnitTests")
dir = Path.GetFullPath (Path.Combine (dir, ".."));

ProjectDir = Path.GetFullPath (dir);
}
}
}
13 changes: 11 additions & 2 deletions UnitTests/TestMobileProvisionIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@
namespace UnitTests {
[TestFixture]
public class TestMobileProvisionIndex {
static readonly string [] ProfileDirectories;

static TestMobileProvisionIndex ()
{
ProfileDirectories = new string [] {
Path.Combine (TestHelper.ProjectDir, "TestData", "Provisioning Profiles")
};
}

[Test]
public void TestCreateIndex ()
{
var index = MobileProvisionIndex.CreateIndex ("../../TestData/Provisioning Profiles", "profiles.index");
var index = MobileProvisionIndex.CreateIndex (ProfileDirectories, "profiles.index");

Assert.AreEqual (2, index.ProvisioningProfiles.Count);

Expand Down Expand Up @@ -77,7 +86,7 @@ public void TestCreateIndex ()
[Test]
public void TestOpenIndex ()
{
var index = MobileProvisionIndex.OpenIndex ("../../TestData/Provisioning Profiles", "profiles.index");
var index = MobileProvisionIndex.OpenIndex (ProfileDirectories, "profiles.index");

Assert.AreEqual (2, index.ProvisioningProfiles.Count);

Expand Down
72 changes: 40 additions & 32 deletions Xamarin.MacDev/MobileProvision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ public class MobileProvision {
public const string AutomaticAppStore = "Automatic:AppStore";
public const string AutomaticInHouse = "Automatic:InHouse";
public const string AutomaticAdHoc = "Automatic:AdHoc";
public static readonly string ProfileDirectory;
public static readonly string [] ProfileDirectories;

static MobileProvision ()
{
if (Environment.OSVersion.Platform == PlatformID.MacOSX
|| Environment.OSVersion.Platform == PlatformID.Unix) {
string personal = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
ProfileDirectory = Path.Combine (personal, "Library", "MobileDevice", "Provisioning Profiles");

ProfileDirectories = new string [] {
// Xcode >= 16.x uses ~/Library/Developer/Xcode/UserData/Provisioning Profiles
Path.Combine (personal, "Library", "Developer", "Xcode", "UserData", "Provisioning Profiles"),

// Xcode < 16.x uses ~/Library/MobileDevice/Provisioning Profiles
Path.Combine (personal, "Library", "MobileDevice", "Provisioning Profiles"),
};
} else {
ProfileDirectory = Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),
"Xamarin",
"iOS",
"Provisioning",
"Profiles");
var appDataLocal = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);

ProfileDirectories = new string [] {
Path.Combine (appDataLocal, "Xamarin", "iOS", "Provisioning", "Profiles")
};
}
}

Expand Down Expand Up @@ -145,9 +151,6 @@ public static IList<MobileProvision> GetAllInstalledProvisions (MobileProvisionP
/// </summary>
public static IList<MobileProvision> GetAllInstalledProvisions (MobileProvisionPlatform platform, bool includeExpired)
{
if (!Directory.Exists (ProfileDirectory))
return new MobileProvision [0];

var uuids = new Dictionary<string, MobileProvision> ();
var list = new List<MobileProvision> ();
var now = DateTime.Now;
Expand All @@ -165,33 +168,38 @@ public static IList<MobileProvision> GetAllInstalledProvisions (MobileProvisionP
throw new ArgumentOutOfRangeException (nameof (platform));
}

foreach (var file in Directory.EnumerateFiles (ProfileDirectory, pattern)) {
try {
var data = File.ReadAllBytes (file);

var m = new MobileProvision ();
m.Load (PDictionary.FromBinaryXml (data));
m.Data = data;

if (includeExpired || m.ExpirationDate > now) {
if (uuids.ContainsKey (m.Uuid)) {
// we always want the most recently created/updated provision
if (m.CreationDate > uuids [m.Uuid].CreationDate) {
int index = list.IndexOf (uuids [m.Uuid]);
uuids [m.Uuid] = m;
list [index] = m;
foreach (var profileDir in ProfileDirectories) {
if (!Directory.Exists (profileDir))
continue;

foreach (var file in Directory.EnumerateFiles (profileDir, pattern)) {
try {
var data = File.ReadAllBytes (file);

var m = new MobileProvision ();
m.Load (PDictionary.FromBinaryXml (data));
m.Data = data;

if (includeExpired || m.ExpirationDate > now) {
if (uuids.ContainsKey (m.Uuid)) {
// we always want the most recently created/updated provision
if (m.CreationDate > uuids [m.Uuid].CreationDate) {
int index = list.IndexOf (uuids [m.Uuid]);
uuids [m.Uuid] = m;
list [index] = m;
}
} else {
uuids.Add (m.Uuid, m);
list.Add (m);
}
} else {
uuids.Add (m.Uuid, m);
list.Add (m);
}
} catch (Exception ex) {
LoggingService.LogWarning ("Error reading " + platform + " provision file '" + file + "'", ex);
}
} catch (Exception ex) {
LoggingService.LogWarning ("Error reading " + platform + " provision file '" + file + "'", ex);
}
}

//newest first
// newest first
list.Sort ((x, y) => y.CreationDate.CompareTo (x.CreationDate));

return list;
Expand Down
Loading

0 comments on commit bd3d133

Please sign in to comment.