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

📄 solutioninputconverter.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.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.Resources;

using System.Xml;
using System.Xml.Xsl;

using ICSharpCode.Core.AddIns;

using ICSharpCode.Core.Properties;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.Core.Services;

using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Dialogs;

using ICSharpCode.SharpDevelop.Commands;
using ICSharpCode.SharpDevelop.ProjectImportExporter.Converters;
using ICSharpCode.SharpDevelop.ProjectImportExporter.Dialogs;

namespace ICSharpCode.SharpDevelop.ProjectImportExporter.Converters
{
	public class SolutionInputConverter : AbstractInputConverter
	{
		string projectTitle;
		string projectInputDirectory;
		string projectOutputDirectory;
		string combineOutputFile;
		
		public override string FormatName {
			get {
				return "Visual Studio.NET 7 / 2003 Solutions";
			}
		}
		
		public override string OutputFile {
			get {
				return combineOutputFile;
			}
		}
		
		
		public override bool CanConvert(string fileName)
		{
			string upperExtension = Path.GetExtension(fileName).ToUpper();
			return upperExtension == ".SLN";
		}
		
		public override bool Convert(string solutionInputFile, string outputPath)
		{
			projectTitle = projectInputDirectory = projectOutputDirectory = combineOutputFile = null;
			
			ArrayList projects          = ReadSolution(solutionInputFile);
			if (projects == null) return false;
			ArrayList convertedProjects = new ArrayList();
			for (int i = 0; i < projects.Count; ++i) {
				DictionaryEntry entry = (DictionaryEntry)projects[i];
				this.projectTitle = entry.Key.ToString();
				
				string projectFile  = entry.Value.ToString();
				
				string projectInputFile       = Path.Combine(Path.GetDirectoryName(solutionInputFile), projectFile);
				this.projectOutputDirectory   = Path.Combine(outputPath, Path.GetDirectoryName(projectFile));
				
				if (!File.Exists(projectFile)) {
					using (ChooseProjectLocationDialog cpld = new ChooseProjectLocationDialog()) {
						cpld.FileName = projectFile;
						DialogResult res = cpld.ShowDialog();
						if (res == DialogResult.OK) {
							projectInputFile = projectFile = cpld.FileName;
							this.projectOutputDirectory   = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(projectFile));
							entry = new DictionaryEntry(entry.Key, Path.Combine(Path.GetFileNameWithoutExtension(projectFile), Path.ChangeExtension(Path.GetFileName(projectFile), ".prjx")));
							projects[i] = entry;
						} else {
							continue;
						}
					}
				}
				
				string projectOutputFile      = Path.Combine(projectOutputDirectory, Path.ChangeExtension(Path.GetFileName(projectFile), ".prjx"));
				
				if (!Directory.Exists(projectOutputDirectory)) {
					Directory.CreateDirectory(projectOutputDirectory);
				}
				
				this.projectInputDirectory  = Path.GetDirectoryName(projectInputFile);
				Console.WriteLine("Convert {0} to {1}.", projectInputFile, projectOutputFile);
				switch (Path.GetExtension(projectFile).ToUpper()) {
					case ".VBPROJ":
						if (ConvertProject(projectInputFile, projectOutputFile, "VBSolutionConversion.xsl")) {
							convertedProjects.Add(entry);
						}
						break;
					case ".CSPROJ":
						if (ConvertProject(projectInputFile, projectOutputFile, "CSSolutionConversion.xsl")) {
							convertedProjects.Add(entry);
						}
						break;
					default:
						IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
						StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
						stringParserService.Properties["ProjectFile"] = projectFile;
						messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.CantConvertProjectFileError}");
						break;
				}
			}
			combineOutputFile = Path.Combine(outputPath, Path.ChangeExtension(Path.GetFileName(solutionInputFile), ".cmbx"));
			WriteCombine(combineOutputFile, convertedProjects);
			return true;
		}
		
		bool ConvertProject(string inputFile, string outputFile, string resourceStreamFile)
		{
			return VSProjectInputConverter.ConvertProject(inputFile, outputFile, resourceStreamFile, projectTitle, projectInputDirectory, projectOutputDirectory);
		}
		
		ArrayList ReadSolution(string fileName)
		{
			StreamReader sr           = File.OpenText(fileName);
			string firstLine = sr.ReadLine();
			if (firstLine.IndexOf("Format Version 9.") > 0) {
				IMessageService messageService =(IMessageService)ServiceManager.Services.GetService(typeof(IMessageService));
				messageService.ShowError("${res:ICSharpCode.SharpDevelop.ProjectImportExporter.Converters.SolutionInputConverter.MSBuildProjectFile}");
				return null;
			}
			Regex projectLinePattern  = new Regex("Project\\(.*\\)\\s+=\\s+\"(?<Title>.*)\",\\s*\"(?<Location>.*)\",", RegexOptions.Compiled);
			ArrayList projects = new ArrayList();
			while (true) {
				string line = sr.ReadLine();
				if (line == null) {
					break;
				}
				Match match = projectLinePattern.Match(line);
				if (match.Success) {
					projects.Add(new DictionaryEntry(match.Result("${Title}"), match.Result("${Location}")));
				}
			}
			sr.Close();
			return projects;
		}
		
		void WriteCombine(string fileName, ArrayList projects)
		{
			StreamWriter sw = File.CreateText(fileName);
			sw.WriteLine("<Combine fileversion=\"1.0\" name=\"" + Path.GetFileNameWithoutExtension(fileName) + "\" description=\"Converted Visual Studio.NET Solution\">");
			string firstEntry = null;
			sw.WriteLine("<Entries>");
			foreach (DictionaryEntry entry in projects) {
				if (firstEntry == null) {
					firstEntry = entry.Key.ToString();
				}
				sw.WriteLine("\t<Entry filename=\"." + Path.DirectorySeparatorChar + Path.ChangeExtension(entry.Value.ToString(), ".prjx") + "\" />");
			}
			sw.WriteLine("</Entries>");
			sw.WriteLine("<StartMode startupentry=\"" + firstEntry + "\" single=\"True\"/>");
			sw.WriteLine("<Configurations active=\"Debug\">");
			sw.WriteLine("<Configuration name=\"Debug\">");
			foreach (DictionaryEntry entry in projects) {
				sw.WriteLine("\t<Entry name=\"" + entry.Key + "\" configurationname=\"Debug\" build=\"False\" />");
			}
			sw.WriteLine("</Configuration>");
			sw.WriteLine("<Configuration name=\"Release\">");
			foreach (DictionaryEntry entry in projects) {
				sw.WriteLine("\t<Entry name=\"" + entry.Key + "\" configurationname=\"Release\" build=\"False\" />");
			}
			sw.WriteLine("</Configuration>");
			sw.WriteLine("</Configurations>");
			sw.WriteLine("</Combine>");
			sw.Close();
		}
	}
}

⌨️ 快捷键说明

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