~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.MacDev/AppleSdkSettings.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// AppleSdkSettings.cs
3
 
//  
4
 
// Author:
5
 
//       Michael Hutchinson <mhutch@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2011 Xamarin Inc. (http://xamarin.com)
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
 
27
 
using System;
28
 
using System.Collections.Generic;
29
 
using MonoDevelop.Core;
30
 
using System.Linq;
31
 
using MonoMac.Foundation;
32
 
using System.IO;
33
 
 
34
 
namespace MonoDevelop.MacDev
35
 
{
36
 
        public static class AppleSdkSettings
37
 
        {
38
 
                const string SDK_KEY = "MonoDevelop.MacDev.AppleSdkRoot";
39
 
                
40
 
                internal static string DefaultRoot {
41
 
                        get {
42
 
                                // If the current developer root corresponds to a default SDK location for
43
 
                                // any of the supported configurations, return that as the default root. If
44
 
                                // they have configured an SDK in a non-default location, fall back to using
45
 
                                // the default location for the latest SDK.
46
 
                                if (DefaultRoots.Contains (DeveloperRoot))
47
 
                                        return DeveloperRoot;
48
 
                                return DefaultRoots [0];
49
 
                        }
50
 
                }
51
 
                
52
 
                // Put newer SDKs at the top as we scan from 0 -> List.Count
53
 
                static readonly IList<string> DefaultRoots = new List<string> {
54
 
                        "/Applications/Xcode.app",
55
 
                        "/Developer"
56
 
                };
57
 
                static DateTime lastWritten;
58
 
                
59
 
                static string GetEnvLocation ()
60
 
                {
61
 
                        return Environment.GetEnvironmentVariable ("MD_APPLE_SDK_ROOT");
62
 
                }
63
 
                
64
 
                static void GetNewPaths (FilePath root, out FilePath xcode, out FilePath vplist, out FilePath devroot)
65
 
                {
66
 
                        xcode = root;
67
 
                        vplist = root.Combine ("Contents", "version.plist");
68
 
                        devroot = root.Combine ("Contents", "Developer");
69
 
                }
70
 
                
71
 
                static void GetOldPaths (FilePath root, out FilePath xcode, out FilePath vplist, out FilePath devroot)
72
 
                {
73
 
                        xcode = root.Combine ("Applications", "Xcode.app");
74
 
                        vplist = root.Combine ("Library", "version.plist");
75
 
                        devroot = root;
76
 
                }
77
 
                
78
 
                static bool ValidatePaths (FilePath xcode, FilePath vplist, FilePath devroot)
79
 
                {
80
 
                        return Directory.Exists (xcode)
81
 
                                && Directory.Exists (devroot)
82
 
                                && File.Exists (vplist)
83
 
                                && File.Exists (xcode.Combine ("Contents", "Info.plist"));
84
 
                }
85
 
                
86
 
                internal static bool ValidateSdkLocation (FilePath location, out FilePath xcode, out FilePath vplist, out FilePath devroot)
87
 
                {
88
 
                        GetNewPaths (location, out xcode, out vplist, out devroot);
89
 
                        if (ValidatePaths (xcode, vplist, devroot))
90
 
                                return true;
91
 
                        
92
 
                        GetOldPaths (location, out xcode, out vplist, out devroot);
93
 
                        if (ValidatePaths (xcode, vplist, devroot))
94
 
                                return true;
95
 
                        
96
 
                        return false;
97
 
                }
98
 
                
99
 
                internal static void SetConfiguredSdkLocation (FilePath location)
100
 
                {
101
 
                        if (location.IsNullOrEmpty || location == DefaultRoots.First ())
102
 
                                location = null;
103
 
                        if (location == PropertyService.Get<string> (SDK_KEY))
104
 
                                return;
105
 
                        PropertyService.Set (SDK_KEY, location);
106
 
                        if (GetEnvLocation () != null) {
107
 
                                Init ();
108
 
                                Changed ();
109
 
                        }
110
 
                }
111
 
                
112
 
                internal static FilePath GetConfiguredSdkLocation ()
113
 
                {
114
 
                        return PropertyService.Get<string> (SDK_KEY, null);
115
 
                }
116
 
                
117
 
                static void SetInvalid ()
118
 
                {
119
 
                        XcodePath = FilePath.Empty;
120
 
                        DeveloperRoot = FilePath.Empty;
121
 
                        DeveloperRootVersionPlist = FilePath.Empty;
122
 
                        IsValid = false;
123
 
                        DTXcode = null;
124
 
                        IsXcode4 = false;
125
 
                        lastWritten = DateTime.MinValue;
126
 
                }
127
 
                
128
 
                static AppleSdkSettings ()
129
 
                {
130
 
                        MonoDevelop.MacInterop.Cocoa.InitMonoMac ();
131
 
                        Init ();
132
 
                }
133
 
                
134
 
                static void Init ()
135
 
                {
136
 
                        SetInvalid ();
137
 
                        
138
 
                        DeveloperRoot = Environment.GetEnvironmentVariable ("MD_APPLE_SDK_ROOT");
139
 
                        if (DeveloperRoot.IsNullOrEmpty) {
140
 
                                DeveloperRoot = GetConfiguredSdkLocation ();
141
 
                        }
142
 
                        
143
 
                        bool foundSdk = false;
144
 
                        FilePath xcode, vplist, devroot;
145
 
                        
146
 
                        if (DeveloperRoot.IsNullOrEmpty) {
147
 
                                foreach (var v in DefaultRoots)  {
148
 
                                        if (ValidateSdkLocation (v, out xcode, out vplist, out devroot)) {
149
 
                                                foundSdk = true;
150
 
                                                break;
151
 
                                        } else {
152
 
                                                LoggingService.LogDebug ("Apple iOS SDK not found at '{0}'", v);
153
 
                                        }
154
 
                                }
155
 
                        } else {
156
 
                                foundSdk = ValidateSdkLocation (DeveloperRoot, out xcode, out vplist, out devroot);
157
 
                        }
158
 
                        
159
 
                        if (foundSdk) {
160
 
                                XcodePath = xcode;
161
 
                                DeveloperRoot = devroot;
162
 
                                DeveloperRootVersionPlist = vplist;
163
 
                        } else {
164
 
                                SetInvalid ();
165
 
                                return;
166
 
                        }
167
 
 
168
 
                        try {
169
 
                                var plist = XcodePath.Combine ("Contents", "Info.plist");
170
 
                                if (!File.Exists (plist))
171
 
                                        return;
172
 
                                lastWritten = File.GetLastWriteTime (plist);
173
 
                                
174
 
                                // DTXCode was introduced after xcode 3.2.6 so it may not exist
175
 
                                using (var pool = new NSAutoreleasePool ()) {
176
 
                                        var dict = NSDictionary.FromFile (plist);
177
 
                                        NSObject value;
178
 
                                        if (dict.TryGetValue (new NSString ("DTXcode"), out value))
179
 
                                                DTXcode = ((NSString) value).ToString ();
180
 
                                }
181
 
                                IsXcode4 = !string.IsNullOrEmpty (DTXcode) && int.Parse (DTXcode) >= 0400;
182
 
                                IsXcode42 = !string.IsNullOrEmpty (DTXcode) && int.Parse (DTXcode) >= 0420;
183
 
                                IsValid = true;
184
 
                        } catch (Exception ex) {
185
 
                                LoggingService.LogError ("Error loading Xcode information for prefix '" + DeveloperRoot + "'", ex);
186
 
                                SetInvalid ();
187
 
                        }
188
 
                }
189
 
                
190
 
                public static FilePath DeveloperRoot { get; private set; }
191
 
 
192
 
                public static FilePath DeveloperRootVersionPlist {
193
 
                        get; private set;
194
 
                }
195
 
 
196
 
                public static FilePath XcodePath {
197
 
                        get; private set;
198
 
                }
199
 
 
200
 
                public static void CheckChanged ()
201
 
                {
202
 
                        var plist = XcodePath.Combine ("Contents", "Info.plist");
203
 
                        DateTime w = DateTime.MinValue;
204
 
                        if (File.Exists (plist))
205
 
                                w = File.GetLastWriteTime (plist);
206
 
                        if (w != lastWritten) {
207
 
                                Init ();
208
 
                                Changed ();
209
 
                        }
210
 
                }
211
 
                
212
 
                public static bool IsValid { get; private set; }
213
 
                public static string DTXcode { get; private set; }
214
 
                public static bool IsXcode4 { get; private set; }
215
 
                public static bool IsXcode42 { get; private set; }
216
 
                
217
 
                public static event Action Changed;
218
 
        }
219
 
        
220
 
        class AppleSdkAboutInformation : ISystemInformationProvider
221
 
        {
222
 
                public string Description {
223
 
                        get {
224
 
                                var sb = new System.Text.StringBuilder ();
225
 
                                sb.AppendLine ("Apple Developer Tools:");
226
 
                                if (!AppleSdkSettings.IsValid) {
227
 
                                        sb.AppendLine ("\t(Not Found)");
228
 
                                        return sb.ToString ();
229
 
                                }
230
 
                                
231
 
                                using (var pool = new NSAutoreleasePool ()) {
232
 
                                        var dict = NSDictionary.FromFile (AppleSdkSettings.XcodePath.Combine ("Contents", "Info.plist"));
233
 
                                        sb.AppendFormat ("\t Xcode {0} ({1})",
234
 
                                                dict[(NSString)"CFBundleShortVersionString"],
235
 
                                                dict[(NSString)"CFBundleVersion"]);
236
 
                                        sb.AppendLine ();
237
 
                                        
238
 
                                        dict = NSDictionary.FromFile (AppleSdkSettings.DeveloperRootVersionPlist);
239
 
                                        sb.AppendFormat ("\t Build {0}",
240
 
                                                dict[(NSString)"ProductBuildVersion"]);
241
 
                                }
242
 
                                return sb.ToString ();
243
 
                        }
244
 
                }
245
 
        }
246
 
}
 
 
b'\\ No newline at end of file'