📄 solution.cs
字号:
static void SaveProjectSections(IEnumerable<ProjectSection> sections, StringBuilder projectSection)
{
foreach (ProjectSection section in sections) {
projectSection.Append("\tProjectSection(");
projectSection.Append(section.Name);
projectSection.Append(") = ");
projectSection.Append(section.SectionType);
projectSection.Append(Environment.NewLine);
section.AppendSection(projectSection, "\t\t");
projectSection.Append("\tEndProjectSection");
projectSection.Append(Environment.NewLine);
}
}
static Regex versionPattern = new Regex("Microsoft Visual Studio Solution File, Format Version\\s+(?<Version>.*)", RegexOptions.Compiled);
static Regex projectLinePattern = new Regex("Project\\(\"(?<ProjectGuid>.*)\"\\)\\s+=\\s+\"(?<Title>.*)\",\\s*\"(?<Location>.*)\",\\s*\"(?<Guid>.*)\"", RegexOptions.Compiled);
static Regex globalSectionPattern = new Regex("\\s*GlobalSection\\((?<Name>.*)\\)\\s*=\\s*(?<Type>.*)", RegexOptions.Compiled);
static string GetFirstNonCommentLine(TextReader sr)
{
string line = "";
while ((line = sr.ReadLine()) != null) {
line = line.Trim();
if (line.Length > 0 && line[0] != '#')
return line;
}
return "";
}
/// <summary>
/// Reads the specified solution file. The project-location-guid information is written into the conversion class.
/// </summary>
/// <returns>The version number of the solution.</returns>
public static string ReadSolutionInformation(string solutionFileName, Converter.PrjxToSolutionProject.Conversion conversion)
{
LoggingService.Debug("ReadSolutionInformation: " + solutionFileName);
string solutionDirectory = Path.GetDirectoryName(solutionFileName);
using (StreamReader sr = File.OpenText(solutionFileName)) {
string line = GetFirstNonCommentLine(sr);
Match match = versionPattern.Match(line);
if (!match.Success) {
return null;
}
string version = match.Result("${Version}");
while ((line = sr.ReadLine()) != null) {
match = projectLinePattern.Match(line);
if (match.Success) {
string projectGuid = match.Result("${ProjectGuid}");
string title = match.Result("${Title}");
string location = Path.Combine(solutionDirectory, match.Result("${Location}"));
string guid = match.Result("${Guid}");
LoggingService.Debug(guid + ": " + title);
conversion.NameToGuid[title] = new Guid(guid);
conversion.NameToPath[title] = location;
conversion.GuidToPath[new Guid(guid)] = location;
}
}
return version;
}
}
static bool SetupSolution(Solution newSolution, string fileName)
{
string solutionDirectory = Path.GetDirectoryName(fileName);
ProjectSection nestedProjectsSection = null;
bool needsConversion = false;
using (StreamReader sr = File.OpenText(fileName)) {
string line = GetFirstNonCommentLine(sr);
Match match = versionPattern.Match(line);
if (!match.Success) {
MessageService.ShowErrorFormatted("${res:SharpDevelop.Solution.InvalidSolutionFile}", fileName);
return false;
}
switch (match.Result("${Version}")) {
case "7.00":
needsConversion = true;
if (!MessageService.AskQuestion("${res:SharpDevelop.Solution.ConvertSolutionVersion7}")) {
return false;
}
break;
case "8.00":
needsConversion = true;
if (!MessageService.AskQuestion("${res:SharpDevelop.Solution.ConvertSolutionVersion8}")) {
return false;
}
break;
case "9.00":
break;
default:
MessageService.ShowErrorFormatted("${res:SharpDevelop.Solution.UnknownSolutionVersion}", match.Result("${Version}"));
return false;
}
while (true) {
line = sr.ReadLine();
if (line == null) {
break;
}
match = projectLinePattern.Match(line);
if (match.Success) {
string projectGuid = match.Result("${ProjectGuid}");
string title = match.Result("${Title}");
string location = match.Result("${Location}");
string guid = match.Result("${Guid}");
if (!FileUtility.IsUrl(location)) {
location = Path.Combine(solutionDirectory, location);
}
if (projectGuid == FolderGuid) {
SolutionFolder newFolder = SolutionFolder.ReadFolder(sr, title, location, guid);
newSolution.AddFolder(newFolder);
} else {
IProject newProject = LanguageBindingService.LoadProject(location, title, projectGuid);
ReadProjectSections(sr, newProject.ProjectSections);
newProject.IdGuid = guid;
newSolution.AddFolder(newProject);
}
match = match.NextMatch();
} else {
match = globalSectionPattern.Match(line);
if (match.Success) {
ProjectSection newSection = ProjectSection.ReadGlobalSection(sr, match.Result("${Name}"), match.Result("${Type}"));
// Don't put the NestedProjects section into the global sections list
// because it's transformed to a tree representation and the tree representation
// is transformed back to the NestedProjects section during save.
if (newSection.Name == "NestedProjects") {
nestedProjectsSection = newSection;
} else {
newSolution.Sections.Add(newSection);
}
}
}
}
}
// Create solution folder 'tree'.
if (nestedProjectsSection != null) {
foreach (SolutionItem item in nestedProjectsSection.Items) {
string from = item.Name;
string to = item.Location;
ISolutionFolderContainer folder = newSolution.guidDictionary[to] as ISolutionFolderContainer;
folder.AddFolder(newSolution.guidDictionary[from]);
}
}
if (newSolution.FixSolutionConfiguration(newSolution.Projects) || needsConversion) {
// save in new format
newSolution.Save();
}
return true;
}
public ProjectSection GetSolutionConfigurationsSection()
{
foreach (ProjectSection sec in Sections) {
if (sec.Name == "SolutionConfigurationPlatforms")
return sec;
}
ProjectSection newSec = new ProjectSection("SolutionConfigurationPlatforms", "preSolution");
Sections.Insert(0, newSec);
return newSec;
}
public ProjectSection GetProjectConfigurationsSection()
{
foreach (ProjectSection sec in Sections) {
if (sec.Name == "ProjectConfigurationPlatforms")
return sec;
}
ProjectSection newSec = new ProjectSection("ProjectConfigurationPlatforms", "postSolution");
Sections.Add(newSec);
return newSec;
}
public bool FixSolutionConfiguration(IEnumerable<IProject> projects)
{
ProjectSection solSec = GetSolutionConfigurationsSection();
ProjectSection prjSec = GetProjectConfigurationsSection();
bool changed = false;
if (solSec.Items.Count == 0) {
solSec.Items.Add(new SolutionItem("Debug|Any CPU", "Debug|Any CPU"));
solSec.Items.Add(new SolutionItem("Release|Any CPU", "Release|Any CPU"));
LoggingService.Warn("!! Inserted default SolutionConfigurationPlatforms !!");
changed = true;
}
foreach (IProject project in projects) {
string guid = project.IdGuid.ToUpperInvariant();
foreach (SolutionItem configuration in solSec.Items) {
string searchKey = guid + "." + configuration.Name + ".Build.0";
if (!prjSec.Items.Exists(delegate (SolutionItem item) {
return item.Name == searchKey;
}))
{
prjSec.Items.Add(new SolutionItem(searchKey, configuration.Location));
changed = true;
}
searchKey = guid + "." + configuration.Name + ".ActiveCfg";
if (!prjSec.Items.Exists(delegate (SolutionItem item) {
return item.Name == searchKey;
}))
{
prjSec.Items.Add(new SolutionItem(searchKey, configuration.Location));
changed = true;
}
}
}
return changed;
}
public IList<string> GetConfigurationNames()
{
List<string> configurationNames = new List<string>();
foreach (SolutionItem item in GetSolutionConfigurationsSection().Items) {
string name = AbstractProject.GetConfigurationNameFromKey(item.Name);
if (!configurationNames.Contains(name))
configurationNames.Add(name);
}
return configurationNames;
}
public IList<string> GetPlatformNames()
{
List<string> platformNames = new List<string>();
foreach (SolutionItem item in GetSolutionConfigurationsSection().Items) {
string name = AbstractProject.GetPlatformNameFromKey(item.Name);
if (!platformNames.Contains(name))
platformNames.Add(name);
}
return platformNames;
}
public void ApplySolutionConfigurationToProjects()
{
// TODO: Use assignments from project configuration section
foreach (IProject p in Projects) {
p.Configuration = preferences.ActiveConfiguration;
}
}
public void ApplySolutionPlatformToProjects()
{
// TODO: Use assignments from project configuration section
foreach (IProject p in Projects) {
p.Platform = preferences.ActivePlatform;
}
}
static Solution solutionBeingLoaded;
public static Solution SolutionBeingLoaded {
get {
return solutionBeingLoaded;
}
}
public static Solution Load(string fileName)
{
Solution newSolution = new Solution();
solutionBeingLoaded = newSolution;
newSolution.Name = Path.GetFileNameWithoutExtension(fileName);
string extension = Path.GetExtension(fileName).ToUpperInvariant();
if (extension == ".CMBX") {
if (!MessageService.AskQuestion("${res:SharpDevelop.Solution.ImportCmbx}")) {
return null;
}
newSolution.fileName = Path.ChangeExtension(fileName, ".sln");
ICSharpCode.SharpDevelop.Project.Converter.CombineToSolution.ConvertSolution(newSolution, fileName);
if (newSolution.FixSolutionConfiguration(newSolution.Projects)) {
newSolution.Save();
}
} else if (extension == ".PRJX") {
if (!MessageService.AskQuestion("${res:SharpDevelop.Solution.ImportPrjx}")) {
return null;
}
newSolution.fileName = Path.ChangeExtension(fileName, ".sln");
ICSharpCode.SharpDevelop.Project.Converter.CombineToSolution.ConvertProject(newSolution, fileName);
if (newSolution.FixSolutionConfiguration(newSolution.Projects)) {
newSolution.Save();
}
} else {
newSolution.fileName = fileName;
if (!SetupSolution(newSolution, fileName)) {
return null;
}
}
solutionBeingLoaded = null;
return newSolution;
}
#region System.IDisposable interface implementation
public void Dispose()
{
foreach (IProject project in Projects) {
project.Dispose();
}
}
#endregion
public void RunMSBuild(string target, MSBuildEngineCallback callback)
{
MSBuildProject.RunMSBuild(FileName, target, preferences.ActiveConfiguration, preferences.ActivePlatform, false, callback);
}
public void Build(MSBuildEngineCallback callback)
{
RunMSBuild(null, callback);
}
public void Rebuild(MSBuildEngineCallback callback)
{
RunMSBuild("Rebuild", callback);
}
public void Clean(MSBuildEngineCallback callback)
{
RunMSBuild("Clean", callback);
}
public void Publish(MSBuildEngineCallback callback)
{
RunMSBuild("Publish", callback);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -