Skip to content

Commit 02d6d05

Browse files
authored
[Xamarin.MacDev] Add an AppleSdkVersion struct which replaces IPhoneSdkVersion and MacOSXSdkVersion. (#87)
This reduces code duplication and makes it possible to simplify logic in a few places.
1 parent e7ec7ef commit 02d6d05

File tree

9 files changed

+443
-234
lines changed

9 files changed

+443
-234
lines changed

Xamarin.MacDev/AppleSdk.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public abstract class AppleSdk : IAppleSdk
4242
public string SimPlatform { get { return Path.Combine (DeveloperRoot, "Platforms/" + SimulatorPlatformName + ".platform"); } }
4343

4444
public bool IsInstalled { get; private set; }
45-
public IPhoneSdkVersion[] InstalledSdkVersions { get; private set; }
46-
public IPhoneSdkVersion[] InstalledSimVersions { get; private set; }
45+
public AppleSdkVersion[] InstalledSdkVersions { get; private set; }
46+
public AppleSdkVersion[] InstalledSimVersions { get; private set; }
4747

4848
readonly Dictionary<string, AppleDTSdkSettings> sdkSettingsCache = new Dictionary<string, AppleDTSdkSettings> ();
4949
readonly Dictionary<string, AppleDTSdkSettings> simSettingsCache = new Dictionary<string, AppleDTSdkSettings> ();
@@ -61,8 +61,8 @@ protected void Init ()
6161
InstalledSdkVersions = EnumerateSdks (Path.Combine (DevicePlatform, "Developer/SDKs"), DevicePlatformName);
6262
InstalledSimVersions = EnumerateSdks (Path.Combine (SimPlatform, "Developer/SDKs"), SimulatorPlatformName);
6363
} else {
64-
InstalledSdkVersions = new IPhoneSdkVersion[0];
65-
InstalledSimVersions = new IPhoneSdkVersion[0];
64+
InstalledSdkVersions = new AppleSdkVersion [0];
65+
InstalledSimVersions = new AppleSdkVersion [0];
6666
}
6767
}
6868

@@ -72,7 +72,7 @@ public string GetPlatformPath (bool sim)
7272
return sim ? SimPlatform : DevicePlatform;
7373
}
7474

75-
public string GetSdkPath (IPhoneSdkVersion version, bool sim)
75+
public string GetSdkPath (IAppleSdkVersion version, bool sim)
7676
{
7777
return GetSdkPath (version.ToString (), sim);
7878
}
@@ -92,18 +92,18 @@ string GetSdkPlistFilename (string version, bool sim)
9292

9393
bool IAppleSdk.SdkIsInstalled (IAppleSdkVersion version, bool isSimulator)
9494
{
95-
return SdkIsInstalled ((IPhoneSdkVersion) version, isSimulator);
95+
return SdkIsInstalled ((AppleSdkVersion) version, isSimulator);
9696
}
9797

98-
public bool SdkIsInstalled (IPhoneSdkVersion version, bool sim)
98+
public bool SdkIsInstalled (AppleSdkVersion version, bool sim)
9999
{
100100
foreach (var v in (sim? InstalledSimVersions : InstalledSdkVersions))
101101
if (v.Equals (version))
102102
return true;
103103
return false;
104104
}
105105

106-
public AppleDTSdkSettings GetSdkSettings (IPhoneSdkVersion sdk, bool isSim)
106+
public AppleDTSdkSettings GetSdkSettings (AppleSdkVersion sdk, bool isSim)
107107
{
108108
return GetSdkSettings ((IAppleSdkVersion) sdk, isSim);
109109
}
@@ -178,33 +178,33 @@ public AppleDTSettings GetAppleDTSettings ()
178178

179179
IAppleSdkVersion IAppleSdk.GetClosestInstalledSdk (IAppleSdkVersion version, bool isSimulator)
180180
{
181-
return GetClosestInstalledSdk ((IPhoneSdkVersion) version, isSimulator);
181+
return GetClosestInstalledSdk ((AppleSdkVersion) version, isSimulator);
182182
}
183183

184-
public IPhoneSdkVersion GetClosestInstalledSdk (IPhoneSdkVersion v, bool sim)
184+
public AppleSdkVersion GetClosestInstalledSdk (AppleSdkVersion v, bool sim)
185185
{
186186
//sorted low to high, so get first that's >= requested version
187187
foreach (var i in GetInstalledSdkVersions (sim)) {
188188
if (i.CompareTo (v) >= 0)
189189
return i;
190190
}
191-
return IPhoneSdkVersion.UseDefault;
191+
return AppleSdkVersion.UseDefault;
192192
}
193193

194194
IList<IAppleSdkVersion> IAppleSdk.GetInstalledSdkVersions (bool isSimulator)
195195
{
196196
return GetInstalledSdkVersions (isSimulator).Cast<IAppleSdkVersion> ().ToArray ();
197197
}
198198

199-
public IList<IPhoneSdkVersion> GetInstalledSdkVersions (bool sim)
199+
public IList<AppleSdkVersion> GetInstalledSdkVersions (bool sim)
200200
{
201201
return sim ? InstalledSimVersions : InstalledSdkVersions;
202202
}
203203

204-
protected static IPhoneSdkVersion[] EnumerateSdks (string sdkDir, string name)
204+
protected static AppleSdkVersion [] EnumerateSdks (string sdkDir, string name)
205205
{
206206
if (!Directory.Exists (sdkDir))
207-
return new IPhoneSdkVersion[0];
207+
return new AppleSdkVersion [0];
208208

209209
var sdks = new List<string> ();
210210

@@ -225,10 +225,10 @@ protected static IPhoneSdkVersion[] EnumerateSdks (string sdkDir, string name)
225225
sdks.Add (d);
226226
}
227227

228-
var vs = new List<IPhoneSdkVersion> ();
228+
var vs = new List<AppleSdkVersion> ();
229229
foreach (var s in sdks) {
230230
try {
231-
vs.Add (IPhoneSdkVersion.Parse (s));
231+
vs.Add (AppleSdkVersion.Parse (s));
232232
} catch (Exception ex) {
233233
LoggingService.LogError ("Could not parse {0} SDK version '{1}':\n{2}", name, s, ex.ToString ());
234234
}
@@ -255,7 +255,7 @@ protected static string GrabRootString (string file, string key)
255255

256256
bool IAppleSdk.TryParseSdkVersion (string value, out IAppleSdkVersion version)
257257
{
258-
return IAppleSdkVersion_Extensions.TryParse<IPhoneSdkVersion> (value, out version);
258+
return IAppleSdkVersion_Extensions.TryParse<AppleSdkVersion> (value, out version);
259259
}
260260
}
261261
}

Xamarin.MacDev/AppleSdkVersion.cs

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
//
2+
// AppleSdkVersion.cs
3+
//
4+
// Authors: Michael Hutchinson <mhutchinson@novell.com>
5+
// Jeffrey Stedfast <jeff@xamarin.com>
6+
//
7+
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
8+
// Copyright (c) 2011-2013 Xamarin Inc. (http://www.xamarin.com)
9+
// Copyright (c) 2021 Microsoft Corp. (http://www.microsoft.com)
10+
//
11+
// Permission is hereby granted, free of charge, to any person obtaining a copy
12+
// of this software and associated documentation files (the "Software"), to deal
13+
// in the Software without restriction, including without limitation the rights
14+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
// copies of the Software, and to permit persons to whom the Software is
16+
// furnished to do so, subject to the following conditions:
17+
//
18+
// The above copyright notice and this permission notice shall be included in
19+
// all copies or substantial portions of the Software.
20+
//
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
// THE SOFTWARE.
28+
29+
using System;
30+
31+
namespace Xamarin.MacDev {
32+
public struct AppleSdkVersion : IComparable<AppleSdkVersion>, IEquatable<AppleSdkVersion>, IAppleSdkVersion {
33+
int [] version;
34+
35+
public AppleSdkVersion (Version version)
36+
{
37+
if (version == null)
38+
throw new ArgumentNullException ();
39+
40+
if (version.Build != -1) {
41+
this.version = new int [3];
42+
this.version [2] = version.Build;
43+
} else {
44+
this.version = new int [2];
45+
}
46+
47+
this.version [0] = version.Major;
48+
this.version [1] = version.Minor;
49+
}
50+
51+
public AppleSdkVersion (params int [] version)
52+
{
53+
if (version == null)
54+
throw new ArgumentNullException (nameof (version));
55+
56+
this.version = version;
57+
}
58+
59+
void IAppleSdkVersion.SetVersion (int [] version)
60+
{
61+
this.version = version;
62+
}
63+
64+
public static AppleSdkVersion Parse (string s)
65+
{
66+
return IAppleSdkVersion_Extensions.Parse<AppleSdkVersion> (s);
67+
}
68+
69+
public static bool TryParse (string s, out AppleSdkVersion result)
70+
{
71+
return IAppleSdkVersion_Extensions.TryParse (s, out result);
72+
}
73+
74+
public int [] Version { get { return version; } }
75+
76+
public override string ToString ()
77+
{
78+
return IAppleSdkVersion_Extensions.ToString (this);
79+
}
80+
81+
public int CompareTo (AppleSdkVersion other)
82+
{
83+
return IAppleSdkVersion_Extensions.CompareTo (this, other);
84+
}
85+
86+
public int CompareTo (IAppleSdkVersion other)
87+
{
88+
return IAppleSdkVersion_Extensions.CompareTo (this, other);
89+
}
90+
91+
public bool Equals (IAppleSdkVersion other)
92+
{
93+
return IAppleSdkVersion_Extensions.Equals (this, other);
94+
}
95+
96+
public bool Equals (AppleSdkVersion other)
97+
{
98+
return IAppleSdkVersion_Extensions.Equals (this, other);
99+
}
100+
101+
public override bool Equals (object obj)
102+
{
103+
return IAppleSdkVersion_Extensions.Equals (this, obj);
104+
}
105+
106+
public override int GetHashCode ()
107+
{
108+
return IAppleSdkVersion_Extensions.GetHashCode (this);
109+
}
110+
111+
public static bool operator == (AppleSdkVersion a, IAppleSdkVersion b)
112+
{
113+
return a.Equals (b);
114+
}
115+
116+
public static bool operator != (AppleSdkVersion a, IAppleSdkVersion b)
117+
{
118+
return !a.Equals (b);
119+
}
120+
121+
public static bool operator < (AppleSdkVersion a, IAppleSdkVersion b)
122+
{
123+
return a.CompareTo (b) < 0;
124+
}
125+
126+
public static bool operator > (AppleSdkVersion a, IAppleSdkVersion b)
127+
{
128+
return a.CompareTo (b) > 0;
129+
}
130+
131+
public static bool operator <= (AppleSdkVersion a, IAppleSdkVersion b)
132+
{
133+
return a.CompareTo (b) <= 0;
134+
}
135+
136+
public static bool operator >= (AppleSdkVersion a, IAppleSdkVersion b)
137+
{
138+
return a.CompareTo (b) >= 0;
139+
}
140+
141+
public bool IsUseDefault {
142+
get { return version == null || version.Length == 0; }
143+
}
144+
145+
IAppleSdkVersion IAppleSdkVersion.GetUseDefault ()
146+
{
147+
return UseDefault;
148+
}
149+
150+
#if !WINDOWS
151+
public AppleSdkVersion ResolveIfDefault (AppleSdk sdk, bool sim)
152+
{
153+
return IsUseDefault ? GetDefault (sdk, sim) : this;
154+
}
155+
156+
public static AppleSdkVersion GetDefault (AppleSdk sdk, bool sim)
157+
{
158+
var v = sdk.GetInstalledSdkVersions (sim);
159+
return v.Count > 0 ? v [v.Count - 1] : UseDefault;
160+
}
161+
#endif
162+
163+
public static readonly AppleSdkVersion UseDefault = new AppleSdkVersion (new int [0]);
164+
165+
public static readonly AppleSdkVersion V1_0 = new AppleSdkVersion (1, 0);
166+
public static readonly AppleSdkVersion V2_0 = new AppleSdkVersion (2, 0);
167+
public static readonly AppleSdkVersion V2_1 = new AppleSdkVersion (2, 1);
168+
public static readonly AppleSdkVersion V2_2 = new AppleSdkVersion (2, 2);
169+
public static readonly AppleSdkVersion V3_0 = new AppleSdkVersion (3, 0);
170+
public static readonly AppleSdkVersion V3_1 = new AppleSdkVersion (3, 1);
171+
public static readonly AppleSdkVersion V3_2 = new AppleSdkVersion (3, 2);
172+
public static readonly AppleSdkVersion V3_99 = new AppleSdkVersion (3, 99);
173+
public static readonly AppleSdkVersion V4_0 = new AppleSdkVersion (4, 0);
174+
public static readonly AppleSdkVersion V4_1 = new AppleSdkVersion (4, 1);
175+
public static readonly AppleSdkVersion V4_2 = new AppleSdkVersion (4, 2);
176+
public static readonly AppleSdkVersion V4_3 = new AppleSdkVersion (4, 3);
177+
public static readonly AppleSdkVersion V5_0 = new AppleSdkVersion (5, 0);
178+
public static readonly AppleSdkVersion V5_1 = new AppleSdkVersion (5, 1);
179+
public static readonly AppleSdkVersion V5_1_1 = new AppleSdkVersion (5, 1, 1);
180+
public static readonly AppleSdkVersion V5_2 = new AppleSdkVersion (5, 2);
181+
public static readonly AppleSdkVersion V6_0 = new AppleSdkVersion (6, 0);
182+
public static readonly AppleSdkVersion V6_1 = new AppleSdkVersion (6, 1);
183+
public static readonly AppleSdkVersion V6_2 = new AppleSdkVersion (6, 2);
184+
public static readonly AppleSdkVersion V7_0 = new AppleSdkVersion (7, 0);
185+
public static readonly AppleSdkVersion V7_1 = new AppleSdkVersion (7, 1);
186+
public static readonly AppleSdkVersion V7_2_1 = new AppleSdkVersion (7, 2, 1);
187+
public static readonly AppleSdkVersion V8_0 = new AppleSdkVersion (8, 0);
188+
public static readonly AppleSdkVersion V8_1 = new AppleSdkVersion (8, 1);
189+
public static readonly AppleSdkVersion V8_2 = new AppleSdkVersion (8, 2);
190+
public static readonly AppleSdkVersion V8_3 = new AppleSdkVersion (8, 3);
191+
public static readonly AppleSdkVersion V8_4 = new AppleSdkVersion (8, 4);
192+
public static readonly AppleSdkVersion V9_0 = new AppleSdkVersion (9, 0);
193+
public static readonly AppleSdkVersion V9_1 = new AppleSdkVersion (9, 1);
194+
public static readonly AppleSdkVersion V9_2 = new AppleSdkVersion (9, 2);
195+
public static readonly AppleSdkVersion V9_3 = new AppleSdkVersion (9, 3);
196+
public static readonly AppleSdkVersion V10_0 = new AppleSdkVersion (10, 0);
197+
public static readonly AppleSdkVersion V10_1 = new AppleSdkVersion (10, 1);
198+
public static readonly AppleSdkVersion V10_2 = new AppleSdkVersion (10, 2);
199+
public static readonly AppleSdkVersion V10_3 = new AppleSdkVersion (10, 3);
200+
public static readonly AppleSdkVersion V10_4 = new AppleSdkVersion (10, 4);
201+
public static readonly AppleSdkVersion V10_5 = new AppleSdkVersion (10, 5);
202+
public static readonly AppleSdkVersion V10_6 = new AppleSdkVersion (10, 6);
203+
public static readonly AppleSdkVersion V10_7 = new AppleSdkVersion (10, 7);
204+
public static readonly AppleSdkVersion V10_8 = new AppleSdkVersion (10, 8);
205+
public static readonly AppleSdkVersion V10_9 = new AppleSdkVersion (10, 9);
206+
public static readonly AppleSdkVersion V10_10 = new AppleSdkVersion (10, 10);
207+
public static readonly AppleSdkVersion V10_11 = new AppleSdkVersion (10, 11);
208+
public static readonly AppleSdkVersion V10_12 = new AppleSdkVersion (10, 12);
209+
public static readonly AppleSdkVersion V10_13 = new AppleSdkVersion (10, 13);
210+
public static readonly AppleSdkVersion V10_14 = new AppleSdkVersion (10, 14);
211+
public static readonly AppleSdkVersion V10_15 = new AppleSdkVersion (10, 15);
212+
public static readonly AppleSdkVersion V11_0 = new AppleSdkVersion (11, 0);
213+
}
214+
}

0 commit comments

Comments
 (0)