📄 msbuildproject.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 1367 $</version>
// </file>
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Xsl;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Internal.Templates;
using ICSharpCode.SharpDevelop.Gui;
namespace ICSharpCode.SharpDevelop.Project
{
public class MSBuildProject : AbstractProject
{
List<string> unknownXmlSections = new List<string>();
List<string> userUnknownXmlSections = new List<string>();
protected char BuildConstantSeparator = ';';
public MSBuildProject()
{
}
protected virtual void Create(ProjectCreateInformation information)
{
Name = information.ProjectName;
IdGuid = "{" + Guid.NewGuid().ToString().ToUpperInvariant() + "}";
BaseConfiguration["OutputType"] = "Exe";
BaseConfiguration["RootNamespace"] = information.ProjectName;
BaseConfiguration["AssemblyName"] = information.ProjectName;
BaseConfiguration["Configuration"] = "Debug";
BaseConfiguration.SetIsGuarded("Configuration", true);
BaseConfiguration["Platform"] = "AnyCPU";
BaseConfiguration.SetIsGuarded("Platform", true);
configurations["Debug|*"] = new PropertyGroup();
if (information.CreateProjectWithDefaultOutputPath) {
configurations["Debug|*"]["OutputPath"] = @"bin\Debug\";
}
configurations["Debug|*"]["Optimize"] = "False";
configurations["Debug|*"]["DefineConstants"] = "DEBUG" + BuildConstantSeparator + "TRACE";
configurations["Debug|*"]["DebugSymbols"] = "True";
configurations["Debug|*"]["DebugType"] = "Full";
configurations["Release|*"] = new PropertyGroup();
if (information.CreateProjectWithDefaultOutputPath) {
configurations["Release|*"]["OutputPath"] = @"bin\Release\";
}
configurations["Release|*"]["Optimize"] = "True";
configurations["Release|*"]["DefineConstants"] = "TRACE";
configurations["Release|*"]["DebugSymbols"] = "False";
configurations["Release|*"]["DebugType"] = "None";
this.FileName = Path.GetFullPath(information.OutputProjectFileName);
}
public override bool CanCompile(string fileName)
{
return true;
}
#region Xml reading routines
public override ProjectItem CreateProjectItem(string itemType)
{
return ProjectItemFactory.CreateProjectItem(this, itemType);
}
static PropertyGroup ReadPropertyGroup(XmlReader reader)
{
PropertyGroup properties = new PropertyGroup();
PropertyGroup.ReadProperties(reader, properties, "PropertyGroup");
return properties;
}
readonly static Regex configurationRegEx = new Regex(@"\s*'(?<property>[^']*)'\s*==\s*'(?<value>[^']*)'", RegexOptions.Compiled);
protected void SetupProject(string projectFileName)
{
this.FileName = Path.GetFullPath(projectFileName);
using (MSBuildFileReader reader = new MSBuildFileReader(projectFileName)) {
reader.WhitespaceHandling = WhitespaceHandling.Significant;
reader.Namespaces = false;
reader.MoveToContent(); // we have to skip over the XmlDeclaration (if it exists)
if (reader.Name == "VisualStudioProject") {
reader.Close();
Converter.PrjxToSolutionProject.ConvertVSNetProject(projectFileName);
SetupProject(projectFileName);
return;
}
while (reader.Read()) {
if (reader.IsStartElement()) {
switch (reader.LocalName) {
case "PropertyGroup":
LoadPropertyGroup(reader, false);
break;
case "ItemGroup":
ProjectItem.ReadItemGroup(reader, this, Items);
break;
case "Import":
string import = reader.GetAttribute("Project");
Imports.Add(import);
break;
default:
unknownXmlSections.Add(reader.ReadOuterXml());
break;
}
}
}
}
ExpandWildcards();
string userSettingsFileName = projectFileName + ".user";
if (File.Exists(userSettingsFileName)) {
using (MSBuildFileReader reader = new MSBuildFileReader(userSettingsFileName)) {
reader.WhitespaceHandling = WhitespaceHandling.Significant;
reader.MoveToContent(); // we have to skip over the XmlDeclaration (if it exists)
while (reader.Read()){
if (reader.IsStartElement()) {
switch (reader.LocalName) {
case "PropertyGroup":
LoadPropertyGroup(reader, true);
break;
default:
userUnknownXmlSections.Add(reader.ReadOuterXml());
break;
}
}
}
}
}
}
void ExpandWildcards()
{
for (int i = 0; i < items.Count; i++) {
ProjectItem item = items[i];
if (item.Include.IndexOf('*') >= 0 && item is FileProjectItem) {
items.RemoveAt(i--);
try {
string path = Path.Combine(this.Directory, Path.GetDirectoryName(item.Include));
foreach (string file in System.IO.Directory.GetFiles(path, Path.GetFileName(item.Include))) {
ProjectItem n = item.Clone();
n.Include = FileUtility.GetRelativePath(this.Directory, file);
items.Insert(++i, n);
}
} catch (Exception ex) {
MessageService.ShowError(ex, "Error expanding wildcards in " + item.Include);
}
}
}
}
void LoadPropertyGroup(XmlReader reader, bool isUserFile)
{
string condition = reader.GetAttribute("Condition");
if (condition == null) {
if (isUserFile)
UserBaseConfiguration.Merge(ReadPropertyGroup(reader));
else
BaseConfiguration.Merge(ReadPropertyGroup(reader));
return;
}
Match match = configurationRegEx.Match(condition);
if (match.Success) {
Dictionary<string, PropertyGroup> configurations = isUserFile ? this.userConfigurations : this.configurations;
string conditionProperty = match.Result("${property}");
string configuration = match.Result("${value}");
if (conditionProperty == "$(Configuration)|$(Platform)") {
// configuration is ok
} else if (conditionProperty == "$(Configuration)") {
configuration += "|*";
} else if (conditionProperty == "$(Platform)") {
configuration = "*|" + configuration;
} else {
configuration = null;
}
if (configuration != null) {
PropertyGroup propertyGroup = ReadPropertyGroup(reader);
if (!configurations.ContainsKey(configuration)) {
configurations[configuration] = propertyGroup;
} else {
configurations[configuration].Merge(propertyGroup);
}
return;
}
}
if (isUserFile)
userUnknownXmlSections.Add(reader.ReadOuterXml());
else
unknownXmlSections.Add(reader.ReadOuterXml());
}
public override void Save(string fileName)
{
string outputDirectory = Path.GetDirectoryName(fileName);
if (!System.IO.Directory.Exists(outputDirectory)) {
System.IO.Directory.CreateDirectory(outputDirectory);
}
using (MSBuildFileWriter writer = new MSBuildFileWriter(fileName, Encoding.UTF8)) {
writer.Formatting = Formatting.Indented;
writer.Namespaces = false;
writer.WriteStartElement("Project");
// writer.WriteAttributeString("MSBuildVersion", "2.0");
writer.WriteAttributeString("DefaultTargets", "Build");
writer.WriteAttributeString("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
BaseConfiguration["ProjectGuid"] = IdGuid;
SaveProperties(writer, BaseConfiguration, configurations);
List<ProjectItem> references = new List<ProjectItem>();
List<ProjectItem> imports = new List<ProjectItem>();
List<ProjectItem> projectFiles = new List<ProjectItem>();
List<ProjectItem> other = new List<ProjectItem>();
foreach (ProjectItem item in this.items) {
switch (item.ItemType) {
case ItemType.Reference:
references.Add(item);
break;
case ItemType.Compile:
case ItemType.EmbeddedResource:
case ItemType.None:
projectFiles.Add(item);
break;
case ItemType.Import:
imports.Add(item);
break;
default:
other.Add(item);
break;
}
}
if (references.Count > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -