📄 combine.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Kr黦er" email="mike@icsharpcode.net"/>
// <version value="$version"/>
// </file>
using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Diagnostics;
using System.CodeDom.Compiler;
using System.ComponentModel;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.Core.Properties;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Components;
using System.Windows.Forms;
namespace ICSharpCode.SharpDevelop.Internal.Project
{
public class Combine : LocalizedObject, IDisposable
{
string name = null;
string description = null;
/// <summary>
/// name of the project to startup in singlestartup mode.
/// </summary>
string startProject = null;
bool singleStartup = true;
ArrayList entries = new ArrayList();
CombineConfiguration activeConfiguration;
Hashtable configurations = new Hashtable();
ArrayList combineExecuteDefinitions = new ArrayList();
IResourceService resourceService = (IResourceService)ServiceManager.Services.GetService(typeof(IResourceService));
[LocalizedProperty("${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.ActiveConfiguration}",
Description = "${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.ActiveConfiguration.Description}")]
[TypeConverter(typeof(CombineActiveConfigurationTypeConverter))]
public CombineConfiguration ActiveConfiguration {
get {
return activeConfiguration;
}
set {
activeConfiguration = value;
}
}
[Browsable(false)]
public Hashtable Configurations {
get {
return configurations;
}
}
[Browsable(false)]
public ArrayList CombineExecuteDefinitions {
get {
return combineExecuteDefinitions;
}
}
[Browsable(false)]
public ArrayList Entries {
get {
return entries;
}
}
[Browsable(false)]
public string SingleStartProjectName {
get {
return startProject;
}
set {
startProject = value;
OnStartupPropertyChanged(null);
}
}
[Browsable(false)]
public bool SingleStartupProject {
get {
return singleStartup;
}
set {
singleStartup = value;
OnStartupPropertyChanged(null);
}
}
[LocalizedProperty("${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.Name}",
Description ="${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.Name.Description}")]
public string Name {
get {
return name;
}
set {
if (name != value && value != null && value.Length > 0) {
name = value;
OnNameChanged(EventArgs.Empty);
}
}
}
[LocalizedProperty("${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.Description}",
Description ="${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.Description.Description}")]
public string Description {
get {
return description;
}
set {
description = value;
}
}
[LocalizedProperty("${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.NeedsBuilding}",
Description ="${res:ICSharpCode.SharpDevelop.Internal.Project.Combine.NeedsBuilding.Description}")]
public bool NeedsBuilding {
get {
ArrayList projects = new ArrayList();
GetAllProjects(projects, this);
foreach (ProjectCombineEntry projectEntry in projects) {
if (projectEntry.IsDirty) {
return true;
}
}
return false;
}
}
public void Dispose()
{
if (entries != null) {
foreach (object o in entries) {
if (o is IDisposable) {
((IDisposable)o).Dispose();
}
}
}
}
public Combine()
{
}
public Combine(string filename)
{
LoadCombine(filename);
}
public IProject LoadProject(string filename)
{
LanguageBindingService languageBindingService = (LanguageBindingService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(LanguageBindingService));
StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
ILanguageBinding binding = languageBindingService.GetBindingPerProjectFile(filename);
if (binding == null) {
IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
messageService.ShowError(stringParserService.Parse("${res:Internal.Project.Combine.CantFindLanguageBindingError}", new string[,] {{"FILENAME", filename}}));
return null;
}
IProject project = binding.CreateProject(null, null);
project.LoadProject(filename);
return project;
}
public void LoadCombine(string filename)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
string path = Path.GetDirectoryName(filename);
XmlElement root = doc.DocumentElement;
name = root.Attributes["name"].InnerText;
description = root.Attributes["description"].InnerText;
startProject = root["StartMode"].Attributes["startupentry"].InnerText;
singleStartup = Boolean.Parse(root["StartMode"].Attributes["single"].InnerText);
XmlNodeList nodes = root["Entries"].ChildNodes;
entries.Clear();
FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
foreach (XmlElement el in nodes) {
string fileName = el.Attributes["filename"].InnerText;
fileName = fileName.Replace('\\', Path.DirectorySeparatorChar);
fileName = fileName.Replace('/', Path.DirectorySeparatorChar);
string abs_path = fileUtilityService.RelativeToAbsolutePath(path, fileName);
AddEntry(abs_path);
}
nodes = root["StartMode"].ChildNodes;
combineExecuteDefinitions.Clear();
foreach (XmlElement el in nodes) {
if (el.Name == "Execute") {
CombineExecuteDefinition ced = new CombineExecuteDefinition();
ced.Entry = GetEntry(el.Attributes["entry"].InnerText);
ced.Type = (EntryExecuteType)Enum.Parse(typeof(EntryExecuteType), el.Attributes["type"].InnerText);
combineExecuteDefinitions.Add(ced);
}
}
nodes = root["Configurations"].ChildNodes;
configurations.Clear();
foreach (XmlElement el in nodes) {
CombineConfiguration cconf = new CombineConfiguration(el, this);
configurations[cconf.Name] = cconf;
// set the active configuration, either to the first (if the active attribute is not set)
// or to the active configuration specified by the active attribute.
if ((doc.DocumentElement["Configurations"].Attributes["active"] == null) || cconf.Name == doc.DocumentElement["Configurations"].Attributes["active"].InnerText) { // ok, I know that && has a higher priority than ||, but many programmers think that a bracket is easier to read ... one thing I don't find easy to read are long lines :)
ActiveConfiguration = cconf;
}
}
}
public void SaveCombine(string filename)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Combine fileversion=\"1.0\"/>");
XmlAttribute combineNameAttribute = doc.CreateAttribute("name");
combineNameAttribute.InnerText = name;
doc.DocumentElement.Attributes.Append(combineNameAttribute);
XmlAttribute combineDescriptionAttribute = doc.CreateAttribute("description");
combineDescriptionAttribute.InnerText = description;
doc.DocumentElement.Attributes.Append(combineDescriptionAttribute);
string path = Path.GetDirectoryName(filename);
XmlElement startupnode = doc.CreateElement("StartMode");
XmlAttribute single = doc.CreateAttribute("startupentry");
single.InnerText = startProject;
startupnode.Attributes.Append(single);
XmlAttribute activeconf = doc.CreateAttribute("single");
activeconf.InnerText = singleStartup.ToString();
startupnode.Attributes.Append(activeconf);
foreach (CombineExecuteDefinition ced in combineExecuteDefinitions) {
XmlElement el = doc.CreateElement("Execute");
XmlAttribute a1 = doc.CreateAttribute("entry");
CombineEntry centry = ced.Entry;
if (centry == null || centry.Entry == null) {
continue;
}
if (centry.Entry is IProject) {
a1.InnerText = ((IProject)centry.Entry).Name;
} else {
a1.InnerText = ((Combine)centry.Entry).Name;
}
el.Attributes.Append(a1);
XmlAttribute a2 = doc.CreateAttribute("type");
a2.InnerText = ced.Type.ToString();
el.Attributes.Append(a2);
startupnode.AppendChild(el);
}
doc.DocumentElement.AppendChild(startupnode);
XmlElement projectsnode = doc.CreateElement("Entries");
FileUtilityService fileUtilityService = (FileUtilityService)ServiceManager.Services.GetService(typeof(FileUtilityService));
foreach (CombineEntry entry in entries) {
XmlElement el = doc.CreateElement("Entry");
XmlAttribute entrynameattr = doc.CreateAttribute("filename");
entrynameattr.InnerText = fileUtilityService.AbsoluteToRelativePath(path, entry.Filename);
el.Attributes.Append(entrynameattr);
projectsnode.AppendChild(el);
}
doc.DocumentElement.AppendChild(projectsnode);
XmlElement confnode = doc.CreateElement("Configurations");
if (ActiveConfiguration != null) {
XmlAttribute activeconfattr = doc.CreateAttribute("active");
activeconfattr.InnerText = ActiveConfiguration.Name;
confnode.Attributes.Append(activeconfattr);
}
foreach (DictionaryEntry dentry in configurations) {
confnode.AppendChild(((CombineConfiguration)dentry.Value).ToXmlElement(doc));
}
doc.DocumentElement.AppendChild(confnode);
fileUtilityService.ObservedSave(new NamedFileOperationDelegate(doc.Save),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -