// // AddinFileSystemExtension.cs // // Author: // Lluis Sanchez Gual // // 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 { /// /// An add-in file system extension. /// /// /// File system extensions can override the behavior of the add-in scanner and provide custom rules for /// locating and scanning assemblies. /// [Serializable] public class AddinFileSystemExtension { IAssemblyReflector reflector; /// /// Called when the add-in scan is about to start /// public virtual void ScanStarted () { } /// /// Called when the add-in scan has finished /// public virtual void ScanFinished () { } /// /// Checks if a directory exists /// /// /// 'true' if the directory exists /// /// /// Directory path /// public virtual bool DirectoryExists (string path) { return Directory.Exists (path); } /// /// Checks if a file exists /// /// /// 'true' if the file exists /// /// /// Path to the file /// public virtual bool FileExists (string path) { return File.Exists (path); } /// /// Gets the files in a directory /// /// /// The full path of the files in the directory /// /// /// Directory path /// public virtual System.Collections.Generic.IEnumerable GetFiles (string path) { return Directory.EnumerateFiles (path); } /// /// Gets the subdirectories of a directory /// /// /// The subdirectories. /// /// /// The directory /// public virtual System.Collections.Generic.IEnumerable GetDirectories (string path) { return Directory.EnumerateDirectories (path); } /// /// Gets the last write time of a file /// /// /// The last write time. /// /// /// File path. /// public virtual DateTime GetLastWriteTime (string filePath) { return File.GetLastWriteTime (filePath); } /// /// Opens a text file /// /// /// The text file stream /// /// /// File path. /// public virtual System.IO.StreamReader OpenTextFile (string path) { return new StreamReader (path); } /// /// Opens a file. /// /// /// The file stream. /// /// /// The file path. /// public virtual System.IO.Stream OpenFile (string path) { return File.OpenRead (path); } /// /// Gets an assembly reflector for a file. /// /// /// The reflector for the file. /// /// /// An assembly locator /// /// /// A file path /// 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; } /// /// Deletes a file /// /// File path. public virtual void DeleteFile (string filePath) { File.Delete (filePath); } internal void CleanupReflector() { var disposable = reflector as IDisposable; if (disposable != null) disposable.Dispose (); } /// /// Gets a value indicating whether this needs to be isolated from the main execution process /// /// /// true if requires isolation; otherwise, false. /// public virtual bool RequiresIsolation { get { return true; } } } }