// // CompiledAssemblyProject.cs // // Author: // Lluis Sanchez Gual // // Copyright (c) 2010 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 MonoDevelop.Core; using MonoDevelop.Core.Assemblies; using Mono.Cecil; using MonoDevelop.Core.Execution; using MonoDevelop.Core.ProgressMonitoring; using System.Collections.Generic; using Mono.Cecil.Mdb; using Mono.Cecil.Cil; namespace MonoDevelop.Projects { public class CompiledAssemblyProject: Project, IAssemblyProject { TargetFramework targetFramework; public CompiledAssemblyProject () { AddNewConfiguration ("Default"); } public override IEnumerable GetProjectTypes () { yield return "CompiledAssembly"; } public override IconId StockIcon { get { return "md-assembly-project"; } } public override SolutionItemConfiguration CreateConfiguration (string name) { return new ProjectConfiguration (name); } public TargetFramework TargetFramework { get { return targetFramework; } } public MonoDevelop.Core.Assemblies.TargetRuntime TargetRuntime { get { return Runtime.SystemAssemblyService.DefaultRuntime; } } public void LoadFrom (FilePath assemblyPath) { FileName = assemblyPath; var tid = Runtime.SystemAssemblyService.GetTargetFrameworkForAssembly (Runtime.SystemAssemblyService.DefaultRuntime, assemblyPath); if (tid != null) targetFramework = Runtime.SystemAssemblyService.GetTargetFramework (tid); AssemblyDefinition adef = AssemblyDefinition.ReadAssembly (assemblyPath); MdbReaderProvider mdbProvider = new MdbReaderProvider (); try { ISymbolReader reader = mdbProvider.GetSymbolReader (adef.MainModule, assemblyPath); adef.MainModule.ReadSymbols (reader); } catch { // Ignore } var files = new HashSet (); foreach (TypeDefinition type in adef.MainModule.Types) { foreach (MethodDefinition met in type.Methods) { if (met.HasBody && met.Body.Instructions != null && met.Body.Instructions.Count > 0) { SequencePoint sp = met.Body.Instructions[0].SequencePoint; if (sp != null) files.Add (sp.Document.Url); } } } FilePath rootPath = FilePath.Empty; foreach (FilePath file in files) { AddFile (file, BuildAction.Compile); if (rootPath.IsNullOrEmpty) rootPath = file.ParentDirectory; else if (!file.IsChildPathOf (rootPath)) rootPath = FindCommonRoot (rootPath, file); } if (!rootPath.IsNullOrEmpty) BaseDirectory = rootPath; /* foreach (AssemblyNameReference aref in adef.MainModule.AssemblyReferences) { if (aref.Name == "mscorlib") continue; string asm = assemblyPath.ParentDirectory.Combine (aref.Name); if (File.Exists (asm + ".dll")) References.Add (new ProjectReference (ReferenceType.Assembly, asm + ".dll")); else if (File.Exists (asm + ".exe")) References.Add (new ProjectReference (ReferenceType.Assembly, asm + ".exe")); else References.Add (new ProjectReference (ReferenceType.Package, aref.FullName)); }*/ } FilePath FindCommonRoot (FilePath p1, FilePath p2) { string[] s1 = p1.ToString ().Split (Path.DirectorySeparatorChar); string[] s2 = p2.ToString ().Split (Path.DirectorySeparatorChar); int n; for (n=0; n (configuration.EnvironmentVariables); return cmd; } } public class CompiledAssemblyExtension: ProjectServiceExtension { public override bool IsSolutionItemFile (string fileName) { if (fileName.ToLower().EndsWith (".exe") || fileName.ToLower().EndsWith (".dll")) return true; return base.IsSolutionItemFile (fileName); } protected override SolutionEntityItem LoadSolutionItem (IProgressMonitor monitor, string fileName) { if (fileName.ToLower().EndsWith (".exe") || fileName.ToLower().EndsWith (".dll")) { CompiledAssemblyProject p = new CompiledAssemblyProject (); p.LoadFrom (fileName); return p; } return base.LoadSolutionItem (monitor, fileName); } public override void Save (IProgressMonitor monitor, SolutionEntityItem item) { // if (item is CompiledAssemblyProject) // return; base.Save (monitor, item); } } }