Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AddinFileSystemExtension.cs « Mono.Addins.Database « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c812c4a6ebc5f244bac969e3e7cf49d9cedaeca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// 
// AddinFileSystemExtension.cs
//  
// Author:
//       Lluis Sanchez Gual <lluis@novell.com>
// 
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Reflection;

namespace Mono.Addins.Database
{
	/// <summary>
	/// An add-in file system extension.
	/// </summary>
	/// <remarks>
	/// File system extensions can override the behavior of the add-in scanner and provide custom rules for
	/// locating and scanning assemblies.
	/// </remarks>
	[Serializable]
	public class AddinFileSystemExtension
	{
		IAssemblyReflector reflector;
		
		/// <summary>
		/// Called when the add-in scan is about to start
		/// </summary>
		public virtual void ScanStarted ()
		{
		}
		
		/// <summary>
		/// Called when the add-in scan has finished
		/// </summary>
		public virtual void ScanFinished ()
		{
		}
		
		/// <summary>
		/// Checks if a directory exists
		/// </summary>
		/// <returns>
		/// 'true' if the directory exists
		/// </returns>
		/// <param name='path'>
		/// Directory path
		/// </param>
		public virtual bool DirectoryExists (string path)
		{
			return Directory.Exists (path);
		}
		
		/// <summary>
		/// Checks if a file exists
		/// </summary>
		/// <returns>
		/// 'true' if the file exists
		/// </returns>
		/// <param name='path'>
		/// Path to the file
		/// </param>
		public virtual bool FileExists (string path)
		{
			return File.Exists (path);
		}
		
		/// <summary>
		/// Gets the files in a directory
		/// </summary>
		/// <returns>
		/// The full path of the files in the directory
		/// </returns>
		/// <param name='path'>
		/// Directory path
		/// </param>
		public virtual System.Collections.Generic.IEnumerable<string> GetFiles (string path)
		{
			return Directory.EnumerateFiles (path);
		}
		
		/// <summary>
		/// Gets the subdirectories of a directory
		/// </summary>
		/// <returns>
		/// The subdirectories.
		/// </returns>
		/// <param name='path'>
		/// The directory
		/// </param>
		public virtual System.Collections.Generic.IEnumerable<string> GetDirectories (string path)
		{
			return Directory.EnumerateDirectories (path);
		}

		/// <summary>
		/// Gets the last write time of a file
		/// </summary>
		/// <returns>
		/// The last write time.
		/// </returns>
		/// <param name='filePath'>
		/// File path.
		/// </param>
		public virtual DateTime GetLastWriteTime (string filePath)
		{
			return File.GetLastWriteTime (filePath);
		}
		
		/// <summary>
		/// Opens a text file
		/// </summary>
		/// <returns>
		/// The text file stream
		/// </returns>
		/// <param name='path'>
		/// File path.
		/// </param>
		public virtual System.IO.StreamReader OpenTextFile (string path)
		{
			return new StreamReader (path);
		}
		
		/// <summary>
		/// Opens a file.
		/// </summary>
		/// <returns>
		/// The file stream.
		/// </returns>
		/// <param name='path'>
		/// The file path.
		/// </param>
		public virtual System.IO.Stream OpenFile (string path)
		{
			return File.OpenRead (path);
		}
		
		/// <summary>
		/// Gets an assembly reflector for a file.
		/// </summary>
		/// <returns>
		/// The reflector for the file.
		/// </returns>
		/// <param name='locator'>
		/// An assembly locator
		/// </param>
		/// <param name='path'>
		/// A file path
		/// </param>
		public virtual IAssemblyReflector GetReflectorForFile (IAssemblyLocator locator, string path)
		{
			if (reflector != null)
				return reflector;
			
			// If there is a local copy of the cecil reflector, use it instead of the one in the gac
			Type t;
			string asmFile = Path.Combine (Path.GetDirectoryName (GetType().Assembly.Location), "Mono.Addins.CecilReflector.dll");
			if (File.Exists (asmFile)) {
				// Make sure to load the Mono.Cecil next to the cecil reflector
				var cecil = Path.Combine (Path.GetDirectoryName (GetType ().Assembly.Location), "Mono.Cecil.dll");
				if (File.Exists (cecil))
					Assembly.LoadFile (cecil);

				Assembly asm = Assembly.LoadFrom (asmFile);
				t = asm.GetType ("Mono.Addins.CecilReflector.Reflector");
			}
			else {
				string refName = GetType().Assembly.FullName;
				int i = refName.IndexOf (',');
				refName = "Mono.Addins.CecilReflector.Reflector, Mono.Addins.CecilReflector" + refName.Substring (i);
				t = Type.GetType (refName, false);
			}
			if (t != null)
				reflector = (IAssemblyReflector) Activator.CreateInstance (t);
			else
				reflector = new DefaultAssemblyReflector ();
			
			reflector.Initialize (locator);
			return reflector;
		}

		/// <summary>
		/// Deletes a file
		/// </summary>
		/// <param name="filePath">File path.</param>
		public virtual void DeleteFile (string filePath)
		{
			File.Delete (filePath);
		}

		internal void CleanupReflector()
		{
			var disposable = reflector as IDisposable;
			if (disposable != null)
				disposable.Dispose ();
		}
		
		/// <summary>
		/// Gets a value indicating whether this <see cref="Mono.Addins.Database.AddinFileSystemExtension"/> needs to be isolated from the main execution process
		/// </summary>
		/// <value>
		/// <c>true</c> if requires isolation; otherwise, <c>false</c>.
		/// </value>
		public virtual bool RequiresIsolation {
			get { return true; }
		}
	}
}