⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 solutionconvertertool.cs

📁 全功能c#编译器
💻 CS
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Security;
using System.Security.Permissions;

using System.Windows.Forms;

using MSjogren.GacTool.FusionNative;

using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.ProjectImportExporter.Commands;

namespace ICSharpCode.SharpDevelop.ProjectImportExporter.Converters
{
//	[UIPermissionAttribute(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows, Unrestricted = true)]
	public class SolutionConversionTool
	{
		public ArrayList copiedFiles = new ArrayList();
		
		Hashtable GacReferences = new Hashtable();
		string projectTitle;
		string projectInputDirectory;
		string projectOutputDirectory;
		
		void WriteLine(string str)
		{
			Console.WriteLine(str);
//			TaskService taskService = (TaskService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(TaskService));
//			taskService.CompilerOutput += str + "\n";
//			taskService.NotifyTaskChange();
		}
		
		public SolutionConversionTool(string projectTitle, string projectInputDirectory, string projectOutputDirectory)
		{
			GenerateGacReferences();
			this.projectTitle           = projectTitle;
			this.projectInputDirectory  = projectInputDirectory;
			this.projectOutputDirectory = projectOutputDirectory;
		}
		
		void GenerateGacReferences()
		{
			IApplicationContext applicationContext = null;
			IAssemblyEnum assemblyEnum = null;
			IAssemblyName assemblyName = null;
			
			Fusion.CreateAssemblyEnum(out assemblyEnum, null, null, 2, 0);
				
			while (assemblyEnum.GetNextAssembly(out applicationContext, out assemblyName, 0) == 0) {
				uint nChars = 0;
				assemblyName.GetDisplayName(null, ref nChars, 0);
									
				StringBuilder sb = new StringBuilder((int)nChars);
				assemblyName.GetDisplayName(sb, ref nChars, 0);
				
				string[] info = sb.ToString().Split(',');
				
				string aName    = info[0];
				string aVersion = info[1].Substring(info[1].LastIndexOf('=') + 1);
				GacReferences[aName] = sb.ToString();
			}
		}
		
		static string[] commonAssemblies = new string[] {
			"mscorlib",
			"Accessibility",
			"Microsoft.Vsa",
			"System.Configuration.Install",
			"System.Data",
			"System.Design",
			"System.DirectoryServices",
			"System",
			"System.Drawing.Design",
			"System.Drawing",
			"System.EnterpriseServices",
			"System.Management",
			"System.Messaging",
			"System.Runtime.Remoting",
			"System.Runtime.Serialization.Formatters.Soap",
			"System.Security",
			"System.ServiceProcess",
			"System.Web",
			"System.Web.RegularExpressions",
			"System.Web.Services",
			"System.Windows.Forms",
			"System.XML"
		};
		
		public bool ShouldGenerateReference(bool filter, string assemblyName, string hintPath)
		{
			if (filter) {
				foreach (string reference in commonAssemblies) {
					if (reference.ToUpper() == assemblyName.ToUpper()) {
						return false;
					}
				}
			}
			
			if (hintPath != null && hintPath.Length > 0) {
				string assemblyLocation = Path.Combine(this.projectInputDirectory, hintPath);
				// TODO: Exists needs permission to access the file system, otherwise it always returns false!
				//this.WriteLine(Path.GetFullPath(assemblyLocation)); // this throws a SecurityExceptin
				if (File.Exists(assemblyLocation)) {
					return true;
				}
			}
			
			if (!File.Exists(Path.Combine(this.projectInputDirectory, assemblyName))) {
				if (GacReferences[assemblyName] != null) {
					return true;
				}
			} else {
				return true;
			}
			this.WriteLine("Can't import reference " + assemblyName + " (" + hintPath + ")");
			return false;
		}
		
		public string GenerateReferenceType(string assemblyName, string hintPath)
		{
			if (hintPath != null && hintPath.Length > 0) {
				string assemblyLocation = Path.Combine(this.projectInputDirectory, hintPath);
				if (File.Exists(assemblyLocation)) {
					return "Assembly";
				}
			}
			
			if (!File.Exists(Path.Combine(this.projectInputDirectory, assemblyName))) {
				if (GacReferences[assemblyName] == null) {
					this.WriteLine("Can't find Assembly reference " + assemblyName);
				} else {
					return "Gac";
				}
			} else {
				return "Assembly";
			}
			
			this.WriteLine("Can't determine reference type for " + assemblyName);
			return "Assembly";
		}
		
		public string GenerateReference(string assemblyName, string hintPath)
		{
			if (hintPath != null && hintPath.Length > 0) {
				string assemblyLocation = this.projectInputDirectory + hintPath;
				if (File.Exists(assemblyLocation)) {
					return hintPath;
				}
			}
			
			if (!File.Exists(this.projectInputDirectory + assemblyName)) {
				if (GacReferences[assemblyName] == null) {
					this.WriteLine("Can't find Assembly reference " + assemblyName);
				} else {
					return GacReferences[assemblyName].ToString();
				}
			} else {
				return "." + Path.DirectorySeparatorChar + assemblyName;
			}
			
			this.WriteLine("Created illegal, empty reference (should never happen) remove manually");
			return null;
		}
		
		public string VerifyFileLocation(string itemFile)
		{
			if (itemFile.Length == 0) {
				return String.Empty;
			}
			string itemInputFile = Path.Combine(this.projectInputDirectory, itemFile);
			if (itemInputFile.StartsWith("..")) {
				string correctLocation = this.projectOutputDirectory + Path.DirectorySeparatorChar + 
				                         "MovedFiles" + Path.DirectorySeparatorChar + Path.GetFileName(itemFile);
				
				try {
					if (File.Exists(correctLocation)) {
						File.Delete(correctLocation);
					}
					this.WriteLine("Copy file " + itemInputFile + " to " + correctLocation);
					copiedFiles.Add(new DictionaryEntry(itemInputFile, correctLocation));
				} catch (Exception) {
//					IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
//					messageService.ShowError(e, "Can't copy " + itemInputFile + " to " + correctLocation +"\nCheck for write permission.");
				}
				return "." + correctLocation.Substring(this.projectOutputDirectory.Length);
			}
			copiedFiles.Add(new DictionaryEntry(itemInputFile, this.projectOutputDirectory + Path.DirectorySeparatorChar + itemFile));
			return itemFile.StartsWith(".") ? itemFile : "." + Path.DirectorySeparatorChar + itemFile;
		}
		
		public string EnsureBool(string txt)
		{
			if (txt.ToUpper() == "TRUE") {
				return true.ToString();
			}
			return false.ToString();
		}
		
		public string ImportResource(string resourceFile)
		{
			WriteLine("Import resource " + resourceFile);

			string resourceInputFile = Path.Combine(this.projectInputDirectory, resourceFile);
			string resourceFileExtension = Path.GetExtension(resourceInputFile).ToUpper();
			string resourceOutputFileRelative = resourceFile;
			string resourceOutputFileFull = Path.Combine(this.projectOutputDirectory, resourceOutputFileRelative);
 		
 			if (resourceFileExtension == ".RESX") {
				WriteLine("Convert resource file to .resource format : " + resourceFile);
				resourceOutputFileRelative = Path.ChangeExtension(resourceFile, ".resources");
				resourceOutputFileFull = Path.Combine(this.projectOutputDirectory, resourceOutputFileRelative);
			} else {
				WriteLine("Needed to copy file " + resourceInputFile + " to " + resourceOutputFileFull);
			} 
			copiedFiles.Add(new DictionaryEntry(resourceInputFile, resourceOutputFileFull));
			return (resourceOutputFileRelative.StartsWith(".") ? "" : "." + Path.DirectorySeparatorChar) + resourceOutputFileRelative;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -