7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using System.Collections;
33
using System.Reflection;
34
using Mono.Addins.Description;
35
using Mono.Addins.Serialization;
37
namespace Mono.Addins.Database
41
public static bool IsWindows {
42
get { return Path.DirectorySeparatorChar == '\\'; }
45
public static void CheckWrittableFloder (string path)
47
string testFile = null;
50
testFile = Path.Combine (path, new Random ().Next ().ToString ());
52
} while (File.Exists (testFile) && n < 100);
54
throw new InvalidOperationException ("Could not create file in directory: " + path);
56
StreamWriter w = new StreamWriter (testFile);
58
File.Delete (testFile);
61
public static void AddDependencies (AddinDescription desc, AddinScanResult scanResult)
63
// Not implemented in AddinScanResult to avoid making AddinDescription remotable
64
foreach (ModuleDescription mod in desc.AllModules) {
65
foreach (Dependency dep in mod.Dependencies) {
66
AddinDependency adep = dep as AddinDependency;
67
if (adep == null) continue;
68
string depid = Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version);
69
scanResult.AddAddinToUpdateRelations (depid);
74
public static Assembly LoadAssemblyForReflection (string fileName)
76
/* if (!gotLoadMethod) {
77
reflectionOnlyLoadFrom = typeof(Assembly).GetMethod ("ReflectionOnlyLoadFrom");
79
LoadAssemblyForReflection (typeof(Util).Assembly.Location);
82
if (reflectionOnlyLoadFrom != null)
83
return (Assembly) reflectionOnlyLoadFrom.Invoke (null, new string [] { fileName });
85
*/ return Assembly.LoadFile (fileName);
88
// Works like Path.GetFullPath, but it does not require the path to exist
89
public static string GetFullPath (string path)
92
throw new ArgumentNullException ("path");
94
if (!Path.IsPathRooted (path))
95
path = Path.Combine (Environment.CurrentDirectory, path);
97
string root = Path.GetPathRoot (path);
98
path = path.Substring (root.Length);
100
string[] parts = path.Split (Path.DirectorySeparatorChar);
101
string[] newParts = new string [parts.Length];
103
for (int n=0; n<parts.Length; n++) {
104
string p = parts [n];
105
if (p == null || p.Length == 0 || p == ".")
114
return root + string.Join (new string (Path.DirectorySeparatorChar, 1), newParts, 0, i);
117
public static int GetStringHashCode (string s)
121
for (; n < s.Length - 1; n+=2) {
122
h = (h << 5) - h + s[n];
123
h = (h << 5) - h + s[n+1];
126
h = (h << 5) - h + s[n];
130
public static string GetGacPath (string fullName)
132
string gacDir = typeof(Uri).Assembly.Location;
133
gacDir = Path.GetDirectoryName (gacDir);
134
gacDir = Path.GetDirectoryName (gacDir);
135
gacDir = Path.GetDirectoryName (gacDir);
137
string[] parts = fullName.Split (',');
138
if (parts.Length != 4) return null;
139
string name = parts[0].Trim ();
141
int i = parts[1].IndexOf ('=');
142
string version = i != -1 ? parts[1].Substring (i+1).Trim () : parts[1].Trim ();
144
i = parts[2].IndexOf ('=');
145
string culture = i != -1 ? parts[2].Substring (i+1).Trim () : parts[2].Trim ();
146
if (culture == "neutral") culture = "";
148
i = parts[3].IndexOf ('=');
149
string token = i != -1 ? parts[3].Substring (i+1).Trim () : parts[3].Trim ();
151
string file = Path.Combine (gacDir, name);
152
file = Path.Combine (file, version + "_" + culture + "_" + token);