Skip to content

Commit 45c5a68

Browse files
authored
[Xamarin.MacDev] Add interfaces to bridge Xamarin.iOS- and Xamarin.Mac-specific classes. (#72)
* [Xamarin.MacDev] Add interfaces to bridge Xamarin.iOS- and Xamarin.Mac-specific classes. * Add IAppleSdk interface to bridge AppleSdk and MacOSXSdk. * Add IAppleSdkVersion interface to bridge IPhoneSdkVersion and MacOXSdkVersion. * Use camel case.
1 parent a0a11af commit 45c5a68

File tree

6 files changed

+157
-37
lines changed

6 files changed

+157
-37
lines changed

Xamarin.MacDev/AppleSdk.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace Xamarin.MacDev
3232
{
33-
public abstract class AppleSdk
33+
public abstract class AppleSdk : IAppleSdk
3434
{
3535
public string DeveloperRoot { get; protected set; }
3636
public string VersionPlist { get; protected set; }
@@ -90,6 +90,11 @@ string GetSdkPlistFilename (string version, bool sim)
9090
return Path.Combine (GetSdkPath (version, sim), "SDKSettings.plist");
9191
}
9292

93+
bool IAppleSdk.SdkIsInstalled (IAppleSdkVersion version, bool isSimulator)
94+
{
95+
return SdkIsInstalled ((IPhoneSdkVersion) version, isSimulator);
96+
}
97+
9398
public bool SdkIsInstalled (IPhoneSdkVersion version, bool sim)
9499
{
95100
foreach (var v in (sim? InstalledSimVersions : InstalledSdkVersions))
@@ -161,6 +166,11 @@ public AppleDTSettings GetDTSettings ()
161166
});
162167
}
163168

169+
IAppleSdkVersion IAppleSdk.GetClosestInstalledSdk (IAppleSdkVersion version, bool isSimulator)
170+
{
171+
return GetClosestInstalledSdk ((IPhoneSdkVersion) version, isSimulator);
172+
}
173+
164174
public IPhoneSdkVersion GetClosestInstalledSdk (IPhoneSdkVersion v, bool sim)
165175
{
166176
//sorted low to high, so get first that's >= requested version
@@ -171,6 +181,11 @@ public IPhoneSdkVersion GetClosestInstalledSdk (IPhoneSdkVersion v, bool sim)
171181
return IPhoneSdkVersion.UseDefault;
172182
}
173183

184+
IList<IAppleSdkVersion> IAppleSdk.GetInstalledSdkVersions (bool isSimulator)
185+
{
186+
return GetInstalledSdkVersions (isSimulator).Cast<IAppleSdkVersion> ().ToArray ();
187+
}
188+
174189
public IList<IPhoneSdkVersion> GetInstalledSdkVersions (bool sim)
175190
{
176191
return sim ? InstalledSimVersions : InstalledSdkVersions;
@@ -227,5 +242,10 @@ protected static string GrabRootString (string file, string key)
227242

228243
return null;
229244
}
245+
246+
bool IAppleSdk.TryParseSdkVersion (string value, out IAppleSdkVersion version)
247+
{
248+
return IAppleSdkVersion_Extensions.TryParse<IPhoneSdkVersion> (value, out version);
249+
}
230250
}
231251
}

Xamarin.MacDev/IAppleSdk.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Xamarin.MacDev {
5+
public interface IAppleSdk {
6+
bool IsInstalled { get; }
7+
string DeveloperRoot { get; }
8+
string GetPlatformPath (bool isSimulator);
9+
string GetSdkPath (string version, bool isSimulator);
10+
bool SdkIsInstalled (IAppleSdkVersion version, bool isSimulator);
11+
bool TryParseSdkVersion (string value, out IAppleSdkVersion version);
12+
IAppleSdkVersion GetClosestInstalledSdk (IAppleSdkVersion version, bool isSimulator);
13+
IList<IAppleSdkVersion> GetInstalledSdkVersions (bool isSimulator);
14+
}
15+
}

Xamarin.MacDev/IAppleSdkVersion.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
namespace Xamarin.MacDev {
3+
public interface IAppleSdkVersion : IEquatable<IAppleSdkVersion> {
4+
bool IsUseDefault { get; }
5+
void SetVersion (int [] version);
6+
IAppleSdkVersion GetUseDefault ();
7+
}
8+
9+
public static class IAppleSdkVersion_Extensions {
10+
public static IAppleSdkVersion ResolveIfDefault (this IAppleSdkVersion @this, IAppleSdk sdk, bool sim)
11+
{
12+
return @this.IsUseDefault ? @this.GetDefault (sdk, sim) : @this;
13+
}
14+
15+
public static IAppleSdkVersion GetDefault (this IAppleSdkVersion @this, IAppleSdk sdk, bool sim)
16+
{
17+
var v = sdk.GetInstalledSdkVersions (sim);
18+
return v.Count > 0 ? v [v.Count - 1] : @this.GetUseDefault ();
19+
}
20+
21+
public static bool TryParse<T> (string s, out T result) where T : IAppleSdkVersion, new()
22+
{
23+
result = new T ();
24+
if (s == null)
25+
return false;
26+
27+
var vstr = s.Split ('.');
28+
var vint = new int [vstr.Length];
29+
30+
for (int j = 0; j < vstr.Length; j++) {
31+
int component;
32+
if (!int.TryParse (vstr [j], out component))
33+
return false;
34+
35+
vint [j] = component;
36+
}
37+
38+
result.SetVersion (vint);
39+
return true;
40+
}
41+
42+
public static bool TryParse<T> (string s, out IAppleSdkVersion result) where T : IAppleSdkVersion, new()
43+
{
44+
var rv = TryParse<T> (s, out T tmp);
45+
result = tmp;
46+
return rv;
47+
}
48+
}
49+
}

Xamarin.MacDev/IPhoneSdkVersion.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace Xamarin.MacDev
3131
{
32-
public struct IPhoneSdkVersion : IComparable<IPhoneSdkVersion>, IEquatable<IPhoneSdkVersion>
32+
public struct IPhoneSdkVersion : IComparable<IPhoneSdkVersion>, IEquatable<IPhoneSdkVersion>, IAppleSdkVersion
3333
{
3434
int[] version;
3535

@@ -57,6 +57,11 @@ public IPhoneSdkVersion (params int[] version)
5757
this.version = version;
5858
}
5959

60+
void IAppleSdkVersion.SetVersion (int[] version)
61+
{
62+
this.version = version;
63+
}
64+
6065
public static IPhoneSdkVersion Parse (string s)
6166
{
6267
var vstr = s.Split ('.');
@@ -70,24 +75,7 @@ public static IPhoneSdkVersion Parse (string s)
7075

7176
public static bool TryParse (string s, out IPhoneSdkVersion result)
7277
{
73-
result = new IPhoneSdkVersion ();
74-
75-
if (s == null)
76-
return false;
77-
78-
var vstr = s.Split ('.');
79-
var vint = new int[vstr.Length];
80-
81-
for (int j = 0; j < vstr.Length; j++) {
82-
int component;
83-
if (!int.TryParse (vstr[j], out component))
84-
return false;
85-
86-
vint[j] = component;
87-
}
88-
89-
result.version = vint;
90-
return true;
78+
return IAppleSdkVersion_Extensions.TryParse<IPhoneSdkVersion> (s, out result);
9179
}
9280

9381
public int[] Version { get { return version; } }
@@ -145,6 +133,11 @@ public override bool Equals (object obj)
145133
return false;
146134
}
147135

136+
bool IEquatable<IAppleSdkVersion>.Equals (IAppleSdkVersion other)
137+
{
138+
return Equals ((object) other);
139+
}
140+
148141
public override int GetHashCode ()
149142
{
150143
unchecked {
@@ -190,6 +183,11 @@ public bool IsUseDefault {
190183
get { return version == null || version.Length == 0; }
191184
}
192185

186+
IAppleSdkVersion IAppleSdkVersion.GetUseDefault ()
187+
{
188+
return UseDefault;
189+
}
190+
193191
#if !WINDOWS
194192
public IPhoneSdkVersion ResolveIfDefault (AppleSdk sdk, bool sim)
195193
{

Xamarin.MacDev/MacOSXSdk.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace Xamarin.MacDev
3232
{
33-
public class MacOSXSdk
33+
public class MacOSXSdk : IAppleSdk
3434
{
3535
List<MacOSXSdkVersion> knownOSVersions = new List<MacOSXSdkVersion> {
3636
MacOSXSdkVersion.V10_7,
@@ -133,6 +133,11 @@ public string GetPlatformPath ()
133133
{
134134
return DesktopPlatform;
135135
}
136+
137+
string IAppleSdk.GetPlatformPath (bool isSimulator)
138+
{
139+
return GetPlatformPath ();
140+
}
136141

137142
public string GetSdkPath (MacOSXSdkVersion version)
138143
{
@@ -143,12 +148,22 @@ public string GetSdkPath (string version)
143148
{
144149
return Path.Combine (SdkDeveloperRoot, "SDKs", "MacOSX" + version + ".sdk");
145150
}
146-
151+
152+
string IAppleSdk.GetSdkPath (string version, bool isSimulator)
153+
{
154+
return GetSdkPath (version);
155+
}
156+
147157
string GetSdkPlistFilename (string version)
148158
{
149159
return Path.Combine (GetSdkPath (version), "SDKSettings.plist");
150160
}
151-
161+
162+
bool IAppleSdk.SdkIsInstalled (IAppleSdkVersion version, bool isSimulator)
163+
{
164+
return SdkIsInstalled ((MacOSXSdkVersion) version);
165+
}
166+
152167
public bool SdkIsInstalled (MacOSXSdkVersion version)
153168
{
154169
foreach (var v in InstalledSdkVersions) {
@@ -231,6 +246,11 @@ static string GrabRootString (string file, string key)
231246
return null;
232247
}
233248

249+
IAppleSdkVersion IAppleSdk.GetClosestInstalledSdk (IAppleSdkVersion version, bool isSimulator)
250+
{
251+
return GetClosestInstalledSdk ((MacOSXSdkVersion) version);
252+
}
253+
234254
public MacOSXSdkVersion GetClosestInstalledSdk (MacOSXSdkVersion v)
235255
{
236256
// sorted low to high, so get first that's >= requested version
@@ -241,11 +261,21 @@ public MacOSXSdkVersion GetClosestInstalledSdk (MacOSXSdkVersion v)
241261
return MacOSXSdkVersion.UseDefault;
242262
}
243263

264+
IList<IAppleSdkVersion> IAppleSdk.GetInstalledSdkVersions (bool isSimulator)
265+
{
266+
return GetInstalledSdkVersions ().Cast<IAppleSdkVersion> ().ToArray ();
267+
}
268+
244269
public IList<MacOSXSdkVersion> GetInstalledSdkVersions ()
245270
{
246271
return InstalledSdkVersions;
247272
}
248273

274+
bool IAppleSdk.TryParseSdkVersion (string value, out IAppleSdkVersion version)
275+
{
276+
return IAppleSdkVersion_Extensions.TryParse<MacOSXSdkVersion> (value, out version);
277+
}
278+
249279
public class DTSettings
250280
{
251281
public string DTXcodeBuild { get; set; }

Xamarin.MacDev/MacOSXSdkVersion.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace Xamarin.MacDev
2929
{
30-
public struct MacOSXSdkVersion : IComparable<MacOSXSdkVersion>, IEquatable<MacOSXSdkVersion>
30+
public struct MacOSXSdkVersion : IComparable<MacOSXSdkVersion>, IEquatable<MacOSXSdkVersion>, IAppleSdkVersion
3131
{
3232
int[] version;
3333

@@ -38,6 +38,11 @@ public MacOSXSdkVersion (params int[] version)
3838
this.version = version;
3939
}
4040

41+
void IAppleSdkVersion.SetVersion (int [] version)
42+
{
43+
this.version = version;
44+
}
45+
4146
public MacOSXSdkVersion (Version version)
4247
{
4348
if (version == null)
@@ -56,19 +61,7 @@ public static MacOSXSdkVersion Parse (string s)
5661

5762
public static bool TryParse (string s, out MacOSXSdkVersion result)
5863
{
59-
result = new MacOSXSdkVersion ();
60-
if (s == null)
61-
return false;
62-
var vstr = s.Split ('.');
63-
var vint = new int[vstr.Length];
64-
for (int j = 0; j < vstr.Length; j++) {
65-
int component;
66-
if (!int.TryParse (vstr[j], out component))
67-
return false;
68-
vint[j] = component;
69-
}
70-
result.version = vint;
71-
return true;
64+
return IAppleSdkVersion_Extensions.TryParse<MacOSXSdkVersion> (s, out result);
7265
}
7366

7467
public int[] Version { get { return version; } }
@@ -124,6 +117,11 @@ public override bool Equals (object obj)
124117
return false;
125118
}
126119

120+
bool IEquatable<IAppleSdkVersion>.Equals (IAppleSdkVersion other)
121+
{
122+
return Equals ((object) other);
123+
}
124+
127125
public override int GetHashCode ()
128126
{
129127
unchecked {
@@ -171,6 +169,16 @@ public bool IsUseDefault {
171169
}
172170
}
173171

172+
IAppleSdkVersion IAppleSdkVersion.GetUseDefault ()
173+
{
174+
return UseDefault;
175+
}
176+
177+
public static MacOSXSdkVersion GetDefault (IAppleSdk sdk)
178+
{
179+
return GetDefault ((MacOSXSdk) sdk);
180+
}
181+
174182
public static MacOSXSdkVersion GetDefault (MacOSXSdk sdk)
175183
{
176184
var v = sdk.GetInstalledSdkVersions ();

0 commit comments

Comments
 (0)