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

📄 abstractproject.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 3 页
字号:
					}
				}
			}
		}
		
		public virtual void SaveProject(string fileName)
		{
			basedirectory = Path.GetDirectoryName(fileName);
			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<Project/>");

			SetXmlAttributes(doc, doc.DocumentElement, this);
			
			// set version attribute to the root node
			XmlAttribute versionAttribute = doc.CreateAttribute("version");
			versionAttribute.InnerText    = currentProjectFileVersion;
			doc.DocumentElement.Attributes.Append(versionAttribute);
			
			
			// set projecttype attribute to the root node
			XmlAttribute projecttypeAttribute = doc.CreateAttribute("projecttype");
			projecttypeAttribute.InnerText    = ProjectType;
			doc.DocumentElement.Attributes.Append(projecttypeAttribute);
			
			// create the configuration nodes
			// I choosed to add the configuration nodes 'per hand' instead of using the automated
			// version, because it is more cleaner for the language binding implementors to just 
			// creating a factory method for their configurations.
			XmlElement configurationElement = doc.CreateElement("Configurations");
			XmlAttribute activeConfigAttribute = doc.CreateAttribute("active");
			activeConfigAttribute.InnerText = ActiveConfiguration == null ? String.Empty : ActiveConfiguration.Name;
			configurationElement.Attributes.Append(activeConfigAttribute);
			
			foreach (IConfiguration configuration in Configurations) {
				XmlElement newConfig = doc.CreateElement(configurationNodeName);
				SetXmlAttributes(doc, newConfig, configuration);
				configurationElement.AppendChild(newConfig);
			}
			
			doc.DocumentElement.AppendChild(configurationElement);
			
			FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
			IResourceService resourceService = (IResourceService)ServiceManager.Services.GetService(typeof(IResourceService));
			fileUtilityService.ObservedSave(new NamedFileOperationDelegate(doc.Save), 
			                                fileName, 
			                                resourceService.GetString("Internal.Project.Project.CantSaveProjectErrorText"),
			                                FileErrorPolicy.ProvideAlternative);
		}
		
		public virtual string GetParseableFileContent(string fileName)
		{
			StreamReader sr = File.OpenText(fileName);
			string content = sr.ReadToEnd();
			sr.Close();
			return content;
		}
		
		public void SaveProjectAs()
		{
			SaveFileDialog fdiag = new SaveFileDialog();
			fdiag.OverwritePrompt = true;
			fdiag.AddExtension    = true;

			fdiag.Filter          = String.Join("|", (string[])(AddInTreeSingleton.AddInTree.GetTreeNode("/SharpDevelop/Workbench/Combine/FileFilter").BuildChildItems(this)).ToArray(typeof(string)));

			if (fdiag.ShowDialog() == DialogResult.OK) {
				string filename = fdiag.FileName;
				SaveProject(filename);
				IResourceService resourceService = (IResourceService)ServiceManager.Services.GetService(typeof(IResourceService));
				IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
				messageService.ShowMessage(filename, resourceService.GetString("Internal.Project.DefaultProject.ProjectSavedMessage"));
			}
		}
		
		// i really hate code duplication, see AssemblyInformation
		// After .NET 2.0 we need a clean, application domain based assembly loading mechanism!!!
		
		byte[] GetBytes(string fileName)
		{
			FileStream fs = System.IO.File.OpenRead(fileName);
			long size = fs.Length;
			byte[] outArray = new byte[size];
			fs.Read(outArray, 0, (int)size);
			fs.Close();
			return outArray;
		}
		
		string loadingPath = String.Empty;
		Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
		{
			string file = args.Name;
			int idx = file.IndexOf(',');
			if (idx >= 0) {
				file = file.Substring(0, idx);
			}
			try {
				if (File.Exists(loadingPath + file + ".exe")) {
					return Assembly.Load(GetBytes(loadingPath + file + ".exe"));
				} 
				if (File.Exists(loadingPath + file + ".dll")) {
					return Assembly.Load(GetBytes(loadingPath + file + ".dll"));
				} 
			} catch (Exception ex) {
				Console.WriteLine("Can't load assembly : " + ex.ToString());
			}
			return null;
		}
		
		void CopyAssemblyWithReferencesToPath(string referenceFileName, string destination, bool force)
		{
			try {
				string destinationFileName = fileUtilityService.GetDirectoryNameWithSeparator(destination) + Path.GetFileName(referenceFileName);
				try {
					if (destinationFileName != referenceFileName) {
						File.Copy(referenceFileName, destinationFileName, true);
						if (File.Exists(Path.ChangeExtension(referenceFileName, ".pdb"))) {
							File.Copy(Path.ChangeExtension(referenceFileName, ".pdb"), Path.ChangeExtension(destinationFileName, ".pdb"), true);
						}
					}
				} catch (Exception e) {
					Console.WriteLine("Can't copy reference file from {0} to {1} reason {2}", referenceFileName, destinationFileName, e);
				}
				
				string referencePath = Path.GetDirectoryName(referenceFileName).ToLower();
				Assembly asm = null;
				try {
					AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
					loadingPath = Path.GetDirectoryName(referenceFileName) + Path.DirectorySeparatorChar;
					asm = Assembly.Load(GetBytes(referenceFileName));
				} finally {
					AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(MyResolveEventHandler);
				}						
				if (asm != null) {
					AssemblyName[] referenceNames = asm.GetReferencedAssemblies();
					foreach (AssemblyName name in referenceNames) {
						string fileName = Path.Combine(referencePath, name.Name + ".dll");
						if (!File.Exists(fileName)) {
							fileName = Path.Combine(referencePath, name.Name + ".exe");
						}
						if (File.Exists(fileName)) {
							CopyAssemblyWithReferencesToPath(fileName, destination, force);
						}
					}
				}
			} catch (Exception e) {
				Console.WriteLine("Exception while copying references : " + e.ToString());
			}
		}
		
		public void CopyReferencesToPath(string destination, bool force)
		{
			foreach (ProjectReference projectReference in ProjectReferences) {
				if ((projectReference.LocalCopy || force) && projectReference.ReferenceType != ReferenceType.Gac) {
					string referenceFileName   = projectReference.GetReferencedFileName(this);
					string destinationFileName = fileUtilityService.GetDirectoryNameWithSeparator(destination) + Path.GetFileName(referenceFileName);
					
					// copy references from referenced projects. (needed, no cyclic references (shouldn't be possible) -> this works)
					if (projectReference.ReferenceType == ReferenceType.Project) {
						IProjectService projectService = (IProjectService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IProjectService));
						IProject project = projectService.GetProject(projectReference.Reference);
						if (project != null) {
							project.CopyReferencesToPath(destination, force);
						}
					}
					
					if (projectReference.ReferenceType == ReferenceType.Assembly) {
						CopyAssemblyWithReferencesToPath(referenceFileName, destination, force);
					} else {
						try {
							if (File.Exists(destinationFileName)) {
								if (File.GetLastWriteTime(destinationFileName).ToString() != File.GetLastWriteTime(referenceFileName).ToString()) {
									File.Copy(referenceFileName, destinationFileName, true);
									if (File.Exists(Path.ChangeExtension(referenceFileName, ".pdb"))) {
										File.Copy(Path.ChangeExtension(referenceFileName, ".pdb"), Path.ChangeExtension(destinationFileName, ".pdb"), true);
									}
								}
							} else {
								File.Copy(referenceFileName, destinationFileName, true);
								if (File.Exists(Path.ChangeExtension(referenceFileName, ".pdb"))) {
									File.Copy(Path.ChangeExtension(referenceFileName, ".pdb"), Path.ChangeExtension(destinationFileName, ".pdb"), true);
								}
							}
						} catch (Exception e) {
							Console.WriteLine("Can't copy reference file from {0} to {1} reason {2}", referenceFileName, destinationFileName, e);
						}
					}
				}
			}
		}
		
		public void CopyReferencesToOutputPath(bool force)
		{
			AbstractProjectConfiguration config = ActiveConfiguration as AbstractProjectConfiguration;
			if (config == null) {
				return;
			}
			CopyReferencesToPath(config.OutputDirectory, force);
		}
		
		public virtual void Dispose()
		{
		}
		
		public abstract IConfiguration CreateConfiguration();
		
		public virtual  IConfiguration CreateConfiguration(string name)
		{
			IConfiguration config = CreateConfiguration();
			config.Name = name;
			
			return config;
		}
		
		protected virtual void OnNameChanged(EventArgs e)
		{
			if (NameChanged != null) {
				NameChanged(this, e);
			}
		}
		
		public event EventHandler NameChanged;
	}
	
	public class ProjectActiveConfigurationTypeConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType)
		{
			return true;
		}
		
		public override  bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return true;
		}
		
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,  object value)
		{
			IProject project = (IProject)context.Instance;
			foreach (IConfiguration configuration in project.Configurations) {
				if (configuration.Name == value.ToString()) {
					return configuration;
				}
			}
			return null;
		}
		
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			IConfiguration config = value as IConfiguration;
			System.Diagnostics.Debug.Assert(config != null, String.Format("Tried to convert {0} to IConfiguration", config));
			if (config != null) {
				return config.Name;
			}
			return String.Empty;
		}
		
		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}
		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return true;
		}
		
		public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
		{
			return new TypeConverter.StandardValuesCollection(((IProject)context.Instance).Configurations);
		}
		
	}
}

⌨️ 快捷键说明

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