|
| 1 | +// |
| 2 | +// MacCatalystSupport.cs |
| 3 | +// |
| 4 | +// Author: Rolf Kvinge <[email protected]> |
| 5 | +// |
| 6 | +// Copyright (c) 2021 Microsoft Corp. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | + |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.IO; |
| 29 | + |
| 30 | +namespace Xamarin.MacDev { |
| 31 | + public static class MacCatalystSupport { |
| 32 | + // Versions for Mac Catalyst are complicated. In some cases we have to use the |
| 33 | + // corresponding iOS version of the SDK, and in some cases we have to use the |
| 34 | + // macOS version that iOS version correspond to. In Xcode, when you select the |
| 35 | + // deployment target, you select a macOS version in the UI, but the corresponding |
| 36 | + // iOS version is written to the project file. This means that there's a mapping |
| 37 | + // between the two, and it turns out Apple stores it in the SDKSettings.plist in |
| 38 | + // the macOS SDK. Here we provide a method that loads Apple's mapping from the |
| 39 | + // SDKSettings.plist. |
| 40 | + public static void LoadVersionMaps (string sdkDirectory, out Dictionary<string, string> map_ios_to_macos, out Dictionary<string, string> map_macos_to_ios) |
| 41 | + { |
| 42 | + map_ios_to_macos = new Dictionary<string, string> (); |
| 43 | + map_macos_to_ios = new Dictionary<string, string> (); |
| 44 | + |
| 45 | + var fn = Path.Combine (sdkDirectory, "SDKSettings.plist"); |
| 46 | + var plist = PDictionary.FromFile (fn); |
| 47 | + if (plist.TryGetValue ("VersionMap", out PDictionary versionMap)) { |
| 48 | + if (versionMap.TryGetValue ("iOSMac_macOS", out PDictionary versionMapiOSToMac)) { |
| 49 | + foreach (var kvp in versionMapiOSToMac) |
| 50 | + map_ios_to_macos [kvp.Key] = ((PString) kvp.Value).Value; |
| 51 | + } |
| 52 | + if (versionMap.TryGetValue ("macOS_iOSMac", out PDictionary versionMapMacToiOS)) { |
| 53 | + foreach (var kvp in versionMapMacToiOS) |
| 54 | + map_macos_to_ios [kvp.Key] = ((PString) kvp.Value).Value; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static bool TryGetMacOSVersion (string sdkDirectory, Version iOSVersion, out Version macOSVersion) |
| 60 | + { |
| 61 | + macOSVersion = null; |
| 62 | + |
| 63 | + if (!TryGetMacOSVersion (sdkDirectory, iOSVersion.ToString (), out var strValue)) |
| 64 | + return false; |
| 65 | + |
| 66 | + return Version.TryParse (strValue, out macOSVersion); |
| 67 | + } |
| 68 | + |
| 69 | + public static bool TryGetMacOSVersion (string sdkDirectory, string iOSVersion, out string macOSVersion) |
| 70 | + { |
| 71 | + LoadVersionMaps (sdkDirectory, out var map, out var _); |
| 72 | + |
| 73 | + return map.TryGetValue (iOSVersion.ToString (), out macOSVersion); |
| 74 | + } |
| 75 | + |
| 76 | + public static bool TryGetiOSVersion (string sdkDirectory, Version macOSVersion, out Version iOSVersion) |
| 77 | + { |
| 78 | + iOSVersion = null; |
| 79 | + |
| 80 | + if (!TryGetiOSVersion (sdkDirectory, macOSVersion.ToString (), out var strValue)) |
| 81 | + return false; |
| 82 | + |
| 83 | + return Version.TryParse (strValue, out iOSVersion); |
| 84 | + } |
| 85 | + |
| 86 | + public static bool TryGetiOSVersion (string sdkDirectory, string macOSVersion, out string iOSVersion) |
| 87 | + { |
| 88 | + LoadVersionMaps (sdkDirectory, out var _, out var map); |
| 89 | + |
| 90 | + return map.TryGetValue (macOSVersion.ToString (), out iOSVersion); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments