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

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Database/Util.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
// Util.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Collections;
 
32
using System.IO;
 
33
using System.Reflection;
 
34
using Mono.Addins.Description;
 
35
using Mono.Addins.Serialization;
 
36
using System.Collections.Generic;
 
37
 
 
38
namespace Mono.Addins.Database
 
39
{
 
40
        internal class Util
 
41
        {
 
42
                static int isMono;
 
43
                static string monoVersion;
 
44
                
 
45
                public static bool IsWindows {
 
46
                        get { return Path.DirectorySeparatorChar == '\\'; }
 
47
                }
 
48
                
 
49
                public static bool IsMono {
 
50
                        get {
 
51
                                if (isMono == 0)
 
52
                                        isMono = Type.GetType ("Mono.Runtime") != null ? 1 : -1;
 
53
                                return isMono == 1;
 
54
                        }
 
55
                }
 
56
                
 
57
                public static string MonoVersion {
 
58
                        get {
 
59
                                if (monoVersion == null) {
 
60
                                        if (!IsMono)
 
61
                                                throw new InvalidOperationException ();
 
62
                                        MethodInfo mi = Type.GetType ("Mono.Runtime").GetMethod ("GetDisplayName", BindingFlags.NonPublic|BindingFlags.Static);
 
63
                                        if (mi != null)
 
64
                                                monoVersion = (string) mi.Invoke (null, null);
 
65
                                        else
 
66
                                                monoVersion = string.Empty;
 
67
                                }
 
68
                                return monoVersion;
 
69
                        }
 
70
                }
 
71
                        
 
72
                public static void CheckWrittableFloder (string path)
 
73
                {
 
74
                        string testFile = null;
 
75
                        int n = 0;
 
76
                        do {
 
77
                                testFile = Path.Combine (path, new Random ().Next ().ToString ());
 
78
                                n++;
 
79
                        } while (File.Exists (testFile) && n < 100);
 
80
                        if (n == 100)
 
81
                                throw new InvalidOperationException ("Could not create file in directory: " + path);
 
82
                        
 
83
                        StreamWriter w = new StreamWriter (testFile);
 
84
                        w.Close ();
 
85
                        File.Delete (testFile);
 
86
                }
 
87
                
 
88
                public static void AddDependencies (AddinDescription desc, AddinScanResult scanResult)
 
89
                {
 
90
                        // Not implemented in AddinScanResult to avoid making AddinDescription remotable
 
91
                        foreach (ModuleDescription mod in desc.AllModules) {
 
92
                                foreach (Dependency dep in mod.Dependencies) {
 
93
                                        AddinDependency adep = dep as AddinDependency;
 
94
                                        if (adep == null) continue;
 
95
                                        string depid = Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version);
 
96
                                        scanResult.AddAddinToUpdateRelations (depid);
 
97
                                }
 
98
                        }
 
99
                }
 
100
                
 
101
                public static Assembly LoadAssemblyForReflection (string fileName)
 
102
                {
 
103
/*                      if (!gotLoadMethod) {
 
104
                                reflectionOnlyLoadFrom = typeof(Assembly).GetMethod ("ReflectionOnlyLoadFrom");
 
105
                                gotLoadMethod = true;
 
106
                                LoadAssemblyForReflection (typeof(Util).Assembly.Location);
 
107
                        }
 
108
                        
 
109
                        if (reflectionOnlyLoadFrom != null)
 
110
                                return (Assembly) reflectionOnlyLoadFrom.Invoke (null, new string [] { fileName });
 
111
                        else
 
112
*/                              return Assembly.LoadFile (fileName);
 
113
                }
 
114
                
 
115
                public static string NormalizePath (string path)
 
116
                {
 
117
                        if (path.Length > 2 && path [0] == '[') {
 
118
                                int i = path.IndexOf (']', 1);
 
119
                                if (i != -1) {
 
120
                                        try {
 
121
                                                string fname = path.Substring (1, i - 1);
 
122
                                                Environment.SpecialFolder sf = (Environment.SpecialFolder) Enum.Parse (typeof(Environment.SpecialFolder), fname, true);
 
123
                                                path = Environment.GetFolderPath (sf) + path.Substring (i + 1);
 
124
                                        } catch {
 
125
                                                // Ignore
 
126
                                        }
 
127
                                }
 
128
                        }
 
129
                        if (IsWindows)
 
130
                                return path.Replace ('/','\\');
 
131
                        else
 
132
                                return path.Replace ('\\','/');
 
133
                }
 
134
                
 
135
                // A private hash calculation method is used to be able to get consistent
 
136
                // results across different .NET versions and implementations.
 
137
                public static int GetStringHashCode (string s)
 
138
                {
 
139
                        int h = 0;
 
140
                        int n = 0;
 
141
                        for (; n < s.Length - 1; n+=2) {
 
142
                                h = unchecked ((h << 5) - h + s[n]);
 
143
                                h = unchecked ((h << 5) - h + s[n+1]);
 
144
                        }
 
145
                        if (n < s.Length)
 
146
                                h = unchecked ((h << 5) - h + s[n]);
 
147
                        return h;
 
148
                }
 
149
                
 
150
                public static string GetGacPath (string fullName)
 
151
                {
 
152
                        string[] parts = fullName.Split (',');
 
153
                        if (parts.Length != 4) return null;
 
154
                        string name = parts[0].Trim ();
 
155
                        
 
156
                        int i = parts[1].IndexOf ('=');
 
157
                        string version = i != -1 ? parts[1].Substring (i+1).Trim () : parts[1].Trim ();
 
158
                        
 
159
                        i = parts[2].IndexOf ('=');
 
160
                        string culture = i != -1 ? parts[2].Substring (i+1).Trim () : parts[2].Trim ();
 
161
                        if (culture == "neutral") culture = "";
 
162
                        
 
163
                        i = parts[3].IndexOf ('=');
 
164
                        string token = i != -1 ? parts[3].Substring (i+1).Trim () : parts[3].Trim ();
 
165
 
 
166
                        string versionDirName = version + "_" + culture + "_" + token;
 
167
                        
 
168
                        if (Util.IsMono) {
 
169
                                string gacDir = typeof(Uri).Assembly.Location;
 
170
                                gacDir = Path.GetDirectoryName (gacDir);
 
171
                                gacDir = Path.GetDirectoryName (gacDir);
 
172
                                gacDir = Path.GetDirectoryName (gacDir);
 
173
                                string dir = Path.Combine (gacDir, name);
 
174
                                return Path.Combine (dir, versionDirName);
 
175
                        } else {
 
176
                                // .NET 4.0 introduces a new GAC directory structure and location.
 
177
                                // Assembly version directory names are now prefixed with the CLR version
 
178
                                // Since there can be different assembly versions for different target CLR runtimes,
 
179
                                // we now look for the best match, that is, the assembly with the higher CLR version
 
180
                                
 
181
                                var currentVersion = new Version (Environment.Version.Major, Environment.Version.Minor);
 
182
                                
 
183
                                foreach (var gacDir in GetDotNetGacDirectories ()) {
 
184
                                        var asmDir = Path.Combine (gacDir, name);
 
185
                                        if (!Directory.Exists (asmDir))
 
186
                                                continue;
 
187
                                        Version bestVersion = new Version (0, 0);
 
188
                                        string bestDir = null;
 
189
                                        foreach (var dir in Directory.GetDirectories (asmDir, "v*_" + versionDirName)) {
 
190
                                                var dirName = Path.GetFileName (dir);
 
191
                                                i = dirName.IndexOf ('_');
 
192
                                                Version av;
 
193
                                                if (Version.TryParse (dirName.Substring (1, i - 1), out av)) {
 
194
                                                        if (av == currentVersion)
 
195
                                                                return dir;
 
196
                                                        else if (av < currentVersion && av > bestVersion) {
 
197
                                                                bestDir = dir;
 
198
                                                                bestVersion = av;
 
199
                                                        }
 
200
                                                }
 
201
                                        }
 
202
                                        if (bestDir != null)
 
203
                                                return bestDir;
 
204
                                }
 
205
                                
 
206
                                // Look in the old GAC. There are no CLR prefixes here
 
207
                                
 
208
                                foreach (var gacDir in GetLegacyDotNetGacDirectories ()) {
 
209
                                        var asmDir = Path.Combine (gacDir, name);
 
210
                                        asmDir = Path.Combine (asmDir, versionDirName);
 
211
                                        if (Directory.Exists (asmDir))
 
212
                                                return asmDir;
 
213
                                }
 
214
                                return null;
 
215
                        }
 
216
                }
 
217
 
 
218
                static IEnumerable<string> GetLegacyDotNetGacDirectories ()
 
219
                {
 
220
                        var winDir = Path.GetFullPath (Environment.SystemDirectory + "\\..");
 
221
 
 
222
                        string gacDir = winDir + "\\assembly\\GAC";
 
223
                        if (Directory.Exists (gacDir))
 
224
                                yield return gacDir;
 
225
                        if (Directory.Exists (gacDir + "_32"))
 
226
                                yield return gacDir + "_32";
 
227
                        if (Directory.Exists (gacDir + "_64"))
 
228
                                yield return gacDir + "_64";
 
229
                        if (Directory.Exists (gacDir + "_MSIL"))
 
230
                                yield return gacDir + "_MSIL";
 
231
                }
 
232
 
 
233
                static IEnumerable<string> GetDotNetGacDirectories ()
 
234
                {
 
235
                        var winDir = Path.GetFullPath (Environment.SystemDirectory + "\\..");
 
236
                        
 
237
                        string gacDir = winDir + "\\Microsoft.NET\\assembly\\GAC";
 
238
                        if (Directory.Exists (gacDir))
 
239
                                yield return gacDir;
 
240
                        if (Directory.Exists (gacDir + "_32"))
 
241
                                yield return gacDir + "_32";
 
242
                        if (Directory.Exists (gacDir + "_64"))
 
243
                                yield return gacDir + "_64";
 
244
                        if (Directory.Exists (gacDir + "_MSIL"))
 
245
                                yield return gacDir + "_MSIL";
 
246
                }
 
247
        }
 
248
}